use std::path::{Path, PathBuf};
pub fn canonicalize(path: &Path) -> std::io::Result<PathBuf> {
let canonical = fs_err::canonicalize(path)?;
Ok(dunce::simplified(&canonical).to_path_buf())
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
use tempfile::TempDir;
#[test]
fn canonicalizes_existing_dir_to_absolute() {
let tmp = TempDir::new().unwrap();
let resolved = canonicalize(tmp.path()).unwrap();
assert!(resolved.is_absolute());
}
#[test]
#[cfg(windows)]
fn strips_verbatim_prefix_on_windows() {
let tmp = TempDir::new().unwrap();
let resolved = canonicalize(tmp.path()).unwrap();
let as_str = resolved.to_string_lossy();
assert!(
!as_str.starts_with(r"\\?\"),
"verbatim prefix must be stripped so the path is shell-usable, got: {as_str}"
);
}
#[test]
fn missing_path_is_not_found() {
let tmp = TempDir::new().unwrap();
let err = canonicalize(&tmp.path().join("does-not-exist")).unwrap_err();
assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
}
}