| 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 |
// |
| 7 |
// spell-checker:ignore binvalid finvalid hinvalid iinvalid linvalid nabcabc nabcabcabc ninvalid vinvalid winvalid dabc näää févr |
| 8 |
|
| 9 |
use uutests::{at_and_ucmd, new_ucmd, util::TestScenario, util_name}; |
| 10 |
|
| 11 |
#[test] |
| 12 |
#[cfg(target_os = "linux")] |
| 13 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 14 |
fn test_non_utf8_paths() { |
| 15 |
use std::os::unix::ffi::OsStringExt; |
| 16 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 17 |
|
| 18 |
let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]); |
| 19 |
std::fs::write(at.plus(&filename), b"line 1\nline 2\nline 3\n").unwrap(); |
| 20 |
|
| 21 |
ucmd.arg(&filename) |
| 22 |
.succeeds() |
| 23 |
.stdout_contains("1\t") |
| 24 |
.stdout_contains("2\t") |
| 25 |
.stdout_contains("3\t"); |
| 26 |
} |
| 27 |
|
| 28 |
#[test] |
| 29 |
fn test_invalid_arg() { |
| 30 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 31 |
} |
| 32 |
|
| 33 |
#[test] |
| 34 |
fn test_stdin_no_newline() { |
| 35 |
new_ucmd!() |
| 36 |
.pipe_in("No Newline") |
| 37 |
.succeeds() |
| 38 |
.stdout_is(" 1\tNo Newline\n"); |
| 39 |
} |
| 40 |
|
| 41 |
#[test] |
| 42 |
fn test_stdin_newline() { |
| 43 |
new_ucmd!() |
| 44 |
.args(&["-s", "-", "-w", "1"]) |
| 45 |
.pipe_in("Line One\nLine Two\n") |
| 46 |
.succeeds() |
| 47 |
.stdout_is("1-Line One\n2-Line Two\n"); |
| 48 |
} |
| 49 |
|
| 50 |
#[test] |
| 51 |
fn test_padding_without_overflow() { |
| 52 |
new_ucmd!() |
| 53 |
.args(&["-i", "1000", "-s", "x", "-n", "rz", "simple.txt"]) |
| 54 |
.succeeds() |
| 55 |
.stdout_is( |
| 56 |
"000001xL1\n001001xL2\n002001xL3\n003001xL4\n004001xL5\n005001xL6\n006001xL7\n0070\ |
| 57 |
01xL8\n008001xL9\n009001xL10\n010001xL11\n011001xL12\n012001xL13\n013001xL14\n014\ |
| 58 |
001xL15\n", |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn test_padding_with_overflow() { |
| 64 |
new_ucmd!() |
| 65 |
.args(&["-i", "1000", "-s", "x", "-n", "rz", "-w", "4", "simple.txt"]) |
| 66 |
.succeeds() |
| 67 |
.stdout_is( |
| 68 |
"0001xL1\n1001xL2\n2001xL3\n3001xL4\n4001xL5\n5001xL6\n6001xL7\n7001xL8\n8001xL9\n\ |
| 69 |
9001xL10\n10001xL11\n11001xL12\n12001xL13\n13001xL14\n14001xL15\n", |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn test_sections_and_styles() { |
| 75 |
// spell-checker:disable |
| 76 |
for (fixture, output) in [ |
| 77 |
( |
| 78 |
"section.txt", |
| 79 |
"\n HEADER1\n HEADER2\n\n1 |BODY1\n2 \ |
| 80 |
|BODY2\n\n FOOTER1\n FOOTER2\n\n NEXTHEADER1\n NEXTHEADER2\n\n1 \ |
| 81 |
|NEXTBODY1\n2 |NEXTBODY2\n\n NEXTFOOTER1\n NEXTFOOTER2\n", |
| 82 |
), |
| 83 |
( |
| 84 |
"joinblanklines.txt", |
| 85 |
"1 |Nonempty\n2 |Nonempty\n3 |Followed by 10x empty\n \n \n \n \n4 \ |
| 86 |
|\n \n \n \n \n5 |\n6 |Followed by 5x empty\n \n \n \n \n7 |\n8 \ |
| 87 |
|Followed by 4x empty\n \n \n \n \n9 |Nonempty\n10 |Nonempty\n11 \ |
| 88 |
|Nonempty.\n", |
| 89 |
), |
| 90 |
] { |
| 91 |
new_ucmd!() |
| 92 |
.args(&[ |
| 93 |
"-s", "|", "-n", "ln", "-w", "3", "-b", "a", "-l", "5", fixture, |
| 94 |
]) |
| 95 |
.succeeds() |
| 96 |
.stdout_is(output); |
| 97 |
} |
| 98 |
// spell-checker:enable |
| 99 |
} |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn test_no_renumber() { |
| 103 |
for arg in ["-p", "--no-renumber"] { |
| 104 |
new_ucmd!() |
| 105 |
.arg(arg) |
| 106 |
.pipe_in("a\n\\:\\:\nb") |
| 107 |
.succeeds() |
| 108 |
.stdout_is(" 1\ta\n\n 2\tb\n"); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
#[test] |
| 113 |
fn test_number_format_ln() { |
| 114 |
for arg in ["-nln", "--number-format=ln"] { |
| 115 |
new_ucmd!() |
| 116 |
.arg(arg) |
| 117 |
.pipe_in("test") |
| 118 |
.succeeds() |
| 119 |
.stdout_is("1 \ttest\n"); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
#[test] |
| 124 |
fn test_number_format_rn() { |
| 125 |
for arg in ["-nrn", "--number-format=rn"] { |
| 126 |
new_ucmd!() |
| 127 |
.arg(arg) |
| 128 |
.pipe_in("test") |
| 129 |
.succeeds() |
| 130 |
.stdout_is(" 1\ttest\n"); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn test_number_format_rz() { |
| 136 |
for arg in ["-nrz", "--number-format=rz"] { |
| 137 |
new_ucmd!() |
| 138 |
.arg(arg) |
| 139 |
.pipe_in("test") |
| 140 |
.succeeds() |
| 141 |
.stdout_is("000001\ttest\n"); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
#[test] |
| 146 |
fn test_number_format_rz_with_negative_line_number() { |
| 147 |
for arg in ["-nrz", "--number-format=rz"] { |
| 148 |
new_ucmd!() |
| 149 |
.arg(arg) |
| 150 |
.arg("-v-12") |
| 151 |
.pipe_in("test") |
| 152 |
.succeeds() |
| 153 |
.stdout_is("-00012\ttest\n"); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
#[test] |
| 158 |
fn test_invalid_number_format() { |
| 159 |
for arg in ["-ninvalid", "--number-format=invalid"] { |
| 160 |
new_ucmd!() |
| 161 |
.arg(arg) |
| 162 |
.fails() |
| 163 |
.stderr_contains("invalid value 'invalid'"); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
#[test] |
| 168 |
fn test_number_width() { |
| 169 |
for width in 1..10 { |
| 170 |
for arg in [format!("-w{width}"), format!("--number-width={width}")] { |
| 171 |
let spaces = " ".repeat(width - 1); |
| 172 |
new_ucmd!() |
| 173 |
.arg(arg) |
| 174 |
.pipe_in("test") |
| 175 |
.succeeds() |
| 176 |
.stdout_is(format!("{spaces}1\ttest\n")); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
#[test] |
| 182 |
fn test_number_width_zero() { |
| 183 |
for arg in ["-w0", "--number-width=0"] { |
| 184 |
new_ucmd!() |
| 185 |
.arg(arg) |
| 186 |
.fails() |
| 187 |
.stderr_contains("is not in 1..=2147483647"); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
#[test] |
| 192 |
fn test_number_width_too_large() { |
| 193 |
// Values > i32::MAX must be rejected to match GNU nl behavior and avoid |
| 194 |
// a capacity-overflow panic in " ".repeat(number_width + 1). |
| 195 |
for arg in ["-w2147483648", "--number-width=2147483648"] { |
| 196 |
new_ucmd!() |
| 197 |
.arg(arg) |
| 198 |
.pipe_in("") |
| 199 |
.fails() |
| 200 |
.stderr_contains("is not in 1..=2147483647"); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
#[test] |
| 205 |
fn test_number_width_max_i32() { |
| 206 |
// i32::MAX (2147483647) is the largest value GNU nl accepts; it must not panic. |
| 207 |
new_ucmd!() |
| 208 |
.args(&["-w", "2147483647"]) |
| 209 |
.pipe_in("") |
| 210 |
.succeeds(); |
| 211 |
} |
| 212 |
|
| 213 |
#[test] |
| 214 |
fn test_invalid_number_width() { |
| 215 |
for arg in ["-winvalid", "--number-width=invalid"] { |
| 216 |
new_ucmd!() |
| 217 |
.arg(arg) |
| 218 |
.fails() |
| 219 |
.stderr_contains("invalid value 'invalid'"); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
#[test] |
| 224 |
fn test_number_separator() { |
| 225 |
for arg in ["-s:-:", "--number-separator=:-:"] { |
| 226 |
new_ucmd!() |
| 227 |
.arg(arg) |
| 228 |
.pipe_in("test") |
| 229 |
.succeeds() |
| 230 |
.stdout_is(" 1:-:test\n"); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
#[test] |
| 235 |
#[cfg(unix)] |
| 236 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 237 |
fn test_number_separator_non_utf8() { |
| 238 |
use std::{ffi::OsString, os::unix::ffi::OsStringExt}; |
| 239 |
|
| 240 |
let separator_bytes = [0xFF, 0xFE]; |
| 241 |
let mut v = b"--number-separator=".to_vec(); |
| 242 |
v.extend_from_slice(&separator_bytes); |
| 243 |
|
| 244 |
let arg = OsString::from_vec(v); |
| 245 |
|
| 246 |
// Raw bytes should be preserved in the separator output |
| 247 |
let mut expected = b" 1".to_vec(); |
| 248 |
expected.extend_from_slice(&separator_bytes); |
| 249 |
expected.extend_from_slice(b"test\n"); |
| 250 |
|
| 251 |
new_ucmd!() |
| 252 |
.arg(arg) |
| 253 |
.pipe_in("test") |
| 254 |
.succeeds() |
| 255 |
.stdout_is_bytes(expected); |
| 256 |
} |
| 257 |
|
| 258 |
#[test] |
| 259 |
fn test_starting_line_number() { |
| 260 |
for arg in ["-v10", "--starting-line-number=10"] { |
| 261 |
new_ucmd!() |
| 262 |
.arg(arg) |
| 263 |
.pipe_in("test") |
| 264 |
.succeeds() |
| 265 |
.stdout_is(" 10\ttest\n"); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn test_negative_starting_line_number() { |
| 271 |
for arg in ["-v-10", "--starting-line-number=-10"] { |
| 272 |
new_ucmd!() |
| 273 |
.arg(arg) |
| 274 |
.pipe_in("test") |
| 275 |
.succeeds() |
| 276 |
.stdout_is(" -10\ttest\n"); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
#[test] |
| 281 |
fn test_invalid_starting_line_number() { |
| 282 |
for arg in ["-vinvalid", "--starting-line-number=invalid"] { |
| 283 |
new_ucmd!() |
| 284 |
.arg(arg) |
| 285 |
.fails() |
| 286 |
.stderr_contains("invalid value 'invalid'"); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
#[test] |
| 291 |
fn test_line_increment() { |
| 292 |
for arg in ["-i10", "--line-increment=10"] { |
| 293 |
new_ucmd!() |
| 294 |
.arg(arg) |
| 295 |
.pipe_in("a\nb") |
| 296 |
.succeeds() |
| 297 |
.stdout_is(" 1\ta\n 11\tb\n"); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
#[test] |
| 302 |
fn test_line_increment_from_negative_starting_line() { |
| 303 |
for arg in ["-i10", "--line-increment=10"] { |
| 304 |
new_ucmd!() |
| 305 |
.arg(arg) |
| 306 |
.arg("-v-19") |
| 307 |
.pipe_in("a\nb\nc") |
| 308 |
.succeeds() |
| 309 |
.stdout_is(" -19\ta\n -9\tb\n 1\tc\n"); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
#[test] |
| 314 |
fn test_negative_line_increment() { |
| 315 |
for arg in ["-i-10", "--line-increment=-10"] { |
| 316 |
new_ucmd!() |
| 317 |
.arg(arg) |
| 318 |
.pipe_in("a\nb\nc") |
| 319 |
.succeeds() |
| 320 |
.stdout_is(" 1\ta\n -9\tb\n -19\tc\n"); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
#[test] |
| 325 |
fn test_invalid_line_increment() { |
| 326 |
for arg in ["-iinvalid", "--line-increment=invalid"] { |
| 327 |
new_ucmd!() |
| 328 |
.arg(arg) |
| 329 |
.fails() |
| 330 |
.stderr_contains("invalid value 'invalid'"); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
#[test] |
| 335 |
fn test_join_blank_lines() { |
| 336 |
for arg in ["-l3", "--join-blank-lines=3"] { |
| 337 |
new_ucmd!() |
| 338 |
.arg(arg) |
| 339 |
.arg("--body-numbering=a") |
| 340 |
.pipe_in("\n\n\n\n\n\n") |
| 341 |
.succeeds() |
| 342 |
.stdout_is(concat!( |
| 343 |
" \n", |
| 344 |
" \n", |
| 345 |
" 1\t\n", |
| 346 |
" \n", |
| 347 |
" \n", |
| 348 |
" 2\t\n", |
| 349 |
)); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
#[test] |
| 354 |
fn test_join_blank_lines_zero() { |
| 355 |
for arg in ["-l0", "--join-blank-lines=0"] { |
| 356 |
new_ucmd!() |
| 357 |
.arg(arg) |
| 358 |
.arg("--body-numbering=a") |
| 359 |
.pipe_in("\n\n\n\n\n\n") |
| 360 |
.succeeds() |
| 361 |
.stdout_is(concat!( |
| 362 |
" 1\t\n", |
| 363 |
" 2\t\n", |
| 364 |
" 3\t\n", |
| 365 |
" 4\t\n", |
| 366 |
" 5\t\n", |
| 367 |
" 6\t\n", |
| 368 |
)); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
#[test] |
| 373 |
fn test_join_blank_lines_multiple_files() { |
| 374 |
let scene = TestScenario::new(util_name!()); |
| 375 |
let at = &scene.fixtures; |
| 376 |
|
| 377 |
at.write("a.txt", "\n\n"); |
| 378 |
at.write("b.txt", "\n\n"); |
| 379 |
at.write("c.txt", "\n\n"); |
| 380 |
|
| 381 |
for arg in ["-l3", "--join-blank-lines=3"] { |
| 382 |
scene |
| 383 |
.ucmd() |
| 384 |
.args(&[arg, "--body-numbering=a", "a.txt", "b.txt", "c.txt"]) |
| 385 |
.succeeds() |
| 386 |
.stdout_is(concat!( |
| 387 |
" \n", |
| 388 |
" \n", |
| 389 |
" 1\t\n", |
| 390 |
" \n", |
| 391 |
" \n", |
| 392 |
" 2\t\n", |
| 393 |
)); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
#[test] |
| 398 |
fn test_invalid_join_blank_lines() { |
| 399 |
for arg in ["-linvalid", "--join-blank-lines=invalid"] { |
| 400 |
new_ucmd!() |
| 401 |
.arg(arg) |
| 402 |
.fails() |
| 403 |
.stderr_contains("invalid value 'invalid'"); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
#[test] |
| 408 |
fn test_default_body_numbering() { |
| 409 |
new_ucmd!() |
| 410 |
.pipe_in("a\n\nb") |
| 411 |
.succeeds() |
| 412 |
.stdout_is(" 1\ta\n \n 2\tb\n"); |
| 413 |
} |
| 414 |
|
| 415 |
#[test] |
| 416 |
fn test_default_body_numbering_multiple_files() { |
| 417 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 418 |
|
| 419 |
at.write("a.txt", "a"); |
| 420 |
at.write("b.txt", "b"); |
| 421 |
at.write("c.txt", "c"); |
| 422 |
|
| 423 |
ucmd.args(&["a.txt", "b.txt", "c.txt"]) |
| 424 |
.succeeds() |
| 425 |
.stdout_is(" 1\ta\n 2\tb\n 3\tc\n"); |
| 426 |
} |
| 427 |
|
| 428 |
#[test] |
| 429 |
fn test_default_body_numbering_multiple_files_and_stdin() { |
| 430 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 431 |
|
| 432 |
at.write("a.txt", "a"); |
| 433 |
at.write("c.txt", "c"); |
| 434 |
|
| 435 |
ucmd.args(&["a.txt", "-", "c.txt"]) |
| 436 |
.pipe_in("b") |
| 437 |
.succeeds() |
| 438 |
.stdout_is(" 1\ta\n 2\tb\n 3\tc\n"); |
| 439 |
} |
| 440 |
|
| 441 |
#[test] |
| 442 |
fn test_default_body_numbering_multiple_files_with_non_existing_file() { |
| 443 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 444 |
|
| 445 |
at.write("a.txt", "a"); |
| 446 |
at.write("b.txt", "b"); |
| 447 |
|
| 448 |
ucmd.args(&["a.txt", "non_existing", "b.txt"]) |
| 449 |
.fails_with_code(1) |
| 450 |
.stdout_is(" 1\ta\n 2\tb\n") |
| 451 |
.stderr_is("nl: non_existing: No such file or directory\n"); |
| 452 |
} |
| 453 |
|
| 454 |
#[test] |
| 455 |
fn test_body_numbering_all_lines_without_delimiter() { |
| 456 |
for arg in ["-ba", "--body-numbering=a"] { |
| 457 |
new_ucmd!() |
| 458 |
.arg(arg) |
| 459 |
.pipe_in("a\n\nb") |
| 460 |
.succeeds() |
| 461 |
.stdout_is(" 1\ta\n 2\t\n 3\tb\n"); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
#[test] |
| 466 |
fn test_body_numbering_no_lines_without_delimiter() { |
| 467 |
for arg in ["-bn", "--body-numbering=n"] { |
| 468 |
new_ucmd!() |
| 469 |
.arg(arg) |
| 470 |
.pipe_in("a\n\nb") |
| 471 |
.succeeds() |
| 472 |
.stdout_is(" a\n \n b\n"); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
#[test] |
| 477 |
fn test_body_numbering_non_empty_lines_without_delimiter() { |
| 478 |
for arg in ["-bt", "--body-numbering=t"] { |
| 479 |
new_ucmd!() |
| 480 |
.arg(arg) |
| 481 |
.pipe_in("a\n\nb") |
| 482 |
.succeeds() |
| 483 |
.stdout_is(" 1\ta\n \n 2\tb\n"); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
#[test] |
| 488 |
fn test_body_numbering_matched_lines_without_delimiter() { |
| 489 |
for arg in ["-bp^[ac]", "--body-numbering=p^[ac]"] { |
| 490 |
new_ucmd!() |
| 491 |
.arg(arg) |
| 492 |
.pipe_in("a\nb\nc") |
| 493 |
.succeeds() |
| 494 |
.stdout_is(" 1\ta\n b\n 2\tc\n"); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
#[test] |
| 499 |
fn test_numbering_all_lines() { |
| 500 |
let delimiters_and_args = [ |
| 501 |
("\\:\\:\\:\n", ["-ha", "--header-numbering=a"]), |
| 502 |
("\\:\\:\n", ["-ba", "--body-numbering=a"]), |
| 503 |
("\\:\n", ["-fa", "--footer-numbering=a"]), |
| 504 |
]; |
| 505 |
|
| 506 |
for (delimiter, args) in delimiters_and_args { |
| 507 |
for arg in args { |
| 508 |
new_ucmd!() |
| 509 |
.arg(arg) |
| 510 |
.pipe_in(format!("{delimiter}a\n\nb")) |
| 511 |
.succeeds() |
| 512 |
.stdout_is("\n 1\ta\n 2\t\n 3\tb\n"); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
#[test] |
| 518 |
fn test_numbering_no_lines() { |
| 519 |
let delimiters_and_args = [ |
| 520 |
("\\:\\:\\:\n", ["-hn", "--header-numbering=n"]), |
| 521 |
("\\:\\:\n", ["-bn", "--body-numbering=n"]), |
| 522 |
("\\:\n", ["-fn", "--footer-numbering=n"]), |
| 523 |
]; |
| 524 |
|
| 525 |
for (delimiter, args) in delimiters_and_args { |
| 526 |
for arg in args { |
| 527 |
new_ucmd!() |
| 528 |
.arg(arg) |
| 529 |
.pipe_in(format!("{delimiter}a\n\nb")) |
| 530 |
.succeeds() |
| 531 |
.stdout_is("\n a\n \n b\n"); |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
#[test] |
| 537 |
fn test_numbering_non_empty_lines() { |
| 538 |
let delimiters_and_args = [ |
| 539 |
("\\:\\:\\:\n", ["-ht", "--header-numbering=t"]), |
| 540 |
("\\:\\:\n", ["-bt", "--body-numbering=t"]), |
| 541 |
("\\:\n", ["-ft", "--footer-numbering=t"]), |
| 542 |
]; |
| 543 |
|
| 544 |
for (delimiter, args) in delimiters_and_args { |
| 545 |
for arg in args { |
| 546 |
new_ucmd!() |
| 547 |
.arg(arg) |
| 548 |
.pipe_in(format!("{delimiter}a\n\nb")) |
| 549 |
.succeeds() |
| 550 |
.stdout_is("\n 1\ta\n \n 2\tb\n"); |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
#[test] |
| 556 |
fn test_numbering_matched_lines() { |
| 557 |
let delimiters_and_args = [ |
| 558 |
("\\:\\:\\:\n", ["-hp^[ac]", "--header-numbering=p^[ac]"]), |
| 559 |
("\\:\\:\n", ["-bp^[ac]", "--body-numbering=p^[ac]"]), |
| 560 |
("\\:\n", ["-fp^[ac]", "--footer-numbering=p^[ac]"]), |
| 561 |
]; |
| 562 |
|
| 563 |
for (delimiter, args) in delimiters_and_args { |
| 564 |
for arg in args { |
| 565 |
new_ucmd!() |
| 566 |
.arg(arg) |
| 567 |
.pipe_in(format!("{delimiter}a\nb\nc")) |
| 568 |
.succeeds() |
| 569 |
.stdout_is("\n 1\ta\n b\n 2\tc\n"); |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
#[test] |
| 575 |
fn test_invalid_numbering() { |
| 576 |
let invalid_args = [ |
| 577 |
"-hinvalid", |
| 578 |
"--header-numbering=invalid", |
| 579 |
"-binvalid", |
| 580 |
"--body-numbering=invalid", |
| 581 |
"-finvalid", |
| 582 |
"--footer-numbering=invalid", |
| 583 |
]; |
| 584 |
|
| 585 |
for invalid_arg in invalid_args { |
| 586 |
new_ucmd!() |
| 587 |
.arg(invalid_arg) |
| 588 |
.fails() |
| 589 |
.stderr_contains("invalid numbering style: 'invalid'"); |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
#[test] |
| 594 |
fn test_invalid_regex_numbering() { |
| 595 |
let invalid_args = [ |
| 596 |
"-hp[", |
| 597 |
"--header-numbering=p[", |
| 598 |
"-bp[", |
| 599 |
"--body-numbering=p[", |
| 600 |
"-fp[", |
| 601 |
"--footer-numbering=p[", |
| 602 |
]; |
| 603 |
|
| 604 |
for invalid_arg in invalid_args { |
| 605 |
new_ucmd!() |
| 606 |
.arg(invalid_arg) |
| 607 |
.fails() |
| 608 |
.stderr_contains("invalid regular expression"); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
#[test] |
| 613 |
fn test_line_number_overflow() { |
| 614 |
new_ucmd!() |
| 615 |
.arg(format!("--starting-line-number={}", i64::MAX)) |
| 616 |
.pipe_in("a\nb") |
| 617 |
.fails() |
| 618 |
.stdout_is(format!("{}\ta\n", i64::MAX)) |
| 619 |
.stderr_is("nl: line number overflow\n"); |
| 620 |
|
| 621 |
new_ucmd!() |
| 622 |
.arg(format!("--starting-line-number={}", i64::MIN)) |
| 623 |
.arg("--line-increment=-1") |
| 624 |
.pipe_in("a\nb") |
| 625 |
.fails() |
| 626 |
.stdout_is(format!("{}\ta\n", i64::MIN)) |
| 627 |
.stderr_is("nl: line number overflow\n"); |
| 628 |
} |
| 629 |
|
| 630 |
#[test] |
| 631 |
fn test_line_number_no_overflow() { |
| 632 |
new_ucmd!() |
| 633 |
.arg(format!("--starting-line-number={}", i64::MAX)) |
| 634 |
.pipe_in("a\n\\:\\:\nb") |
| 635 |
.succeeds() |
| 636 |
.stdout_is(format!("{0}\ta\n\n{0}\tb\n", i64::MAX)); |
| 637 |
|
| 638 |
new_ucmd!() |
| 639 |
.arg(format!("--starting-line-number={}", i64::MIN)) |
| 640 |
.arg("--line-increment=-1") |
| 641 |
.pipe_in("a\n\\:\\:\nb") |
| 642 |
.succeeds() |
| 643 |
.stdout_is(format!("{0}\ta\n\n{0}\tb\n", i64::MIN)); |
| 644 |
} |
| 645 |
|
| 646 |
#[test] |
| 647 |
fn test_section_delimiter() { |
| 648 |
for arg in ["-dabc", "--section-delimiter=abc"] { |
| 649 |
new_ucmd!() |
| 650 |
.arg(arg) |
| 651 |
.pipe_in("a\nabcabcabc\nb") // header section |
| 652 |
.succeeds() |
| 653 |
.stdout_is(" 1\ta\n\n b\n"); |
| 654 |
|
| 655 |
new_ucmd!() |
| 656 |
.arg(arg) |
| 657 |
.pipe_in("a\nabcabc\nb") // body section |
| 658 |
.succeeds() |
| 659 |
.stdout_is(" 1\ta\n\n 1\tb\n"); |
| 660 |
|
| 661 |
new_ucmd!() |
| 662 |
.arg(arg) |
| 663 |
.pipe_in("a\nabc\nb") // footer section |
| 664 |
.succeeds() |
| 665 |
.stdout_is(" 1\ta\n\n b\n"); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
#[test] |
| 670 |
#[cfg(unix)] |
| 671 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 672 |
fn test_section_delimiter_non_utf8() { |
| 673 |
use std::{ffi::OsString, os::unix::ffi::OsStringExt}; |
| 674 |
|
| 675 |
fn create_arg(prefix: &[u8]) -> OsString { |
| 676 |
let section_delimiter = [0xFF, 0xFE]; |
| 677 |
let mut v = prefix.to_vec(); |
| 678 |
v.extend_from_slice(§ion_delimiter); |
| 679 |
OsString::from_vec(v) |
| 680 |
} |
| 681 |
|
| 682 |
let short = create_arg(b"-d"); |
| 683 |
let long = create_arg(b"--section-delimiter="); |
| 684 |
|
| 685 |
for arg in [short, long] { |
| 686 |
let header_section: Vec<u8> = |
| 687 |
vec![b'a', b'\n', 0xFF, 0xFE, 0xFF, 0xFE, 0xFF, 0xFE, b'\n', b'b']; |
| 688 |
|
| 689 |
new_ucmd!() |
| 690 |
.arg(&arg) |
| 691 |
.pipe_in(header_section) |
| 692 |
.succeeds() |
| 693 |
.stdout_is(" 1\ta\n\n b\n"); |
| 694 |
|
| 695 |
let body_section: Vec<u8> = vec![b'a', b'\n', 0xFF, 0xFE, 0xFF, 0xFE, b'\n', b'b']; |
| 696 |
|
| 697 |
new_ucmd!() |
| 698 |
.arg(&arg) |
| 699 |
.pipe_in(body_section) |
| 700 |
.succeeds() |
| 701 |
.stdout_is(" 1\ta\n\n 1\tb\n"); |
| 702 |
|
| 703 |
let footer_section: Vec<u8> = vec![b'a', b'\n', 0xFF, 0xFE, b'\n', b'b']; |
| 704 |
|
| 705 |
new_ucmd!() |
| 706 |
.arg(&arg) |
| 707 |
.pipe_in(footer_section) |
| 708 |
.succeeds() |
| 709 |
.stdout_is(" 1\ta\n\n b\n"); |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
#[test] |
| 714 |
fn test_one_char_section_delimiter() { |
| 715 |
for arg in ["-da", "--section-delimiter=a"] { |
| 716 |
new_ucmd!() |
| 717 |
.arg(arg) |
| 718 |
.pipe_in("a\na:a:a:\nb") // header section |
| 719 |
.succeeds() |
| 720 |
.stdout_is(" 1\ta\n\n b\n"); |
| 721 |
|
| 722 |
new_ucmd!() |
| 723 |
.arg(arg) |
| 724 |
.pipe_in("a\na:a:\nb") // body section |
| 725 |
.succeeds() |
| 726 |
.stdout_is(" 1\ta\n\n 1\tb\n"); |
| 727 |
|
| 728 |
new_ucmd!() |
| 729 |
.arg(arg) |
| 730 |
.pipe_in("a\na:\nb") // footer section |
| 731 |
.succeeds() |
| 732 |
.stdout_is(" 1\ta\n\n b\n"); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
#[test] |
| 737 |
#[cfg(unix)] |
| 738 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 739 |
fn test_one_byte_section_delimiter() { |
| 740 |
use std::{ffi::OsString, os::unix::ffi::OsStringExt}; |
| 741 |
|
| 742 |
fn create_arg(prefix: &[u8]) -> OsString { |
| 743 |
let mut v = prefix.to_vec(); |
| 744 |
v.push(0xFF); |
| 745 |
OsString::from_vec(v) |
| 746 |
} |
| 747 |
|
| 748 |
let short = create_arg(b"-d"); |
| 749 |
let long = create_arg(b"--section-delimiter="); |
| 750 |
|
| 751 |
for arg in [short, long] { |
| 752 |
let header_section: Vec<u8> = |
| 753 |
vec![b'a', b'\n', 0xFF, b':', 0xFF, b':', 0xFF, b':', b'\n', b'b']; |
| 754 |
|
| 755 |
new_ucmd!() |
| 756 |
.arg(&arg) |
| 757 |
.pipe_in(header_section) |
| 758 |
.succeeds() |
| 759 |
.stdout_is(" 1\ta\n\n b\n"); |
| 760 |
|
| 761 |
let body_section: Vec<u8> = vec![b'a', b'\n', 0xFF, b':', 0xFF, b':', b'\n', b'b']; |
| 762 |
|
| 763 |
new_ucmd!() |
| 764 |
.arg(&arg) |
| 765 |
.pipe_in(body_section) |
| 766 |
.succeeds() |
| 767 |
.stdout_is(" 1\ta\n\n 1\tb\n"); |
| 768 |
|
| 769 |
let footer_section: Vec<u8> = vec![b'a', b'\n', 0xFF, b':', b'\n', b'b']; |
| 770 |
|
| 771 |
new_ucmd!() |
| 772 |
.arg(&arg) |
| 773 |
.pipe_in(footer_section) |
| 774 |
.succeeds() |
| 775 |
.stdout_is(" 1\ta\n\n b\n"); |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
#[test] |
| 780 |
fn test_multi_byte_one_char_section_delimiter() { |
| 781 |
for arg in ["-dä", "--section-delimiter=ä"] { |
| 782 |
new_ucmd!() |
| 783 |
.arg(arg) |
| 784 |
.pipe_in("a\nä:ä:ä:\nb") // header section |
| 785 |
.succeeds() |
| 786 |
.stdout_is(" 1\ta\n\n b\n"); |
| 787 |
|
| 788 |
new_ucmd!() |
| 789 |
.arg(arg) |
| 790 |
.pipe_in("a\nä:ä:\nb") // body section |
| 791 |
.succeeds() |
| 792 |
.stdout_is(" 1\ta\n\n 1\tb\n"); |
| 793 |
|
| 794 |
new_ucmd!() |
| 795 |
.arg(arg) |
| 796 |
.pipe_in("a\nä:\nb") // footer section |
| 797 |
.succeeds() |
| 798 |
.stdout_is(" 1\ta\n\n b\n"); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
#[test] |
| 803 |
fn test_empty_section_delimiter() { |
| 804 |
for arg in ["-d ''", "--section-delimiter=''"] { |
| 805 |
new_ucmd!() |
| 806 |
.arg(arg) |
| 807 |
.pipe_in("a\n\nb") |
| 808 |
.succeeds() |
| 809 |
.stdout_is(" 1\ta\n \n 2\tb\n"); |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
#[test] |
| 814 |
fn test_directory_as_input() { |
| 815 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 816 |
let dir = "dir"; |
| 817 |
let file = "file"; |
| 818 |
let content = "aaa"; |
| 819 |
|
| 820 |
at.mkdir(dir); |
| 821 |
at.write(file, content); |
| 822 |
|
| 823 |
ucmd.arg(dir) |
| 824 |
.arg(file) |
| 825 |
.fails() |
| 826 |
.stderr_is(format!("nl: {dir}: Is a directory\n")) |
| 827 |
.stdout_contains(content); |
| 828 |
} |
| 829 |
|
| 830 |
#[test] |
| 831 |
fn test_file_with_non_utf8_content() { |
| 832 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 833 |
|
| 834 |
let filename = "file"; |
| 835 |
let content: &[u8] = b"a\n\xFF\xFE\nb"; |
| 836 |
|
| 837 |
at.write_bytes(filename, content); |
| 838 |
|
| 839 |
// Raw bytes should be preserved in output (not converted to UTF-8 replacement chars) |
| 840 |
let expected: Vec<u8> = b" 1\ta\n 2\t\xFF\xFE\n 3\tb\n".to_vec(); |
| 841 |
ucmd.arg(filename).succeeds().stdout_is_bytes(expected); |
| 842 |
} |
| 843 |
|
| 844 |
#[test] |
| 845 |
fn test_stdin_non_utf8_preserved() { |
| 846 |
// Verify that non-UTF8 bytes are preserved in output, not converted to replacement chars |
| 847 |
// This is important for locale compatibility |
| 848 |
let input: Vec<u8> = b"f\xe9vr.\n".to_vec(); // "févr." in Latin-1 |
| 849 |
let expected: Vec<u8> = b" 1\tf\xe9vr.\n".to_vec(); |
| 850 |
new_ucmd!() |
| 851 |
.pipe_in(input) |
| 852 |
.succeeds() |
| 853 |
.stdout_is_bytes(expected); |
| 854 |
} |
| 855 |
|
| 856 |
// Regression tests for issue #9132: repeated flags should use last value |
| 857 |
#[test] |
| 858 |
fn test_repeated_body_numbering_flag() { |
| 859 |
// -ba -bt should use -bt (t=nonempty) |
| 860 |
new_ucmd!() |
| 861 |
.args(&["-ba", "-bt"]) |
| 862 |
.pipe_in("a\n\nb\n\nc") |
| 863 |
.succeeds() |
| 864 |
.stdout_is(" 1\ta\n \n 2\tb\n \n 3\tc\n"); |
| 865 |
} |
| 866 |
|
| 867 |
#[test] |
| 868 |
fn test_repeated_header_numbering_flag() { |
| 869 |
// -ha -ht should use -ht (number only nonempty lines in header) |
| 870 |
new_ucmd!() |
| 871 |
.args(&["-ha", "-ht"]) |
| 872 |
.pipe_in("\\:\\:\\:\na\nb\n\nc") |
| 873 |
.succeeds() |
| 874 |
.stdout_is("\n 1\ta\n 2\tb\n \n 3\tc\n"); |
| 875 |
} |
| 876 |
|
| 877 |
#[test] |
| 878 |
fn test_repeated_footer_numbering_flag() { |
| 879 |
// -fa -ft should use -ft (t=nonempty in footer) |
| 880 |
new_ucmd!() |
| 881 |
.args(&["-fa", "-ft"]) |
| 882 |
.pipe_in("\\:\na\nb\n\nc") |
| 883 |
.succeeds() |
| 884 |
.stdout_is("\n 1\ta\n 2\tb\n \n 3\tc\n"); |
| 885 |
} |
| 886 |
|
| 887 |
#[test] |
| 888 |
fn test_repeated_number_format_flag() { |
| 889 |
// -n ln -n rn should use -n rn (rn=right aligned) |
| 890 |
new_ucmd!() |
| 891 |
.args(&["-n", "ln", "-n", "rn"]) |
| 892 |
.pipe_in("a\nb\nc") |
| 893 |
.succeeds() |
| 894 |
.stdout_is(" 1\ta\n 2\tb\n 3\tc\n"); |
| 895 |
} |
| 896 |
|
| 897 |
#[test] |
| 898 |
fn test_repeated_number_separator_flag() { |
| 899 |
// -s ':' -s '|' should use -s '|' |
| 900 |
new_ucmd!() |
| 901 |
.args(&["-s", ":", "-s", "|"]) |
| 902 |
.pipe_in("a\nb\nc") |
| 903 |
.succeeds() |
| 904 |
.stdout_is(" 1|a\n 2|b\n 3|c\n"); |
| 905 |
} |
| 906 |
|
| 907 |
#[test] |
| 908 |
fn test_repeated_number_width_flag() { |
| 909 |
// -w 3 -w 8 should use -w 8 |
| 910 |
new_ucmd!() |
| 911 |
.args(&["-w", "3", "-w", "8"]) |
| 912 |
.pipe_in("a\nb\nc") |
| 913 |
.succeeds() |
| 914 |
.stdout_is(" 1\ta\n 2\tb\n 3\tc\n"); |
| 915 |
} |
| 916 |
|
| 917 |
#[test] |
| 918 |
fn test_repeated_line_increment_flag() { |
| 919 |
// -i 1 -i 5 should use -i 5 |
| 920 |
new_ucmd!() |
| 921 |
.args(&["-i", "1", "-i", "5"]) |
| 922 |
.pipe_in("a\nb\nc") |
| 923 |
.succeeds() |
| 924 |
.stdout_is(" 1\ta\n 6\tb\n 11\tc\n"); |
| 925 |
} |
| 926 |
|
| 927 |
#[test] |
| 928 |
fn test_repeated_starting_line_number_flag() { |
| 929 |
// -v 1 -v 10 should use -v 10 |
| 930 |
new_ucmd!() |
| 931 |
.args(&["-v", "1", "-v", "10"]) |
| 932 |
.pipe_in("a\nb\nc") |
| 933 |
.succeeds() |
| 934 |
.stdout_is(" 10\ta\n 11\tb\n 12\tc\n"); |
| 935 |
} |
| 936 |
|
| 937 |
#[test] |
| 938 |
fn test_repeated_join_blank_lines_flag() { |
| 939 |
// -l 1 -l 2 should use -l 2 |
| 940 |
new_ucmd!() |
| 941 |
.args(&["-l", "1", "-l", "2", "-ba"]) |
| 942 |
.pipe_in("a\n\n\nb") |
| 943 |
.succeeds() |
| 944 |
.stdout_is(" 1\ta\n \n 2\t\n 3\tb\n"); |
| 945 |
} |
| 946 |
|
| 947 |
#[test] |
| 948 |
fn test_repeated_section_delimiter_flag() { |
| 949 |
// -d ':' -d '|' should use -d '|' |
| 950 |
new_ucmd!() |
| 951 |
.args(&["-d", ":", "-d", "|"]) |
| 952 |
.pipe_in("|:|:|:\na\nb\nc") |
| 953 |
.succeeds() |
| 954 |
.stdout_is("\n a\n b\n c\n"); |
| 955 |
} |
| 956 |
|
| 957 |
#[test] |
| 958 |
#[cfg(target_os = "linux")] |
| 959 |
fn test_no_skip_after_error() { |
| 960 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 961 |
at.write("f", "hello"); |
| 962 |
ucmd.args(&["/proc/self/mem", "f"]) |
| 963 |
.fails() |
| 964 |
.stdout_is(" 1\thello\n"); |
| 965 |
} |