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"
);
})
}
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")
}
#[rstest]
#[case(".", "'*z'", &["ablez", "baker", "charliez"], &["ablez", "charliez"], "simple glob")]
#[case(".", "'qqq'", &["ablez", "baker", "charliez"], &[], "glob matches none")]
#[case("foo/bar", r"'*[\]}]*'", &["foo/bar/ab}le", "foo/bar/baker", "foo/bar/cha]rlie"], &["foo/bar/ab}le", "foo/bar/cha]rlie"], "glob has quoted metachars")]
#[case("foo/bar", "'../*'", &["foo/able", "foo/bar/baker", "foo/charlie"], &["foo/bar/../able", "foo/bar/../bar", "foo/bar/../charlie"], "glob matches files in parent")]
#[case("foo", "'./{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,
format!(r#"glob {glob} | sort | str join " ""#)
);
let mut expected: Vec<String> = vec![];
for e in exp {
#[cfg(windows)]
let e = {
let mut path = PathBuf::new();
for c in Path::new(e).components() {
if c == std::path::Component::ParentDir {
path.pop();
} else {
path.push(c)
}
}
path
};
#[cfg(not(windows))]
let e = PathBuf::from(e);
expected.push(dirs.test().join(e).to_string_lossy().to_string());
}
let expected = expected.join(" ");
assert_eq!(actual.out, expected, "\n test: {tag}");
});
}
#[test]
fn glob_follow_symlinks() {
Playground::setup("glob_follow_symlinks", |dirs, sandbox| {
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");
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"
);
})
}