diff --git a/src/tests/test_config_path.rs b/src/tests/test_config_path.rs index 4553408d09..9f9c490a32 100644 --- a/src/tests/test_config_path.rs +++ b/src/tests/test_config_path.rs @@ -1,17 +1,58 @@ use nu_test_support::nu; +use std::fs; +use std::path::Path; + +#[cfg(not(target_os = "windows"))] +fn adjust_canonicalization>(p: P) -> String { + p.as_ref().display().to_string() +} + +#[cfg(target_os = "windows")] +fn adjust_canonicalization>(p: P) -> String { + const VERBATIM_PREFIX: &str = r#"\\?\"#; + let p = p.as_ref().display().to_string(); + if p.starts_with(VERBATIM_PREFIX) { + p[VERBATIM_PREFIX.len()..].to_string() + } else { + p + } +} #[test] fn test_default_config_path() { let config_dir = nu_path::config_dir().expect("Could not get config directory"); + let config_dir_nushell = config_dir.join("nushell"); + // Create the config dir folder structure if it does not already exist + if !config_dir_nushell.exists() { + let _ = fs::create_dir_all(&config_dir_nushell); + } let cwd = std::env::current_dir().expect("Could not get current working directory"); + let config_path = config_dir_nushell.join("config.nu"); + + // Create an empty file for canonicalization if it doesn't already exist + if !config_path.exists() { + let _ = std::fs::File::create(&config_path); + } + + // We use canonicalize here in case the config or env is symlinked since $nu.config-path is returning the canonicalized path in #8653 + let canon_config_path = adjust_canonicalization( + std::fs::canonicalize(config_path).expect("canonicalize config-path failed"), + ); - let config_path = config_dir.join("nushell").join("config.nu"); let actual = nu!(cwd: &cwd, "$nu.config-path"); - assert_eq!(actual.out, config_path.to_string_lossy().to_string()); + assert_eq!(actual.out, canon_config_path); + let env_path = config_dir_nushell.join("env.nu"); - let env_path = config_dir.join("nushell").join("env.nu"); + // Create an empty file for canonicalization if it doesn't already exist + if !env_path.exists() { + let _ = std::fs::File::create(&env_path); + } + + let canon_env_path = adjust_canonicalization( + std::fs::canonicalize(env_path).expect("canonicalize of env-path failed"), + ); let actual = nu!(cwd: &cwd, "$nu.env-path"); - assert_eq!(actual.out, env_path.to_string_lossy().to_string()); + assert_eq!(actual.out, canon_env_path); } #[test]