Allow ls works inside dir with [] brackets (#12625)

# Description
Fixes: #12429

To fix the issue, we need to pass the `input pattern` itself to
`glob_from` function, but currently on latest main, nushell pass
`expanded path of input pattern` to `glob_from` function.
It causes globbing failed if expanded path includes `[]` brackets.

It's a pity that I have to duplicate `nu_engine::glob_from` function
into `ls`, because `ls` might convert from `NuGlob::NotExpand` to
`NuGlob::Expand`, in that case, `nu_engine::glob_from` won't work if
user want to ls for a directory which includes tilde:
```
mkdir "~abc"
ls "~abc"
```
So I need to duplicate `glob_from` function and pass original
`expand_tilde` information.

# User-Facing Changes
Nan

# Tests + Formatting
Done

# After Submitting
Nan
This commit is contained in:
Wind
2024-05-06 14:01:32 +08:00
committed by GitHub
parent e879d4ecaf
commit 460a1c8f87
2 changed files with 97 additions and 61 deletions

View File

@ -85,13 +85,13 @@ fn lists_regular_files_in_special_folder() {
assert_eq!(actual.out, "2");
let actual = nu!(
cwd: dirs.test().join("abcd/*"), format!(r#"ls ../* | length"#));
assert_eq!(actual.out, "3");
assert_eq!(actual.out, "2");
let actual = nu!(
cwd: dirs.test().join("abcd/?"), format!(r#"ls -D ../* | length"#));
assert_eq!(actual.out, "2");
let actual = nu!(
cwd: dirs.test().join("abcd/?"), format!(r#"ls ../* | length"#));
assert_eq!(actual.out, "3");
assert_eq!(actual.out, "2");
})
}
@ -237,6 +237,12 @@ fn list_files_from_two_parents_up_using_multiple_dots() {
);
assert_eq!(actual.out, "5");
let actual = nu!(
cwd: dirs.test().join("foo/bar"),
r#"ls ... | sort-by name | get name.0 | str replace -a '\' '/'"#
);
assert_eq!(actual.out, "../../andres.xml");
})
}
@ -759,3 +765,46 @@ fn list_with_multiple_path() {
assert_eq!(actual.out, "0");
})
}
#[test]
fn list_inside_glob_metachars_dir() {
Playground::setup("list_files_inside_glob_metachars_dir", |dirs, sandbox| {
let sub_dir = "test[]";
sandbox
.within(sub_dir)
.with_files(&[EmptyFile("test_file.txt")]);
let actual = nu!(
cwd: dirs.test().join(sub_dir),
"ls test_file.txt | get name.0 | path basename",
);
assert!(actual.out.contains("test_file.txt"));
});
}
#[test]
fn list_inside_tilde_glob_metachars_dir() {
Playground::setup(
"list_files_inside_tilde_glob_metachars_dir",
|dirs, sandbox| {
let sub_dir = "~test[]";
sandbox
.within(sub_dir)
.with_files(&[EmptyFile("test_file.txt")]);
// need getname.0 | path basename because the output path
// might be too long to output as a single line.
let actual = nu!(
cwd: dirs.test().join(sub_dir),
"ls test_file.txt | get name.0 | path basename",
);
assert!(actual.out.contains("test_file.txt"));
let actual = nu!(
cwd: dirs.test(),
"ls '~test[]' | get name.0 | path basename"
);
assert!(actual.out.contains("test_file.txt"));
},
);
}