mirror of
https://github.com/nushell/nushell.git
synced 2025-04-09 21:28:55 +02:00
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:
parent
3a20fbfe94
commit
e5145358eb
@ -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]
|
#[test]
|
||||||
fn lists_regular_files_using_question_mark_wildcard() {
|
fn lists_regular_files_using_question_mark_wildcard() {
|
||||||
Playground::setup("ls_test_3", |dirs, sandbox| {
|
Playground::setup("ls_test_3", |dirs, sandbox| {
|
||||||
|
@ -7,6 +7,8 @@ use nu_glob::MatchOptions;
|
|||||||
use nu_path::{canonicalize_with, expand_path_with};
|
use nu_path::{canonicalize_with, expand_path_with};
|
||||||
use nu_protocol::{ShellError, Span, Spanned};
|
use nu_protocol::{ShellError, Span, Spanned};
|
||||||
|
|
||||||
|
const GLOB_CHARS: &[char] = &['*', '?', '['];
|
||||||
|
|
||||||
/// This function is like `nu_glob::glob` from the `glob` crate, except it is relative to a given cwd.
|
/// This function is like `nu_glob::glob` from the `glob` crate, except it is relative to a given cwd.
|
||||||
///
|
///
|
||||||
/// It returns a tuple of two values: the first is an optional prefix that the expanded filenames share.
|
/// It returns a tuple of two values: the first is an optional prefix that the expanded filenames share.
|
||||||
@ -27,25 +29,16 @@ pub fn glob_from(
|
|||||||
),
|
),
|
||||||
ShellError,
|
ShellError,
|
||||||
> {
|
> {
|
||||||
let path = PathBuf::from(&pattern.item);
|
let (prefix, pattern) = if pattern.item.contains(GLOB_CHARS) {
|
||||||
let path = expand_path_with(path, cwd);
|
// Pattern contains glob, split it
|
||||||
let is_symlink = match fs::symlink_metadata(&path) {
|
|
||||||
Ok(attr) => attr.file_type().is_symlink(),
|
|
||||||
Err(_) => false,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Check for brackets first
|
|
||||||
let (prefix, pattern) = if path.to_string_lossy().contains('[') {
|
|
||||||
// Path is a glob pattern => do not check for existence
|
|
||||||
// Select the longest prefix until the first '*'
|
|
||||||
let mut p = PathBuf::new();
|
let mut p = PathBuf::new();
|
||||||
|
let path = PathBuf::from(&pattern.item);
|
||||||
let components = path.components();
|
let components = path.components();
|
||||||
let mut counter = 0;
|
let mut counter = 0;
|
||||||
|
|
||||||
// Get the path up to the pattern which we'll call the prefix
|
|
||||||
for c in components {
|
for c in components {
|
||||||
if let Component::Normal(os) = c {
|
if let Component::Normal(os) = c {
|
||||||
if os.to_string_lossy().contains('*') {
|
if os.to_string_lossy().contains(GLOB_CHARS) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -53,7 +46,6 @@ pub fn glob_from(
|
|||||||
counter += 1;
|
counter += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Let's separate the pattern from the path and we'll call this the pattern
|
|
||||||
let mut just_pattern = PathBuf::new();
|
let mut just_pattern = PathBuf::new();
|
||||||
for c in counter..path.components().count() {
|
for c in counter..path.components().count() {
|
||||||
if let Some(comp) = path.components().nth(c) {
|
if let Some(comp) = path.components().nth(c) {
|
||||||
@ -61,30 +53,29 @@ pub fn glob_from(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(Some(p), just_pattern)
|
// Now expand `p` to get full prefix
|
||||||
} else if path.to_string_lossy().contains('*') {
|
let path = expand_path_with(p, cwd);
|
||||||
// Path is a glob pattern => do not check for existence
|
let escaped_prefix = PathBuf::from(nu_glob::Pattern::escape(&path.to_string_lossy()));
|
||||||
// Select the longest prefix until the first '*'
|
|
||||||
let mut p = PathBuf::new();
|
|
||||||
for c in path.components() {
|
|
||||||
if let Component::Normal(os) = c {
|
|
||||||
if os.to_string_lossy().contains('*') {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p.push(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
(Some(p), path)
|
(Some(path), escaped_prefix.join(just_pattern))
|
||||||
} else if is_symlink {
|
|
||||||
(path.parent().map(|parent| parent.to_path_buf()), path)
|
|
||||||
} else {
|
} else {
|
||||||
let path = if let Ok(p) = canonicalize_with(path, cwd) {
|
let path = PathBuf::from(&pattern.item);
|
||||||
p
|
let path = expand_path_with(path, cwd);
|
||||||
} else {
|
let is_symlink = match fs::symlink_metadata(&path) {
|
||||||
return Err(ShellError::DirectoryNotFound(pattern.span, None));
|
Ok(attr) => attr.file_type().is_symlink(),
|
||||||
|
Err(_) => false,
|
||||||
};
|
};
|
||||||
(path.parent().map(|parent| parent.to_path_buf()), path)
|
|
||||||
|
if is_symlink {
|
||||||
|
(path.parent().map(|parent| parent.to_path_buf()), path)
|
||||||
|
} else {
|
||||||
|
let path = if let Ok(p) = canonicalize_with(path, cwd) {
|
||||||
|
p
|
||||||
|
} else {
|
||||||
|
return Err(ShellError::DirectoryNotFound(pattern.span, None));
|
||||||
|
};
|
||||||
|
(path.parent().map(|parent| parent.to_path_buf()), path)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let pattern = pattern.to_string_lossy().to_string();
|
let pattern = pattern.to_string_lossy().to_string();
|
||||||
|
@ -4629,7 +4629,9 @@ pub fn parse_value(
|
|||||||
SyntaxShape::Any
|
SyntaxShape::Any
|
||||||
| SyntaxShape::List(_)
|
| SyntaxShape::List(_)
|
||||||
| SyntaxShape::Table(_)
|
| SyntaxShape::Table(_)
|
||||||
| SyntaxShape::Signature => {}
|
| SyntaxShape::Signature
|
||||||
|
| SyntaxShape::Filepath
|
||||||
|
| SyntaxShape::String => {}
|
||||||
_ => {
|
_ => {
|
||||||
working_set.error(ParseError::Expected("non-[] value", span));
|
working_set.error(ParseError::Expected("non-[] value", span));
|
||||||
return Expression::garbage(span);
|
return Expression::garbage(span);
|
||||||
|
Loading…
Reference in New Issue
Block a user