| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
use regex::Regex; |
| 7 |
use uutests::new_ucmd; |
| 8 |
use uutests::util::TestScenario; |
| 9 |
use uutests::util_name; |
| 10 |
|
| 11 |
/* |
| 12 |
* As vdir use the same functions than ls, we don't have to retest them here. |
| 13 |
* We just test the default and the column output |
| 14 |
*/ |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn test_vdir() { |
| 18 |
new_ucmd!().succeeds(); |
| 19 |
} |
| 20 |
|
| 21 |
#[test] |
| 22 |
fn test_default_output() { |
| 23 |
let scene = TestScenario::new(util_name!()); |
| 24 |
let at = &scene.fixtures; |
| 25 |
at.mkdir("some-dir1"); |
| 26 |
at.touch("some-file1"); |
| 27 |
filetime::set_file_mtime( |
| 28 |
at.plus("some-file1"), |
| 29 |
filetime::FileTime::from_unix_time(978_307_200, 0), |
| 30 |
) |
| 31 |
.unwrap(); |
| 32 |
|
| 33 |
scene.ucmd().succeeds().stdout_contains("some-file1"); |
| 34 |
|
| 35 |
scene |
| 36 |
.ucmd() |
| 37 |
.succeeds() |
| 38 |
.stdout_contains("Jan 1 2001 some-file1\n"); |
| 39 |
scene |
| 40 |
.ucmd() |
| 41 |
.arg("--time-style=long-iso") |
| 42 |
.succeeds() |
| 43 |
.stdout_contains("2001-01-01 00:00 some-file1\n"); |
| 44 |
scene |
| 45 |
.ucmd() |
| 46 |
.env("TIME_STYLE", "long-iso") |
| 47 |
.succeeds() |
| 48 |
.stdout_contains("2001-01-01 00:00 some-file1\n"); |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn test_default_format_overrides() { |
| 53 |
let scene = TestScenario::new(util_name!()); |
| 54 |
scene.fixtures.touch("file"); |
| 55 |
for (args, expected) in [ |
| 56 |
(vec!["-1"], "file\n"), |
| 57 |
(vec!["-C"], "file\n"), |
| 58 |
(vec!["-x"], "file\n"), |
| 59 |
(vec!["-m"], "file\n"), |
| 60 |
(vec!["--format=single-column"], "file\n"), |
| 61 |
(vec!["--zero"], "file\0"), |
| 62 |
(vec!["-g", "-C"], "file\n"), |
| 63 |
] { |
| 64 |
scene |
| 65 |
.ucmd() |
| 66 |
.env("TIME_STYLE", "invalid") |
| 67 |
.args(&args) |
| 68 |
.succeeds() |
| 69 |
.stdout_only(expected); |
| 70 |
} |
| 71 |
for args in [["-l", "-1"], ["-g", "--zero"], ["-C", "-g"]] { |
| 72 |
scene |
| 73 |
.ucmd() |
| 74 |
.args(&args) |
| 75 |
.succeeds() |
| 76 |
.stdout_contains("total 0"); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
#[test] |
| 81 |
fn test_quoting_defaults() { |
| 82 |
let scene = TestScenario::new(util_name!()); |
| 83 |
scene.fixtures.touch("a b"); |
| 84 |
scene.ucmd().succeeds().stdout_contains(" a\\ b\n"); |
| 85 |
scene.ucmd().arg("--zero").succeeds().stdout_only("a b\0"); |
| 86 |
scene |
| 87 |
.ucmd() |
| 88 |
.env("QUOTING_STYLE", "literal") |
| 89 |
.succeeds() |
| 90 |
.stdout_contains(" a b\n"); |
| 91 |
scene |
| 92 |
.ucmd() |
| 93 |
.env("QUOTING_STYLE", "literal") |
| 94 |
.arg("-b") |
| 95 |
.succeeds() |
| 96 |
.stdout_contains(" a\\ b\n"); |
| 97 |
scene |
| 98 |
.ucmd() |
| 99 |
.arg("--dired") |
| 100 |
.succeeds() |
| 101 |
.stdout_contains(" a\\ b\n"); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
#[cfg(unix)] |
| 106 |
fn test_literal_quoting_on_terminal() { |
| 107 |
let scene = TestScenario::new(util_name!()); |
| 108 |
scene.fixtures.touch("a\nb"); |
| 109 |
scene |
| 110 |
.ucmd() |
| 111 |
.env("QUOTING_STYLE", "literal") |
| 112 |
.terminal_simulation(true) |
| 113 |
.succeeds() |
| 114 |
.stdout_contains(" a\r\nb\r\n"); |
| 115 |
} |
| 116 |
|
| 117 |
#[test] |
| 118 |
fn test_time_selection_preserves_name_sort() { |
| 119 |
let scene = TestScenario::new(util_name!()); |
| 120 |
for (name, seconds) in [("a", 978_307_200), ("b", 1_009_843_200)] { |
| 121 |
scene.fixtures.touch(name); |
| 122 |
filetime::set_file_atime( |
| 123 |
scene.fixtures.plus(name), |
| 124 |
filetime::FileTime::from_unix_time(seconds, 0), |
| 125 |
) |
| 126 |
.unwrap(); |
| 127 |
} |
| 128 |
let name_order = Regex::new(r"(?s)2001 a\n.*2002 b\n").unwrap(); |
| 129 |
let zero_output = Regex::new(r"\Ab\x00a\x00\z").unwrap(); |
| 130 |
for (time, zero) in itertools::iproduct!(["-u", "--time=atime"], [false, true]) { |
| 131 |
scene |
| 132 |
.ucmd() |
| 133 |
.args(&[time, "--time-style=+%Y", "a", "b"]) |
| 134 |
.args(if zero { &["--zero"] } else { &[] }) |
| 135 |
.succeeds() |
| 136 |
.no_stderr() |
| 137 |
.stdout_matches(if zero { &zero_output } else { &name_order }); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn test_column_output() { |
| 143 |
let scene = TestScenario::new(util_name!()); |
| 144 |
let at = &scene.fixtures; |
| 145 |
at.mkdir("some-dir1"); |
| 146 |
at.touch("some-file1"); |
| 147 |
|
| 148 |
scene |
| 149 |
.ucmd() |
| 150 |
.arg("-C") |
| 151 |
.succeeds() |
| 152 |
.stdout_contains("some-file1"); |
| 153 |
|
| 154 |
scene |
| 155 |
.ucmd() |
| 156 |
.arg("-C") |
| 157 |
.succeeds() |
| 158 |
.stdout_does_not_match(&Regex::new("[rwx-]{10}.*some-file1$").unwrap()); |
| 159 |
} |
| 160 |
|
| 161 |
#[test] |
| 162 |
fn test_invalid_option_exit_code() { |
| 163 |
new_ucmd!().arg("-/").fails().code_is(2); |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn test_help_shows_vdir_not_ls() { |
| 168 |
let result = new_ucmd!().arg("--help").succeeds(); |
| 169 |
let output = result.stdout_str(); |
| 170 |
|
| 171 |
// Verify help text contains "vdir" in the usage line |
| 172 |
assert!( |
| 173 |
output.contains("vdir [OPTION]"), |
| 174 |
"Help should show 'vdir [OPTION]'" |
| 175 |
); |
| 176 |
|
| 177 |
// Verify help text does not incorrectly show "ls" |
| 178 |
assert!( |
| 179 |
!output.contains("ls [OPTION]"), |
| 180 |
"Help should not show 'ls [OPTION]'" |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
#[test] |
| 185 |
fn test_version() { |
| 186 |
new_ucmd!() |
| 187 |
.arg("--version") |
| 188 |
.succeeds() |
| 189 |
.no_stderr() |
| 190 |
.stdout_is(format!("vdir {}\n", uucore::crate_version!())); |
| 191 |
} |
| 192 |
|
| 193 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 194 |
mod diagnostics { |
| 195 |
use super::*; |
| 196 |
|
| 197 |
#[cfg(unix)] |
| 198 |
#[test] |
| 199 |
fn test_snippet_points_at_the_unknown_unit_of_block_size() { |
| 200 |
let result = new_ucmd!() |
| 201 |
.terminal_sim_stderr() |
| 202 |
.arg("--block-size=1fb") |
| 203 |
.fails_with_code(2); |
| 204 |
let stderr = result.stderr_as_displayed(); |
| 205 |
|
| 206 |
// The report names the utility as it was called, not `ls`. |
| 207 |
assert!(stderr.contains("vdir:1:"), "{stderr}"); |
| 208 |
assert!(stderr.contains("not a known unit"), "{stderr}"); |
| 209 |
} |
| 210 |
|
| 211 |
#[test] |
| 212 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 213 |
new_ucmd!() |
| 214 |
.arg("--block-size=1fb") |
| 215 |
.fails_with_code(2) |
| 216 |
.stderr_is("vdir: invalid --block-size argument '1fb'\n"); |
| 217 |
} |
| 218 |
} |