Files
noentropy/src/cli/path_utils.rs
glitchySid f68d960cfe Add tests for handlers and fix path display bug
- Fix compilation error in path_utils.rs where path.display() was
  used after the path was moved into spawn_blocking closure
- Add 12 tests for handle_undo handler covering no undo log,
  no completed moves, custom paths, dry run, invalid paths, etc.
- Add 16 tests for validate_and_normalize_path covering path
  validation, directories, normalization, edge cases
- Add 21 tests for cache module covering key generation,
  hit/miss behavior, eviction, persistence, etc.

All 86 tests pass successfully.
2026-01-11 22:31:43 +05:30

32 lines
1.2 KiB
Rust

use std::path::{Path, PathBuf};
/// Validates that a path exists and is a readable directory.
/// Returns the canonicalized path if validation succeeds.
pub async fn validate_and_normalize_path(path: &Path) -> Result<PathBuf, String> {
// Use tokio::fs for async file operations
let metadata = tokio::fs::metadata(path).await.map_err(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
format!("Path '{}' does not exist", path.display())
} else {
format!("Cannot access '{}': {}", path.display(), e)
}
})?;
if !metadata.is_dir() {
return Err(format!("Path '{}' is not a directory", path.display()));
}
// Check if we can read the directory
let _ = tokio::fs::read_dir(path)
.await
.map_err(|e| format!("Cannot access directory '{}': {}", path.display(), e))?;
// canonicalize is sync-only, use spawn_blocking
let path_display = path.display().to_string();
let path_owned = path.to_path_buf();
tokio::task::spawn_blocking(move || path_owned.canonicalize())
.await
.map_err(|e| format!("Task failed: {}", e))?
.map_err(|e| format!("Failed to normalize path '{}': {}", path_display, e))
}