| 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 |
#[cfg(unix)] |
| 7 |
use uutests::at_and_ucmd; |
| 8 |
use uutests::new_ucmd; |
| 9 |
use uutests::util::vec_of_size; |
| 10 |
|
| 11 |
// spell-checker:ignore (flags) lwmcL clmwL ; (path) bogusfile emptyfile manyemptylines moby notrailingnewline onelongemptyline onelongword weirdchars ioerrdir |
| 12 |
#[test] |
| 13 |
fn test_invalid_arg() { |
| 14 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 15 |
} |
| 16 |
|
| 17 |
#[test] |
| 18 |
fn test_count_bytes_large_stdin() { |
| 19 |
for n in [ |
| 20 |
0, |
| 21 |
1, |
| 22 |
42, |
| 23 |
16 * 1024 - 7, |
| 24 |
16 * 1024 - 1, |
| 25 |
16 * 1024, |
| 26 |
16 * 1024 + 1, |
| 27 |
16 * 1024 + 3, |
| 28 |
32 * 1024, |
| 29 |
64 * 1024, |
| 30 |
80 * 1024, |
| 31 |
96 * 1024, |
| 32 |
112 * 1024, |
| 33 |
128 * 1024, |
| 34 |
] { |
| 35 |
let data = vec_of_size(n); |
| 36 |
let expected = format!("{n}\n"); |
| 37 |
new_ucmd!() |
| 38 |
.args(&["-c"]) |
| 39 |
.pipe_in(data) |
| 40 |
.succeeds() |
| 41 |
.stdout_is_bytes(expected.as_bytes()); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn test_stdin_default() { |
| 47 |
new_ucmd!() |
| 48 |
.pipe_in_fixture("lorem_ipsum.txt") |
| 49 |
.succeeds() |
| 50 |
.stdout_is(" 13 109 772\n"); |
| 51 |
} |
| 52 |
|
| 53 |
#[test] |
| 54 |
fn test_stdin_explicit() { |
| 55 |
new_ucmd!() |
| 56 |
.pipe_in_fixture("lorem_ipsum.txt") |
| 57 |
.arg("-") |
| 58 |
.succeeds() |
| 59 |
.stdout_is(" 13 109 772 -\n"); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn test_utf8() { |
| 64 |
new_ucmd!() |
| 65 |
.args(&["-lwmcL"]) |
| 66 |
.pipe_in_fixture("UTF_8_test.txt") |
| 67 |
.succeeds() |
| 68 |
.stdout_is(" 303 2178 22457 23025 79\n"); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn test_utf8_words() { |
| 73 |
new_ucmd!() |
| 74 |
.arg("-w") |
| 75 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 76 |
.succeeds() |
| 77 |
.stdout_is("89\n"); |
| 78 |
} |
| 79 |
|
| 80 |
#[test] |
| 81 |
fn test_utf8_line_length_words() { |
| 82 |
new_ucmd!() |
| 83 |
.arg("-Lw") |
| 84 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 85 |
.succeeds() |
| 86 |
.stdout_is(" 89 48\n"); |
| 87 |
} |
| 88 |
|
| 89 |
#[test] |
| 90 |
fn test_utf8_line_length_chars() { |
| 91 |
new_ucmd!() |
| 92 |
.arg("-Lm") |
| 93 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 94 |
.succeeds() |
| 95 |
.stdout_is(" 442 48\n"); |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn test_utf8_line_length_chars_words() { |
| 100 |
new_ucmd!() |
| 101 |
.arg("-Lmw") |
| 102 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 103 |
.succeeds() |
| 104 |
.stdout_is(" 89 442 48\n"); |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn test_utf8_chars() { |
| 109 |
new_ucmd!() |
| 110 |
.arg("-m") |
| 111 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 112 |
.succeeds() |
| 113 |
.stdout_is("442\n"); |
| 114 |
} |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn test_utf8_bytes_chars() { |
| 118 |
new_ucmd!() |
| 119 |
.arg("-cm") |
| 120 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 121 |
.succeeds() |
| 122 |
.stdout_is(" 442 513\n"); |
| 123 |
} |
| 124 |
|
| 125 |
#[test] |
| 126 |
fn test_utf8_bytes_lines() { |
| 127 |
new_ucmd!() |
| 128 |
.arg("-cl") |
| 129 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 130 |
.succeeds() |
| 131 |
.stdout_is(" 25 513\n"); |
| 132 |
} |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn test_utf8_bytes_chars_lines() { |
| 136 |
new_ucmd!() |
| 137 |
.arg("-cml") |
| 138 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 139 |
.succeeds() |
| 140 |
.stdout_is(" 25 442 513\n"); |
| 141 |
} |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn test_utf8_chars_words() { |
| 145 |
new_ucmd!() |
| 146 |
.arg("-mw") |
| 147 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 148 |
.succeeds() |
| 149 |
.stdout_is(" 89 442\n"); |
| 150 |
} |
| 151 |
|
| 152 |
#[test] |
| 153 |
fn test_utf8_line_length_lines() { |
| 154 |
new_ucmd!() |
| 155 |
.arg("-Ll") |
| 156 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 157 |
.succeeds() |
| 158 |
.stdout_is(" 25 48\n"); |
| 159 |
} |
| 160 |
|
| 161 |
#[test] |
| 162 |
fn test_utf8_line_length_lines_words() { |
| 163 |
new_ucmd!() |
| 164 |
.arg("-Llw") |
| 165 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 166 |
.succeeds() |
| 167 |
.stdout_is(" 25 89 48\n"); |
| 168 |
} |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn test_utf8_lines_chars() { |
| 172 |
new_ucmd!() |
| 173 |
.arg("-ml") |
| 174 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 175 |
.succeeds() |
| 176 |
.stdout_is(" 25 442\n"); |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
fn test_utf8_lines_words_chars() { |
| 181 |
new_ucmd!() |
| 182 |
.arg("-mlw") |
| 183 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 184 |
.succeeds() |
| 185 |
.stdout_is(" 25 89 442\n"); |
| 186 |
} |
| 187 |
|
| 188 |
#[test] |
| 189 |
fn test_utf8_line_length_lines_chars() { |
| 190 |
new_ucmd!() |
| 191 |
.arg("-Llm") |
| 192 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 193 |
.succeeds() |
| 194 |
.stdout_is(" 25 442 48\n"); |
| 195 |
} |
| 196 |
|
| 197 |
#[test] |
| 198 |
fn test_utf8_all() { |
| 199 |
new_ucmd!() |
| 200 |
.arg("-lwmcL") |
| 201 |
.pipe_in_fixture("UTF_8_weirdchars.txt") |
| 202 |
.succeeds() |
| 203 |
.stdout_is(" 25 89 442 513 48\n"); |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn test_ascii_control() { |
| 208 |
// GNU coreutils "d1" test |
| 209 |
new_ucmd!() |
| 210 |
.arg("-w") |
| 211 |
.pipe_in(*b"\x01\n") |
| 212 |
.succeeds() |
| 213 |
.stdout_is("1\n"); |
| 214 |
} |
| 215 |
|
| 216 |
#[test] |
| 217 |
fn test_stdin_line_len_regression() { |
| 218 |
new_ucmd!() |
| 219 |
.args(&["-L"]) |
| 220 |
.pipe_in("\n123456") |
| 221 |
.succeeds() |
| 222 |
.stdout_is("6\n"); |
| 223 |
} |
| 224 |
|
| 225 |
#[test] |
| 226 |
fn test_stdin_only_bytes() { |
| 227 |
new_ucmd!() |
| 228 |
.args(&["-c"]) |
| 229 |
.pipe_in_fixture("lorem_ipsum.txt") |
| 230 |
.succeeds() |
| 231 |
.stdout_is("772\n"); |
| 232 |
} |
| 233 |
|
| 234 |
#[test] |
| 235 |
fn test_stdin_all_counts() { |
| 236 |
new_ucmd!() |
| 237 |
.args(&["-c", "-m", "-l", "-L", "-w"]) |
| 238 |
.pipe_in_fixture("alice_in_wonderland.txt") |
| 239 |
.succeeds() |
| 240 |
.stdout_is(" 5 57 302 302 66\n"); |
| 241 |
} |
| 242 |
|
| 243 |
#[test] |
| 244 |
fn test_single_default() { |
| 245 |
new_ucmd!() |
| 246 |
.arg("moby_dick.txt") |
| 247 |
.succeeds() |
| 248 |
.stdout_is(" 18 204 1115 moby_dick.txt\n"); |
| 249 |
} |
| 250 |
|
| 251 |
#[test] |
| 252 |
fn test_single_only_lines() { |
| 253 |
new_ucmd!() |
| 254 |
.args(&["-l", "moby_dick.txt"]) |
| 255 |
.succeeds() |
| 256 |
.stdout_is("18 moby_dick.txt\n"); |
| 257 |
} |
| 258 |
|
| 259 |
#[test] |
| 260 |
fn test_single_only_bytes() { |
| 261 |
new_ucmd!() |
| 262 |
.args(&["-c", "lorem_ipsum.txt"]) |
| 263 |
.succeeds() |
| 264 |
.stdout_is("772 lorem_ipsum.txt\n"); |
| 265 |
} |
| 266 |
|
| 267 |
#[test] |
| 268 |
fn test_single_all_counts() { |
| 269 |
new_ucmd!() |
| 270 |
.args(&["-c", "-l", "-L", "-m", "-w", "alice_in_wonderland.txt"]) |
| 271 |
.succeeds() |
| 272 |
.stdout_is(" 5 57 302 302 66 alice_in_wonderland.txt\n"); |
| 273 |
} |
| 274 |
|
| 275 |
#[cfg(unix)] |
| 276 |
#[test] |
| 277 |
fn test_gnu_compatible_quotation() { |
| 278 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 279 |
|
| 280 |
at.mkdir("some-dir1"); |
| 281 |
at.touch("some-dir1/12\n34.txt"); |
| 282 |
|
| 283 |
ucmd.args(&["some-dir1/12\n34.txt"]) |
| 284 |
.succeeds() |
| 285 |
.stdout_is("0 0 0 'some-dir1/12'$'\\n''34.txt'\n"); |
| 286 |
} |
| 287 |
|
| 288 |
#[cfg(feature = "test_risky_names")] |
| 289 |
#[test] |
| 290 |
fn test_non_unicode_names() { |
| 291 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 292 |
|
| 293 |
let target1 = uucore::os_str_from_bytes(b"some-dir1/1\xC0\n.txt") |
| 294 |
.expect("Only unix platforms can test non-unicode names"); |
| 295 |
let target2 = uucore::os_str_from_bytes(b"some-dir1/2\xC0\t.txt") |
| 296 |
.expect("Only unix platforms can test non-unicode names"); |
| 297 |
|
| 298 |
at.mkdir("some-dir1"); |
| 299 |
at.touch(target1); |
| 300 |
at.touch(target2); |
| 301 |
|
| 302 |
ucmd.args(&[target1, target2]).succeeds().stdout_is_bytes( |
| 303 |
[ |
| 304 |
b"0 0 0 'some-dir1/1'$'\\300\\n''.txt'\n".to_vec(), |
| 305 |
b"0 0 0 some-dir1/2\xC0\t.txt\n".to_vec(), |
| 306 |
b"0 0 0 total\n".to_vec(), |
| 307 |
] |
| 308 |
.concat(), |
| 309 |
); |
| 310 |
} |
| 311 |
|
| 312 |
#[test] |
| 313 |
fn test_multiple_default() { |
| 314 |
new_ucmd!() |
| 315 |
.args(&[ |
| 316 |
"lorem_ipsum.txt", |
| 317 |
"moby_dick.txt", |
| 318 |
"alice_in_wonderland.txt", |
| 319 |
"alice in wonderland.txt", |
| 320 |
]) |
| 321 |
.succeeds() |
| 322 |
.stdout_is(concat!( |
| 323 |
" 13 109 772 lorem_ipsum.txt\n", |
| 324 |
" 18 204 1115 moby_dick.txt\n", |
| 325 |
" 5 57 302 alice_in_wonderland.txt\n", |
| 326 |
" 5 57 302 alice in wonderland.txt\n", |
| 327 |
" 41 427 2491 total\n", |
| 328 |
)); |
| 329 |
} |
| 330 |
|
| 331 |
/// Test for an empty file. |
| 332 |
#[test] |
| 333 |
fn test_file_empty() { |
| 334 |
new_ucmd!() |
| 335 |
.args(&["-clmwL", "emptyfile.txt"]) |
| 336 |
.succeeds() |
| 337 |
.stdout_is("0 0 0 0 0 emptyfile.txt\n"); |
| 338 |
} |
| 339 |
|
| 340 |
/// Test for an file containing a single non-whitespace character |
| 341 |
/// *without* a trailing newline. |
| 342 |
#[test] |
| 343 |
fn test_file_single_line_no_trailing_newline() { |
| 344 |
new_ucmd!() |
| 345 |
.args(&["-clmwL", "notrailingnewline.txt"]) |
| 346 |
.succeeds() |
| 347 |
.stdout_is("1 1 2 2 1 notrailingnewline.txt\n"); |
| 348 |
} |
| 349 |
|
| 350 |
/// Test for a file that has 100 empty lines (that is, the contents of |
| 351 |
/// the file are the newline character repeated one hundred times). |
| 352 |
#[test] |
| 353 |
fn test_file_many_empty_lines() { |
| 354 |
new_ucmd!() |
| 355 |
.args(&["-clmwL", "manyemptylines.txt"]) |
| 356 |
.succeeds() |
| 357 |
.stdout_is("100 0 100 100 0 manyemptylines.txt\n"); |
| 358 |
} |
| 359 |
|
| 360 |
/// Test for a file that has one long line comprising only spaces. |
| 361 |
#[test] |
| 362 |
fn test_file_one_long_line_only_spaces() { |
| 363 |
new_ucmd!() |
| 364 |
.args(&["-clmwL", "onelongemptyline.txt"]) |
| 365 |
.succeeds() |
| 366 |
.stdout_is(" 1 0 10001 10001 10000 onelongemptyline.txt\n"); |
| 367 |
} |
| 368 |
|
| 369 |
/// Test for a file that has one long line comprising a single "word". |
| 370 |
#[test] |
| 371 |
fn test_file_one_long_word() { |
| 372 |
new_ucmd!() |
| 373 |
.args(&["-clmwL", "onelongword.txt"]) |
| 374 |
.succeeds() |
| 375 |
.stdout_is(" 1 1 10001 10001 10000 onelongword.txt\n"); |
| 376 |
} |
| 377 |
|
| 378 |
/// Test that the total size of all the files in the input dictates |
| 379 |
/// the display width. |
| 380 |
/// |
| 381 |
/// The width in digits of any count is the width in digits of the |
| 382 |
/// number of bytes in the file, regardless of whether the number of |
| 383 |
/// bytes are displayed. |
| 384 |
#[test] |
| 385 |
fn test_file_bytes_dictate_width() { |
| 386 |
// . is a directory, so minimum_width should get set to 7 |
| 387 |
#[cfg(not(windows))] |
| 388 |
const STDOUT: &str = concat!( |
| 389 |
" 0 0 0 emptyfile.txt\n", |
| 390 |
" 0 0 0 .\n", |
| 391 |
" 0 0 0 total\n", |
| 392 |
); |
| 393 |
#[cfg(windows)] |
| 394 |
const STDOUT: &str = concat!( |
| 395 |
" 0 0 0 emptyfile.txt\n", |
| 396 |
" 0 0 0 total\n", |
| 397 |
); |
| 398 |
|
| 399 |
// This file has 10,001 bytes. Five digits are required to |
| 400 |
// represent that. Even though the number of lines is 1 and the |
| 401 |
// number of words is 0, each of those counts is formatted with |
| 402 |
// five characters, filled with whitespace. |
| 403 |
new_ucmd!() |
| 404 |
.args(&["-lw", "onelongemptyline.txt"]) |
| 405 |
.succeeds() |
| 406 |
.stdout_is(" 1 0 onelongemptyline.txt\n"); |
| 407 |
|
| 408 |
// This file has zero bytes. Only one digit is required to |
| 409 |
// represent that. |
| 410 |
new_ucmd!() |
| 411 |
.args(&["-lw", "emptyfile.txt"]) |
| 412 |
.succeeds() |
| 413 |
.stdout_is("0 0 emptyfile.txt\n"); |
| 414 |
|
| 415 |
// lorem_ipsum.txt contains 772 bytes, and alice_in_wonderland.txt contains |
| 416 |
// 302 bytes. The total is 1074 bytes, which has a width of 4 |
| 417 |
new_ucmd!() |
| 418 |
.args(&["-lwc", "alice_in_wonderland.txt", "lorem_ipsum.txt"]) |
| 419 |
.succeeds() |
| 420 |
.stdout_is(concat!( |
| 421 |
" 5 57 302 alice_in_wonderland.txt\n", |
| 422 |
" 13 109 772 lorem_ipsum.txt\n", |
| 423 |
" 18 166 1074 total\n", |
| 424 |
)); |
| 425 |
|
| 426 |
new_ucmd!() |
| 427 |
.args(&["-lwc", "emptyfile.txt", "."]) |
| 428 |
.fails() |
| 429 |
.stdout_is(STDOUT); |
| 430 |
} |
| 431 |
|
| 432 |
/// Test that getting counts from a directory is an error. |
| 433 |
#[test] |
| 434 |
fn test_read_from_directory_error() { |
| 435 |
let cmd = new_ucmd!().args(&["."]).fails(); |
| 436 |
if std::env::var("UUTESTS_WASM_RUNNER").is_ok() { |
| 437 |
// wasi-libc may report a different error string than the host libc |
| 438 |
cmd.stderr_contains("wc: .:"); |
| 439 |
} else if cfg!(windows) { |
| 440 |
cmd.stderr_contains(".: Permission denied").no_stdout(); |
| 441 |
} else { |
| 442 |
cmd.stderr_contains(".: Is a directory") |
| 443 |
.stdout_is(" 0 0 0 .\n"); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
#[cfg(unix)] |
| 448 |
#[test] |
| 449 |
fn test_read_error_order_with_stderr_to_stdout() { |
| 450 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 451 |
at.mkdir("ioerrdir"); |
| 452 |
|
| 453 |
let cmd = ucmd.arg("ioerrdir").stderr_to_stdout().fails(); |
| 454 |
if std::env::var("UUTESTS_WASM_RUNNER").is_ok() { |
| 455 |
// wasi-libc may report a different error string than the host libc |
| 456 |
cmd.stdout_contains("wc: ioerrdir:"); |
| 457 |
} else { |
| 458 |
let expected = format!( |
| 459 |
"{:>7} {:>7} {:>7} ioerrdir\nwc: ioerrdir: Is a directory\n", |
| 460 |
0, 0, 0 |
| 461 |
); |
| 462 |
cmd.stdout_only(expected); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
/// Test that getting counts from nonexistent file is an error. |
| 467 |
#[test] |
| 468 |
fn test_read_from_nonexistent_file() { |
| 469 |
new_ucmd!() |
| 470 |
.args(&["bogusfile"]) |
| 471 |
.fails() |
| 472 |
.stderr_only("wc: bogusfile: No such file or directory\n"); |
| 473 |
} |
| 474 |
|
| 475 |
#[test] |
| 476 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 477 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths (/proc) not visible")] |
| 478 |
fn test_files_from_pseudo_filesystem() { |
| 479 |
use pretty_assertions::assert_ne; |
| 480 |
let result = new_ucmd!().arg("-c").arg("/proc/cpuinfo").succeeds(); |
| 481 |
assert_ne!(result.stdout_str(), "0 /proc/cpuinfo\n"); |
| 482 |
|
| 483 |
// the following block fails on Android with a "Permission denied" error |
| 484 |
// also skip in case the kernel was not built with profiling support, e.g. WSL |
| 485 |
#[cfg(target_os = "linux")] |
| 486 |
{ |
| 487 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 488 |
if at.file_exists("/sys/kernel/profiling") { |
| 489 |
let result = ucmd.arg("-c").arg("/sys/kernel/profiling").succeeds(); |
| 490 |
let actual = at.read("/sys/kernel/profiling").len(); |
| 491 |
assert_eq!( |
| 492 |
result.stdout_str(), |
| 493 |
format!("{actual} /sys/kernel/profiling\n") |
| 494 |
); |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
#[test] |
| 500 |
fn test_files0_disabled_files_argument() { |
| 501 |
const MSG: &str = |
| 502 |
"extra operand 'lorem_ipsum.txt'\nfile operands cannot be combined with --files0-from"; |
| 503 |
new_ucmd!() |
| 504 |
.args(&["--files0-from=files0_list.txt"]) |
| 505 |
.arg("lorem_ipsum.txt") |
| 506 |
.fails() |
| 507 |
.stderr_contains(MSG) |
| 508 |
.no_stdout(); |
| 509 |
} |
| 510 |
|
| 511 |
#[test] |
| 512 |
fn test_files0_from() { |
| 513 |
// file |
| 514 |
new_ucmd!() |
| 515 |
.args(&["--files0-from=files0_list.txt"]) |
| 516 |
.succeeds() |
| 517 |
.stdout_is(concat!( |
| 518 |
" 13 109 772 lorem_ipsum.txt\n", |
| 519 |
" 18 204 1115 moby_dick.txt\n", |
| 520 |
" 5 57 302 alice_in_wonderland.txt\n", |
| 521 |
" 36 370 2189 total\n", |
| 522 |
)); |
| 523 |
|
| 524 |
// stream |
| 525 |
new_ucmd!() |
| 526 |
.args(&["--files0-from=-"]) |
| 527 |
.pipe_in_fixture("files0_list.txt") |
| 528 |
.succeeds() |
| 529 |
.stdout_is(concat!( |
| 530 |
"13 109 772 lorem_ipsum.txt\n", |
| 531 |
"18 204 1115 moby_dick.txt\n", |
| 532 |
"5 57 302 alice_in_wonderland.txt\n", |
| 533 |
"36 370 2189 total\n", |
| 534 |
)); |
| 535 |
} |
| 536 |
|
| 537 |
#[test] |
| 538 |
fn test_files0_from_with_stdin() { |
| 539 |
new_ucmd!() |
| 540 |
.args(&["--files0-from=-"]) |
| 541 |
.pipe_in("lorem_ipsum.txt") |
| 542 |
.succeeds() |
| 543 |
.stdout_is("13 109 772 lorem_ipsum.txt\n"); |
| 544 |
} |
| 545 |
|
| 546 |
#[test] |
| 547 |
fn test_files0_from_with_stdin_in_file() { |
| 548 |
new_ucmd!() |
| 549 |
.args(&["--files0-from=files0_list_with_stdin.txt"]) |
| 550 |
.pipe_in_fixture("alice_in_wonderland.txt") |
| 551 |
.succeeds() |
| 552 |
.stdout_is(concat!( |
| 553 |
" 13 109 772 lorem_ipsum.txt\n", |
| 554 |
" 18 204 1115 moby_dick.txt\n", |
| 555 |
" 5 57 302 -\n", // alice_in_wonderland.txt |
| 556 |
" 36 370 2189 total\n", |
| 557 |
)); |
| 558 |
} |
| 559 |
|
| 560 |
#[test] |
| 561 |
fn test_files0_from_with_stdin_try_read_from_stdin() { |
| 562 |
const MSG: &str = "when reading file names from standard input, no file name of '-' allowed"; |
| 563 |
new_ucmd!() |
| 564 |
.args(&["--files0-from=-"]) |
| 565 |
.pipe_in("-") |
| 566 |
.fails() |
| 567 |
.stderr_contains(MSG) |
| 568 |
.no_stdout(); |
| 569 |
} |
| 570 |
|
| 571 |
#[test] |
| 572 |
fn test_total_auto() { |
| 573 |
new_ucmd!() |
| 574 |
.args(&["lorem_ipsum.txt", "--total=auto"]) |
| 575 |
.succeeds() |
| 576 |
.stdout_is(" 13 109 772 lorem_ipsum.txt\n"); |
| 577 |
new_ucmd!() |
| 578 |
.args(&["lorem_ipsum.txt", "--tot=au"]) |
| 579 |
.succeeds() |
| 580 |
.stdout_is(" 13 109 772 lorem_ipsum.txt\n"); |
| 581 |
|
| 582 |
new_ucmd!() |
| 583 |
.args(&["lorem_ipsum.txt", "moby_dick.txt", "--total=auto"]) |
| 584 |
.succeeds() |
| 585 |
.stdout_is(concat!( |
| 586 |
" 13 109 772 lorem_ipsum.txt\n", |
| 587 |
" 18 204 1115 moby_dick.txt\n", |
| 588 |
" 31 313 1887 total\n", |
| 589 |
)); |
| 590 |
} |
| 591 |
|
| 592 |
#[test] |
| 593 |
fn test_total_always() { |
| 594 |
new_ucmd!() |
| 595 |
.args(&["lorem_ipsum.txt", "--total=always"]) |
| 596 |
.succeeds() |
| 597 |
.stdout_is(concat!( |
| 598 |
" 13 109 772 lorem_ipsum.txt\n", |
| 599 |
" 13 109 772 total\n", |
| 600 |
)); |
| 601 |
new_ucmd!() |
| 602 |
.args(&["lorem_ipsum.txt", "--total=al"]) |
| 603 |
.succeeds() |
| 604 |
.stdout_is(concat!( |
| 605 |
" 13 109 772 lorem_ipsum.txt\n", |
| 606 |
" 13 109 772 total\n", |
| 607 |
)); |
| 608 |
|
| 609 |
new_ucmd!() |
| 610 |
.args(&["lorem_ipsum.txt", "moby_dick.txt", "--total=always"]) |
| 611 |
.succeeds() |
| 612 |
.stdout_is(concat!( |
| 613 |
" 13 109 772 lorem_ipsum.txt\n", |
| 614 |
" 18 204 1115 moby_dick.txt\n", |
| 615 |
" 31 313 1887 total\n", |
| 616 |
)); |
| 617 |
} |
| 618 |
|
| 619 |
#[test] |
| 620 |
fn test_total_never() { |
| 621 |
new_ucmd!() |
| 622 |
.args(&["lorem_ipsum.txt", "--total=never"]) |
| 623 |
.succeeds() |
| 624 |
.stdout_is(" 13 109 772 lorem_ipsum.txt\n"); |
| 625 |
|
| 626 |
new_ucmd!() |
| 627 |
.args(&["lorem_ipsum.txt", "moby_dick.txt", "--total=never"]) |
| 628 |
.succeeds() |
| 629 |
.stdout_is(concat!( |
| 630 |
" 13 109 772 lorem_ipsum.txt\n", |
| 631 |
" 18 204 1115 moby_dick.txt\n", |
| 632 |
)); |
| 633 |
new_ucmd!() |
| 634 |
.args(&["lorem_ipsum.txt", "moby_dick.txt", "--total=n"]) |
| 635 |
.succeeds() |
| 636 |
.stdout_is(concat!( |
| 637 |
" 13 109 772 lorem_ipsum.txt\n", |
| 638 |
" 18 204 1115 moby_dick.txt\n", |
| 639 |
)); |
| 640 |
} |
| 641 |
|
| 642 |
#[test] |
| 643 |
fn test_total_only() { |
| 644 |
new_ucmd!() |
| 645 |
.args(&["lorem_ipsum.txt", "--total=only"]) |
| 646 |
.succeeds() |
| 647 |
.stdout_is("13 109 772\n"); |
| 648 |
|
| 649 |
new_ucmd!() |
| 650 |
.args(&["lorem_ipsum.txt", "moby_dick.txt", "--total=only"]) |
| 651 |
.succeeds() |
| 652 |
.stdout_is("31 313 1887\n"); |
| 653 |
new_ucmd!() |
| 654 |
.args(&["lorem_ipsum.txt", "moby_dick.txt", "--t=o"]) |
| 655 |
.succeeds() |
| 656 |
.stdout_is("31 313 1887\n"); |
| 657 |
} |
| 658 |
|
| 659 |
#[test] |
| 660 |
fn test_zero_length_files() { |
| 661 |
// A trailing zero is ignored, but otherwise empty file names are an error... |
| 662 |
const LIST: &str = "\0moby_dick.txt\0\0alice_in_wonderland.txt\0\0lorem_ipsum.txt\0"; |
| 663 |
|
| 664 |
// Try with and without the last \0 |
| 665 |
for l in [LIST.len(), LIST.len() - 1] { |
| 666 |
new_ucmd!() |
| 667 |
.args(&["--files0-from=-"]) |
| 668 |
.pipe_in(&LIST[..l]) |
| 669 |
.fails() |
| 670 |
.stdout_is(concat!( |
| 671 |
"18 204 1115 moby_dick.txt\n", |
| 672 |
"5 57 302 alice_in_wonderland.txt\n", |
| 673 |
"13 109 772 lorem_ipsum.txt\n", |
| 674 |
"36 370 2189 total\n", |
| 675 |
)) |
| 676 |
.stderr_is(concat!( |
| 677 |
"wc: -:1: invalid zero-length file name\n", |
| 678 |
"wc: -:3: invalid zero-length file name\n", |
| 679 |
"wc: -:5: invalid zero-length file name\n", |
| 680 |
)); |
| 681 |
} |
| 682 |
|
| 683 |
// But, just as important, a zero-length file name may still be at the end... |
| 684 |
new_ucmd!() |
| 685 |
.args(&["--files0-from=-"]) |
| 686 |
.pipe_in( |
| 687 |
LIST.as_bytes() |
| 688 |
.iter() |
| 689 |
.chain(b"\0") |
| 690 |
.copied() |
| 691 |
.collect::<Vec<_>>(), |
| 692 |
) |
| 693 |
.fails() |
| 694 |
.stdout_is(concat!( |
| 695 |
"18 204 1115 moby_dick.txt\n", |
| 696 |
"5 57 302 alice_in_wonderland.txt\n", |
| 697 |
"13 109 772 lorem_ipsum.txt\n", |
| 698 |
"36 370 2189 total\n", |
| 699 |
)) |
| 700 |
.stderr_is(concat!( |
| 701 |
"wc: -:1: invalid zero-length file name\n", |
| 702 |
"wc: -:3: invalid zero-length file name\n", |
| 703 |
"wc: -:5: invalid zero-length file name\n", |
| 704 |
"wc: -:7: invalid zero-length file name\n", |
| 705 |
)); |
| 706 |
} |
| 707 |
|
| 708 |
#[test] |
| 709 |
fn test_files0_errors_quoting() { |
| 710 |
// Column padding differs on WASI (different terminal width detection) |
| 711 |
let stdout = if std::env::var("UUTESTS_WASM_RUNNER").is_ok() { |
| 712 |
" 0 0 0 total\n" |
| 713 |
} else { |
| 714 |
"0 0 0 total\n" |
| 715 |
}; |
| 716 |
new_ucmd!() |
| 717 |
.args(&["--files0-from=files0 with nonexistent.txt"]) |
| 718 |
.fails() |
| 719 |
.stderr_is(concat!( |
| 720 |
"wc: this_file_does_not_exist.txt: No such file or directory\n", |
| 721 |
"wc: 'files0 with nonexistent.txt':2: invalid zero-length file name\n", |
| 722 |
"wc: 'this file does not exist.txt': No such file or directory\n", |
| 723 |
"wc: \"this files doesn't exist either.txt\": No such file or directory\n", |
| 724 |
)) |
| 725 |
.stdout_is(stdout); |
| 726 |
} |
| 727 |
|
| 728 |
#[test] |
| 729 |
fn test_files0_progressive_stream() { |
| 730 |
use std::process::Stdio; |
| 731 |
// You should be able to run wc and have a back-and-forth exchange with wc... |
| 732 |
let mut child = new_ucmd!() |
| 733 |
.args(&["--files0-from=-"]) |
| 734 |
.set_stdin(Stdio::piped()) |
| 735 |
.set_stdout(Stdio::piped()) |
| 736 |
.set_stderr(Stdio::piped()) |
| 737 |
.run_no_wait(); |
| 738 |
|
| 739 |
macro_rules! chk { |
| 740 |
($fn:ident, $exp:literal) => { |
| 741 |
assert_eq!(child.$fn($exp.len()), $exp.as_bytes()); |
| 742 |
}; |
| 743 |
} |
| 744 |
|
| 745 |
// File in, count out... |
| 746 |
child.write_in("moby_dick.txt\0"); |
| 747 |
chk!(stdout_exact_bytes, "18 204 1115 moby_dick.txt\n"); |
| 748 |
child.write_in("lorem_ipsum.txt\0"); |
| 749 |
chk!(stdout_exact_bytes, "13 109 772 lorem_ipsum.txt\n"); |
| 750 |
|
| 751 |
// Introduce an error! |
| 752 |
child.write_in("\0"); |
| 753 |
chk!( |
| 754 |
stderr_exact_bytes, |
| 755 |
"wc: -:3: invalid zero-length file name\n" |
| 756 |
); |
| 757 |
|
| 758 |
// wc is quick to forgive, let's move on... |
| 759 |
child.write_in("alice_in_wonderland.txt\0"); |
| 760 |
chk!(stdout_exact_bytes, "5 57 302 alice_in_wonderland.txt\n"); |
| 761 |
|
| 762 |
// Fin. |
| 763 |
child |
| 764 |
.wait() |
| 765 |
.expect("wc should finish") |
| 766 |
.failure() |
| 767 |
.stdout_only("36 370 2189 total\n"); |
| 768 |
} |
| 769 |
|
| 770 |
#[cfg(target_os = "linux")] |
| 771 |
#[test] |
| 772 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 773 |
fn test_files0_stops_after_stdout_write_error() { |
| 774 |
use std::fs::OpenOptions; |
| 775 |
|
| 776 |
let dev_full = OpenOptions::new().write(true).open("/dev/full").unwrap(); |
| 777 |
|
| 778 |
let stderr = new_ucmd!() |
| 779 |
.args(&["--files0-from=-", "--total=never"]) |
| 780 |
.set_stdout(dev_full) |
| 781 |
.pipe_in(b"/dev/null\0/dev/null\0/dev/null\0") |
| 782 |
.fails() |
| 783 |
.stderr_str() |
| 784 |
.to_string(); |
| 785 |
|
| 786 |
assert_eq!( |
| 787 |
stderr |
| 788 |
.matches("failed to print result for /dev/null") |
| 789 |
.count(), |
| 790 |
1, |
| 791 |
"wc should stop after the first stdout write error: {stderr:?}" |
| 792 |
); |
| 793 |
} |
| 794 |
|
| 795 |
#[test] |
| 796 |
fn files0_from_dir() { |
| 797 |
#[cfg(not(windows))] |
| 798 |
macro_rules! dir_err { |
| 799 |
($p:literal) => { |
| 800 |
concat!("wc: ", $p, ": read error: Is a directory\n") |
| 801 |
}; |
| 802 |
} |
| 803 |
#[cfg(windows)] |
| 804 |
macro_rules! dir_err { |
| 805 |
($p:literal) => { |
| 806 |
concat!("wc: cannot open ", $p, " for reading: Permission denied\n") |
| 807 |
}; |
| 808 |
} |
| 809 |
#[cfg(windows)] |
| 810 |
const DOT_ERR: &str = dir_err!("'.'"); |
| 811 |
#[cfg(not(windows))] |
| 812 |
const DOT_ERR: &str = dir_err!("."); |
| 813 |
|
| 814 |
// On Unix, `read(open("."))` fails. On Windows, `open(".")` fails. Thus, the errors happen in |
| 815 |
// different contexts. On WASI, the error string may differ (e.g., "Bad file descriptor"). |
| 816 |
let wasm = std::env::var("UUTESTS_WASM_RUNNER").is_ok(); |
| 817 |
|
| 818 |
let cmd = new_ucmd!().args(&["--files0-from=dir with spaces"]).fails(); |
| 819 |
if wasm { |
| 820 |
cmd.stderr_contains("wc: 'dir with spaces': read error:"); |
| 821 |
} else { |
| 822 |
cmd.stderr_only(dir_err!("'dir with spaces'")); |
| 823 |
} |
| 824 |
|
| 825 |
// Those contexts have different rules about quoting in errors... |
| 826 |
let cmd = new_ucmd!().args(&["--files0-from=."]).fails(); |
| 827 |
if wasm { |
| 828 |
cmd.stderr_contains("wc: .: read error:"); |
| 829 |
} else { |
| 830 |
cmd.stderr_only(DOT_ERR); |
| 831 |
} |
| 832 |
|
| 833 |
// That also means you cannot `< . wc --files0-from=-` on Windows. |
| 834 |
#[cfg(not(windows))] |
| 835 |
new_ucmd!() |
| 836 |
.args(&["--files0-from=-"]) |
| 837 |
.set_stdin(std::fs::File::open(".").unwrap()) |
| 838 |
.fails() |
| 839 |
.stderr_only(dir_err!("-")); |
| 840 |
} |
| 841 |
|
| 842 |
#[test] |
| 843 |
fn test_args_override() { |
| 844 |
new_ucmd!() |
| 845 |
.args(&["-ll", "-l", "alice_in_wonderland.txt"]) |
| 846 |
.succeeds() |
| 847 |
.stdout_is("5 alice_in_wonderland.txt\n"); |
| 848 |
|
| 849 |
new_ucmd!() |
| 850 |
.args(&["--total=always", "--total=never", "alice_in_wonderland.txt"]) |
| 851 |
.succeeds() |
| 852 |
.stdout_is(" 5 57 302 alice_in_wonderland.txt\n"); |
| 853 |
} |
| 854 |
|
| 855 |
#[test] |
| 856 |
fn wc_w_words_with_emoji_separator() { |
| 857 |
new_ucmd!() |
| 858 |
.args(&["-w"]) |
| 859 |
.pipe_in("foo 💐 bar\n") |
| 860 |
.succeeds() |
| 861 |
.stdout_contains("3"); |
| 862 |
} |
| 863 |
|
| 864 |
#[test] |
| 865 |
fn test_invalid_byte_sequence_word_count() { |
| 866 |
// wc should count invalid byte sequences as words |
| 867 |
// Input: "a \xff b\n" should produce: 1 line, 3 words, 6 bytes |
| 868 |
new_ucmd!() |
| 869 |
.pipe_in([b'a', b' ', 0xff, b' ', b'b', b'\n']) |
| 870 |
.succeeds() |
| 871 |
.stdout_is(" 1 3 6\n"); |
| 872 |
} |
| 873 |
|
| 874 |
#[cfg(unix)] |
| 875 |
#[test] |
| 876 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 877 |
fn test_simd_respects_glibc_tunables() { |
| 878 |
use std::fmt::Write as _; |
| 879 |
|
| 880 |
// Ensure debug output reflects that SIMD paths are disabled via GLIBC_TUNABLES |
| 881 |
let debug_output = new_ucmd!() |
| 882 |
.args(&["-l", "--debug", "/dev/null"]) |
| 883 |
.env("GLIBC_TUNABLES", "glibc.cpu.hwcaps=-AVX2,-AVX512F") |
| 884 |
.succeeds() |
| 885 |
.stderr_str() |
| 886 |
.to_string(); |
| 887 |
assert!( |
| 888 |
!debug_output.contains("using hardware support"), |
| 889 |
"SIMD should be reported as disabled when GLIBC_TUNABLES blocks AVX features: {debug_output}" |
| 890 |
); |
| 891 |
assert!( |
| 892 |
debug_output.contains("hardware support disabled"), |
| 893 |
"Debug output should acknowledge GLIBC_TUNABLES restrictions: {debug_output}" |
| 894 |
); |
| 895 |
|
| 896 |
// WC results should be identical with and without GLIBC_TUNABLES overrides |
| 897 |
let sample_sizes = [0usize, 1, 7, 128, 513, 999]; |
| 898 |
for &lines in &sample_sizes { |
| 899 |
let content: String = (0..lines).fold(String::new(), |mut acc, i| { |
| 900 |
// Build the input buffer efficiently without allocating per line. |
| 901 |
let _ = writeln!(acc, "{i}"); |
| 902 |
acc |
| 903 |
}); |
| 904 |
|
| 905 |
let base = new_ucmd!() |
| 906 |
.arg("-l") |
| 907 |
.pipe_in(content.clone()) |
| 908 |
.succeeds() |
| 909 |
.stdout_str() |
| 910 |
.trim() |
| 911 |
.to_string(); |
| 912 |
|
| 913 |
let no_avx512 = new_ucmd!() |
| 914 |
.arg("-l") |
| 915 |
.env("GLIBC_TUNABLES", "glibc.cpu.hwcaps=-AVX512F") |
| 916 |
.pipe_in(content.clone()) |
| 917 |
.succeeds() |
| 918 |
.stdout_str() |
| 919 |
.trim() |
| 920 |
.to_string(); |
| 921 |
|
| 922 |
let no_avx2_avx512 = new_ucmd!() |
| 923 |
.arg("-l") |
| 924 |
.env("GLIBC_TUNABLES", "glibc.cpu.hwcaps=-AVX2,-AVX512F") |
| 925 |
.pipe_in(content) |
| 926 |
.succeeds() |
| 927 |
.stdout_str() |
| 928 |
.trim() |
| 929 |
.to_string(); |
| 930 |
|
| 931 |
assert_eq!( |
| 932 |
base, no_avx512, |
| 933 |
"Line counts should not change when AVX512 is disabled (lines={lines})" |
| 934 |
); |
| 935 |
assert_eq!( |
| 936 |
base, no_avx2_avx512, |
| 937 |
"Line counts should not change when AVX2/AVX512 are disabled (lines={lines})" |
| 938 |
); |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
#[test] |
| 943 |
fn test_posixly_correct_whitespace() { |
| 944 |
let input = "word\u{00A0}word"; // Non-breaking space |
| 945 |
|
| 946 |
// Default: Unicode whitespace is respected |
| 947 |
new_ucmd!() |
| 948 |
.arg("-w") |
| 949 |
.pipe_in(input) |
| 950 |
.succeeds() |
| 951 |
.stdout_is("2\n"); |
| 952 |
|
| 953 |
// POSIXLY_CORRECT: Only ASCII whitespace |
| 954 |
new_ucmd!() |
| 955 |
.arg("-w") |
| 956 |
.env("POSIXLY_CORRECT", "1") |
| 957 |
.pipe_in(input) |
| 958 |
.succeeds() |
| 959 |
.stdout_is("1\n"); |
| 960 |
} |