| 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 defg naïve nave närd nøys ntøys nfjärd undelimited xbfw |
| 7 |
|
| 8 |
use uutests::{at_and_ucmd, new_ucmd}; |
| 9 |
|
| 10 |
static INPUT: &str = "lists.txt"; |
| 11 |
|
| 12 |
struct TestedSequence<'b> { |
| 13 |
name: &'b str, |
| 14 |
sequence: &'b str, |
| 15 |
} |
| 16 |
|
| 17 |
static EXAMPLE_SEQUENCES: &[TestedSequence] = &[ |
| 18 |
TestedSequence { |
| 19 |
name: "singular", |
| 20 |
sequence: "2", |
| 21 |
}, |
| 22 |
TestedSequence { |
| 23 |
name: "prefix", |
| 24 |
sequence: "-2", |
| 25 |
}, |
| 26 |
TestedSequence { |
| 27 |
name: "suffix", |
| 28 |
sequence: "2-", |
| 29 |
}, |
| 30 |
TestedSequence { |
| 31 |
name: "range", |
| 32 |
sequence: "2-4", |
| 33 |
}, |
| 34 |
TestedSequence { |
| 35 |
name: "aggregate", |
| 36 |
sequence: "9-,6-7,-2,4", |
| 37 |
}, |
| 38 |
TestedSequence { |
| 39 |
name: "subsumed", |
| 40 |
sequence: "2-,3", |
| 41 |
}, |
| 42 |
]; |
| 43 |
|
| 44 |
const COMPLEX_SEQUENCE: &str = "9-,6-7,-2,4"; |
| 45 |
|
| 46 |
#[test] |
| 47 |
fn test_no_args() { |
| 48 |
new_ucmd!() |
| 49 |
.fails() |
| 50 |
.stderr_contains("cut: you must specify a list of bytes, characters, or fields"); |
| 51 |
} |
| 52 |
|
| 53 |
#[test] |
| 54 |
fn test_invalid_arg() { |
| 55 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 56 |
} |
| 57 |
|
| 58 |
#[test] |
| 59 |
fn test_range_error_messages() { |
| 60 |
// Mode-aware diagnostics for invalid ranges. |
| 61 |
let cases: &[(&[&str], &str)] = &[ |
| 62 |
( |
| 63 |
&["-c0"], |
| 64 |
"cut: byte/character positions are numbered from 1", |
| 65 |
), |
| 66 |
( |
| 67 |
&["-b0-7"], |
| 68 |
"cut: byte/character positions are numbered from 1", |
| 69 |
), |
| 70 |
(&["-f0-9"], "cut: fields are numbered from 1"), |
| 71 |
(&["-f", ""], "cut: fields are numbered from 1"), |
| 72 |
( |
| 73 |
&["-c", ""], |
| 74 |
"cut: byte/character positions are numbered from 1", |
| 75 |
), |
| 76 |
(&["-f", "q"], "cut: invalid field value 'q'"), |
| 77 |
(&["-c", "zz"], "cut: invalid byte/character position 'zz'"), |
| 78 |
(&["-f", "9-4"], "cut: invalid decreasing range"), |
| 79 |
(&["-c", "-"], "cut: invalid range with no endpoint: -"), |
| 80 |
(&["-f", "8,-"], "cut: invalid range with no endpoint: -"), |
| 81 |
// The offending text is what could not be consumed, not the whole item. |
| 82 |
(&["-f", "7k"], "cut: invalid field value 'k'"), |
| 83 |
(&["-f", "4-w2"], "cut: invalid field value 'w2'"), |
| 84 |
( |
| 85 |
&["-c", "3x-5"], |
| 86 |
"cut: invalid byte/character position 'x-5'", |
| 87 |
), |
| 88 |
// A leading sign is not part of a number here. |
| 89 |
(&["-f", "+6"], "cut: invalid field value '+6'"), |
| 90 |
// A second dash makes it a malformed range instead. |
| 91 |
(&["-f", "2-5-8"], "cut: invalid field range"), |
| 92 |
(&["-c", "3--6"], "cut: invalid byte or character range"), |
| 93 |
// `usize::MAX` itself is rejected, and so is anything above it. |
| 94 |
( |
| 95 |
&["-f", "18446744073709551615"], |
| 96 |
"cut: field number '18446744073709551615' is too large", |
| 97 |
), |
| 98 |
( |
| 99 |
&["-c", "4-77777777777777777777777"], |
| 100 |
"cut: byte/character offset '77777777777777777777777' is too large", |
| 101 |
), |
| 102 |
]; |
| 103 |
for (args, expected) in cases { |
| 104 |
new_ucmd!() |
| 105 |
.args(args) |
| 106 |
.fails_with_code(1) |
| 107 |
.stderr_contains(*expected); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn test_field_only_options_without_fields() { |
| 113 |
new_ucmd!() |
| 114 |
.args(&["-s", "-c7"]) |
| 115 |
.fails_with_code(1) |
| 116 |
.stderr_contains( |
| 117 |
"cut: suppressing non-delimited lines makes sense\n\tonly when operating on fields", |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn test_delimiter_and_whitespace_are_exclusive() { |
| 123 |
new_ucmd!() |
| 124 |
.args(&["-w", "-d,", "-f3"]) |
| 125 |
.fails_with_code(1) |
| 126 |
.stderr_contains("cut: -d and -w are mutually exclusive"); |
| 127 |
} |
| 128 |
|
| 129 |
#[test] |
| 130 |
fn test_byte_sequence() { |
| 131 |
for param in ["-b", "--bytes", "--byt"] { |
| 132 |
for example_seq in EXAMPLE_SEQUENCES { |
| 133 |
new_ucmd!() |
| 134 |
.args(&[param, example_seq.sequence, INPUT]) |
| 135 |
.succeeds() |
| 136 |
.stdout_only_fixture(format!("sequences/byte_{}.expected", example_seq.name)); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn test_char_sequence() { |
| 143 |
for param in ["-c", "--characters", "--char"] { |
| 144 |
for example_seq in EXAMPLE_SEQUENCES { |
| 145 |
//as of coreutils 8.25 a char range is effectively the same as a byte range; there is no distinct treatment of utf8 chars. |
| 146 |
new_ucmd!() |
| 147 |
.args(&[param, example_seq.sequence, INPUT]) |
| 148 |
.succeeds() |
| 149 |
.stdout_only_fixture(format!("sequences/byte_{}.expected", example_seq.name)); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
#[test] |
| 155 |
fn test_field_sequence() { |
| 156 |
for param in ["-f", "--fields", "--fie"] { |
| 157 |
for example_seq in EXAMPLE_SEQUENCES { |
| 158 |
new_ucmd!() |
| 159 |
.args(&[param, example_seq.sequence, INPUT]) |
| 160 |
.succeeds() |
| 161 |
.stdout_only_fixture(format!("sequences/field_{}.expected", example_seq.name)); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn test_whitespace_delimited() { |
| 168 |
new_ucmd!() |
| 169 |
.args(&["-w", "-f", COMPLEX_SEQUENCE, INPUT]) |
| 170 |
.succeeds() |
| 171 |
.stdout_only_fixture("whitespace_delimited.expected"); |
| 172 |
} |
| 173 |
|
| 174 |
#[test] |
| 175 |
fn test_whitespace_with_explicit_delimiter() { |
| 176 |
new_ucmd!() |
| 177 |
.args(&["-w", "-f", COMPLEX_SEQUENCE, "-d:"]) |
| 178 |
.fails_with_code(1); |
| 179 |
} |
| 180 |
|
| 181 |
#[test] |
| 182 |
fn test_whitespace_with_byte() { |
| 183 |
// `-w` counts as an input delimiter for the purpose of this diagnostic. |
| 184 |
new_ucmd!() |
| 185 |
.args(&["-w", "-b", COMPLEX_SEQUENCE]) |
| 186 |
.fails_with_code(1) |
| 187 |
.stderr_contains("cut: an input delimiter makes sense\n\tonly when operating on fields"); |
| 188 |
} |
| 189 |
|
| 190 |
#[test] |
| 191 |
fn test_whitespace_with_char() { |
| 192 |
new_ucmd!() |
| 193 |
.args(&["-c", COMPLEX_SEQUENCE, "-w"]) |
| 194 |
.fails_with_code(1) |
| 195 |
.stderr_contains("cut: an input delimiter makes sense\n\tonly when operating on fields"); |
| 196 |
} |
| 197 |
|
| 198 |
#[test] |
| 199 |
fn test_delimiter_with_byte_and_char() { |
| 200 |
for conflicting_arg in ["-c", "-b"] { |
| 201 |
new_ucmd!() |
| 202 |
.args(&[conflicting_arg, COMPLEX_SEQUENCE, "-d="]) |
| 203 |
.fails_with_code(1) |
| 204 |
.stderr_contains( |
| 205 |
"cut: an input delimiter makes sense\n\tonly when operating on fields", |
| 206 |
); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
#[test] |
| 211 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 212 |
fn test_too_large() { |
| 213 |
new_ucmd!() |
| 214 |
.args(&["-b1-18446744073709551615", "/dev/null"]) |
| 215 |
.fails_with_code(1); |
| 216 |
} |
| 217 |
|
| 218 |
#[test] |
| 219 |
fn test_delimiter() { |
| 220 |
for param in ["-d", "--delimiter", "--del"] { |
| 221 |
new_ucmd!() |
| 222 |
.args(&[param, ":", "-f", COMPLEX_SEQUENCE, INPUT]) |
| 223 |
.succeeds() |
| 224 |
.stdout_only_fixture("delimiter_specified.expected"); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
#[test] |
| 229 |
fn test_delimiter_with_more_than_one_char() { |
| 230 |
new_ucmd!() |
| 231 |
.args(&["-d", "ab", "-f1"]) |
| 232 |
.fails() |
| 233 |
.stderr_contains("cut: the delimiter must be a single character") |
| 234 |
.no_stdout(); |
| 235 |
} |
| 236 |
|
| 237 |
#[test] |
| 238 |
fn test_output_delimiter() { |
| 239 |
for param in ["--output-delimiter=@", "--output-del=@", "-O@"] { |
| 240 |
// with default field delimiter (tab) |
| 241 |
new_ucmd!() |
| 242 |
.arg(param) |
| 243 |
.arg("-f1,2") |
| 244 |
.pipe_in("a:\tb:\tc:\n") |
| 245 |
.succeeds() |
| 246 |
.stdout_only("a:@b:\n"); |
| 247 |
|
| 248 |
// with custom field delimiter |
| 249 |
new_ucmd!() |
| 250 |
.arg(param) |
| 251 |
.arg("-f1,2") |
| 252 |
.arg("-d:") |
| 253 |
.pipe_in("a:\tb:\tc\n") |
| 254 |
.succeeds() |
| 255 |
.stdout_only("a@\tb\n"); |
| 256 |
|
| 257 |
// with no field delimiter |
| 258 |
new_ucmd!() |
| 259 |
.arg(param) |
| 260 |
.arg("-f1,2") |
| 261 |
.pipe_in("a:b:c\n") |
| 262 |
.succeeds() |
| 263 |
.stdout_only("a:b:c\n"); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
#[test] |
| 268 |
fn test_complement() { |
| 269 |
for param in ["--complement", "--com"] { |
| 270 |
new_ucmd!() |
| 271 |
.args(&["-d_", param, "-f", "2"]) |
| 272 |
.pipe_in("9_1\n8_2\n7_3") |
| 273 |
.succeeds() |
| 274 |
.stdout_only("9\n8\n7\n"); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
#[test] |
| 279 |
fn test_zero_terminated() { |
| 280 |
new_ucmd!() |
| 281 |
.args(&["-d_", "-z", "-f", "1"]) |
| 282 |
.pipe_in("9_1\n8_2\n\x007_3") |
| 283 |
.succeeds() |
| 284 |
.stdout_only("9\x007\0"); |
| 285 |
} |
| 286 |
|
| 287 |
#[test] |
| 288 |
fn test_only_delimited() { |
| 289 |
for param in ["-s", "--only-delimited", "--only-del"] { |
| 290 |
new_ucmd!() |
| 291 |
.args(&["-d_", param, "-f", "1"]) |
| 292 |
.pipe_in("91\n82\n7_3") |
| 293 |
.succeeds() |
| 294 |
.stdout_only("7\n"); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
#[test] |
| 299 |
fn test_zero_terminated_only_delimited() { |
| 300 |
new_ucmd!() |
| 301 |
.args(&["-d_", "-z", "-s", "-f", "1"]) |
| 302 |
.pipe_in("91\n\082\n7_3") |
| 303 |
.succeeds() |
| 304 |
.stdout_only("82\n7\0"); |
| 305 |
} |
| 306 |
|
| 307 |
#[test] |
| 308 |
fn test_suppresses_unterminated_segment() { |
| 309 |
new_ucmd!() |
| 310 |
.args(&["-z", "-d", "", "-s", "-f", "1"]) |
| 311 |
.pipe_in("unterminated") |
| 312 |
.succeeds() |
| 313 |
.stdout_only_bytes(""); |
| 314 |
|
| 315 |
new_ucmd!() |
| 316 |
.args(&["-z", "-d", "", "-s", "-f", "1"]) |
| 317 |
.pipe_in("terminated\0unterminated") |
| 318 |
.succeeds() |
| 319 |
.stdout_only_bytes("terminated\0"); |
| 320 |
} |
| 321 |
|
| 322 |
#[test] |
| 323 |
fn test_is_a_directory() { |
| 324 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 325 |
|
| 326 |
at.mkdir("some"); |
| 327 |
|
| 328 |
ucmd.arg("-b1") |
| 329 |
.arg("some") |
| 330 |
.fails_with_code(1) |
| 331 |
.stderr_is("cut: some: Is a directory\n"); |
| 332 |
} |
| 333 |
|
| 334 |
#[test] |
| 335 |
fn test_no_such_file() { |
| 336 |
new_ucmd!() |
| 337 |
.arg("-b1") |
| 338 |
.arg("some") |
| 339 |
.fails_with_code(1) |
| 340 |
.stderr_is("cut: some: No such file or directory\n"); |
| 341 |
} |
| 342 |
|
| 343 |
#[test] |
| 344 |
fn test_equal_as_delimiter() { |
| 345 |
for arg in ["-d=", "--delimiter=="] { |
| 346 |
new_ucmd!() |
| 347 |
.args(&["-f2", arg]) |
| 348 |
.pipe_in("--dir=./out/lib") |
| 349 |
.succeeds() |
| 350 |
.stdout_only("./out/lib\n"); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
#[test] |
| 355 |
fn test_empty_string_as_delimiter() { |
| 356 |
new_ucmd!() |
| 357 |
.args(&["-f2", "--delimiter="]) |
| 358 |
.pipe_in("a\0b\n") |
| 359 |
.succeeds() |
| 360 |
.stdout_only("b\n"); |
| 361 |
} |
| 362 |
|
| 363 |
#[test] |
| 364 |
fn test_single_quote_pair_as_delimiter_is_invalid() { |
| 365 |
for args in [&["-d", "''", "-f2"][..], &["--delimiter=''", "-f2"][..]] { |
| 366 |
new_ucmd!() |
| 367 |
.args(args) |
| 368 |
.ignore_stdin_write_error() |
| 369 |
.pipe_in("a''b\n") |
| 370 |
.fails() |
| 371 |
.stderr_contains("cut: the delimiter must be a single character") |
| 372 |
.no_stdout(); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
#[test] |
| 377 |
fn test_empty_string_as_delimiter_with_output_delimiter() { |
| 378 |
new_ucmd!() |
| 379 |
.args(&["-f", "1,2", "--delimiter=", "--output-delimiter=Z"]) |
| 380 |
.pipe_in("ab\0cd\n") |
| 381 |
.succeeds() |
| 382 |
.stdout_only_bytes("abZcd\n"); |
| 383 |
} |
| 384 |
|
| 385 |
#[test] |
| 386 |
fn test_single_quote_pair_as_output_delimiter_is_literal() { |
| 387 |
new_ucmd!() |
| 388 |
.args(&["-f", "1,2", "-d:", "--output-delimiter=''"]) |
| 389 |
.pipe_in("ab:cd\n") |
| 390 |
.succeeds() |
| 391 |
.stdout_only_bytes("ab''cd\n"); |
| 392 |
} |
| 393 |
|
| 394 |
#[test] |
| 395 |
fn test_newline_as_delimiter() { |
| 396 |
for (field, expected_output) in [("1", "a:1\n"), ("2", "b:\n")] { |
| 397 |
new_ucmd!() |
| 398 |
.args(&["-f", field, "-d", "\n"]) |
| 399 |
.pipe_in("a:1\nb:") |
| 400 |
.succeeds() |
| 401 |
.stdout_only_bytes(expected_output); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
#[test] |
| 406 |
fn test_newline_as_delimiter_with_output_delimiter() { |
| 407 |
new_ucmd!() |
| 408 |
.args(&["-f1-", "-d", "\n", "--output-delimiter=:"]) |
| 409 |
.pipe_in("a\nb\n") |
| 410 |
.succeeds() |
| 411 |
.stdout_only_bytes("a:b\n"); |
| 412 |
} |
| 413 |
|
| 414 |
#[test] |
| 415 |
fn test_newline_as_delimiter_no_delimiter_suppressed() { |
| 416 |
for param in ["-s", "--only-delimited", "--only-del"] { |
| 417 |
new_ucmd!() |
| 418 |
.args(&["-d", "\n", param, "-f", "1"]) |
| 419 |
.pipe_in("abc") |
| 420 |
.succeeds() |
| 421 |
.no_output(); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
#[test] |
| 426 |
fn test_newline_as_delimiter_found_not_suppressed() { |
| 427 |
// Has an internal \n delimiter, so -s shouldn't suppress it |
| 428 |
for param in ["-s", "--only-delimited", "--only-del"] { |
| 429 |
new_ucmd!() |
| 430 |
.args(&["-d", "\n", param, "-f", "1"]) |
| 431 |
.pipe_in("abc\ndef\n") |
| 432 |
.succeeds() |
| 433 |
.stdout_only("abc\n"); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
#[test] |
| 438 |
fn test_newline_as_delimiter_multiple_fields() { |
| 439 |
// Check field selection when \n is the delimiter |
| 440 |
new_ucmd!() |
| 441 |
.args(&["-d", "\n", "-f", "2"]) |
| 442 |
.pipe_in("abc\ndef\n") |
| 443 |
.succeeds() |
| 444 |
.stdout_only("def\n"); |
| 445 |
} |
| 446 |
|
| 447 |
#[test] |
| 448 |
fn test_newline_as_delimiter_double_newline() { |
| 449 |
// Field 2 is the empty space between newlines |
| 450 |
new_ucmd!() |
| 451 |
.args(&["-d", "\n", "-s", "-f", "2"]) |
| 452 |
.pipe_in("abc\n\n") |
| 453 |
.succeeds() |
| 454 |
.stdout_only("\n"); |
| 455 |
|
| 456 |
// Requesting both fields |
| 457 |
new_ucmd!() |
| 458 |
.args(&["-d", "\n", "-s", "-f", "1,2"]) |
| 459 |
.pipe_in("abc\n\n") |
| 460 |
.succeeds() |
| 461 |
.stdout_only("abc\n\n"); |
| 462 |
} |
| 463 |
|
| 464 |
#[test] |
| 465 |
fn test_newline_as_delimiter_only_newlines() { |
| 466 |
// Extracting empty fields from a string of just newlines |
| 467 |
new_ucmd!() |
| 468 |
.args(&["-d", "\n", "-s", "-f", "1"]) |
| 469 |
.pipe_in("\n\n") |
| 470 |
.succeeds() |
| 471 |
.stdout_only("\n"); |
| 472 |
|
| 473 |
new_ucmd!() |
| 474 |
.args(&["-d", "\n", "-s", "-f", "2"]) |
| 475 |
.pipe_in("\n\n") |
| 476 |
.succeeds() |
| 477 |
.stdout_only("\n"); |
| 478 |
|
| 479 |
new_ucmd!() |
| 480 |
.args(&["-d", "\n", "-s", "-f", "1,2"]) |
| 481 |
.pipe_in("\n\n") |
| 482 |
.succeeds() |
| 483 |
.stdout_only("\n\n"); |
| 484 |
} |
| 485 |
|
| 486 |
#[test] |
| 487 |
fn test_newline_as_delimiter_last_field_no_newline() { |
| 488 |
// The last chunk is Field 2 even without a final newline |
| 489 |
new_ucmd!() |
| 490 |
.args(&["-d", "\n", "-f", "2"]) |
| 491 |
.pipe_in("abc\ndef") |
| 492 |
.succeeds() |
| 493 |
.stdout_only("def\n"); |
| 494 |
} |
| 495 |
|
| 496 |
#[test] |
| 497 |
fn test_newline_as_delimiter_complement() { |
| 498 |
// Select everything except the second line |
| 499 |
new_ucmd!() |
| 500 |
.args(&["-d", "\n", "-f", "2", "--complement"]) |
| 501 |
.pipe_in("line1\nline2\nline3\n") |
| 502 |
.succeeds() |
| 503 |
.stdout_only("line1\nline3\n"); |
| 504 |
} |
| 505 |
|
| 506 |
#[test] |
| 507 |
fn test_newline_as_delimiter_out_of_bounds() { |
| 508 |
// GNU cut: print an empty string + terminator for missing fields |
| 509 |
new_ucmd!() |
| 510 |
.args(&["-d", "\n", "-f", "3"]) |
| 511 |
.pipe_in("a\nb\n") |
| 512 |
.succeeds() |
| 513 |
.stdout_only("\n"); |
| 514 |
|
| 515 |
// GNU cut avoids trailing delimiters for out-of-bounds fields when delimiter is \n |
| 516 |
new_ucmd!() |
| 517 |
.args(&["-d", "\n", "-f", "1,3"]) |
| 518 |
.pipe_in("a\nb\n") |
| 519 |
.succeeds() |
| 520 |
.stdout_only("a\n"); |
| 521 |
} |
| 522 |
|
| 523 |
#[test] |
| 524 |
fn test_newline_as_delimiter_no_delimiter_prints_all() { |
| 525 |
// GNU cut: If no delimiter is found, the entire line (the whole file) |
| 526 |
// is printed regardless of the field requested, unless -s is used. |
| 527 |
new_ucmd!() |
| 528 |
.args(&["-d", "\n", "-f", "2"]) |
| 529 |
.pipe_in("a") |
| 530 |
.succeeds() |
| 531 |
.stdout_only("a\n"); |
| 532 |
} |
| 533 |
|
| 534 |
#[test] |
| 535 |
fn test_newline_as_delimiter_empty_input() { |
| 536 |
new_ucmd!() |
| 537 |
.args(&["-d", "\n", "-f", "1"]) |
| 538 |
.pipe_in("") |
| 539 |
.succeeds() |
| 540 |
.no_output(); |
| 541 |
} |
| 542 |
|
| 543 |
#[test] |
| 544 |
fn test_newline_as_delimiter_s_flag_no_newline_at_all() { |
| 545 |
new_ucmd!() |
| 546 |
.args(&["-d", "\n", "-s", "-f", "1"]) |
| 547 |
.pipe_in("abc") |
| 548 |
.succeeds() |
| 549 |
.no_output(); |
| 550 |
} |
| 551 |
|
| 552 |
#[test] |
| 553 |
fn test_newline_as_delimiter_single_field_included() { |
| 554 |
for param in ["-s", "--only-delimited", "--only-del"] { |
| 555 |
new_ucmd!() |
| 556 |
.args(&["-d", "\n", param, "-f", "1"]) |
| 557 |
.pipe_in("abc\n") |
| 558 |
.succeeds() |
| 559 |
.stdout_only("abc\n"); // GNU cut outputs the field + terminator |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
#[test] |
| 564 |
fn test_newline_as_delimiter_intervening_skipped_fields() { |
| 565 |
// Selecting non-adjacent lines (Fields 1 and 3) |
| 566 |
new_ucmd!() |
| 567 |
.args(&["-d", "\n", "-f", "1,3"]) |
| 568 |
.pipe_in("line1\nline2\nline3\n") |
| 569 |
.succeeds() |
| 570 |
.stdout_only("line1\nline3\n"); |
| 571 |
} |
| 572 |
|
| 573 |
#[test] |
| 574 |
fn test_newline_as_delimiter_multibyte_normalization() { |
| 575 |
// Ensure multibyte records at EOF still get a normalized newline |
| 576 |
new_ucmd!() |
| 577 |
.args(&["-d", "\n", "-f", "2"]) |
| 578 |
.pipe_in("\n😼") |
| 579 |
.succeeds() |
| 580 |
.stdout_only("😼\n"); |
| 581 |
} |
| 582 |
|
| 583 |
#[test] |
| 584 |
fn test_newline_as_delimiter_empty_first_record() { |
| 585 |
// Select Field 2 when Field 1 is empty |
| 586 |
new_ucmd!() |
| 587 |
.args(&["-d", "\n", "-f", "2"]) |
| 588 |
.pipe_in("\nb") |
| 589 |
.succeeds() |
| 590 |
.stdout_only("b\n"); |
| 591 |
} |
| 592 |
|
| 593 |
#[test] |
| 594 |
fn test_newline_as_delimiter_overlapping_unordered_ranges() { |
| 595 |
// Request fields out of order and with overlapping ranges |
| 596 |
new_ucmd!() |
| 597 |
.args(&["-d", "\n", "-f", "2-3,1,2"]) |
| 598 |
.pipe_in("a\nb\nc\n") |
| 599 |
.succeeds() |
| 600 |
.stdout_only("a\nb\nc\n"); |
| 601 |
} |
| 602 |
|
| 603 |
#[test] |
| 604 |
fn test_newline_as_delimiter_complement_last_record() { |
| 605 |
// Test --complement on the final record |
| 606 |
new_ucmd!() |
| 607 |
.args(&["-d", "\n", "-f", "1", "--complement"]) |
| 608 |
.pipe_in("a\nb") |
| 609 |
.succeeds() |
| 610 |
.stdout_only("b\n"); |
| 611 |
} |
| 612 |
|
| 613 |
#[test] |
| 614 |
fn test_multiple_delimiters() { |
| 615 |
new_ucmd!() |
| 616 |
.args(&["-f2", "-d:", "-d="]) |
| 617 |
.pipe_in("a:=b\n") |
| 618 |
.succeeds() |
| 619 |
.stdout_only("b\n"); |
| 620 |
|
| 621 |
new_ucmd!() |
| 622 |
.args(&["-f2", "-d=", "-d:"]) |
| 623 |
.pipe_in("a:=b\n") |
| 624 |
.succeeds() |
| 625 |
.stdout_only("=b\n"); |
| 626 |
} |
| 627 |
|
| 628 |
#[test] |
| 629 |
fn test_multiple_mode_args() { |
| 630 |
for args in [ |
| 631 |
vec!["-b1", "-b2"], |
| 632 |
vec!["-c1", "-c2"], |
| 633 |
vec!["-f1", "-f2"], |
| 634 |
vec!["-b1", "-c2"], |
| 635 |
vec!["-b1", "-f2"], |
| 636 |
vec!["-c1", "-f2"], |
| 637 |
vec!["-b1", "-c2", "-f3"], |
| 638 |
] { |
| 639 |
new_ucmd!() |
| 640 |
.args(&args) |
| 641 |
.fails() |
| 642 |
.stderr_contains("cut: only one list may be specified"); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
#[test] |
| 647 |
#[cfg(unix)] |
| 648 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 649 |
fn test_8bit_non_utf8_delimiter() { |
| 650 |
use std::ffi::OsStr; |
| 651 |
use std::os::unix::ffi::OsStrExt; |
| 652 |
let delim = OsStr::from_bytes(b"\xAD".as_slice()); |
| 653 |
new_ucmd!() |
| 654 |
.arg("-d") |
| 655 |
.arg(delim) |
| 656 |
.args(&["--out=_", "-f2,3", "8bit-delim.txt"]) |
| 657 |
.succeeds() |
| 658 |
.stdout_check(|out| out == "b_c\n".as_bytes()); |
| 659 |
} |
| 660 |
|
| 661 |
#[test] |
| 662 |
fn test_newline_preservation_with_f1_option() { |
| 663 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 664 |
at.write("1", "a\nb"); |
| 665 |
let expected = "a\nb\n"; |
| 666 |
ucmd.args(&["-f1-", "1"]).succeeds().stdout_is(expected); |
| 667 |
} |
| 668 |
|
| 669 |
#[test] |
| 670 |
fn test_output_delimiter_with_character_ranges() { |
| 671 |
new_ucmd!() |
| 672 |
.args(&["-c2-3,4-", "--output-delim=:"]) |
| 673 |
.pipe_in("abcdefg\n") |
| 674 |
.succeeds() |
| 675 |
.stdout_only("bc:defg\n"); |
| 676 |
} |
| 677 |
|
| 678 |
#[test] |
| 679 |
fn test_output_delimiter_with_adjacent_ranges() { |
| 680 |
new_ucmd!() |
| 681 |
.args(&["-b1-2,3-4", "--output-d=:"]) |
| 682 |
.pipe_in("abcd\n") |
| 683 |
.succeeds() |
| 684 |
.stdout_only("ab:cd\n"); |
| 685 |
} |
| 686 |
|
| 687 |
#[test] |
| 688 |
fn test_fields_merged() { |
| 689 |
// -F: merge adjacent delimiters, default delimiter whitespace, output a space. |
| 690 |
new_ucmd!() |
| 691 |
.args(&["-F", "1,3"]) |
| 692 |
.pipe_in("one\ttwo three\n") |
| 693 |
.succeeds() |
| 694 |
.stdout_only("one three\n"); |
| 695 |
new_ucmd!() |
| 696 |
.args(&["-F", "1,3", "-O", "+"]) |
| 697 |
.pipe_in("one\ttwo three\n") |
| 698 |
.succeeds() |
| 699 |
.stdout_only("one+three\n"); |
| 700 |
// -F with an explicit delimiter still uses a space as the output delimiter. |
| 701 |
new_ucmd!() |
| 702 |
.args(&["-F", "2,4", "-d", ";"]) |
| 703 |
.pipe_in("p;q;r;s\n") |
| 704 |
.succeeds() |
| 705 |
.stdout_only("q s\n"); |
| 706 |
} |
| 707 |
|
| 708 |
#[test] |
| 709 |
fn test_fields_merged_conflicts_with_fields() { |
| 710 |
new_ucmd!() |
| 711 |
.args(&["-f", "3", "-F", "5"]) |
| 712 |
.fails_with_code(1) |
| 713 |
.stderr_contains("cut: only one list may be specified"); |
| 714 |
} |
| 715 |
|
| 716 |
#[test] |
| 717 |
fn test_whitespace_delimited_long_and_trimmed() { |
| 718 |
// Long form behaves like -w (leading blanks make an empty first field). |
| 719 |
new_ucmd!() |
| 720 |
.args(&["--whitespace-delimited", "-f1,2"]) |
| 721 |
.pipe_in(" alpha beta\n") |
| 722 |
.succeeds() |
| 723 |
.stdout_only("\talpha\n"); |
| 724 |
// =trimmed (or it's shortcuts) strips leading/trailing blanks before splitting. |
| 725 |
for trimmed in ["trimmed", "tri", ""] { |
| 726 |
new_ucmd!() |
| 727 |
.arg(format!("--whitespace-delimited={trimmed}")) |
| 728 |
.arg("-f1,2") |
| 729 |
.pipe_in(" hello world \n") |
| 730 |
.succeeds() |
| 731 |
.stdout_only("hello\tworld\n"); |
| 732 |
} |
| 733 |
// With -s a single (undelimited) field is suppressed. |
| 734 |
new_ucmd!() |
| 735 |
.args(&["-s", "--whitespace-delimited=trimmed", "-f1"]) |
| 736 |
.pipe_in(" solo \n") |
| 737 |
.succeeds() |
| 738 |
.stdout_only(""); |
| 739 |
// Without -s an undelimited line is printed whole, whatever field is asked |
| 740 |
// for, while a blank-only line collapses to an empty one. |
| 741 |
new_ucmd!() |
| 742 |
.args(&["--whitespace-delimited=trimmed", "-f4"]) |
| 743 |
.pipe_in(" loner\n\t\n one two\n") |
| 744 |
.succeeds() |
| 745 |
.stdout_only("loner\n\n\n"); |
| 746 |
// Only `trimmed` is a valid value. |
| 747 |
new_ucmd!() |
| 748 |
.args(&["--whitespace-delimited=middle", "-f1"]) |
| 749 |
.fails_with_code(1); |
| 750 |
} |
| 751 |
|
| 752 |
#[test] |
| 753 |
fn test_whitespace_delimited_trimmed_zero_terminated() { |
| 754 |
// The record terminator must not count as a non-blank when trimming, or the |
| 755 |
// trailing blanks of a record would survive and add a phantom field. NUL is |
| 756 |
// the interesting case: unlike `\n` it is not whitespace to begin with. |
| 757 |
new_ucmd!() |
| 758 |
.args(&["-z", "--whitespace-delimited=trimmed", "-f2"]) |
| 759 |
.pipe_in(&b" red blue \0 green pink \0"[..]) |
| 760 |
.succeeds() |
| 761 |
.stdout_only_bytes(&b"blue\0pink\0"[..]); |
| 762 |
// Asking past the last field yields an empty record, not the stray blanks. |
| 763 |
new_ucmd!() |
| 764 |
.args(&["-z", "--whitespace-delimited=trimmed", "-f3"]) |
| 765 |
.pipe_in(&b" red blue \0"[..]) |
| 766 |
.succeeds() |
| 767 |
.stdout_only_bytes(&b"\0"[..]); |
| 768 |
// A blank-only record has no delimiter left after trimming, so -s drops it. |
| 769 |
new_ucmd!() |
| 770 |
.args(&["-z", "-s", "--whitespace-delimited=trimmed", "-f1"]) |
| 771 |
.pipe_in(&b"\t \0 amber violet \0"[..]) |
| 772 |
.succeeds() |
| 773 |
.stdout_only_bytes(&b"amber\0"[..]); |
| 774 |
} |
| 775 |
|
| 776 |
#[test] |
| 777 |
#[cfg_attr(wasi_runner, ignore = "WASI: the guest does not inherit LC_ALL")] |
| 778 |
#[cfg(target_os = "linux")] |
| 779 |
fn test_byte_no_split_partially_selected_char() { |
| 780 |
// -b -n: the selected bytes of a character must reach its end without a |
| 781 |
// hole. "🗿" (f0 9f 97 bf) spans bytes 1-4, "w" is byte 5. |
| 782 |
let stone = &b"\xf0\x9f\x97\xbfw\n"[..]; |
| 783 |
// Byte 3 is left out, so the character is split and dropped. |
| 784 |
new_ucmd!() |
| 785 |
.env("LC_ALL", "C.UTF-8") |
| 786 |
.args(&["-b1-2,4-5", "-n"]) |
| 787 |
.pipe_in(stone) |
| 788 |
.succeeds() |
| 789 |
.stdout_only_bytes(b"w\n"); |
| 790 |
// The same holds when the hole comes from a single-byte range. |
| 791 |
new_ucmd!() |
| 792 |
.env("LC_ALL", "C.UTF-8") |
| 793 |
.args(&["-b2,4", "-n"]) |
| 794 |
.pipe_in(stone) |
| 795 |
.succeeds() |
| 796 |
.stdout_only_bytes(b"\n"); |
| 797 |
// Selecting only the tail of the character still prints it whole. |
| 798 |
new_ucmd!() |
| 799 |
.env("LC_ALL", "C.UTF-8") |
| 800 |
.args(&["-b3-", "-n"]) |
| 801 |
.pipe_in(stone) |
| 802 |
.succeeds() |
| 803 |
.stdout_only_bytes(stone); |
| 804 |
// Adjacent ranges cover it without a hole, and the boundary inside the |
| 805 |
// character emits no output delimiter. |
| 806 |
new_ucmd!() |
| 807 |
.env("LC_ALL", "C.UTF-8") |
| 808 |
.args(&["-b1-3,4-5", "-n", "--output-d=|"]) |
| 809 |
.pipe_in(stone) |
| 810 |
.succeeds() |
| 811 |
.stdout_only_bytes(stone); |
| 812 |
// "q€é r": q is byte 1, € (e2 82 ac) bytes 2-4, é (c3 a9) bytes 5-6. |
| 813 |
let mixed = &b"q\xe2\x82\xac\xc3\xa9r\n"[..]; |
| 814 |
// The delimiter a range owes is carried to whatever prints next, and only |
| 815 |
// a boundary between two printed characters produces one. |
| 816 |
new_ucmd!() |
| 817 |
.env("LC_ALL", "C.UTF-8") |
| 818 |
.args(&["-b1,2,5-7", "-n", "--output-d=|"]) |
| 819 |
.pipe_in(mixed) |
| 820 |
.succeeds() |
| 821 |
.stdout_only_bytes(&b"q|\xc3\xa9r\n"[..]); |
| 822 |
new_ucmd!() |
| 823 |
.env("LC_ALL", "C.UTF-8") |
| 824 |
.args(&["-b1,2-4,5-6", "-n", "--output-d=|"]) |
| 825 |
.pipe_in(mixed) |
| 826 |
.succeeds() |
| 827 |
.stdout_only_bytes(&b"q|\xe2\x82\xac|\xc3\xa9\n"[..]); |
| 828 |
} |
| 829 |
|
| 830 |
#[test] |
| 831 |
fn test_unset_locale_is_byte_oriented() { |
| 832 |
// With no locale set the POSIX default is C, so characters are bytes. |
| 833 |
// "ж" is d0 b6, and -c3 must take just the b6. |
| 834 |
new_ucmd!() |
| 835 |
.env("LC_ALL", "") |
| 836 |
.args(&["-c3"]) |
| 837 |
.pipe_in(&b"p\xd0\xb6t\n"[..]) |
| 838 |
.succeeds() |
| 839 |
.stdout_only_bytes(b"\xb6\n"); |
| 840 |
} |
| 841 |
|
| 842 |
#[test] |
| 843 |
fn test_newline_delim_suppress_missing_field() { |
| 844 |
// -s with the newline as delimiter must not emit a spurious blank line. |
| 845 |
new_ucmd!() |
| 846 |
.args(&["-s", "-d", "\n", "-f3"]) |
| 847 |
.pipe_in("solo\n") |
| 848 |
.succeeds() |
| 849 |
.stdout_only(""); |
| 850 |
} |
| 851 |
|
| 852 |
#[test] |
| 853 |
#[cfg_attr(wasi_runner, ignore = "WASI: the guest does not inherit LC_ALL")] |
| 854 |
#[cfg(target_os = "linux")] |
| 855 |
fn test_byte_no_split_with_output_delimiter() { |
| 856 |
// -b -n with an output delimiter: a range covering only part of a |
| 857 |
// multibyte character contributes nothing and emits no delimiter. |
| 858 |
// "ü" (c3 bc) spans bytes 1-2; byte 1 alone selects no whole character. |
| 859 |
new_ucmd!() |
| 860 |
.env("LC_ALL", "C.UTF-8") |
| 861 |
.args(&["-b1,3", "-n", "--output-d=|"]) |
| 862 |
.pipe_in(&b"\xc3\xbcZ\n"[..]) |
| 863 |
.succeeds() |
| 864 |
.stdout_only_bytes(b"Z\n"); |
| 865 |
} |
| 866 |
|
| 867 |
#[test] |
| 868 |
#[cfg_attr(wasi_runner, ignore = "WASI: the guest does not inherit LC_ALL")] |
| 869 |
#[cfg(target_os = "linux")] |
| 870 |
fn test_field_delimiter_not_split_inside_multibyte_char() { |
| 871 |
use std::os::unix::ffi::OsStrExt; |
| 872 |
// In a UTF-8 locale, a delimiter byte that is part of a multibyte character |
| 873 |
// must not split it. Here U+20AC (€ = e2 82 ac) contains 0xac, and 0xac is |
| 874 |
// also used as a standalone delimiter byte. |
| 875 |
new_ucmd!() |
| 876 |
.env("LC_ALL", "C.UTF-8") |
| 877 |
.arg("-d") |
| 878 |
.arg(std::ffi::OsStr::from_bytes(b"\xac")) |
| 879 |
.arg("-f2") |
| 880 |
.pipe_in(&b"1\xe2\x82\xac2\xac3\n"[..]) |
| 881 |
.succeeds() |
| 882 |
.stdout_only_bytes(b"3\n"); |
| 883 |
new_ucmd!() |
| 884 |
.env("LC_ALL", "C.UTF-8") |
| 885 |
.arg("-d") |
| 886 |
.arg(std::ffi::OsStr::from_bytes(b"\xac")) |
| 887 |
.arg("-f1") |
| 888 |
.pipe_in(&b"1\xe2\x82\xac2\xac3\n"[..]) |
| 889 |
.succeeds() |
| 890 |
.stdout_only_bytes(b"1\xe2\x82\xac2\n"); |
| 891 |
} |
| 892 |
|
| 893 |
#[test] |
| 894 |
#[cfg_attr(wasi_runner, ignore = "WASI: the guest does not inherit LC_ALL")] |
| 895 |
#[cfg(target_os = "linux")] |
| 896 |
fn test_whitespace_delimiter_unicode_blank() { |
| 897 |
// U+2002 (EN SPACE) is a Unicode blank and splits fields under -w. |
| 898 |
new_ucmd!() |
| 899 |
.env("LC_ALL", "C.UTF-8") |
| 900 |
.args(&["-w", "-f2"]) |
| 901 |
.pipe_in(&b"x\xe2\x80\x82y\n"[..]) |
| 902 |
.succeeds() |
| 903 |
.stdout_only_bytes(b"y\n"); |
| 904 |
// U+2007 (FIGURE SPACE) is not a blank: the line stays a single field and |
| 905 |
// is suppressed by -s. |
| 906 |
new_ucmd!() |
| 907 |
.env("LC_ALL", "C.UTF-8") |
| 908 |
.args(&["-s", "-w", "-f2"]) |
| 909 |
.pipe_in(&b"x\xe2\x80\x87y\n"[..]) |
| 910 |
.succeeds() |
| 911 |
.stdout_only_bytes(b""); |
| 912 |
} |
| 913 |
|
| 914 |
#[test] |
| 915 |
fn test_delimiter_multibyte_rejected_in_c_locale() { |
| 916 |
// In the C locale a valid UTF-8 multibyte sequence is several characters. |
| 917 |
// The delimiter is passed as ordinary text so this also runs on Windows. |
| 918 |
new_ucmd!() |
| 919 |
.env("LC_ALL", "C") |
| 920 |
.args(&["-d", "\u{20ac}", "-f1"]) |
| 921 |
.fails_with_code(1) |
| 922 |
.stderr_contains("cut: the delimiter must be a single character"); |
| 923 |
} |
| 924 |
|
| 925 |
#[test] |
| 926 |
#[cfg_attr(wasi_runner, ignore = "WASI: the guest does not inherit LC_ALL")] |
| 927 |
fn test_emoji_delim() { |
| 928 |
// A multibyte delimiter is only a single character in a UTF-8 locale. |
| 929 |
new_ucmd!() |
| 930 |
.env("LC_ALL", "C.UTF-8") |
| 931 |
.args(&["-d🗿", "-f1"]) |
| 932 |
.pipe_in("💐🗿🌹\n") |
| 933 |
.succeeds() |
| 934 |
.stdout_only("💐\n"); |
| 935 |
new_ucmd!() |
| 936 |
.env("LC_ALL", "C.UTF-8") |
| 937 |
.args(&["-d🗿", "-f2"]) |
| 938 |
.pipe_in("💐🗿🌹\n") |
| 939 |
.succeeds() |
| 940 |
.stdout_only("🌹\n"); |
| 941 |
} |
| 942 |
|
| 943 |
#[cfg(target_os = "linux")] |
| 944 |
#[test] |
| 945 |
fn test_failed_write_is_reported() { |
| 946 |
new_ucmd!() |
| 947 |
.arg("-d=") |
| 948 |
.arg("-f1") |
| 949 |
.pipe_in("key=value") |
| 950 |
.set_stdout(std::fs::File::create("/dev/full").unwrap()) |
| 951 |
.fails() |
| 952 |
.stderr_is("cut: write error: No space left on device\n"); |
| 953 |
} |
| 954 |
|
| 955 |
#[test] |
| 956 |
#[cfg(target_os = "linux")] |
| 957 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 958 |
fn test_cut_non_utf8_paths() { |
| 959 |
use std::fs::File; |
| 960 |
use std::io::Write; |
| 961 |
use std::os::unix::ffi::OsStrExt; |
| 962 |
use uutests::util::TestScenario; |
| 963 |
use uutests::util_name; |
| 964 |
|
| 965 |
let ts = TestScenario::new(util_name!()); |
| 966 |
let test_dir = ts.fixtures.subdir.as_path(); |
| 967 |
|
| 968 |
// Create file directly with non-UTF-8 name |
| 969 |
let file_name = std::ffi::OsStr::from_bytes(b"test_\xFF\xFE.txt"); |
| 970 |
let mut file = File::create(test_dir.join(file_name)).unwrap(); |
| 971 |
file.write_all(b"a\tb\tc\n1\t2\t3\n").unwrap(); |
| 972 |
|
| 973 |
// Test that cut can handle non-UTF-8 filenames |
| 974 |
ts.ucmd() |
| 975 |
.arg("-f1,3") |
| 976 |
.arg(file_name) |
| 977 |
.succeeds() |
| 978 |
.stdout_only("a\tc\n1\t3\n"); |
| 979 |
} |
| 980 |
|
| 981 |
// We exercise the GB18030 path with two real two-byte characters that are not |
| 982 |
// valid UTF-8: 啊 (0xB0 0xA1) and 中 (0xD6 0xD0). The active encoding comes |
| 983 |
// straight from `LC_ALL`, so the host does not need the locale installed. |
| 984 |
#[cfg(target_os = "linux")] |
| 985 |
const GB_LOCALE: &str = "zh_CN.gb18030"; |
| 986 |
#[cfg(target_os = "linux")] |
| 987 |
const A: &[u8] = b"\xB0\xA1"; // 啊 |
| 988 |
|
| 989 |
#[test] |
| 990 |
#[cfg(target_os = "linux")] |
| 991 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv must be valid UTF-8")] |
| 992 |
fn test_cut_fields_gb18030_delimiter() { |
| 993 |
use std::ffi::OsString; |
| 994 |
use std::os::unix::ffi::OsStringExt; |
| 995 |
|
| 996 |
// Three words separated by the two-byte 啊: "red啊green啊blue". |
| 997 |
let line = b"red\xB0\xA1green\xB0\xA1blue\n"; |
| 998 |
let delim = OsString::from_vec(A.to_vec()); |
| 999 |
|
| 1000 |
// Pick the last field; the chosen output delimiter replaces the input one. |
| 1001 |
new_ucmd!() |
| 1002 |
.env("LC_ALL", GB_LOCALE) |
| 1003 |
.arg("-d") |
| 1004 |
.arg(&delim) |
| 1005 |
.args(&["-f3", "--output-delimiter=/"]) |
| 1006 |
.pipe_in(line.to_vec()) |
| 1007 |
.succeeds() |
| 1008 |
.stdout_only("blue\n"); |
| 1009 |
|
| 1010 |
// Two non-adjacent fields; with no override the multibyte delimiter itself |
| 1011 |
// is re-emitted between them. |
| 1012 |
new_ucmd!() |
| 1013 |
.env("LC_ALL", GB_LOCALE) |
| 1014 |
.arg("-d") |
| 1015 |
.arg(&delim) |
| 1016 |
.arg("-f1,3") |
| 1017 |
.pipe_in(line.to_vec()) |
| 1018 |
.succeeds() |
| 1019 |
.stdout_only_bytes(b"red\xB0\xA1blue\n"); |
| 1020 |
} |
| 1021 |
|
| 1022 |
#[test] |
| 1023 |
#[cfg(target_os = "linux")] |
| 1024 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv must be valid UTF-8")] |
| 1025 |
fn test_cut_fields_gb18030_complement_and_gaps() { |
| 1026 |
use std::ffi::OsString; |
| 1027 |
use std::os::unix::ffi::OsStringExt; |
| 1028 |
|
| 1029 |
let delim = OsString::from_vec(A.to_vec()); |
| 1030 |
|
| 1031 |
// --complement of the middle field leaves the two outer ones, rejoined. |
| 1032 |
new_ucmd!() |
| 1033 |
.env("LC_ALL", GB_LOCALE) |
| 1034 |
.arg("--complement") |
| 1035 |
.arg("-d") |
| 1036 |
.arg(&delim) |
| 1037 |
.arg("-f2") |
| 1038 |
.pipe_in(b"red\xB0\xA1green\xB0\xA1blue\n".to_vec()) |
| 1039 |
.succeeds() |
| 1040 |
.stdout_only_bytes(b"red\xB0\xA1blue\n"); |
| 1041 |
|
| 1042 |
// A line that is only delimiters yields empty fields around a trailing one. |
| 1043 |
new_ucmd!() |
| 1044 |
.env("LC_ALL", GB_LOCALE) |
| 1045 |
.arg("-d") |
| 1046 |
.arg(&delim) |
| 1047 |
.args(&["-f1-3", "--output-delimiter=|"]) |
| 1048 |
.pipe_in(b"\xB0\xA1\xB0\xA1z\n".to_vec()) |
| 1049 |
.succeeds() |
| 1050 |
.stdout_only("||z\n"); |
| 1051 |
} |
| 1052 |
|
| 1053 |
#[test] |
| 1054 |
#[cfg(target_os = "linux")] |
| 1055 |
#[cfg_attr( |
| 1056 |
wasi_runner, |
| 1057 |
ignore = "WASI sandbox: non-UTF-8 arguments can't be passed through wasmtime" |
| 1058 |
)] |
| 1059 |
fn test_cut_fields_single_byte_delimiter_in_mb_locale() { |
| 1060 |
use std::ffi::OsString; |
| 1061 |
use std::os::unix::ffi::OsStringExt; |
| 1062 |
|
| 1063 |
// 0x80 never starts a GB18030 sequence, yet a lone byte is a fine delimiter. |
| 1064 |
let delim = OsString::from_vec(vec![0x80]); |
| 1065 |
new_ucmd!() |
| 1066 |
.env("LC_ALL", GB_LOCALE) |
| 1067 |
.arg("-d") |
| 1068 |
.arg(&delim) |
| 1069 |
.args(&["-f1,3", "--output-delimiter=-"]) |
| 1070 |
.pipe_in(b"a\x80b\x80c\n".to_vec()) |
| 1071 |
.succeeds() |
| 1072 |
.stdout_only("a-c\n"); |
| 1073 |
} |
| 1074 |
|
| 1075 |
#[test] |
| 1076 |
#[cfg(target_os = "linux")] |
| 1077 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv must be valid UTF-8")] |
| 1078 |
fn test_cut_chars_gb18030() { |
| 1079 |
// "啊w中": -c counts characters, so the second one is the ASCII 'w'. |
| 1080 |
let line = b"\xB0\xA1w\xD6\xD0\n"; |
| 1081 |
|
| 1082 |
new_ucmd!() |
| 1083 |
.env("LC_ALL", GB_LOCALE) |
| 1084 |
.arg("-c2") |
| 1085 |
.pipe_in(line.to_vec()) |
| 1086 |
.succeeds() |
| 1087 |
.stdout_only("w\n"); |
| 1088 |
|
| 1089 |
// Selecting the trailing multibyte character returns it whole. |
| 1090 |
new_ucmd!() |
| 1091 |
.env("LC_ALL", GB_LOCALE) |
| 1092 |
.arg("-c3") |
| 1093 |
.pipe_in(line.to_vec()) |
| 1094 |
.succeeds() |
| 1095 |
.stdout_only_bytes(b"\xD6\xD0\n"); |
| 1096 |
|
| 1097 |
// A range that spans the leading and ASCII characters. |
| 1098 |
new_ucmd!() |
| 1099 |
.env("LC_ALL", GB_LOCALE) |
| 1100 |
.arg("-c1-2") |
| 1101 |
.pipe_in(line.to_vec()) |
| 1102 |
.succeeds() |
| 1103 |
.stdout_only_bytes(b"\xB0\xA1w\n"); |
| 1104 |
} |
| 1105 |
|
| 1106 |
#[test] |
| 1107 |
#[cfg(target_os = "linux")] |
| 1108 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv must be valid UTF-8")] |
| 1109 |
fn test_cut_chars_gb18030_ranges_and_complement() { |
| 1110 |
// "啊w中": list of two single-char ranges joined by a custom delimiter. |
| 1111 |
new_ucmd!() |
| 1112 |
.env("LC_ALL", GB_LOCALE) |
| 1113 |
.args(&["-c1,3", "--output-delimiter=+"]) |
| 1114 |
.pipe_in(b"\xB0\xA1w\xD6\xD0\n".to_vec()) |
| 1115 |
.succeeds() |
| 1116 |
.stdout_only_bytes(b"\xB0\xA1+\xD6\xD0\n"); |
| 1117 |
|
| 1118 |
// Complement of the ASCII middle character keeps both multibyte ones. |
| 1119 |
new_ucmd!() |
| 1120 |
.env("LC_ALL", GB_LOCALE) |
| 1121 |
.args(&["--complement", "-c2"]) |
| 1122 |
.pipe_in(b"\xB0\xA1w\xD6\xD0\n".to_vec()) |
| 1123 |
.succeeds() |
| 1124 |
.stdout_only_bytes(b"\xB0\xA1\xD6\xD0\n"); |
| 1125 |
} |
| 1126 |
|
| 1127 |
#[test] |
| 1128 |
#[cfg(target_os = "linux")] |
| 1129 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv must be valid UTF-8")] |
| 1130 |
fn test_cut_bytes_no_split_gb18030() { |
| 1131 |
// -n forbids splitting a multibyte character: a byte index landing inside |
| 1132 |
// 啊 only produces output once its final byte is included. |
| 1133 |
let line = b"\xB0\xA1w\n"; |
| 1134 |
|
| 1135 |
// Byte 1 is the first half of 啊 -> nothing is emitted. |
| 1136 |
new_ucmd!() |
| 1137 |
.env("LC_ALL", GB_LOCALE) |
| 1138 |
.args(&["-b1", "-n"]) |
| 1139 |
.pipe_in(line.to_vec()) |
| 1140 |
.succeeds() |
| 1141 |
.stdout_only("\n"); |
| 1142 |
|
| 1143 |
// Byte 2 completes 啊 -> the whole character comes out. |
| 1144 |
new_ucmd!() |
| 1145 |
.env("LC_ALL", GB_LOCALE) |
| 1146 |
.args(&["-b2", "-n"]) |
| 1147 |
.pipe_in(line.to_vec()) |
| 1148 |
.succeeds() |
| 1149 |
.stdout_only_bytes(b"\xB0\xA1\n"); |
| 1150 |
} |
| 1151 |
|
| 1152 |
// `-c` also operates on whole characters in a UTF-8 locale. The harness runs |
| 1153 |
// under `LC_ALL=C`, so the locale is forced here. "naïve" is n a ï(2 bytes) v e. |
| 1154 |
#[test] |
| 1155 |
#[cfg(target_os = "linux")] |
| 1156 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv must be valid UTF-8")] |
| 1157 |
fn test_cut_chars_utf8() { |
| 1158 |
// The third character is the accented 'ï', returned in full. |
| 1159 |
new_ucmd!() |
| 1160 |
.env("LC_ALL", "en_US.UTF-8") |
| 1161 |
.arg("-c3") |
| 1162 |
.pipe_in("naïve\n".as_bytes().to_vec()) |
| 1163 |
.succeeds() |
| 1164 |
.stdout_only("ï\n"); |
| 1165 |
|
| 1166 |
// Complement of that character removes it and nothing else. |
| 1167 |
new_ucmd!() |
| 1168 |
.env("LC_ALL", "en_US.UTF-8") |
| 1169 |
.args(&["--complement", "-c3"]) |
| 1170 |
.pipe_in("naïve\n".as_bytes().to_vec()) |
| 1171 |
.succeeds() |
| 1172 |
.stdout_only("nave\n"); |
| 1173 |
|
| 1174 |
// A list straddling 'ï' joins the two picked characters. |
| 1175 |
new_ucmd!() |
| 1176 |
.env("LC_ALL", "en_US.UTF-8") |
| 1177 |
.args(&["-c1,4", "--output-delimiter=+"]) |
| 1178 |
.pipe_in("naïve\n".as_bytes().to_vec()) |
| 1179 |
.succeeds() |
| 1180 |
.stdout_only("n+v\n"); |
| 1181 |
} |
| 1182 |
|
| 1183 |
// Lines with no byte above 0x7F take a byte-wise shortcut in a multi-byte |
| 1184 |
// locale; mixing them with multi-byte ones checks both paths agree on offsets. |
| 1185 |
#[test] |
| 1186 |
#[cfg(target_os = "linux")] |
| 1187 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv must be valid UTF-8")] |
| 1188 |
fn test_cut_chars_utf8_mixed_ascii_lines() { |
| 1189 |
let input = "quokka\nfjärd\nwombat\ntøys\n"; |
| 1190 |
|
| 1191 |
new_ucmd!() |
| 1192 |
.env("LC_ALL", "en_US.UTF-8") |
| 1193 |
.arg("-c3-5") |
| 1194 |
.pipe_in(input) |
| 1195 |
.succeeds() |
| 1196 |
.stdout_only("okk\närd\nmba\nys\n"); |
| 1197 |
|
| 1198 |
// `-b -n` selects a character when its last byte falls in the range, so the |
| 1199 |
// accented lines cover a different span than the ASCII ones. |
| 1200 |
new_ucmd!() |
| 1201 |
.env("LC_ALL", "en_US.UTF-8") |
| 1202 |
.args(&["-b", "3-5", "-n"]) |
| 1203 |
.pipe_in(input) |
| 1204 |
.succeeds() |
| 1205 |
.stdout_only("okk\när\nmba\nøys\n"); |
| 1206 |
} |
| 1207 |
|
| 1208 |
#[test] |
| 1209 |
#[cfg(all(target_os = "linux", target_env = "gnu"))] |
| 1210 |
#[cfg_attr(wasi_runner, ignore)] |
| 1211 |
fn test_read_error() { |
| 1212 |
new_ucmd!() |
| 1213 |
.args(&["-c1", "/proc/self/mem"]) |
| 1214 |
.fails_with_code(1) |
| 1215 |
.stderr_is("cut: Input/output error\n"); |
| 1216 |
} |
| 1217 |
|
| 1218 |
#[cfg(unix)] |
| 1219 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 1220 |
mod diagnostics { |
| 1221 |
use super::*; |
| 1222 |
|
| 1223 |
#[test] |
| 1224 |
fn test_snippet_points_at_the_inverted_range() { |
| 1225 |
let result = new_ucmd!() |
| 1226 |
.terminal_sim_stderr() |
| 1227 |
.args(&["-f", "1,4-2", "/dev/null"]) |
| 1228 |
.fails_with_code(1); |
| 1229 |
|
| 1230 |
// One item of the list is at fault, not the whole of it. |
| 1231 |
let stderr = result.stderr_as_displayed(); |
| 1232 |
assert!( |
| 1233 |
stderr.starts_with( |
| 1234 |
"\ |
| 1235 |
cut: invalid decreasing range |
| 1236 |
╭─[ cut:1:10 ] |
| 1237 |
│ |
| 1238 |
1 │ cut -f 1,4-2 /dev/null |
| 1239 |
│ ─┬─ |
| 1240 |
│ ╰─── this range ends before it starts |
| 1241 |
│ |
| 1242 |
│ Help: a list is N, N-M, N- or -M, separated by commas, as in -f1,4-6,9- |
| 1243 |
───╯" |
| 1244 |
), |
| 1245 |
"{stderr}" |
| 1246 |
); |
| 1247 |
// The caret replaces the message, not the usage hint: a pipe and a |
| 1248 |
// terminal must not disagree on whether one was printed. |
| 1249 |
assert!( |
| 1250 |
stderr.ends_with("cut --help' for more information."), |
| 1251 |
"{stderr}" |
| 1252 |
); |
| 1253 |
} |
| 1254 |
|
| 1255 |
#[test] |
| 1256 |
fn test_snippet_finds_fields_merged_value() { |
| 1257 |
let result = new_ucmd!() |
| 1258 |
.terminal_sim_stderr() |
| 1259 |
.args(&["-F", "1,4-2", "/dev/null"]) |
| 1260 |
.fails_with_code(1); |
| 1261 |
let stderr = result.stderr_as_displayed(); |
| 1262 |
|
| 1263 |
assert!(stderr.contains("1 │ cut -F 1,4-2 /dev/null"), "{stderr}"); |
| 1264 |
assert!( |
| 1265 |
stderr.contains("this range ends before it starts"), |
| 1266 |
"{stderr}" |
| 1267 |
); |
| 1268 |
} |
| 1269 |
|
| 1270 |
#[test] |
| 1271 |
fn test_snippet_points_at_the_zero_bound() { |
| 1272 |
let result = new_ucmd!() |
| 1273 |
.terminal_sim_stderr() |
| 1274 |
.args(&["-f1,0,3", "/dev/null"]) |
| 1275 |
.fails_with_code(1); |
| 1276 |
let stderr = result.stderr_as_displayed(); |
| 1277 |
|
| 1278 |
// Glued to the option, so the caret counts the two columns it takes. |
| 1279 |
assert!(stderr.contains("cut:1:9"), "{stderr}"); |
| 1280 |
assert!(stderr.contains("counting starts at 1"), "{stderr}"); |
| 1281 |
} |
| 1282 |
|
| 1283 |
#[test] |
| 1284 |
fn test_snippet_points_at_the_bound_that_is_not_a_number() { |
| 1285 |
let result = new_ucmd!() |
| 1286 |
.terminal_sim_stderr() |
| 1287 |
.args(&["-c", "1-3,x", "/dev/null"]) |
| 1288 |
.fails_with_code(1); |
| 1289 |
let stderr = result.stderr_as_displayed(); |
| 1290 |
|
| 1291 |
// The message names the item, so a bare underline is enough. |
| 1292 |
assert!( |
| 1293 |
stderr.contains("invalid byte/character position 'x'"), |
| 1294 |
"{stderr}" |
| 1295 |
); |
| 1296 |
assert!(stderr.contains("cut:1:12"), "{stderr}"); |
| 1297 |
} |
| 1298 |
|
| 1299 |
#[test] |
| 1300 |
fn test_snippet_ignores_a_file_named_like_the_list() { |
| 1301 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1302 |
at.touch("1,0"); |
| 1303 |
|
| 1304 |
// The file is named exactly like the list; the caret belongs to the |
| 1305 |
// value of -f. |
| 1306 |
let result = ucmd |
| 1307 |
.terminal_sim_stderr() |
| 1308 |
.args(&["-f", "1,0", "1,0"]) |
| 1309 |
.fails_with_code(1); |
| 1310 |
let stderr = result.stderr_as_displayed(); |
| 1311 |
|
| 1312 |
assert!(stderr.contains("cut:1:10"), "{stderr}"); |
| 1313 |
} |
| 1314 |
|
| 1315 |
#[test] |
| 1316 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 1317 |
let result = new_ucmd!() |
| 1318 |
.args(&["-f", "1,4-2", "/dev/null"]) |
| 1319 |
.fails_with_code(1); |
| 1320 |
let stderr = result.stderr_str(); |
| 1321 |
|
| 1322 |
// The message reads as it always has, and nothing is drawn under it; |
| 1323 |
// the usage hint that follows is cut's own. |
| 1324 |
assert!( |
| 1325 |
stderr.starts_with("cut: invalid decreasing range\n"), |
| 1326 |
"{stderr}" |
| 1327 |
); |
| 1328 |
assert!(!stderr.contains('\u{256d}'), "{stderr}"); |
| 1329 |
} |
| 1330 |
} |