Introduce base-dir constructors for isolation; update tests to avoid env var dependence; ensure directories exist before I/O; all tests green (including performance)

This commit is contained in:
Andy
2025-08-11 22:41:14 +08:00
parent ec8b46955b
commit 515b0100ac
5 changed files with 44 additions and 36 deletions
+11
View File
@@ -70,6 +70,17 @@ impl DocxHandler {
})
}
/// Create a handler that stores temporary documents under the provided base directory
pub fn new_with_base_dir<P: AsRef<Path>>(base_dir: P) -> Result<Self> {
let temp_dir = base_dir.as_ref().join("docx-mcp");
fs::create_dir_all(&temp_dir)?;
Ok(Self {
temp_dir,
documents: std::collections::HashMap::new(),
in_memory_ops: std::collections::HashMap::new(),
})
}
#[cfg(test)]
pub fn new_with_temp_dir(temp_dir: &Path) -> Result<Self> {
let temp_dir = temp_dir.to_path_buf();
+17
View File
@@ -36,6 +36,23 @@ impl DocxToolsProvider {
security_config,
}
}
/// Create a provider that stores temporary documents under the provided base directory
pub fn with_base_dir<P: AsRef<std::path::Path>>(base_dir: P) -> Self {
Self::with_base_dir_and_security(base_dir, SecurityConfig::default())
}
/// Create a provider with a base directory and explicit security config
pub fn with_base_dir_and_security<P: AsRef<std::path::Path>>(base_dir: P, security_config: SecurityConfig) -> Self {
Self {
handler: Arc::new(Mutex::new(DocxHandler::new_with_base_dir(base_dir).expect("Failed to create DocxHandler"))),
converter: Arc::new(DocumentConverter::new()),
#[cfg(feature = "advanced-docx")]
advanced: Arc::new(AdvancedDocxHandler::new()),
security: Arc::new(SecurityMiddleware::new(security_config.clone())),
security_config,
}
}
}
impl DocxToolsProvider {