fix test_default_config_path test after pr 8653 (#8690)

# Description

This PR fixes a testing bug that @rgwood found and PR #8653 introduced,
mentioned
[here](https://github.com/nushell/nushell/pull/8653#issuecomment-1491263657).
Since 8653 returns canonicalizes config paths now, the tests need to
return canonicalized paths as well. Without this PR, if you have your
nushell config dir symlinked, this test will fail. This PR fixes that
test.

# User-Facing Changes



# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-utils/standard_library/tests.nu` to run the
tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
Darren Schroeder 2023-03-31 11:17:37 -05:00 committed by GitHub
parent 3bf5999ef4
commit b2257a5ca3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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: AsRef<Path>>(p: P) -> String {
p.as_ref().display().to_string()
}
#[cfg(target_os = "windows")]
fn adjust_canonicalization<P: AsRef<Path>>(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]