2019-12-17 19:54:39 +01:00
|
|
|
use nu_test_support::fs::Stub::EmptyFile;
|
2021-03-15 08:26:30 +01:00
|
|
|
use nu_test_support::playground::Playground;
|
2020-05-07 13:03:43 +02:00
|
|
|
use nu_test_support::{nu, pipeline};
|
2019-08-02 11:23:39 +02:00
|
|
|
|
|
|
|
#[test]
|
2019-12-15 17:15:06 +01:00
|
|
|
fn lists_regular_files() {
|
2019-08-29 02:32:42 +02:00
|
|
|
Playground::setup("ls_test_1", |dirs, sandbox| {
|
2019-09-11 16:36:50 +02:00
|
|
|
sandbox.with_files(vec![
|
2019-10-15 12:16:47 +02:00
|
|
|
EmptyFile("yehuda.txt"),
|
|
|
|
EmptyFile("jonathan.txt"),
|
|
|
|
EmptyFile("andres.txt"),
|
2019-08-29 02:32:42 +02:00
|
|
|
]);
|
2019-08-02 11:23:39 +02:00
|
|
|
|
2019-08-29 02:32:42 +02:00
|
|
|
let actual = nu!(
|
2019-12-15 17:15:06 +01:00
|
|
|
cwd: dirs.test(), pipeline(
|
2019-08-29 08:31:56 +02:00
|
|
|
r#"
|
2019-09-03 02:49:51 +02:00
|
|
|
ls
|
2021-03-13 22:46:40 +01:00
|
|
|
| length
|
2019-08-29 08:31:56 +02:00
|
|
|
"#
|
2019-08-29 02:32:42 +02:00
|
|
|
));
|
2019-08-02 11:23:39 +02:00
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "3");
|
2019-08-28 19:01:16 +02:00
|
|
|
})
|
2019-08-02 11:23:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-15 17:15:06 +01:00
|
|
|
fn lists_regular_files_using_asterisk_wildcard() {
|
2019-08-29 02:32:42 +02:00
|
|
|
Playground::setup("ls_test_2", |dirs, sandbox| {
|
2019-09-11 16:36:50 +02:00
|
|
|
sandbox.with_files(vec![
|
2019-10-15 12:16:47 +02:00
|
|
|
EmptyFile("los.txt"),
|
|
|
|
EmptyFile("tres.txt"),
|
|
|
|
EmptyFile("amigos.txt"),
|
|
|
|
EmptyFile("arepas.clu"),
|
2019-08-29 02:32:42 +02:00
|
|
|
]);
|
2019-08-02 11:23:39 +02:00
|
|
|
|
2019-08-29 02:32:42 +02:00
|
|
|
let actual = nu!(
|
2019-12-15 17:15:06 +01:00
|
|
|
cwd: dirs.test(), pipeline(
|
2019-08-29 08:31:56 +02:00
|
|
|
r#"
|
2019-09-03 02:49:51 +02:00
|
|
|
ls *.txt
|
2021-03-13 22:46:40 +01:00
|
|
|
| length
|
2019-08-29 08:31:56 +02:00
|
|
|
"#
|
2019-08-29 02:32:42 +02:00
|
|
|
));
|
2019-08-02 11:23:39 +02:00
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "3");
|
2019-08-28 19:01:16 +02:00
|
|
|
})
|
2019-08-02 11:23:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-15 17:15:06 +01:00
|
|
|
fn lists_regular_files_using_question_mark_wildcard() {
|
2019-08-29 02:32:42 +02:00
|
|
|
Playground::setup("ls_test_3", |dirs, sandbox| {
|
2019-09-11 16:36:50 +02:00
|
|
|
sandbox.with_files(vec![
|
|
|
|
EmptyFile("yehuda.10.txt"),
|
|
|
|
EmptyFile("jonathan.10.txt"),
|
|
|
|
EmptyFile("andres.10.txt"),
|
|
|
|
EmptyFile("chicken_not_to_be_picked_up.100.txt"),
|
2019-08-29 02:32:42 +02:00
|
|
|
]);
|
2019-08-02 11:23:39 +02:00
|
|
|
|
2019-08-29 02:32:42 +02:00
|
|
|
let actual = nu!(
|
2019-12-15 17:15:06 +01:00
|
|
|
cwd: dirs.test(), pipeline(
|
2019-08-29 08:31:56 +02:00
|
|
|
r#"
|
2019-09-03 02:49:51 +02:00
|
|
|
ls *.??.txt
|
2021-03-13 22:46:40 +01:00
|
|
|
| length
|
2019-08-29 08:31:56 +02:00
|
|
|
"#
|
2019-08-29 02:32:42 +02:00
|
|
|
));
|
2019-08-02 11:23:39 +02:00
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "3");
|
2019-08-28 19:01:16 +02:00
|
|
|
})
|
2019-08-02 11:23:39 +02:00
|
|
|
}
|
2020-01-19 03:25:07 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lists_all_files_in_directories_from_stream() {
|
|
|
|
Playground::setup("ls_test_4", |dirs, sandbox| {
|
2020-01-22 04:56:12 +01:00
|
|
|
sandbox
|
|
|
|
.with_files(vec![EmptyFile("root1.txt"), EmptyFile("root2.txt")])
|
|
|
|
.within("dir_a")
|
|
|
|
.with_files(vec![
|
|
|
|
EmptyFile("yehuda.10.txt"),
|
|
|
|
EmptyFile("jonathan.10.txt"),
|
|
|
|
])
|
|
|
|
.within("dir_b")
|
|
|
|
.with_files(vec![
|
|
|
|
EmptyFile("andres.10.txt"),
|
|
|
|
EmptyFile("chicken_not_to_be_picked_up.100.txt"),
|
|
|
|
]);
|
2020-01-19 03:25:07 +01:00
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
echo dir_a dir_b
|
2022-02-17 12:40:24 +01:00
|
|
|
| each { |it| ls $it }
|
2022-02-04 21:32:13 +01:00
|
|
|
| flatten | length
|
2020-01-19 03:25:07 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "4");
|
2020-01-19 03:25:07 +01:00
|
|
|
})
|
|
|
|
}
|
2020-02-01 09:34:34 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn does_not_fail_if_glob_matches_empty_directory() {
|
|
|
|
Playground::setup("ls_test_5", |dirs, sandbox| {
|
|
|
|
sandbox.within("dir_a");
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
ls dir_a
|
2021-03-13 22:46:40 +01:00
|
|
|
| length
|
2020-02-01 09:34:34 +01:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "0");
|
2020-02-01 09:34:34 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fails_when_glob_doesnt_match() {
|
|
|
|
Playground::setup("ls_test_5", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![EmptyFile("root1.txt"), EmptyFile("root2.txt")]);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
let actual = nu!(
|
2020-02-01 09:34:34 +01:00
|
|
|
cwd: dirs.test(),
|
|
|
|
"ls root3*"
|
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert!(actual.err.contains("no matches found"));
|
2020-02-01 09:34:34 +01:00
|
|
|
})
|
|
|
|
}
|
2020-04-06 13:28:56 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_files_from_two_parents_up_using_multiple_dots() {
|
|
|
|
Playground::setup("ls_test_6", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
EmptyFile("yahuda.yaml"),
|
|
|
|
EmptyFile("jonathan.json"),
|
|
|
|
EmptyFile("andres.xml"),
|
|
|
|
EmptyFile("kevin.txt"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
sandbox.within("foo").mkdir("bar");
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test().join("foo/bar"),
|
|
|
|
r#"
|
2021-03-13 22:46:40 +01:00
|
|
|
ls ... | length
|
2020-04-06 13:28:56 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "5");
|
2020-04-06 13:28:56 +02:00
|
|
|
})
|
|
|
|
}
|
2020-07-04 22:17:36 +02:00
|
|
|
|
2020-08-22 03:49:34 +02:00
|
|
|
#[test]
|
|
|
|
fn lists_hidden_file_when_explicitly_specified() {
|
|
|
|
Playground::setup("ls_test_7", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
EmptyFile("los.txt"),
|
|
|
|
EmptyFile("tres.txt"),
|
|
|
|
EmptyFile("amigos.txt"),
|
|
|
|
EmptyFile("arepas.clu"),
|
|
|
|
EmptyFile(".testdotfile"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
ls .testdotfile
|
2021-03-13 22:46:40 +01:00
|
|
|
| length
|
2020-08-22 03:49:34 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lists_all_hidden_files_when_glob_contains_dot() {
|
|
|
|
Playground::setup("ls_test_8", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.with_files(vec![
|
|
|
|
EmptyFile("root1.txt"),
|
|
|
|
EmptyFile("root2.txt"),
|
|
|
|
EmptyFile(".dotfile1"),
|
|
|
|
])
|
|
|
|
.within("dir_a")
|
|
|
|
.with_files(vec![
|
|
|
|
EmptyFile("yehuda.10.txt"),
|
|
|
|
EmptyFile("jonathan.10.txt"),
|
|
|
|
EmptyFile(".dotfile2"),
|
|
|
|
])
|
|
|
|
.within("dir_b")
|
|
|
|
.with_files(vec![
|
|
|
|
EmptyFile("andres.10.txt"),
|
|
|
|
EmptyFile("chicken_not_to_be_picked_up.100.txt"),
|
|
|
|
EmptyFile(".dotfile3"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
ls **/.*
|
2021-03-13 22:46:40 +01:00
|
|
|
| length
|
2020-08-22 03:49:34 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "3");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-04 21:32:58 +02:00
|
|
|
#[test]
|
|
|
|
// TODO Remove this cfg value when we have an OS-agnostic way
|
|
|
|
// of creating hidden files using the playground.
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn lists_all_hidden_files_when_glob_does_not_contain_dot() {
|
|
|
|
Playground::setup("ls_test_8", |dirs, sandbox| {
|
|
|
|
sandbox
|
|
|
|
.with_files(vec![
|
|
|
|
EmptyFile("root1.txt"),
|
|
|
|
EmptyFile("root2.txt"),
|
|
|
|
EmptyFile(".dotfile1"),
|
|
|
|
])
|
|
|
|
.within("dir_a")
|
|
|
|
.with_files(vec![
|
|
|
|
EmptyFile("yehuda.10.txt"),
|
|
|
|
EmptyFile("jonathan.10.txt"),
|
|
|
|
EmptyFile(".dotfile2"),
|
|
|
|
])
|
|
|
|
.within(".dir_b")
|
|
|
|
.with_files(vec![
|
|
|
|
EmptyFile("andres.10.txt"),
|
|
|
|
EmptyFile("chicken_not_to_be_picked_up.100.txt"),
|
|
|
|
EmptyFile(".dotfile3"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
ls **/*
|
2021-03-13 22:46:40 +01:00
|
|
|
| length
|
2020-09-04 21:32:58 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "5");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-31 02:13:27 +02:00
|
|
|
#[test]
|
|
|
|
// TODO Remove this cfg value when we have an OS-agnostic way
|
|
|
|
// of creating hidden files using the playground.
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn glob_with_hidden_directory() {
|
|
|
|
Playground::setup("ls_test_8", |dirs, sandbox| {
|
|
|
|
sandbox.within(".dir_b").with_files(vec![
|
|
|
|
EmptyFile("andres.10.txt"),
|
|
|
|
EmptyFile("chicken_not_to_be_picked_up.100.txt"),
|
|
|
|
EmptyFile(".dotfile3"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
ls **/*
|
|
|
|
| length
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "");
|
|
|
|
assert!(actual.err.contains("No matches found"));
|
|
|
|
|
|
|
|
// will list files if provide `-a` flag.
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
ls -a **/*
|
|
|
|
| length
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "4");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-31 08:52:39 +02:00
|
|
|
#[test]
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn fails_with_ls_to_dir_without_permission() {
|
|
|
|
Playground::setup("ls_test_1", |dirs, sandbox| {
|
|
|
|
sandbox.within("dir_a").with_files(vec![
|
|
|
|
EmptyFile("yehuda.11.txt"),
|
|
|
|
EmptyFile("jonathan.10.txt"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
chmod 000 dir_a; ls dir_a
|
|
|
|
"#
|
|
|
|
));
|
2022-05-21 04:48:36 +02:00
|
|
|
|
|
|
|
let check_not_root = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
id -u
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
actual
|
|
|
|
.err
|
|
|
|
.contains("The permissions of 0 do not allow access for this user")
|
|
|
|
|| check_not_root.out == "0"
|
|
|
|
);
|
2021-03-31 08:52:39 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-21 09:56:37 +02:00
|
|
|
#[test]
|
|
|
|
fn lists_files_including_starting_with_dot() {
|
|
|
|
Playground::setup("ls_test_9", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
EmptyFile("yehuda.txt"),
|
|
|
|
EmptyFile("jonathan.txt"),
|
|
|
|
EmptyFile("andres.txt"),
|
|
|
|
EmptyFile(".hidden1.txt"),
|
|
|
|
EmptyFile(".hidden2.txt"),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
ls -a
|
2021-03-13 22:46:40 +01:00
|
|
|
| length
|
2020-09-21 09:56:37 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "5");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-04 22:17:36 +02:00
|
|
|
#[test]
|
|
|
|
fn list_all_columns() {
|
2021-03-15 08:26:30 +01:00
|
|
|
Playground::setup("ls_test_all_columns", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
EmptyFile("Leonardo.yaml"),
|
|
|
|
EmptyFile("Raphael.json"),
|
|
|
|
EmptyFile("Donatello.xml"),
|
|
|
|
EmptyFile("Michelangelo.txt"),
|
|
|
|
]);
|
|
|
|
// Normal Operation
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2022-02-04 21:32:13 +01:00
|
|
|
"ls | columns | to md"
|
2021-03-15 08:26:30 +01:00
|
|
|
);
|
|
|
|
let expected = ["name", "type", "size", "modified"].join("");
|
|
|
|
assert_eq!(actual.out, expected, "column names are incorrect for ls");
|
|
|
|
// Long
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2022-02-04 21:32:13 +01:00
|
|
|
"ls -l | columns | to md"
|
2021-03-15 08:26:30 +01:00
|
|
|
);
|
|
|
|
let expected = {
|
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
[
|
|
|
|
"name",
|
|
|
|
"type",
|
|
|
|
"target",
|
2022-02-04 03:01:45 +01:00
|
|
|
"readonly",
|
|
|
|
"mode",
|
2022-02-04 21:32:13 +01:00
|
|
|
"num_links",
|
|
|
|
"inode",
|
2021-03-15 08:26:30 +01:00
|
|
|
"uid",
|
|
|
|
"group",
|
|
|
|
"size",
|
|
|
|
"created",
|
|
|
|
"accessed",
|
|
|
|
"modified",
|
|
|
|
]
|
|
|
|
.join("")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
{
|
|
|
|
[
|
|
|
|
"name", "type", "target", "readonly", "size", "created", "accessed", "modified",
|
|
|
|
]
|
|
|
|
.join("")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
actual.out, expected,
|
|
|
|
"column names are incorrect for ls long"
|
|
|
|
);
|
|
|
|
});
|
2020-07-04 22:17:36 +02:00
|
|
|
}
|
2022-06-03 18:37:27 +02:00
|
|
|
|
|
|
|
/// Rust's fs::metadata function is unable to read info for certain system files on Windows,
|
|
|
|
/// like the `C:\Windows\System32\Configuration` folder. https://github.com/rust-lang/rust/issues/96980
|
|
|
|
/// This test confirms that Nu can work around this successfully.
|
|
|
|
#[test]
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn can_list_system_folder() {
|
|
|
|
// the awkward `ls Configuration* | where name == "Configuration"` thing is for speed;
|
|
|
|
// listing the entire System32 folder is slow and `ls Configuration*` alone
|
|
|
|
// might return more than 1 file someday
|
|
|
|
let file_type = nu!(
|
|
|
|
cwd: "C:\\Windows\\System32", pipeline(
|
|
|
|
r#"ls Configuration* | where name == "Configuration" | get type.0"#
|
|
|
|
));
|
|
|
|
assert_eq!(file_type.out, "dir");
|
|
|
|
|
|
|
|
let file_size = nu!(
|
|
|
|
cwd: "C:\\Windows\\System32", pipeline(
|
|
|
|
r#"ls Configuration* | where name == "Configuration" | get size.0"#
|
|
|
|
));
|
|
|
|
assert!(file_size.out.trim() != "");
|
|
|
|
|
|
|
|
let file_modified = nu!(
|
|
|
|
cwd: "C:\\Windows\\System32", pipeline(
|
|
|
|
r#"ls Configuration* | where name == "Configuration" | get modified.0"#
|
|
|
|
));
|
|
|
|
assert!(file_modified.out.trim() != "");
|
|
|
|
|
|
|
|
let file_accessed = nu!(
|
|
|
|
cwd: "C:\\Windows\\System32", pipeline(
|
|
|
|
r#"ls -l Configuration* | where name == "Configuration" | get accessed.0"#
|
|
|
|
));
|
|
|
|
assert!(file_accessed.out.trim() != "");
|
|
|
|
|
|
|
|
let file_created = nu!(
|
|
|
|
cwd: "C:\\Windows\\System32", pipeline(
|
|
|
|
r#"ls -l Configuration* | where name == "Configuration" | get created.0"#
|
|
|
|
));
|
|
|
|
assert!(file_created.out.trim() != "");
|
|
|
|
|
|
|
|
let ls_with_filter = nu!(
|
|
|
|
cwd: "C:\\Windows\\System32", pipeline(
|
|
|
|
r#"ls | where size > 10mb"#
|
|
|
|
));
|
|
|
|
assert_eq!(ls_with_filter.err, "");
|
|
|
|
}
|