1
0
mirror of https://github.com/nushell/nushell.git synced 2025-08-01 08:01:31 +02:00
Files
.cargo
.githooks
.github
assets
benches
clippy
crates
nu-cli
nu-cmd-base
nu-cmd-extra
nu-cmd-lang
nu-cmd-plugin
nu-color-config
nu-command
src
tests
commands
assignment
base
bytes
conversions
database
date
debug
hash_
math
move_
network
path
platform
query
random
skip
str_
take
url
alias.rs
all.rs
any.rs
append.rs
break_.rs
cal.rs
cd.rs
chunk_by.rs
chunks.rs
compact.rs
complete.rs
config_env_default.rs
config_nu_default.rs
continue_.rs
debug_info.rs
def.rs
default.rs
detect_columns.rs
do_.rs
drop.rs
du.rs
each.rs
echo.rs
empty.rs
error_make.rs
every.rs
exec.rs
export_def.rs
fill.rs
filter.rs
find.rs
first.rs
flatten.rs
for_.rs
format.rs
generate.rs
get.rs
glob.rs
griddle.rs
group_by.rs
headers.rs
help.rs
histogram.rs
ignore.rs
insert.rs
inspect.rs
interleave.rs
into_datetime.rs
into_duration.rs
into_filesize.rs
into_int.rs
job.rs
join.rs
last.rs
length.rs
let_.rs
lines.rs
loop_.rs
ls.rs
match_.rs
merge.rs
merge_deep.rs
mktemp.rs
mod.rs
mut_.rs
nu_check.rs
open.rs
par_each.rs
parse.rs
prepend.rs
print.rs
redirection.rs
reduce.rs
reject.rs
rename.rs
return_.rs
reverse.rs
rm.rs
roll.rs
rotate.rs
run_external.rs
save.rs
select.rs
semicolon.rs
seq.rs
seq_char.rs
seq_date.rs
slice.rs
sort.rs
sort_by.rs
source_env.rs
split_column.rs
split_row.rs
start.rs
stor.rs
table.rs
tee.rs
terminal.rs
to_text.rs
transpose.rs
try_.rs
ucp.rs
ulimit.rs
umkdir.rs
uname.rs
uniq.rs
uniq_by.rs
update.rs
upsert.rs
use_.rs
utouch.rs
where_.rs
which.rs
while_.rs
window.rs
with_env.rs
wrap.rs
zip.rs
format_conversions
main.rs
sort_utils.rs
Cargo.toml
LICENSE
README.md
nu-derive-value
nu-engine
nu-explore
nu-glob
nu-json
nu-lsp
nu-parser
nu-path
nu-plugin
nu-plugin-core
nu-plugin-engine
nu-plugin-protocol
nu-plugin-test-support
nu-pretty-hex
nu-protocol
nu-std
nu-system
nu-table
nu-term-grid
nu-test-support
nu-utils
nu_plugin_custom_values
nu_plugin_example
nu_plugin_formats
nu_plugin_gstat
nu_plugin_inc
nu_plugin_javascript
nu_plugin_nu_example
nu_plugin_polars
nu_plugin_python
nu_plugin_query
nu_plugin_stress_internals
nuon
README.md
devdocs
docker
scripts
src
tests
wix
.gitattributes
.gitignore
CITATION.cff
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Cargo.lock
Cargo.toml
Cross.toml
LICENSE
README.md
SECURITY.md
rust-toolchain.toml
toolkit.nu
typos.toml
nushell/crates/nu-command/tests/commands/glob.rs
Sebastian Nallar cb57f0a539 Add --follow-symlinks flag to glob command (fixes ) ()
Fixes 

# Description
The glob command wasn't working correctly with symlinks in the /sys
filesystem. This commit adds a new flag that allows users to explicitly
control whether symlinks should be followed, with special handling for
the /sys directory.

The issue was that the glob command didn't follow symbolic links when
traversing the /sys filesystem, resulting in an empty list even though
paths should be found. This implementation adds a new
`--follow-symlinks` flag that explicitly enables following symlinks. By
default, it now follows symlinks in most paths but has special handling
for /sys paths where the flag is required.

Example:
`
# Before: This would return an empty list on Linux systems
glob /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

# Now: This works as expected with the new flag
glob /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
--follow-symlinks
`

# User-Facing Changes

1. Added the --follow-symlinks (-l) flag to the glob command that allows
users to explicitly control whether symbolic links should be followed
2. Added a new example to the glob command help text demonstrating the
use of this flag

# Tests + Formatting

1. Added a test for the new --follow-symlinks flag
2025-04-23 10:47:48 -05:00

208 lines
6.3 KiB
Rust

use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::nu;
use nu_test_support::playground::Playground;
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");
})
}
#[test]
fn glob_subdirs() {
Playground::setup("glob_subdirs", |dirs, sandbox| {
sandbox.with_files(&[
EmptyFile("yehuda.txt"),
EmptyFile("jttxt"),
EmptyFile("andres.txt"),
]);
sandbox.mkdir("children");
sandbox.within("children").with_files(&[
EmptyFile("timothy.txt"),
EmptyFile("tiffany.txt"),
EmptyFile("trish.txt"),
]);
let actual = nu!(
cwd: dirs.test(),
"glob '**/*' | length",
);
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(&[
EmptyFile("yehuda.txt"),
EmptyFile("jttxt"),
EmptyFile("andres.txt"),
]);
sandbox.mkdir("children");
sandbox.within("children").with_files(&[
EmptyFile("timothy.txt"),
EmptyFile("tiffany.txt"),
EmptyFile("trish.txt"),
]);
let actual = nu!(
cwd: dirs.test(),
"glob '**/*' -D | length",
);
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(&[
EmptyFile("yehuda.txt"),
EmptyFile("jttxt"),
EmptyFile("andres.txt"),
]);
sandbox.mkdir("children");
sandbox.within("children").with_files(&[
EmptyFile("timothy.txt"),
EmptyFile("tiffany.txt"),
EmptyFile("trish.txt"),
]);
let actual = nu!(
cwd: dirs.test(),
"glob '*' -F | length",
);
assert_eq!(
actual.out, "1",
"should only find one folder; ignoring cwd, files, subfolders"
);
})
}
// 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")]
#[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);
});
}
#[test]
fn glob_follow_symlinks() {
Playground::setup("glob_follow_symlinks", |dirs, sandbox| {
// Create a directory with some files
sandbox.mkdir("target_dir");
sandbox
.within("target_dir")
.with_files(&[EmptyFile("target_file.txt")]);
let target_dir = dirs.test().join("target_dir");
let symlink_path = dirs.test().join("symlink_dir");
#[cfg(unix)]
std::os::unix::fs::symlink(target_dir, &symlink_path).expect("Failed to create symlink");
#[cfg(windows)]
std::os::windows::fs::symlink_dir(target_dir, &symlink_path)
.expect("Failed to create symlink");
// on some systems/filesystems, symlinks are followed by default
// on others (like Linux /sys), they aren't
// Test that with the --follow-symlinks flag, files are found for sure
let with_flag = nu!(
cwd: dirs.test(),
"glob 'symlink_dir/*.txt' --follow-symlinks | length",
);
assert_eq!(
with_flag.out, "1",
"Should find file with --follow-symlinks flag"
);
})
}