2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-12-13 13:45:53 -05:00
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::Context;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_core::error::AnyError;
|
|
|
|
pub use deno_core::normalize_path;
|
|
|
|
use std::env::current_dir;
|
|
|
|
use std::io::Error;
|
2023-01-14 23:18:58 -05:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
2020-12-13 13:45:53 -05:00
|
|
|
|
|
|
|
/// Similar to `std::fs::canonicalize()` but strips UNC prefixes on Windows.
|
|
|
|
pub fn canonicalize_path(path: &Path) -> Result<PathBuf, Error> {
|
|
|
|
let mut canonicalized_path = path.canonicalize()?;
|
|
|
|
if cfg!(windows) {
|
|
|
|
canonicalized_path = PathBuf::from(
|
|
|
|
canonicalized_path
|
|
|
|
.display()
|
|
|
|
.to_string()
|
|
|
|
.trim_start_matches("\\\\?\\"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(canonicalized_path)
|
|
|
|
}
|
|
|
|
|
2022-08-27 11:20:05 -04:00
|
|
|
#[inline]
|
2020-12-13 13:45:53 -05:00
|
|
|
pub fn resolve_from_cwd(path: &Path) -> Result<PathBuf, AnyError> {
|
2022-08-27 11:20:05 -04:00
|
|
|
if path.is_absolute() {
|
|
|
|
Ok(normalize_path(path))
|
2020-12-13 13:45:53 -05:00
|
|
|
} else {
|
2021-04-21 11:52:00 -04:00
|
|
|
let cwd =
|
|
|
|
current_dir().context("Failed to get current working directory")?;
|
2022-12-17 17:20:15 -05:00
|
|
|
Ok(normalize_path(cwd.join(path)))
|
2022-08-27 11:20:05 -04:00
|
|
|
}
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_child() {
|
|
|
|
let cwd = current_dir().unwrap();
|
|
|
|
assert_eq!(resolve_from_cwd(Path::new("a")).unwrap(), cwd.join("a"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_dot() {
|
|
|
|
let cwd = current_dir().unwrap();
|
|
|
|
assert_eq!(resolve_from_cwd(Path::new(".")).unwrap(), cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_parent() {
|
|
|
|
let cwd = current_dir().unwrap();
|
|
|
|
assert_eq!(resolve_from_cwd(Path::new("a/..")).unwrap(), cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_normalize_path() {
|
|
|
|
assert_eq!(normalize_path(Path::new("a/../b")), PathBuf::from("b"));
|
|
|
|
assert_eq!(normalize_path(Path::new("a/./b/")), PathBuf::from("a/b/"));
|
|
|
|
assert_eq!(
|
|
|
|
normalize_path(Path::new("a/./b/../c")),
|
|
|
|
PathBuf::from("a/c")
|
|
|
|
);
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
assert_eq!(
|
|
|
|
normalize_path(Path::new("C:\\a\\.\\b\\..\\c")),
|
|
|
|
PathBuf::from("C:\\a\\c")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_absolute() {
|
2023-04-12 19:45:53 -04:00
|
|
|
let expected = Path::new("a");
|
|
|
|
let cwd = current_dir().unwrap();
|
|
|
|
let absolute_expected = cwd.join(expected);
|
|
|
|
assert_eq!(resolve_from_cwd(expected).unwrap(), absolute_expected);
|
2020-12-13 13:45:53 -05:00
|
|
|
}
|
|
|
|
}
|