fix $env.FILE_PWD and $env.CURRENT_FILE inside use (#13958)

# Description
Fixes: #13425 

Similar to `source-env`, `use` command should also remove `FILE_PWD` and
`CURRENT_FILE` after evaluating code block in the module file.

And user input can be a directory, in this case, we need to use the
return value of `find_in_dirs_env` carefully, so in case, I renamed
`maybe_file_path` to `maybe_file_path_or_dir` to emphasize it.

# User-Facing Changes
`$env.FILE_PWD` and `$env.CURRENT_FILE` will be more reliable to use.

# Tests + Formatting
Added 2 test cases.

# After Submitting
NaN
This commit is contained in:
Wind
2024-10-10 20:54:00 +08:00
committed by GitHub
parent 2a3805c164
commit 5002d87af4
2 changed files with 67 additions and 7 deletions

View File

@ -308,3 +308,47 @@ fn can_use_sub_subname_from_submodule() {
let actual = nu!(inp);
assert_eq!(actual.out, "bar")
}
#[test]
fn test_use_with_printing_file_pwd() {
Playground::setup("use_with_printing_file_pwd", |dirs, nu| {
let file = dirs.test().join("mod.nu");
nu.with_files(&[FileWithContent(
file.as_os_str().to_str().unwrap(),
r#"
export-env {
print $env.FILE_PWD
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(),
"use ."
);
assert_eq!(actual.out, dirs.test().to_string_lossy());
});
}
#[test]
fn test_use_with_printing_current_file() {
Playground::setup("use_with_printing_current_file", |dirs, nu| {
let file = dirs.test().join("mod.nu");
nu.with_files(&[FileWithContent(
file.as_os_str().to_str().unwrap(),
r#"
export-env {
print $env.CURRENT_FILE
}
"#,
)]);
let actual = nu!(
cwd: dirs.test(),
"use ."
);
assert_eq!(actual.out, dirs.test().join("mod.nu").to_string_lossy());
});
}