| 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 |
// spell-checker:ignore (words) bogusfile emptyfile abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstu |
| 7 |
|
| 8 |
#[cfg(all( |
| 9 |
not(target_vendor = "apple"), |
| 10 |
not(target_os = "android"), |
| 11 |
not(target_os = "freebsd"), |
| 12 |
not(target_os = "openbsd"), |
| 13 |
not(windows) |
| 14 |
))] |
| 15 |
use std::io::Read; |
| 16 |
use uutests::new_ucmd; |
| 17 |
use uutests::util::TestScenario; |
| 18 |
use uutests::util_name; |
| 19 |
static INPUT: &str = "lorem_ipsum.txt"; |
| 20 |
|
| 21 |
#[test] |
| 22 |
fn test_invalid_arg() { |
| 23 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 24 |
} |
| 25 |
|
| 26 |
#[test] |
| 27 |
fn test_stdin_default() { |
| 28 |
new_ucmd!() |
| 29 |
.pipe_in_fixture(INPUT) |
| 30 |
.succeeds() |
| 31 |
.stdout_is_fixture("lorem_ipsum_default.expected"); |
| 32 |
} |
| 33 |
|
| 34 |
#[test] |
| 35 |
fn test_stdin_1_line_obsolete() { |
| 36 |
new_ucmd!() |
| 37 |
.args(&["-1"]) |
| 38 |
.pipe_in_fixture(INPUT) |
| 39 |
.succeeds() |
| 40 |
.stdout_is_fixture("lorem_ipsum_1_line.expected"); |
| 41 |
} |
| 42 |
|
| 43 |
#[test] |
| 44 |
fn test_stdin_1_line() { |
| 45 |
new_ucmd!() |
| 46 |
.args(&["-n", "1"]) |
| 47 |
.pipe_in_fixture(INPUT) |
| 48 |
.succeeds() |
| 49 |
.stdout_is_fixture("lorem_ipsum_1_line.expected"); |
| 50 |
} |
| 51 |
|
| 52 |
#[test] |
| 53 |
fn test_stdin_negative_23_line() { |
| 54 |
new_ucmd!() |
| 55 |
.args(&["-n", "-23"]) |
| 56 |
.pipe_in_fixture(INPUT) |
| 57 |
.succeeds() |
| 58 |
.stdout_is_fixture("lorem_ipsum_1_line.expected"); |
| 59 |
} |
| 60 |
|
| 61 |
#[test] |
| 62 |
fn test_stdin_5_chars() { |
| 63 |
new_ucmd!() |
| 64 |
.args(&["-c", "5"]) |
| 65 |
.pipe_in_fixture(INPUT) |
| 66 |
.succeeds() |
| 67 |
.stdout_is_fixture("lorem_ipsum_5_chars.expected"); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn test_single_default() { |
| 72 |
new_ucmd!() |
| 73 |
.arg(INPUT) |
| 74 |
.succeeds() |
| 75 |
.stdout_is_fixture("lorem_ipsum_default.expected"); |
| 76 |
} |
| 77 |
|
| 78 |
#[test] |
| 79 |
fn test_single_1_line_obsolete() { |
| 80 |
new_ucmd!() |
| 81 |
.args(&["-1", INPUT]) |
| 82 |
.succeeds() |
| 83 |
.stdout_is_fixture("lorem_ipsum_1_line.expected"); |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn test_single_1_line() { |
| 88 |
new_ucmd!() |
| 89 |
.args(&["-n", "1", INPUT]) |
| 90 |
.succeeds() |
| 91 |
.stdout_is_fixture("lorem_ipsum_1_line.expected"); |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn test_single_1_line_presume_input_pipe() { |
| 96 |
new_ucmd!() |
| 97 |
.args(&["---presume-input-pipe", "-n", "1", INPUT]) |
| 98 |
.succeeds() |
| 99 |
.stdout_is_fixture("lorem_ipsum_1_line.expected"); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn test_single_5_chars() { |
| 104 |
new_ucmd!() |
| 105 |
.args(&["-c", "5", INPUT]) |
| 106 |
.succeeds() |
| 107 |
.stdout_is_fixture("lorem_ipsum_5_chars.expected"); |
| 108 |
} |
| 109 |
|
| 110 |
#[test] |
| 111 |
fn test_verbose() { |
| 112 |
new_ucmd!() |
| 113 |
.args(&["-v", INPUT]) |
| 114 |
.succeeds() |
| 115 |
.stdout_is_fixture("lorem_ipsum_verbose.expected"); |
| 116 |
} |
| 117 |
|
| 118 |
#[test] |
| 119 |
fn test_spams_newline() { |
| 120 |
new_ucmd!().pipe_in("a").succeeds().stdout_is("a"); |
| 121 |
} |
| 122 |
|
| 123 |
#[test] |
| 124 |
fn test_byte_syntax() { |
| 125 |
new_ucmd!() |
| 126 |
.args(&["-1c"]) |
| 127 |
.pipe_in("abc") |
| 128 |
.succeeds() |
| 129 |
.stdout_is("a"); |
| 130 |
} |
| 131 |
|
| 132 |
#[test] |
| 133 |
fn test_line_syntax() { |
| 134 |
new_ucmd!() |
| 135 |
.args(&["-n", "2048m"]) |
| 136 |
.pipe_in("a\n") |
| 137 |
.succeeds() |
| 138 |
.stdout_is("a\n"); |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn test_zero_terminated_syntax() { |
| 143 |
new_ucmd!() |
| 144 |
.args(&["-z", "-n", "1"]) |
| 145 |
.pipe_in("x\0y") |
| 146 |
.succeeds() |
| 147 |
.stdout_is("x\0"); |
| 148 |
} |
| 149 |
|
| 150 |
#[test] |
| 151 |
fn test_zero_terminated_syntax_2() { |
| 152 |
new_ucmd!() |
| 153 |
.args(&["-z", "-n", "2"]) |
| 154 |
.pipe_in("x\0y") |
| 155 |
.succeeds() |
| 156 |
.stdout_is("x\0y"); |
| 157 |
} |
| 158 |
|
| 159 |
#[test] |
| 160 |
fn test_non_terminated_input() { |
| 161 |
new_ucmd!() |
| 162 |
.args(&["-n", "-1"]) |
| 163 |
.pipe_in("x\ny") |
| 164 |
.succeeds() |
| 165 |
.stdout_is("x\n"); |
| 166 |
} |
| 167 |
|
| 168 |
#[test] |
| 169 |
fn test_zero_terminated_negative_lines() { |
| 170 |
new_ucmd!() |
| 171 |
.args(&["-z", "-n", "-1"]) |
| 172 |
.pipe_in("x\0y\0z\0") |
| 173 |
.succeeds() |
| 174 |
.stdout_is("x\0y\0"); |
| 175 |
} |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn test_negative_byte_syntax() { |
| 179 |
new_ucmd!() |
| 180 |
.args(&["--bytes=-2"]) |
| 181 |
.pipe_in("a\n") |
| 182 |
.succeeds() |
| 183 |
.no_output(); |
| 184 |
} |
| 185 |
|
| 186 |
#[test] |
| 187 |
fn test_negative_bytes_greater_than_input_size_stdin() { |
| 188 |
new_ucmd!() |
| 189 |
.args(&["-c", "-2"]) |
| 190 |
.pipe_in("a") |
| 191 |
.succeeds() |
| 192 |
.no_output(); |
| 193 |
} |
| 194 |
|
| 195 |
#[test] |
| 196 |
fn test_negative_bytes_greater_than_input_size_file() { |
| 197 |
let ts = TestScenario::new(util_name!()); |
| 198 |
let at = &ts.fixtures; |
| 199 |
at.write_bytes("f", b"a"); |
| 200 |
ts.ucmd().args(&["-c", "-2", "f"]).succeeds().no_output(); |
| 201 |
} |
| 202 |
|
| 203 |
#[test] |
| 204 |
fn test_negative_zero_lines() { |
| 205 |
new_ucmd!() |
| 206 |
.arg("--lines=-0") |
| 207 |
.pipe_in("a\nb\n") |
| 208 |
.succeeds() |
| 209 |
.stdout_is("a\nb\n"); |
| 210 |
new_ucmd!() |
| 211 |
.arg("--lines=-0") |
| 212 |
.pipe_in("a\nb") |
| 213 |
.succeeds() |
| 214 |
.stdout_is("a\nb"); |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
fn test_negative_zero_bytes() { |
| 219 |
new_ucmd!() |
| 220 |
.args(&["--bytes=-0"]) |
| 221 |
.pipe_in("qwerty") |
| 222 |
.succeeds() |
| 223 |
.stdout_is("qwerty"); |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn test_zero_bytes_with_suffix() { |
| 228 |
// --bytes=0K makes head stop before draining stdin, so the harness's write may fail |
| 229 |
// with EPIPE. That is expected and must not fail the test. |
| 230 |
// |
| 231 |
// "0K" must be 0 bytes, not 1KiB (bare suffix parses as 1) |
| 232 |
new_ucmd!() |
| 233 |
.args(&["--bytes=0K"]) |
| 234 |
.ignore_stdin_write_error() |
| 235 |
.pipe_in("qwerty") |
| 236 |
.ignore_stdin_write_error() |
| 237 |
.succeeds() |
| 238 |
.no_output(); |
| 239 |
new_ucmd!() |
| 240 |
.args(&["--bytes=00K"]) |
| 241 |
.ignore_stdin_write_error() |
| 242 |
.pipe_in("qwerty") |
| 243 |
.ignore_stdin_write_error() |
| 244 |
.succeeds() |
| 245 |
.no_output(); |
| 246 |
new_ucmd!() |
| 247 |
.args(&["--bytes=+0K"]) |
| 248 |
.ignore_stdin_write_error() |
| 249 |
.pipe_in("qwerty") |
| 250 |
.ignore_stdin_write_error() |
| 251 |
.succeeds() |
| 252 |
.no_output(); |
| 253 |
} |
| 254 |
#[test] |
| 255 |
fn test_no_such_file_or_directory() { |
| 256 |
new_ucmd!() |
| 257 |
.arg("no_such_file.toml") |
| 258 |
.fails() |
| 259 |
.stderr_contains("cannot open 'no_such_file.toml' for reading: No such file or directory"); |
| 260 |
} |
| 261 |
|
| 262 |
#[test] |
| 263 |
fn test_lines_leading_zeros() { |
| 264 |
new_ucmd!() |
| 265 |
.arg("--lines=010") |
| 266 |
.pipe_in("\n\n\n\n\n\n\n\n\n\n\n\n") |
| 267 |
.succeeds() |
| 268 |
.stdout_is("\n\n\n\n\n\n\n\n\n\n"); |
| 269 |
} |
| 270 |
|
| 271 |
/// Test that each non-existent files gets its own error message printed. |
| 272 |
#[test] |
| 273 |
fn test_multiple_nonexistent_files() { |
| 274 |
new_ucmd!() |
| 275 |
.args(&["bogusfile1", "bogusfile2"]) |
| 276 |
.fails() |
| 277 |
.stdout_does_not_contain("==> bogusfile1 <==") |
| 278 |
.stderr_contains("cannot open 'bogusfile1' for reading: No such file or directory") |
| 279 |
.stdout_does_not_contain("==> bogusfile2 <==") |
| 280 |
.stderr_contains("cannot open 'bogusfile2' for reading: No such file or directory"); |
| 281 |
} |
| 282 |
|
| 283 |
// there was a bug not caught by previous tests |
| 284 |
// where for negative n > 3, the total amount of lines |
| 285 |
// was correct, but it would eat from the second line |
| 286 |
#[test] |
| 287 |
fn test_sequence_fixture() { |
| 288 |
new_ucmd!() |
| 289 |
.args(&["-n", "-10", "sequence"]) |
| 290 |
.succeeds() |
| 291 |
.stdout_is_fixture("sequence.expected"); |
| 292 |
} |
| 293 |
#[test] |
| 294 |
fn test_file_backwards() { |
| 295 |
new_ucmd!() |
| 296 |
.args(&["-c", "-10", "lorem_ipsum.txt"]) |
| 297 |
.succeeds() |
| 298 |
.stdout_is_fixture("lorem_ipsum_backwards_file.expected"); |
| 299 |
} |
| 300 |
|
| 301 |
#[test] |
| 302 |
fn test_zero_terminated() { |
| 303 |
new_ucmd!() |
| 304 |
.args(&["-z", "zero_terminated.txt"]) |
| 305 |
.succeeds() |
| 306 |
.stdout_is_fixture("zero_terminated.expected"); |
| 307 |
} |
| 308 |
|
| 309 |
#[test] |
| 310 |
fn test_obsolete_extras() { |
| 311 |
new_ucmd!() |
| 312 |
.args(&["-5zv"]) |
| 313 |
.pipe_in("1\x002\x003\x004\x005\x006") |
| 314 |
.succeeds() |
| 315 |
.stdout_is("==> standard input <==\n1\x002\x003\x004\x005\0"); |
| 316 |
} |
| 317 |
|
| 318 |
#[test] |
| 319 |
fn test_multiple_files() { |
| 320 |
new_ucmd!() |
| 321 |
.args(&["emptyfile.txt", "emptyfile.txt"]) |
| 322 |
.succeeds() |
| 323 |
.stdout_is("==> emptyfile.txt <==\n\n==> emptyfile.txt <==\n"); |
| 324 |
} |
| 325 |
|
| 326 |
#[test] |
| 327 |
fn test_multiple_files_with_stdin() { |
| 328 |
new_ucmd!() |
| 329 |
.args(&["emptyfile.txt", "-", "emptyfile.txt"]) |
| 330 |
.pipe_in("hello\n") |
| 331 |
.succeeds() |
| 332 |
.stdout_is( |
| 333 |
"==> emptyfile.txt <== |
| 334 |
|
| 335 |
==> standard input <== |
| 336 |
hello |
| 337 |
|
| 338 |
==> emptyfile.txt <== |
| 339 |
", |
| 340 |
); |
| 341 |
} |
| 342 |
|
| 343 |
#[test] |
| 344 |
fn test_bad_utf8() { |
| 345 |
let bytes: &[u8] = b"\xfc\x80\x80\x80\x80\xaf"; |
| 346 |
new_ucmd!() |
| 347 |
.args(&["-c", "6"]) |
| 348 |
.pipe_in(bytes) |
| 349 |
.succeeds() |
| 350 |
.stdout_is_bytes(bytes); |
| 351 |
} |
| 352 |
|
| 353 |
#[test] |
| 354 |
fn test_bad_utf8_lines() { |
| 355 |
let input: &[u8] = |
| 356 |
b"\xfc\x80\x80\x80\x80\xaf\nb\xfc\x80\x80\x80\x80\xaf\nb\xfc\x80\x80\x80\x80\xaf"; |
| 357 |
let output = b"\xfc\x80\x80\x80\x80\xaf\nb\xfc\x80\x80\x80\x80\xaf\n"; |
| 358 |
new_ucmd!() |
| 359 |
.args(&["-n", "2"]) |
| 360 |
.pipe_in(input) |
| 361 |
.succeeds() |
| 362 |
.stdout_is_bytes(output); |
| 363 |
} |
| 364 |
|
| 365 |
#[test] |
| 366 |
fn test_head_invalid_num() { |
| 367 |
new_ucmd!() |
| 368 |
.args(&["-c", "1024R", "emptyfile.txt"]) |
| 369 |
.succeeds() |
| 370 |
.no_output(); |
| 371 |
new_ucmd!() |
| 372 |
.args(&["-n", "1024R", "emptyfile.txt"]) |
| 373 |
.succeeds() |
| 374 |
.no_output(); |
| 375 |
new_ucmd!() |
| 376 |
.args(&["-c", "1Y", "emptyfile.txt"]) |
| 377 |
.succeeds() |
| 378 |
.no_output(); |
| 379 |
new_ucmd!() |
| 380 |
.args(&["-n", "1Y", "emptyfile.txt"]) |
| 381 |
.succeeds() |
| 382 |
.no_output(); |
| 383 |
#[cfg(target_pointer_width = "32")] |
| 384 |
{ |
| 385 |
let sizes = ["1000G", "10T"]; |
| 386 |
for size in &sizes { |
| 387 |
new_ucmd!().args(&["-c", size]).succeeds(); |
| 388 |
} |
| 389 |
} |
| 390 |
#[cfg(target_pointer_width = "32")] |
| 391 |
{ |
| 392 |
let sizes = ["-1000G", "-10T"]; |
| 393 |
for size in &sizes { |
| 394 |
new_ucmd!().args(&["-c", size]).succeeds().no_output(); |
| 395 |
} |
| 396 |
} |
| 397 |
new_ucmd!() |
| 398 |
.args(&["-c", "-³"]) |
| 399 |
.fails() |
| 400 |
.stderr_is("head: invalid number of bytes: '³'\n"); |
| 401 |
} |
| 402 |
|
| 403 |
#[test] |
| 404 |
fn test_head_num_with_undocumented_sign_bytes() { |
| 405 |
// tail: '-' is not documented (8.32 man pages) |
| 406 |
// head: '+' is not documented (8.32 man pages) |
| 407 |
const ALPHABET: &str = "abcdefghijklmnopqrstuvwxyz"; |
| 408 |
new_ucmd!() |
| 409 |
.args(&["-c", "5"]) |
| 410 |
.pipe_in(ALPHABET) |
| 411 |
.succeeds() |
| 412 |
.stdout_is("abcde"); |
| 413 |
new_ucmd!() |
| 414 |
.args(&["-c", "-5"]) |
| 415 |
.pipe_in(ALPHABET) |
| 416 |
.succeeds() |
| 417 |
.stdout_is("abcdefghijklmnopqrstu"); |
| 418 |
new_ucmd!() |
| 419 |
.args(&["-c", "+5"]) |
| 420 |
.pipe_in(ALPHABET) |
| 421 |
.succeeds() |
| 422 |
.stdout_is("abcde"); |
| 423 |
} |
| 424 |
|
| 425 |
#[test] |
| 426 |
fn test_presume_input_pipe_default() { |
| 427 |
new_ucmd!() |
| 428 |
.args(&["---presume-input-pipe"]) |
| 429 |
.pipe_in_fixture(INPUT) |
| 430 |
.succeeds() |
| 431 |
.stdout_is_fixture("lorem_ipsum_default.expected"); |
| 432 |
} |
| 433 |
|
| 434 |
#[test] |
| 435 |
fn test_presume_input_pipe_5_chars() { |
| 436 |
new_ucmd!() |
| 437 |
.args(&["-c", "5", "---presume-input-pipe"]) |
| 438 |
.pipe_in_fixture(INPUT) |
| 439 |
.succeeds() |
| 440 |
.stdout_is_fixture("lorem_ipsum_5_chars.expected"); |
| 441 |
} |
| 442 |
|
| 443 |
#[test] |
| 444 |
#[cfg_attr(wasi_runner, ignore = "WASI: no subprocess spawning")] |
| 445 |
fn test_all_but_last_bytes_large_file_piped() { |
| 446 |
// Validate print-all-but-last-n-bytes with a large piped-in (i.e. non-seekable) file. |
| 447 |
let scene = TestScenario::new(util_name!()); |
| 448 |
let fixtures = &scene.fixtures; |
| 449 |
|
| 450 |
// First, create all our fixtures. |
| 451 |
let seq_20000_file_name = "seq_20000"; |
| 452 |
let seq_19000_file_name = "seq_19000"; |
| 453 |
let seq_19001_20000_file_name = "seq_19001_20000"; |
| 454 |
scene |
| 455 |
.cmd("seq") |
| 456 |
.arg("20000") |
| 457 |
.set_stdout(fixtures.make_file(seq_20000_file_name)) |
| 458 |
.succeeds(); |
| 459 |
scene |
| 460 |
.cmd("seq") |
| 461 |
.arg("19000") |
| 462 |
.set_stdout(fixtures.make_file(seq_19000_file_name)) |
| 463 |
.succeeds(); |
| 464 |
scene |
| 465 |
.cmd("seq") |
| 466 |
.args(&["19001", "20000"]) |
| 467 |
.set_stdout(fixtures.make_file(seq_19001_20000_file_name)) |
| 468 |
.succeeds(); |
| 469 |
|
| 470 |
let seq_19001_20000_file_length = fixtures |
| 471 |
.open(seq_19001_20000_file_name) |
| 472 |
.metadata() |
| 473 |
.unwrap() |
| 474 |
.len(); |
| 475 |
scene |
| 476 |
.ucmd() |
| 477 |
.args(&["-c", &format!("-{seq_19001_20000_file_length}")]) |
| 478 |
.pipe_in_fixture(seq_20000_file_name) |
| 479 |
.succeeds() |
| 480 |
.stdout_only_fixture(seq_19000_file_name); |
| 481 |
} |
| 482 |
|
| 483 |
#[test] |
| 484 |
fn test_all_but_last_lines_large_file_presume_input_pipe() { |
| 485 |
// Validate print-all-but-last-n-lines on the non-seekable path for a large terminated input. |
| 486 |
// This input shape forces repeated buffer reuse while some chunks end mid-line. |
| 487 |
let scene = TestScenario::new(util_name!()); |
| 488 |
let fixtures = &scene.fixtures; |
| 489 |
|
| 490 |
let input_file_name = "reused_line_chunks"; |
| 491 |
let expected_output_file_name = "reused_line_chunks_elide_last"; |
| 492 |
let line = "aaaaaa\n"; |
| 493 |
let input_line_count: usize = 20_000; |
| 494 |
|
| 495 |
let mut input = String::with_capacity(line.len() * input_line_count); |
| 496 |
for _ in 0..input_line_count { |
| 497 |
input.push_str(line); |
| 498 |
} |
| 499 |
fixtures.write(input_file_name, &input); |
| 500 |
fixtures.write( |
| 501 |
expected_output_file_name, |
| 502 |
&line.repeat(input_line_count - 1), |
| 503 |
); |
| 504 |
|
| 505 |
scene |
| 506 |
.ucmd() |
| 507 |
.args(&["---presume-input-pipe", "-n", "-1", input_file_name]) |
| 508 |
.succeeds() |
| 509 |
.stdout_only_fixture(expected_output_file_name); |
| 510 |
} |
| 511 |
|
| 512 |
#[test] |
| 513 |
#[cfg_attr(wasi_runner, ignore = "WASI: no subprocess spawning")] |
| 514 |
fn test_all_but_last_lines_large_file() { |
| 515 |
// Create our fixtures on the fly. We need the input file to be at least double |
| 516 |
// the size of BUF_SIZE as specified in head.rs. Go for something a bit bigger |
| 517 |
// than that. |
| 518 |
let scene = TestScenario::new(util_name!()); |
| 519 |
let fixtures = &scene.fixtures; |
| 520 |
let seq_20000_file_name = "seq_20000"; |
| 521 |
let seq_20000_truncated_file_name = "seq_20000_truncated"; |
| 522 |
let seq_1000_file_name = "seq_1000"; |
| 523 |
scene |
| 524 |
.cmd("seq") |
| 525 |
.arg("20000") |
| 526 |
.set_stdout(fixtures.make_file(seq_20000_file_name)) |
| 527 |
.succeeds(); |
| 528 |
// Create a file the same as seq_20000 except for the final terminating endline. |
| 529 |
scene |
| 530 |
.ucmd() |
| 531 |
.args(&["-c", "-1", seq_20000_file_name]) |
| 532 |
.set_stdout(fixtures.make_file(seq_20000_truncated_file_name)) |
| 533 |
.succeeds(); |
| 534 |
scene |
| 535 |
.cmd("seq") |
| 536 |
.arg("1000") |
| 537 |
.set_stdout(fixtures.make_file(seq_1000_file_name)) |
| 538 |
.succeeds(); |
| 539 |
|
| 540 |
// Now run our tests. |
| 541 |
scene |
| 542 |
.ucmd() |
| 543 |
.args(&["-n", "-19000", seq_20000_file_name]) |
| 544 |
.succeeds() |
| 545 |
.stdout_only_fixture(seq_1000_file_name); |
| 546 |
|
| 547 |
scene |
| 548 |
.ucmd() |
| 549 |
.args(&["-n", "-20000", seq_20000_file_name]) |
| 550 |
.succeeds() |
| 551 |
.stdout_only_fixture("emptyfile.txt"); |
| 552 |
|
| 553 |
scene |
| 554 |
.ucmd() |
| 555 |
.args(&["-n", "-20001", seq_20000_file_name]) |
| 556 |
.succeeds() |
| 557 |
.stdout_only_fixture("emptyfile.txt"); |
| 558 |
|
| 559 |
// Confirm correct behavior when the input file doesn't end with a newline. |
| 560 |
scene |
| 561 |
.ucmd() |
| 562 |
.args(&["-n", "-19000", seq_20000_truncated_file_name]) |
| 563 |
.succeeds() |
| 564 |
.stdout_only_fixture(seq_1000_file_name); |
| 565 |
|
| 566 |
scene |
| 567 |
.ucmd() |
| 568 |
.args(&["-n", "-20000", seq_20000_truncated_file_name]) |
| 569 |
.succeeds() |
| 570 |
.stdout_only_fixture("emptyfile.txt"); |
| 571 |
|
| 572 |
scene |
| 573 |
.ucmd() |
| 574 |
.args(&["-n", "-20001", seq_20000_truncated_file_name]) |
| 575 |
.succeeds() |
| 576 |
.stdout_only_fixture("emptyfile.txt"); |
| 577 |
} |
| 578 |
|
| 579 |
#[cfg(all( |
| 580 |
not(target_vendor = "apple"), |
| 581 |
not(target_os = "android"), |
| 582 |
not(target_os = "freebsd"), |
| 583 |
not(target_os = "openbsd"), |
| 584 |
not(windows) |
| 585 |
))] |
| 586 |
#[test] |
| 587 |
#[cfg_attr( |
| 588 |
wasi_runner, |
| 589 |
ignore = "WASI: stdin file position not preserved through wasmtime" |
| 590 |
)] |
| 591 |
fn test_validate_stdin_offset_lines() { |
| 592 |
// A handful of unix-only tests to validate behavior when reading from stdin on a seekable |
| 593 |
// file. GNU-compatibility requires that the stdin file be left such that if another |
| 594 |
// process is invoked on the same stdin file after head has run, the subsequent file should |
| 595 |
// start reading from the byte after the last byte printed by head. |
| 596 |
// Since this is unix-only requirement, keep this as a separate test rather than adding a |
| 597 |
// conditionally-compiled segment to multiple tests. |
| 598 |
// |
| 599 |
// Test scenarios... |
| 600 |
// 1 - Print the first n lines |
| 601 |
// 2 - Print all-but the last n lines |
| 602 |
// 3 - Print all but the last n lines, large file. |
| 603 |
let scene = TestScenario::new(util_name!()); |
| 604 |
let fixtures = &scene.fixtures; |
| 605 |
|
| 606 |
// Test 1 - Print the first n lines |
| 607 |
fixtures.write("f1", "a\nb\nc\n"); |
| 608 |
let file = fixtures.open("f1"); |
| 609 |
let mut file_shadow = file.try_clone().unwrap(); |
| 610 |
scene |
| 611 |
.ucmd() |
| 612 |
.args(&["-n", "1"]) |
| 613 |
.set_stdin(file) |
| 614 |
.succeeds() |
| 615 |
.stdout_only("a\n"); |
| 616 |
let mut bytes_remaining_in_stdin = vec![]; |
| 617 |
assert_eq!( |
| 618 |
file_shadow |
| 619 |
.read_to_end(&mut bytes_remaining_in_stdin) |
| 620 |
.unwrap(), |
| 621 |
4 |
| 622 |
); |
| 623 |
assert_eq!( |
| 624 |
String::from_utf8(bytes_remaining_in_stdin).unwrap(), |
| 625 |
"b\nc\n" |
| 626 |
); |
| 627 |
|
| 628 |
// Test 2 - Print all-but the last n lines |
| 629 |
fixtures.write("f2", "a\nb\nc\n"); |
| 630 |
let file = fixtures.open("f2"); |
| 631 |
let mut file_shadow = file.try_clone().unwrap(); |
| 632 |
scene |
| 633 |
.ucmd() |
| 634 |
.args(&["-n", "-1"]) |
| 635 |
.set_stdin(file) |
| 636 |
.succeeds() |
| 637 |
.stdout_only("a\nb\n"); |
| 638 |
let mut bytes_remaining_in_stdin = vec![]; |
| 639 |
assert_eq!( |
| 640 |
file_shadow |
| 641 |
.read_to_end(&mut bytes_remaining_in_stdin) |
| 642 |
.unwrap(), |
| 643 |
2 |
| 644 |
); |
| 645 |
assert_eq!(String::from_utf8(bytes_remaining_in_stdin).unwrap(), "c\n"); |
| 646 |
|
| 647 |
// Test 3 - Print all but the last n lines, large input file. |
| 648 |
// First, create all our fixtures. |
| 649 |
let seq_20000_file_name = "seq_20000"; |
| 650 |
let seq_1000_file_name = "seq_1000"; |
| 651 |
let seq_1001_20000_file_name = "seq_1001_20000"; |
| 652 |
scene |
| 653 |
.cmd("seq") |
| 654 |
.arg("20000") |
| 655 |
.set_stdout(fixtures.make_file(seq_20000_file_name)) |
| 656 |
.succeeds(); |
| 657 |
scene |
| 658 |
.cmd("seq") |
| 659 |
.arg("1000") |
| 660 |
.set_stdout(fixtures.make_file(seq_1000_file_name)) |
| 661 |
.succeeds(); |
| 662 |
scene |
| 663 |
.cmd("seq") |
| 664 |
.args(&["1001", "20000"]) |
| 665 |
.set_stdout(fixtures.make_file(seq_1001_20000_file_name)) |
| 666 |
.succeeds(); |
| 667 |
|
| 668 |
let file = fixtures.open(seq_20000_file_name); |
| 669 |
let file_shadow = file.try_clone().unwrap(); |
| 670 |
scene |
| 671 |
.ucmd() |
| 672 |
.args(&["-n", "-19000"]) |
| 673 |
.set_stdin(file) |
| 674 |
.succeeds() |
| 675 |
.stdout_only_fixture(seq_1000_file_name); |
| 676 |
scene |
| 677 |
.cmd("cat") |
| 678 |
.set_stdin(file_shadow) |
| 679 |
.succeeds() |
| 680 |
.stdout_only_fixture(seq_1001_20000_file_name); |
| 681 |
} |
| 682 |
|
| 683 |
#[cfg(all( |
| 684 |
not(target_vendor = "apple"), |
| 685 |
not(target_os = "android"), |
| 686 |
not(target_os = "freebsd"), |
| 687 |
not(target_os = "openbsd"), |
| 688 |
not(windows) |
| 689 |
))] |
| 690 |
#[test] |
| 691 |
#[cfg_attr( |
| 692 |
wasi_runner, |
| 693 |
ignore = "WASI: stdin file position not preserved through wasmtime" |
| 694 |
)] |
| 695 |
fn test_validate_stdin_offset_bytes() { |
| 696 |
// A handful of unix-only tests to validate behavior when reading from stdin on a seekable |
| 697 |
// file. GNU-compatibility requires that the stdin file be left such that if another |
| 698 |
// process is invoked on the same stdin file after head has run, the subsequent file should |
| 699 |
// start reading from the byte after the last byte printed by head. |
| 700 |
// Since this is unix-only requirement, keep this as a separate test rather than adding a |
| 701 |
// conditionally-compiled segment to multiple tests. |
| 702 |
// |
| 703 |
// Test scenarios... |
| 704 |
// 1 - Print the first n bytes |
| 705 |
// 2 - Print all-but the last n bytes |
| 706 |
// 3 - Print all-but the last n bytes, with n=0 (i.e. print everything) |
| 707 |
// 4 - Print all but the last n bytes, large file. |
| 708 |
let scene = TestScenario::new(util_name!()); |
| 709 |
let fixtures = &scene.fixtures; |
| 710 |
|
| 711 |
// Test 1 - Print the first n bytes |
| 712 |
fixtures.write("f1", "abc\ndef\n"); |
| 713 |
let file = fixtures.open("f1"); |
| 714 |
let mut file_shadow = file.try_clone().unwrap(); |
| 715 |
scene |
| 716 |
.ucmd() |
| 717 |
.args(&["-c", "2"]) |
| 718 |
.set_stdin(file) |
| 719 |
.succeeds() |
| 720 |
.stdout_only("ab"); |
| 721 |
let mut bytes_remaining_in_stdin = vec![]; |
| 722 |
assert_eq!( |
| 723 |
file_shadow |
| 724 |
.read_to_end(&mut bytes_remaining_in_stdin) |
| 725 |
.unwrap(), |
| 726 |
6 |
| 727 |
); |
| 728 |
assert_eq!( |
| 729 |
String::from_utf8(bytes_remaining_in_stdin).unwrap(), |
| 730 |
"c\ndef\n" |
| 731 |
); |
| 732 |
|
| 733 |
// Test 2 - Print all-but the last n bytes |
| 734 |
fixtures.write("f2", "abc\ndef\n"); |
| 735 |
let file = fixtures.open("f2"); |
| 736 |
let mut file_shadow = file.try_clone().unwrap(); |
| 737 |
scene |
| 738 |
.ucmd() |
| 739 |
.args(&["-c", "-3"]) |
| 740 |
.set_stdin(file) |
| 741 |
.succeeds() |
| 742 |
.stdout_only("abc\nd"); |
| 743 |
let mut bytes_remaining_in_stdin = vec![]; |
| 744 |
assert_eq!( |
| 745 |
file_shadow |
| 746 |
.read_to_end(&mut bytes_remaining_in_stdin) |
| 747 |
.unwrap(), |
| 748 |
3 |
| 749 |
); |
| 750 |
assert_eq!(String::from_utf8(bytes_remaining_in_stdin).unwrap(), "ef\n"); |
| 751 |
|
| 752 |
// Test 3 - Print all-but the last n bytes, n=0 (i.e. print everything) |
| 753 |
fixtures.write("f3", "abc\ndef\n"); |
| 754 |
let file = fixtures.open("f3"); |
| 755 |
let mut file_shadow = file.try_clone().unwrap(); |
| 756 |
scene |
| 757 |
.ucmd() |
| 758 |
.args(&["-c", "-0"]) |
| 759 |
.set_stdin(file) |
| 760 |
.succeeds() |
| 761 |
.stdout_only("abc\ndef\n"); |
| 762 |
let mut bytes_remaining_in_stdin = vec![]; |
| 763 |
assert_eq!( |
| 764 |
file_shadow |
| 765 |
.read_to_end(&mut bytes_remaining_in_stdin) |
| 766 |
.unwrap(), |
| 767 |
0 |
| 768 |
); |
| 769 |
assert_eq!(String::from_utf8(bytes_remaining_in_stdin).unwrap(), ""); |
| 770 |
|
| 771 |
// Test 4 - Print all but the last n bytes, large input file. |
| 772 |
// First, create all our fixtures. |
| 773 |
let seq_20000_file_name = "seq_20000"; |
| 774 |
let seq_19000_file_name = "seq_19000"; |
| 775 |
let seq_19001_20000_file_name = "seq_19001_20000"; |
| 776 |
scene |
| 777 |
.cmd("seq") |
| 778 |
.arg("20000") |
| 779 |
.set_stdout(fixtures.make_file(seq_20000_file_name)) |
| 780 |
.succeeds(); |
| 781 |
scene |
| 782 |
.cmd("seq") |
| 783 |
.arg("19000") |
| 784 |
.set_stdout(fixtures.make_file(seq_19000_file_name)) |
| 785 |
.succeeds(); |
| 786 |
scene |
| 787 |
.cmd("seq") |
| 788 |
.args(&["19001", "20000"]) |
| 789 |
.set_stdout(fixtures.make_file(seq_19001_20000_file_name)) |
| 790 |
.succeeds(); |
| 791 |
|
| 792 |
let file = fixtures.open(seq_20000_file_name); |
| 793 |
let file_shadow = file.try_clone().unwrap(); |
| 794 |
let seq_19001_20000_file_length = fixtures |
| 795 |
.open(seq_19001_20000_file_name) |
| 796 |
.metadata() |
| 797 |
.unwrap() |
| 798 |
.len(); |
| 799 |
scene |
| 800 |
.ucmd() |
| 801 |
.args(&["-c", &format!("-{seq_19001_20000_file_length}")]) |
| 802 |
.set_stdin(file) |
| 803 |
.succeeds() |
| 804 |
.stdout_only_fixture(seq_19000_file_name); |
| 805 |
scene |
| 806 |
.cmd("cat") |
| 807 |
.set_stdin(file_shadow) |
| 808 |
.succeeds() |
| 809 |
.stdout_only_fixture(seq_19001_20000_file_name); |
| 810 |
} |
| 811 |
|
| 812 |
#[cfg(all( |
| 813 |
not(target_vendor = "apple"), |
| 814 |
not(target_os = "android"), |
| 815 |
not(target_os = "freebsd"), |
| 816 |
not(target_os = "openbsd"), |
| 817 |
not(windows) |
| 818 |
))] |
| 819 |
#[test] |
| 820 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths (/proc) not visible")] |
| 821 |
fn test_read_backwards_bytes_proc_fs_version() { |
| 822 |
let ts = TestScenario::new(util_name!()); |
| 823 |
|
| 824 |
let args = ["-c", "-1", "/proc/version"]; |
| 825 |
let result = ts.ucmd().args(&args).succeeds(); |
| 826 |
assert!(!result.stdout().is_empty()); |
| 827 |
} |
| 828 |
|
| 829 |
#[cfg(all( |
| 830 |
not(target_vendor = "apple"), |
| 831 |
not(target_os = "android"), |
| 832 |
not(target_os = "freebsd"), |
| 833 |
not(target_os = "openbsd"), |
| 834 |
not(windows) |
| 835 |
))] |
| 836 |
#[test] |
| 837 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths (/proc) not visible")] |
| 838 |
fn test_read_backwards_bytes_proc_fs_modules() { |
| 839 |
let ts = TestScenario::new(util_name!()); |
| 840 |
|
| 841 |
let args = ["-c", "-1", "/proc/modules"]; |
| 842 |
let result = ts.ucmd().args(&args).succeeds(); |
| 843 |
|
| 844 |
// Only expect output if the file is not empty, e.g. it is empty in default WSL2. |
| 845 |
if !ts.fixtures.read("/proc/modules").is_empty() { |
| 846 |
assert!(!result.stdout().is_empty()); |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
#[cfg(all( |
| 851 |
not(target_vendor = "apple"), |
| 852 |
not(target_os = "android"), |
| 853 |
not(target_os = "freebsd"), |
| 854 |
not(target_os = "openbsd"), |
| 855 |
not(windows) |
| 856 |
))] |
| 857 |
#[test] |
| 858 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths (/proc) not visible")] |
| 859 |
fn test_read_backwards_lines_proc_fs_modules() { |
| 860 |
let ts = TestScenario::new(util_name!()); |
| 861 |
|
| 862 |
let args = ["--lines", "-1", "/proc/modules"]; |
| 863 |
let result = ts.ucmd().args(&args).succeeds(); |
| 864 |
|
| 865 |
// Only expect output if the file is not empty, e.g. it is empty in default WSL2. |
| 866 |
if !ts.fixtures.read("/proc/modules").is_empty() { |
| 867 |
assert!(!result.stdout().is_empty()); |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
#[cfg(all( |
| 872 |
not(target_vendor = "apple"), |
| 873 |
not(target_os = "android"), |
| 874 |
not(target_os = "freebsd"), |
| 875 |
not(target_os = "openbsd"), |
| 876 |
not(windows) |
| 877 |
))] |
| 878 |
#[test] |
| 879 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths (/sys) not visible")] |
| 880 |
fn test_read_backwards_bytes_sys_kernel_profiling() { |
| 881 |
let ts = TestScenario::new(util_name!()); |
| 882 |
// in case the kernel was not built with profiling support, e.g. WSL |
| 883 |
if !ts.fixtures.file_exists("/sys/kernel/profiling") { |
| 884 |
println!("test skipped: /sys/kernel/profiling does not exist"); |
| 885 |
return; |
| 886 |
} |
| 887 |
let args = ["-c", "-1", "/sys/kernel/profiling"]; |
| 888 |
let result = ts.ucmd().args(&args).succeeds(); |
| 889 |
let stdout_str = result.stdout_str(); |
| 890 |
assert_eq!(stdout_str.len(), 1); |
| 891 |
assert!(stdout_str == "0" || stdout_str == "1"); |
| 892 |
} |
| 893 |
|
| 894 |
#[test] |
| 895 |
fn test_value_too_large() { |
| 896 |
const MAX: u64 = u64::MAX; |
| 897 |
|
| 898 |
new_ucmd!() |
| 899 |
.args(&["-n", format!("{MAX}0").as_str(), "lorem_ipsum.txt"]) |
| 900 |
.succeeds(); |
| 901 |
} |
| 902 |
|
| 903 |
#[test] |
| 904 |
fn test_all_but_last_lines_huge_count_does_not_panic() { |
| 905 |
// https://github.com/uutils/coreutils/issues/12836 |
| 906 |
new_ucmd!() |
| 907 |
.args(&["-n=-116265256266241262252526", "lorem_ipsum.txt"]) |
| 908 |
.succeeds() |
| 909 |
.no_stdout(); |
| 910 |
} |
| 911 |
|
| 912 |
#[test] |
| 913 |
fn test_all_but_last_bytes_huge_count_does_not_panic() { |
| 914 |
// https://github.com/uutils/coreutils/issues/12837 |
| 915 |
new_ucmd!() |
| 916 |
.args(&["-c=-116265256266241262252526", "lorem_ipsum.txt"]) |
| 917 |
.succeeds() |
| 918 |
.no_stdout(); |
| 919 |
} |
| 920 |
|
| 921 |
#[test] |
| 922 |
fn test_all_but_last_lines() { |
| 923 |
new_ucmd!() |
| 924 |
.args(&["-n", "-15", "lorem_ipsum.txt"]) |
| 925 |
.succeeds() |
| 926 |
.stdout_is_fixture("lorem_ipsum_backwards_15_lines.expected"); |
| 927 |
} |
| 928 |
|
| 929 |
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))] |
| 930 |
#[test] |
| 931 |
fn test_write_to_dev_full() { |
| 932 |
use std::fs::OpenOptions; |
| 933 |
|
| 934 |
for append in [true, false] { |
| 935 |
{ |
| 936 |
let dev_full = OpenOptions::new() |
| 937 |
.write(true) |
| 938 |
.append(append) |
| 939 |
.open("/dev/full") |
| 940 |
.unwrap(); |
| 941 |
|
| 942 |
new_ucmd!() |
| 943 |
.pipe_in_fixture(INPUT) |
| 944 |
.set_stdout(dev_full) |
| 945 |
.fails() |
| 946 |
.stderr_is("head: error writing 'standard output': No space left on device\n"); |
| 947 |
} |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
#[test] |
| 952 |
#[cfg(target_os = "linux")] |
| 953 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 954 |
fn test_head_non_utf8_paths() { |
| 955 |
use std::ffi::OsStr; |
| 956 |
use std::os::unix::ffi::OsStrExt; |
| 957 |
|
| 958 |
let scene = TestScenario::new(util_name!()); |
| 959 |
let at = &scene.fixtures; |
| 960 |
|
| 961 |
// Create a test file with non-UTF-8 bytes in the name |
| 962 |
let non_utf8_bytes = b"test_\xFF\xFE.txt"; |
| 963 |
let non_utf8_name = OsStr::from_bytes(non_utf8_bytes); |
| 964 |
|
| 965 |
std::fs::write(at.plus(non_utf8_name), "line1\nline2\nline3\n").unwrap(); |
| 966 |
|
| 967 |
let result = scene.ucmd().arg(non_utf8_name).succeeds(); |
| 968 |
|
| 969 |
let output = result.stdout_str_lossy(); |
| 970 |
assert!(output.contains("line1")); |
| 971 |
assert!(output.contains("line2")); |
| 972 |
assert!(output.contains("line3")); |
| 973 |
} |
| 974 |
// Test that head handles non-UTF-8 file names without crashing |
| 975 |
|
| 976 |
#[test] |
| 977 |
fn test_do_not_attempt_to_read_a_directory() { |
| 978 |
new_ucmd!() |
| 979 |
.arg(".") |
| 980 |
.fails_with_code(1) |
| 981 |
.stderr_contains("error reading '.'"); |
| 982 |
} |
| 983 |
|
| 984 |
/// Regression test for https://github.com/uutils/coreutils/issues/12215 |
| 985 |
/// `head -c0 <directory>` should succeed (nothing to read), matching GNU. |
| 986 |
#[test] |
| 987 |
fn test_zero_bytes_on_directory_succeeds() { |
| 988 |
new_ucmd!().args(&["-c", "0", "."]).succeeds().no_output(); |
| 989 |
} |
| 990 |
|
| 991 |
/// `head -n0 <directory>` should also succeed. |
| 992 |
#[test] |
| 993 |
fn test_zero_lines_on_directory_succeeds() { |
| 994 |
new_ucmd!().args(&["-n", "0", "."]).succeeds().no_output(); |
| 995 |
} |
| 996 |
|
| 997 |
/// GNU `head` prints the `==> name <==` header for every file argument |
| 998 |
/// (including directories) when invoked with multiple files. With non-zero |
| 999 |
/// output, directories also produce an "Is a directory" error after the |
| 1000 |
/// header. |
| 1001 |
#[cfg(not(windows))] |
| 1002 |
#[test] |
| 1003 |
fn test_directory_header_with_multiple_files() { |
| 1004 |
let ts = TestScenario::new(util_name!()); |
| 1005 |
let at = &ts.fixtures; |
| 1006 |
at.mkdir("d"); |
| 1007 |
at.write("f", "hello\n"); |
| 1008 |
ts.ucmd() |
| 1009 |
.args(&["-c", "5", "d", "f"]) |
| 1010 |
.fails_with_code(1) |
| 1011 |
.stdout_is("==> d <==\n\n==> f <==\nhello") |
| 1012 |
.stderr_contains("Is a directory"); |
| 1013 |
} |
| 1014 |
|
| 1015 |
/// With `-c 0` (zero output), the directory header is still printed but no |
| 1016 |
/// error is emitted. |
| 1017 |
#[cfg(not(windows))] |
| 1018 |
#[test] |
| 1019 |
fn test_directory_header_with_multiple_files_zero_output() { |
| 1020 |
let ts = TestScenario::new(util_name!()); |
| 1021 |
let at = &ts.fixtures; |
| 1022 |
at.mkdir("d"); |
| 1023 |
at.write("f", "hello\n"); |
| 1024 |
ts.ucmd() |
| 1025 |
.args(&["-c", "0", "d", "f"]) |
| 1026 |
.succeeds() |
| 1027 |
.stdout_is("==> d <==\n\n==> f <==\n") |
| 1028 |
.no_stderr(); |
| 1029 |
} |
| 1030 |
|
| 1031 |
/// GNU `head` prints the `==> name <==` header only after the file is |
| 1032 |
/// successfully opened. A file that exists but cannot be opened (e.g. no read |
| 1033 |
/// permission) must therefore produce only an error and no header. |
| 1034 |
#[cfg(unix)] |
| 1035 |
#[test] |
| 1036 |
fn test_unreadable_file_prints_no_header() { |
| 1037 |
let ts = TestScenario::new(util_name!()); |
| 1038 |
let at = &ts.fixtures; |
| 1039 |
at.write("unreadable", "secret\n"); |
| 1040 |
at.write("readable", "hello\n"); |
| 1041 |
at.set_mode("unreadable", 0o000); |
| 1042 |
|
| 1043 |
// Running as root bypasses permission checks, so the open would succeed. |
| 1044 |
if std::fs::File::open(at.plus("unreadable")).is_ok() { |
| 1045 |
return; |
| 1046 |
} |
| 1047 |
|
| 1048 |
ts.ucmd() |
| 1049 |
.args(&["-c", "5", "unreadable", "readable"]) |
| 1050 |
.fails_with_code(1) |
| 1051 |
.stdout_is("==> readable <==\nhello") |
| 1052 |
.stdout_does_not_contain("==> unreadable <==") |
| 1053 |
.stderr_contains("cannot open 'unreadable' for reading: Permission denied"); |
| 1054 |
} |
| 1055 |
|
| 1056 |
/// Regression for #13887: writing the `==> filename <==` verbose header to a |
| 1057 |
/// full/closed stdout must surface the write error instead of panicking inside |
| 1058 |
/// `print_verbatim(...).unwrap()`. A filename longer than the stdout buffer |
| 1059 |
/// forces the header write to flush mid-write so the failure surfaces inside |
| 1060 |
/// the filename write rather than at the next checked one. |
| 1061 |
#[test] |
| 1062 |
#[cfg(target_os = "linux")] |
| 1063 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths (/dev) not visible")] |
| 1064 |
fn test_verbose_header_write_error_long_filename() { |
| 1065 |
use std::fs::File; |
| 1066 |
|
| 1067 |
let dev_full = |
| 1068 |
File::create("/dev/full").expect("Failed to open /dev/full - test must run on Linux"); |
| 1069 |
|
| 1070 |
let long_path = format!("/dev/{}null", "./".repeat(512)); |
| 1071 |
|
| 1072 |
new_ucmd!() |
| 1073 |
.arg("-v") |
| 1074 |
.arg(long_path) |
| 1075 |
.set_stdout(dev_full) |
| 1076 |
.fails() |
| 1077 |
.code_is(1) |
| 1078 |
.stderr_contains("No space left on device"); |
| 1079 |
} |
| 1080 |
|
| 1081 |
/// Regression for #11972: head must reject directories detected on the |
| 1082 |
/// open fd, not via a separate `Path::is_dir()` call. A symlink that |
| 1083 |
/// resolves to a directory must still be rejected — verifying the fd |
| 1084 |
/// check survives an indirection. |
| 1085 |
#[test] |
| 1086 |
#[cfg(unix)] |
| 1087 |
fn test_head_rejects_directory_through_symlink() { |
| 1088 |
use std::os::unix::fs::symlink; |
| 1089 |
|
| 1090 |
let scene = TestScenario::new(util_name!()); |
| 1091 |
let at = &scene.fixtures; |
| 1092 |
at.mkdir("real_dir"); |
| 1093 |
symlink("real_dir", at.plus("link_to_dir")).unwrap(); |
| 1094 |
|
| 1095 |
scene |
| 1096 |
.ucmd() |
| 1097 |
.arg("link_to_dir") |
| 1098 |
.fails_with_code(1) |
| 1099 |
.stderr_contains("Is a directory"); |
| 1100 |
} |
| 1101 |
|
| 1102 |
/// Regression for #11972: a symlink that points to a regular file must |
| 1103 |
/// still be readable by head (the fd-based check must distinguish the |
| 1104 |
/// fd's mode, not the symlink's). |
| 1105 |
#[test] |
| 1106 |
#[cfg(unix)] |
| 1107 |
fn test_head_follows_symlink_to_regular_file() { |
| 1108 |
use std::os::unix::fs::symlink; |
| 1109 |
|
| 1110 |
let scene = TestScenario::new(util_name!()); |
| 1111 |
let at = &scene.fixtures; |
| 1112 |
at.write("regular", "hello\n"); |
| 1113 |
symlink("regular", at.plus("link_to_regular")).unwrap(); |
| 1114 |
|
| 1115 |
scene |
| 1116 |
.ucmd() |
| 1117 |
.arg("link_to_regular") |
| 1118 |
.succeeds() |
| 1119 |
.stdout_is("hello\n"); |
| 1120 |
} |
| 1121 |
|
| 1122 |
#[cfg(unix)] |
| 1123 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 1124 |
mod diagnostics { |
| 1125 |
use super::*; |
| 1126 |
|
| 1127 |
#[test] |
| 1128 |
fn test_snippet_points_at_the_unknown_unit() { |
| 1129 |
let result = new_ucmd!() |
| 1130 |
.terminal_sim_stderr() |
| 1131 |
.args(&["-c", "1fb", "/dev/null"]) |
| 1132 |
.fails_with_code(1); |
| 1133 |
|
| 1134 |
// The number parsed; it is the unit that did not. |
| 1135 |
assert_eq!( |
| 1136 |
result.stderr_as_displayed(), |
| 1137 |
"\ |
| 1138 |
head: invalid number of bytes: '1fb' |
| 1139 |
╭─[ head:1:10 ] |
| 1140 |
│ |
| 1141 |
1 │ head -c 1fb /dev/null |
| 1142 |
│ ─┬ |
| 1143 |
│ ╰── not a known unit |
| 1144 |
│ |
| 1145 |
│ Help: a size is a number and an optional unit: K, M, G and so on for 1024, KB, MB, GB for 1000 |
| 1146 |
───╯" |
| 1147 |
); |
| 1148 |
} |
| 1149 |
|
| 1150 |
#[test] |
| 1151 |
fn test_snippet_underlines_a_size_with_no_number() { |
| 1152 |
let result = new_ucmd!() |
| 1153 |
.terminal_sim_stderr() |
| 1154 |
.args(&["--bytes=xyz", "/dev/null"]) |
| 1155 |
.fails_with_code(1); |
| 1156 |
let stderr = result.stderr_as_displayed(); |
| 1157 |
|
| 1158 |
// Nothing usable was read, so the whole value is underlined, and the |
| 1159 |
// message already says what is wrong. |
| 1160 |
assert!(stderr.contains("head:1:14"), "{stderr}"); |
| 1161 |
assert!(!stderr.contains("not a known unit"), "{stderr}"); |
| 1162 |
} |
| 1163 |
|
| 1164 |
#[test] |
| 1165 |
fn test_snippet_preserves_obsolete_option_spelling() { |
| 1166 |
let result = new_ucmd!() |
| 1167 |
.terminal_sim_stderr() |
| 1168 |
.args(&["-5", "-c", "1fb", "/dev/null"]) |
| 1169 |
.fails_with_code(1); |
| 1170 |
let stderr = result.stderr_as_displayed(); |
| 1171 |
|
| 1172 |
assert!(stderr.contains("1 │ head -5 -c 1fb /dev/null"), "{stderr}"); |
| 1173 |
assert!(!stderr.contains("head -n 5 -c"), "{stderr}"); |
| 1174 |
} |
| 1175 |
|
| 1176 |
#[test] |
| 1177 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 1178 |
new_ucmd!() |
| 1179 |
.args(&["-c", "1fb", "/dev/null"]) |
| 1180 |
.fails_with_code(1) |
| 1181 |
.stderr_is("head: invalid number of bytes: '1fb'\n"); |
| 1182 |
} |
| 1183 |
} |
| 1184 |
|
| 1185 |
#[test] |
| 1186 |
fn test_invalid_count_keeps_its_leading_zeros() { |
| 1187 |
// Leading zeros are stripped only so the count is read as decimal rather |
| 1188 |
// than octal. That is internal, so GNU still names the argument as typed. |
| 1189 |
new_ucmd!() |
| 1190 |
.args(&["-c", "0fb", "/dev/null"]) |
| 1191 |
.fails_with_code(1) |
| 1192 |
.stderr_is("head: invalid number of bytes: '0fb'\n"); |
| 1193 |
new_ucmd!() |
| 1194 |
.args(&["-n", "00x", "/dev/null"]) |
| 1195 |
.fails_with_code(1) |
| 1196 |
.stderr_is("head: invalid number of lines: '00x'\n"); |
| 1197 |
} |
| 1198 |
|
| 1199 |
#[test] |
| 1200 |
fn test_lowercase_multiplier_suffixes_rejected() { |
| 1201 |
// GNU accepts a lowercase suffix only for "k" and "m"; every other |
| 1202 |
// multiplier must be uppercase. "b" is bare-only (no B/iB/D form). |
| 1203 |
for suffix in ["g", "t", "p", "e", "z", "y", "r", "q"] { |
| 1204 |
new_ucmd!() |
| 1205 |
.args(&["-c", &format!("2{suffix}")]) |
| 1206 |
.fails_with_code(1) |
| 1207 |
.stderr_is(format!("head: invalid number of bytes: '2{suffix}'\n")); |
| 1208 |
new_ucmd!() |
| 1209 |
.args(&["-n", &format!("2{suffix}")]) |
| 1210 |
.fails_with_code(1) |
| 1211 |
.stderr_is(format!("head: invalid number of lines: '2{suffix}'\n")); |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
#[test] |
| 1216 |
fn test_accepted_multiplier_suffixes() { |
| 1217 |
for suffix in [ |
| 1218 |
"b", "k", "m", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q", "kB", "KiB", "kD", "MiB", |
| 1219 |
"GB", |
| 1220 |
] { |
| 1221 |
new_ucmd!() |
| 1222 |
.args(&["-c", &format!("1{suffix}")]) |
| 1223 |
.pipe_in("x") |
| 1224 |
.ignore_stdin_write_error() |
| 1225 |
.succeeds(); |
| 1226 |
} |
| 1227 |
} |