1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-24 05:37:35 +00:00

fix(windows): avoid verbatim paths (#3638)

This commit is contained in:
David Knaack 2022-02-24 00:32:35 +01:00 committed by GitHub
parent bfff44fddb
commit 1a8aa96b7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 11 deletions

7
Cargo.lock generated
View File

@ -496,6 +496,12 @@ version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1435fa1053d8b2fbbe9be7e97eca7f33d37b28409959813daefc1446a14247f1"
[[package]]
name = "dunce"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "453440c271cf5577fd2a40e4942540cb7d0d2f85e27c8d07dd0023c925a67541"
[[package]]
name = "easy-parallel"
version = "3.2.0"
@ -1605,6 +1611,7 @@ dependencies = [
"clap",
"clap_complete",
"directories-next",
"dunce",
"gethostname",
"git2",
"indexmap",

View File

@ -33,6 +33,7 @@ chrono = "0.4.19"
clap = { version = "3.1.0", features = ["derive", "cargo", "unicode"] }
clap_complete = "3.1.0"
directories-next = "2.0.0"
dunce = "1.0.2"
gethostname = "0.2.2"
git2 = { version = "0.13.25", default-features = false }
indexmap = { version = "1.8.0", features = ["serde"] }

View File

@ -3,4 +3,6 @@ disallowed-methods = [
"std::process::Command::new",
# Setting environment variables can cause issues with non-rust code
"std::env::set_var",
# use `dunce` to avoid UNC/verbatim paths, where possible
"std::fs::canonicalize",
]

View File

@ -137,9 +137,9 @@ impl<'a> Context<'a> {
}
// Canonicalize the current path to resolve symlinks, etc.
// NOTE: On Windows this converts the path to extended-path syntax.
// NOTE: On Windows this may convert the path to extended-path syntax.
let current_dir = Context::expand_tilde(path);
let current_dir = current_dir.canonicalize().unwrap_or(current_dir);
let current_dir = dunce::canonicalize(&current_dir).unwrap_or(current_dir);
let logical_dir = logical_path;
let root_config = config
@ -734,10 +734,8 @@ mod tests {
assert_ne!(context.current_dir, context.logical_dir);
let expected_current_dir = path_actual
.join("yyy")
.canonicalize()
.expect("canonicalize");
let expected_current_dir =
dunce::canonicalize(path_actual.join("yyy")).expect("canonicalize");
assert_eq!(expected_current_dir, context.current_dir);
let expected_logical_dir = test_path;
@ -787,4 +785,21 @@ mod tests {
let expected_logical_dir = test_path;
assert_eq!(expected_logical_dir, context.logical_dir);
}
#[cfg(windows)]
#[test]
fn strip_extended_path_prefix() {
let test_path = Path::new(r"\\?\C:\").to_path_buf();
let context = Context::new_with_shell_and_path(
Properties::default(),
Shell::Unknown,
Target::Main,
test_path.clone(),
test_path,
);
let expected_path = Path::new(r"C:\");
assert_eq!(&context.current_dir, expected_path);
}
}

View File

@ -364,8 +364,8 @@ mod tests {
fs::create_dir_all(&src_dir)?;
init_repo(&repo_dir)?;
let src_variations = [src_dir.clone(), src_dir.canonicalize().unwrap()];
let repo_variations = [repo_dir.clone(), repo_dir.canonicalize().unwrap()];
let src_variations = [src_dir.clone(), dunce::canonicalize(src_dir).unwrap()];
let repo_variations = [repo_dir.clone(), dunce::canonicalize(repo_dir).unwrap()];
for src_dir in &src_variations {
for repo_dir in &repo_variations {
let output = contract_repo_path(src_dir, repo_dir);
@ -1590,7 +1590,7 @@ mod tests {
#[test]
#[cfg(windows)]
fn windows_trims_extended_unc_path_prefix() {
// Under Windows, path canonicalization returns UNC paths using extended-path prefixes `\\?\UNC\`
// Under Windows, path canonicalization may return UNC paths using extended-path prefixes `\\?\UNC\`
// We expect this prefix to be trimmed before being rendered.
let unc_path = Path::new(r"\\?\UNC\server\share\a\b\c");

View File

@ -270,7 +270,7 @@ mod tests {
fn render_valid_paths() -> io::Result<()> {
use io::Write;
let dir = tempfile::tempdir()?;
let root = std::fs::canonicalize(dir.path())?;
let root = dunce::canonicalize(dir.path())?;
let mut yaml = File::create(root.join("Pulumi.yml"))?;
yaml.write_all("name: starship\nruntime: nodejs\ndescription: A thing\n".as_bytes())?;
yaml.sync_all()?;
@ -337,7 +337,7 @@ mod tests {
fn partial_login() -> io::Result<()> {
use io::Write;
let dir = tempfile::tempdir()?;
let root = std::fs::canonicalize(dir.path())?;
let root = dunce::canonicalize(dir.path())?;
let mut yaml = File::create(root.join("Pulumi.yml"))?;
yaml.write_all("name: starship\nruntime: nodejs\ndescription: A thing\n".as_bytes())?;
yaml.sync_all()?;