treat path contains '?' as pattern (#10142)

Fix https://github.com/nushell/nushell/issues/10136

# Description
Current nushell only handle path containing '*' as match pattern and
treat '?' as just normal path.
This pr makes path containing '?' is also processed as pattern.

🔴 **Concerns: Need to design/comfirm a consistent rule to handle
dirs/files with '?' in their names.**

Currently:

- if no dir has exactly same name with pattern, it will print the list
of matched directories
- if pattern exactly matches an empty dir's name, it will just print the
empty dir's content ( i.e. `[]`)
- if pattern exactly matches an dir's name, it will perform pattern
match and print all the dir contains

e.g.
```bash
mkdir src
ls s?c 
```

| name | type | size   | modified                                      |
| ---- | ---- | ------ | --------------------------------------------- |
| src  | dir  | 1.1 KB | Tue, 29 Aug 2023 07:39:41 +0900 (9 hours ago) |

-----------

```bash
mkdir src
mkdir scc
mkdir scs
ls s?c
```

| name | type | size | modified |
| ---- | ---- | ------ |
------------------------------------------------ |
| scc | dir | 64 B | Tue, 29 Aug 2023 16:55:31 +0900 (14 seconds ago) |
| src | dir | 1.1 KB | Tue, 29 Aug 2023 07:39:41 +0900 (9 hours ago) |

-----------

```bash
mkdir  s?c
ls s?c
```

print empty (i.e. ls of dir `s?c`)

-----------

```bash
mkdir -p  s?c/test
ls s?c
```
|name|type|size|modified|
|-|-|-|-|
|s?c/test|dir|64 B|Tue, 29 Aug 2023 16:47:53 +0900 (2 minutes ago)|
|src/bytes|dir|480 B|Fri, 25 Aug 2023 17:43:52 +0900 (3 days ago)|
|src/charting|dir|160 B|Fri, 25 Aug 2023 17:43:52 +0900 (3 days ago)|
|src/conversions|dir|160 B|Fri, 25 Aug 2023 17:43:52 +0900 (3 days ago)|

-----------

# User-Facing Changes

User will be able to use '?' to match directory/file.

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

None

---------

Co-authored-by: Horasal <horsal@horsal.dev>
This commit is contained in:
Horasal
2023-09-04 09:25:00 +09:00
committed by GitHub
parent 3a20fbfe94
commit e5145358eb
3 changed files with 120 additions and 36 deletions

View File

@ -45,6 +45,97 @@ fn lists_regular_files_using_asterisk_wildcard() {
})
}
#[cfg(not(target_os = "windows"))]
#[test]
fn lists_regular_files_in_special_folder() {
Playground::setup("ls_test_3", |dirs, sandbox| {
sandbox
.mkdir("[abcd]")
.mkdir("[bbcd]")
.mkdir("abcd]")
.mkdir("abcd")
.mkdir("abcd/*")
.mkdir("abcd/?")
.with_files(vec![EmptyFile("[abcd]/test.txt")])
.with_files(vec![EmptyFile("abcd]/test.txt")])
.with_files(vec![EmptyFile("abcd/*/test.txt")])
.with_files(vec![EmptyFile("abcd/?/test.txt")])
.with_files(vec![EmptyFile("abcd/?/test2.txt")]);
let actual = nu!(
cwd: dirs.test().join("abcd]"), format!(r#"ls | length"#));
assert_eq!(actual.out, "1");
let actual = nu!(
cwd: dirs.test(), format!(r#"ls abcd] | length"#));
assert_eq!(actual.out, "1");
let actual = nu!(
cwd: dirs.test().join("[abcd]"), format!(r#"ls | length"#));
assert_eq!(actual.out, "1");
let actual = nu!(
cwd: dirs.test().join("[bbcd]"), format!(r#"ls | length"#));
assert_eq!(actual.out, "0");
let actual = nu!(
cwd: dirs.test().join("abcd/*"), format!(r#"ls | length"#));
assert_eq!(actual.out, "1");
let actual = nu!(
cwd: dirs.test().join("abcd/?"), format!(r#"ls | length"#));
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");
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");
})
}
#[rstest::rstest]
#[case("j?.??.txt", 1)]
#[case("j????.txt", 2)]
#[case("?????.txt", 3)]
#[case("????c.txt", 1)]
#[case("ye??da.10.txt", 1)]
#[case("yehuda.?0.txt", 1)]
#[case("??????.10.txt", 2)]
#[case("[abcd]????.txt", 1)]
#[case("??[ac.]??.txt", 3)]
#[case("[ab]bcd/??.txt", 2)]
#[case("?bcd/[xy]y.txt", 2)]
#[case("?bcd/[xy]y.t?t", 2)]
#[case("[[]abcd[]].txt", 1)]
#[case("[[]?bcd[]].txt", 2)]
#[case("??bcd[]].txt", 2)]
#[case("??bcd].txt", 2)]
#[case("[[]?bcd].txt", 2)]
#[case("[[]abcd].txt", 1)]
#[case("[[][abcd]bcd[]].txt", 2)]
fn lists_regular_files_using_question_mark(#[case] command: &str, #[case] expected: usize) {
Playground::setup("ls_test_3", |dirs, sandbox| {
sandbox.mkdir("abcd").mkdir("bbcd").with_files(vec![
EmptyFile("abcd/xy.txt"),
EmptyFile("bbcd/yy.txt"),
EmptyFile("[abcd].txt"),
EmptyFile("[bbcd].txt"),
EmptyFile("yehuda.10.txt"),
EmptyFile("jt.10.txt"),
EmptyFile("jtabc.txt"),
EmptyFile("abcde.txt"),
EmptyFile("andres.10.txt"),
EmptyFile("chicken_not_to_be_picked_up.100.txt"),
]);
let actual = nu!(
cwd: dirs.test(), format!(r#"ls {command} | length"#));
assert_eq!(actual.out, expected.to_string());
})
}
#[test]
fn lists_regular_files_using_question_mark_wildcard() {
Playground::setup("ls_test_3", |dirs, sandbox| {