nushell/crates/nu-command/tests/commands/glob.rs

176 lines
5.2 KiB
Rust
Raw Normal View History

use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::nu;
use nu_test_support::playground::Playground;
glob with ../ prefix now works; (#10504) Fixes #10503 Also improves link to metacharacter help; # Description `glob` code was using pattern as provided by user. If that had leading `..\`, `wax::Glob` is documented to treat them as literal chars to be matched. Fix is to use `wax::Glob.partition()` to split such invariant prefixes off the pattern and tack them onto the working directory computed separately. Before ``` > ls .. ╭───┬───────┬──────┬──────┬───────────────╮ │ # │ name │ type │ size │ modified │ ├───┼───────┼──────┼──────┼───────────────┤ │ 0 │ ../r1 │ dir │ 7 B │ 3 hours ago │ │ 1 │ ../r2 │ dir │ 3 B │ a day ago │ │ 2 │ ../r3 │ dir │ 13 B │ 4 minutes ago │ ╰───┴───────┴──────┴──────┴───────────────╯ > glob ../r* ╭────────────╮ │ empty list │ ╰────────────╯ ``` After ``` > glob ../r* ╭───┬──────────────────────────────╮ │ 0 │ /home/bobhy/src/rust/work/r2 │ │ 1 │ /home/bobhy/src/rust/work/r1 │ │ 2 │ /home/bobhy/src/rust/work/r3 │ ╰───┴──────────────────────────────╯ ``` # User-Facing Changes None # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` # 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. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-09-29 13:48:55 +02:00
use rstest::rstest;
use std::path::{Path, PathBuf};
#[test]
fn empty_glob_pattern_triggers_error() {
Playground::setup("glob_test_1", |dirs, sandbox| {
sandbox.with_files(&[
EmptyFile("yehuda.txt"),
EmptyFile("jttxt"),
EmptyFile("andres.txt"),
]);
let actual = nu!(
cwd: dirs.test(),
"glob ''",
);
assert!(actual.err.contains("must not be empty"));
})
}
#[test]
fn nonempty_glob_lists_matching_paths() {
Playground::setup("glob_sanity_star", |dirs, sandbox| {
sandbox.with_files(&[
EmptyFile("yehuda.txt"),
EmptyFile("jttxt"),
EmptyFile("andres.txt"),
]);
let actual = nu!(
cwd: dirs.test(),
"glob '*' | length",
);
assert_eq!(actual.out, "3");
})
}
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
#[test]
fn glob_subdirs() {
Playground::setup("glob_subdirs", |dirs, sandbox| {
sandbox.with_files(&[
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
EmptyFile("yehuda.txt"),
EmptyFile("jttxt"),
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
EmptyFile("andres.txt"),
]);
sandbox.mkdir("children");
sandbox.within("children").with_files(&[
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
EmptyFile("timothy.txt"),
EmptyFile("tiffany.txt"),
EmptyFile("trish.txt"),
]);
let actual = nu!(
cwd: dirs.test(),
"glob '**/*' | length",
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
);
assert_eq!(
actual.out, "8",
"count must be 8 due to 6 files and 2 folders, including the cwd"
);
})
}
#[test]
fn glob_subdirs_ignore_dirs() {
Playground::setup("glob_subdirs_ignore_directories", |dirs, sandbox| {
sandbox.with_files(&[
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
EmptyFile("yehuda.txt"),
EmptyFile("jttxt"),
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
EmptyFile("andres.txt"),
]);
sandbox.mkdir("children");
sandbox.within("children").with_files(&[
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
EmptyFile("timothy.txt"),
EmptyFile("tiffany.txt"),
EmptyFile("trish.txt"),
]);
let actual = nu!(
cwd: dirs.test(),
"glob '**/*' -D | length",
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
);
assert_eq!(
actual.out, "6",
"directory count must be 6, ignoring the cwd and the children folders"
);
})
}
#[test]
fn glob_ignore_files() {
Playground::setup("glob_ignore_files", |dirs, sandbox| {
sandbox.with_files(&[
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
EmptyFile("yehuda.txt"),
EmptyFile("jttxt"),
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
EmptyFile("andres.txt"),
]);
sandbox.mkdir("children");
sandbox.within("children").with_files(&[
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
EmptyFile("timothy.txt"),
EmptyFile("tiffany.txt"),
EmptyFile("trish.txt"),
]);
let actual = nu!(
cwd: dirs.test(),
"glob '*' -F | length",
Expose filtering by file type in glob (#7834) # Description Add flags for filtering the output of `glob` by file type. I find myself occasionally wanting to do this, and getting a file's [file_type](https://docs.rs/wax/latest/wax/struct.WalkEntry.html#method.file_type) is presumably fast to do as it doesn't have to go through the fallible metadata method. The design of the signature does concern me; it's not as readable as a filter or "include" type list would be. They have to be filtered one by one, which can be annoying if you only want files `-D -S`, or only want folders `-F -S`, or only want symlinks `--butwhy?`. I considered SyntaxShape::Keyword for this but I'll just defer to comments on this PR if they pop up. I'd also like to bring up performance since including these flags technically incurs a `.filter` penalty on all glob calls, which could be optimized out if we added a branch for the no-filters case. But in reality I'd expect the file system to be the bottleneck and the flags to be pretty branch predictor friendly, so eh # User-Facing Changes Three new flags when using `glob` and a slightly more cluttered help page. No breaking changes, I hope. # 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 # 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.
2023-01-28 14:50:12 +01:00
);
assert_eq!(
actual.out, "1",
"should only find one folder; ignoring cwd, files, subfolders"
);
})
}
glob with ../ prefix now works; (#10504) Fixes #10503 Also improves link to metacharacter help; # Description `glob` code was using pattern as provided by user. If that had leading `..\`, `wax::Glob` is documented to treat them as literal chars to be matched. Fix is to use `wax::Glob.partition()` to split such invariant prefixes off the pattern and tack them onto the working directory computed separately. Before ``` > ls .. ╭───┬───────┬──────┬──────┬───────────────╮ │ # │ name │ type │ size │ modified │ ├───┼───────┼──────┼──────┼───────────────┤ │ 0 │ ../r1 │ dir │ 7 B │ 3 hours ago │ │ 1 │ ../r2 │ dir │ 3 B │ a day ago │ │ 2 │ ../r3 │ dir │ 13 B │ 4 minutes ago │ ╰───┴───────┴──────┴──────┴───────────────╯ > glob ../r* ╭────────────╮ │ empty list │ ╰────────────╯ ``` After ``` > glob ../r* ╭───┬──────────────────────────────╮ │ 0 │ /home/bobhy/src/rust/work/r2 │ │ 1 │ /home/bobhy/src/rust/work/r1 │ │ 2 │ /home/bobhy/src/rust/work/r3 │ ╰───┴──────────────────────────────╯ ``` # User-Facing Changes None # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` # 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. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-09-29 13:48:55 +02:00
// clone of fs::create_file_at removing the parent panic, whose purpose I do not grok.
pub fn create_file_at(full_path: impl AsRef<Path>) -> Result<(), std::io::Error> {
let full_path = full_path.as_ref();
std::fs::write(full_path, b"fake data")
}
// playground has root directory and subdirectories foo and foo/bar to play with
// specify all test files relative to root directory.
// OK to use fwd slash in paths, they're hacked to OS dir separator when needed (windows)
#[rstest]
#[case(".", r#"'*z'"#, &["ablez", "baker", "charliez"], &["ablez", "charliez"], "simple glob")]
#[case(".", r#"'qqq'"#, &["ablez", "baker", "charliez"], &[], "glob matches none")]
#[case("foo/bar", r"'*[\]}]*'", &[r#"foo/bar/ab}le"#, "foo/bar/baker", r#"foo/bar/cha]rlie"#], &[r#"foo/bar/ab}le"#, r#"foo/bar/cha]rlie"#], "glob has quoted metachars")]
glob with ../ prefix now works; (#10504) Fixes #10503 Also improves link to metacharacter help; # Description `glob` code was using pattern as provided by user. If that had leading `..\`, `wax::Glob` is documented to treat them as literal chars to be matched. Fix is to use `wax::Glob.partition()` to split such invariant prefixes off the pattern and tack them onto the working directory computed separately. Before ``` > ls .. ╭───┬───────┬──────┬──────┬───────────────╮ │ # │ name │ type │ size │ modified │ ├───┼───────┼──────┼──────┼───────────────┤ │ 0 │ ../r1 │ dir │ 7 B │ 3 hours ago │ │ 1 │ ../r2 │ dir │ 3 B │ a day ago │ │ 2 │ ../r3 │ dir │ 13 B │ 4 minutes ago │ ╰───┴───────┴──────┴──────┴───────────────╯ > glob ../r* ╭────────────╮ │ empty list │ ╰────────────╯ ``` After ``` > glob ../r* ╭───┬──────────────────────────────╮ │ 0 │ /home/bobhy/src/rust/work/r2 │ │ 1 │ /home/bobhy/src/rust/work/r1 │ │ 2 │ /home/bobhy/src/rust/work/r3 │ ╰───┴──────────────────────────────╯ ``` # User-Facing Changes None # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` # 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. --> --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
2023-09-29 13:48:55 +02:00
#[case("foo/bar", r#"'../*'"#, &["foo/able", "foo/bar/baker", "foo/charlie"], &["foo/able", "foo/bar", "foo/charlie"], "glob matches files in parent")]
#[case("foo", r#"'./{a,b}*'"#, &["foo/able", "foo/bar/baker", "foo/charlie"], &["foo/able", "foo/bar"], "glob with leading ./ matches peer files")]
fn glob_files_in_parent(
#[case] wd: &str,
#[case] glob: &str,
#[case] ini: &[&str],
#[case] exp: &[&str],
#[case] tag: &str,
) {
Playground::setup("glob_test", |dirs, sandbox| {
sandbox.within("foo").within("bar");
let working_directory = &dirs.test().join(wd);
for f in ini {
create_file_at(dirs.test().join(f)).expect("couldn't create file");
}
let actual = nu!(
cwd: working_directory,
r#"glob {} | sort | str join " ""#,
glob
);
let mut expected: Vec<String> = vec![];
for e in exp {
expected.push(
dirs.test()
.join(PathBuf::from(e)) // sadly, does *not" convert "foo/bar" to "foo\\bar" on Windows.
.to_string_lossy()
.to_string(),
);
}
let expected = expected
.join(" ")
.replace('/', std::path::MAIN_SEPARATOR_STR);
assert_eq!(actual.out, expected, "\n test: {}", tag);
});
}