| 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 roff |
| 7 |
// spell-checker:ignore funnnnnnnnnnnnnnnnn |
| 8 |
|
| 9 |
use uutests::new_ucmd; |
| 10 |
|
| 11 |
#[test] |
| 12 |
fn test_invalid_arg() { |
| 13 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 14 |
new_ucmd!().arg("-g").arg("0").fails_with_code(1); // clap provided message |
| 15 |
new_ucmd!().arg("-w").arg("0").fails_with_code(1); // clap provided message |
| 16 |
} |
| 17 |
#[test] |
| 18 |
fn test_reference_format_for_stdin() { |
| 19 |
let input = "Rust is good language"; |
| 20 |
let expected_output = concat!( |
| 21 |
r#".xx "" "" "Rust is good language" "" ":1""#, |
| 22 |
"\n", |
| 23 |
r#".xx "" "Rust is" "good language" "" ":1""#, |
| 24 |
"\n", |
| 25 |
r#".xx "" "Rust" "is good language" "" ":1""#, |
| 26 |
"\n", |
| 27 |
r#".xx "" "Rust is good" "language" "" ":1""#, |
| 28 |
"\n", |
| 29 |
); |
| 30 |
new_ucmd!() |
| 31 |
.args(&["-G", "-A"]) |
| 32 |
.pipe_in(input) |
| 33 |
.succeeds() |
| 34 |
.stdout_only(expected_output); |
| 35 |
} |
| 36 |
#[test] |
| 37 |
fn test_tex_format_no_truncation_markers() { |
| 38 |
let input = "Hello world Rust is a fun language"; |
| 39 |
new_ucmd!() |
| 40 |
.args(&["-G", "-w", "30", "--format=tex"]) |
| 41 |
.pipe_in(input) |
| 42 |
.succeeds() |
| 43 |
.stdout_only_fixture("test_tex_format_no_truncation_markers.expected"); |
| 44 |
} |
| 45 |
#[test] |
| 46 |
fn gnu_ext_disabled_chunk_no_over_reading() { |
| 47 |
let input = "Hello World Rust is a fun language"; |
| 48 |
new_ucmd!() |
| 49 |
.args(&["-G", "-w", "30"]) |
| 50 |
.pipe_in(input) |
| 51 |
.succeeds() |
| 52 |
.stdout_only_fixture("gnu_ext_disabled_chunk_no_over_reading.expected"); |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn test_truncation_no_extra_space_in_after() { |
| 57 |
new_ucmd!() |
| 58 |
.args(&["-G", "-w", "30"]) |
| 59 |
.pipe_in("Rust is funnnnnnnnnnnnnnnnn") |
| 60 |
.succeeds() |
| 61 |
.stdout_contains(".xx \"\" \"Rust\" \"is/\" \"\""); |
| 62 |
} |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn gnu_ext_disabled_reference_calculation() { |
| 66 |
let input = "Hello World Rust is good language"; |
| 67 |
let expected_output = concat!( |
| 68 |
r#".xx "language" "" "Hello World Rust is good" "" ":1""#, |
| 69 |
"\n", |
| 70 |
r#".xx "" "Hello World" "Rust is good language" "" ":1""#, |
| 71 |
"\n", |
| 72 |
r#".xx "" "Hello" "World Rust is good language" "" ":1""#, |
| 73 |
"\n", |
| 74 |
r#".xx "" "Hello World Rust is" "good language" "" ":1""#, |
| 75 |
"\n", |
| 76 |
r#".xx "" "Hello World Rust" "is good language" "" ":1""#, |
| 77 |
"\n", |
| 78 |
r#".xx "" "Hello World Rust is good" "language" "" ":1""#, |
| 79 |
"\n", |
| 80 |
); |
| 81 |
new_ucmd!() |
| 82 |
.args(&["-G", "-A"]) |
| 83 |
.pipe_in(input) |
| 84 |
.succeeds() |
| 85 |
.stdout_only(expected_output); |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn gnu_ext_disabled_rightward_no_ref() { |
| 90 |
new_ucmd!() |
| 91 |
.args(&["-G", "-R", "input"]) |
| 92 |
.succeeds() |
| 93 |
.stdout_only_fixture("gnu_ext_disabled_rightward_no_ref.expected"); |
| 94 |
} |
| 95 |
|
| 96 |
#[test] |
| 97 |
fn gnu_ext_disabled_rightward_no_ref_empty_word_regexp() { |
| 98 |
new_ucmd!() |
| 99 |
.args(&["-G", "-R", "-W", "", "input"]) |
| 100 |
.succeeds() |
| 101 |
.stdout_only_fixture("gnu_ext_disabled_rightward_no_ref.expected"); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn gnu_ext_disabled_rightward_no_ref_word_regexp_exc_space() { |
| 106 |
new_ucmd!() |
| 107 |
.args(&["-G", "-R", "-W", "[^\t\n]+", "input"]) |
| 108 |
.succeeds() |
| 109 |
.stdout_only_fixture("gnu_ext_disabled_rightward_no_ref_word_regexp_exc_space.expected"); |
| 110 |
} |
| 111 |
|
| 112 |
#[test] |
| 113 |
fn gnu_ext_disabled_rightward_input_ref() { |
| 114 |
new_ucmd!() |
| 115 |
.args(&["-G", "-r", "-R", "input"]) |
| 116 |
.succeeds() |
| 117 |
.stdout_only_fixture("gnu_ext_disabled_rightward_input_ref.expected"); |
| 118 |
} |
| 119 |
|
| 120 |
#[test] |
| 121 |
fn gnu_ext_disabled_rightward_auto_ref() { |
| 122 |
new_ucmd!() |
| 123 |
.args(&["-G", "-A", "-R", "input"]) |
| 124 |
.succeeds() |
| 125 |
.stdout_only_fixture("gnu_ext_disabled_rightward_auto_ref.expected"); |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn gnu_ext_disabled_tex_no_ref() { |
| 130 |
new_ucmd!() |
| 131 |
.args(&["-G", "-T", "-R", "input"]) |
| 132 |
.succeeds() |
| 133 |
.stdout_only_fixture("gnu_ext_disabled_tex_no_ref.expected"); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn gnu_ext_disabled_tex_input_ref() { |
| 138 |
new_ucmd!() |
| 139 |
.args(&["-G", "-T", "-r", "-R", "input"]) |
| 140 |
.succeeds() |
| 141 |
.stdout_only_fixture("gnu_ext_disabled_tex_input_ref.expected"); |
| 142 |
} |
| 143 |
|
| 144 |
#[test] |
| 145 |
fn gnu_ext_disabled_tex_auto_ref() { |
| 146 |
new_ucmd!() |
| 147 |
.args(&["-G", "-T", "-A", "-R", "input"]) |
| 148 |
.succeeds() |
| 149 |
.stdout_only_fixture("gnu_ext_disabled_tex_auto_ref.expected"); |
| 150 |
} |
| 151 |
|
| 152 |
#[test] |
| 153 |
fn gnu_ext_disabled_ignore_and_only_file() { |
| 154 |
new_ucmd!() |
| 155 |
.args(&["-G", "-o", "only", "-i", "ignore", "input"]) |
| 156 |
.succeeds() |
| 157 |
.stdout_only_fixture("gnu_ext_disabled_ignore_and_only_file.expected"); |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn gnu_ext_disabled_output_width_50() { |
| 162 |
new_ucmd!() |
| 163 |
.args(&["-G", "-w", "50", "input"]) |
| 164 |
.succeeds() |
| 165 |
.stdout_only_fixture("gnu_ext_disabled_output_width_50.expected"); |
| 166 |
} |
| 167 |
|
| 168 |
#[test] |
| 169 |
fn gnu_ext_disabled_output_width_70() { |
| 170 |
new_ucmd!() |
| 171 |
.args(&["-G", "-w", "70", "input"]) |
| 172 |
.succeeds() |
| 173 |
.stdout_only_fixture("gnu_ext_disabled_output_width_70.expected"); |
| 174 |
} |
| 175 |
|
| 176 |
#[test] |
| 177 |
fn gnu_ext_disabled_break_file() { |
| 178 |
new_ucmd!() |
| 179 |
.args(&["-G", "-b", "break_file", "input"]) |
| 180 |
.succeeds() |
| 181 |
.stdout_only_fixture("gnu_ext_disabled_break_file.expected"); |
| 182 |
} |
| 183 |
|
| 184 |
#[test] |
| 185 |
fn gnu_ext_disabled_empty_word_regexp_ignores_break_file() { |
| 186 |
new_ucmd!() |
| 187 |
.args(&["-G", "-b", "break_file", "-R", "-W", "", "input"]) |
| 188 |
.succeeds() |
| 189 |
.stdout_only_fixture("gnu_ext_disabled_rightward_no_ref.expected"); |
| 190 |
} |
| 191 |
|
| 192 |
#[test] |
| 193 |
fn test_reject_too_many_operands() { |
| 194 |
new_ucmd!().args(&["-G", "-", "-", "-"]).fails_with_code(1); |
| 195 |
} |
| 196 |
|
| 197 |
#[test] |
| 198 |
fn test_break_file_regex_escaping() { |
| 199 |
new_ucmd!() |
| 200 |
.pipe_in("\\.+*?()|[]{}^$#&-~") |
| 201 |
.args(&["-G", "-b", "-", "input"]) |
| 202 |
.succeeds() |
| 203 |
.stdout_only_fixture("break_file_regex_escaping.expected"); |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn test_ignore_case() { |
| 208 |
new_ucmd!() |
| 209 |
.args(&["-G", "-f"]) |
| 210 |
.pipe_in("a _") |
| 211 |
.succeeds() |
| 212 |
.stdout_only(".xx \"\" \"\" \"a _\" \"\"\n.xx \"\" \"a\" \"_\" \"\"\n"); |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn test_format() { |
| 217 |
new_ucmd!() |
| 218 |
.args(&["-G", "-O"]) |
| 219 |
.pipe_in("a") |
| 220 |
.succeeds() |
| 221 |
.stdout_only(".xx \"\" \"\" \"a\" \"\"\n"); |
| 222 |
new_ucmd!() |
| 223 |
.args(&["-G", "-T"]) |
| 224 |
.pipe_in("a") |
| 225 |
.succeeds() |
| 226 |
.stdout_only("\\xx {}{}{a}{}{}\n"); |
| 227 |
new_ucmd!() |
| 228 |
.args(&["-G", "--format=roff"]) |
| 229 |
.pipe_in("a") |
| 230 |
.succeeds() |
| 231 |
.stdout_only(".xx \"\" \"\" \"a\" \"\"\n"); |
| 232 |
new_ucmd!() |
| 233 |
.args(&["-G", "--format=tex"]) |
| 234 |
.pipe_in("a") |
| 235 |
.succeeds() |
| 236 |
.stdout_only("\\xx {}{}{a}{}{}\n"); |
| 237 |
} |
| 238 |
|
| 239 |
#[cfg(target_os = "linux")] |
| 240 |
#[test] |
| 241 |
fn test_failed_write_is_reported() { |
| 242 |
new_ucmd!() |
| 243 |
.arg("-G") |
| 244 |
.pipe_in("hello") |
| 245 |
.set_stdout(std::fs::File::create("/dev/full").unwrap()) |
| 246 |
.fails() |
| 247 |
.stderr_is("ptx: write failed: No space left on device\n"); |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn test_utf8() { |
| 252 |
new_ucmd!() |
| 253 |
.args(&["-G"]) |
| 254 |
.pipe_in("it’s disabled\n") |
| 255 |
.succeeds() |
| 256 |
.stdout_only(".xx \"\" \"it’s\" \"disabled\" \"\"\n.xx \"\" \"\" \"it’s disabled\" \"\"\n"); |
| 257 |
new_ucmd!() |
| 258 |
.args(&["-G", "-T"]) |
| 259 |
.pipe_in("it’s disabled\n") |
| 260 |
.succeeds() |
| 261 |
.stdout_only("\\xx {}{it’s}{disabled}{}{}\n\\xx {}{}{it’s}{ disabled}{}\n"); |
| 262 |
} |
| 263 |
|
| 264 |
#[test] |
| 265 |
fn test_sentence_regexp_basic() { |
| 266 |
new_ucmd!() |
| 267 |
.args(&["-G", "-S", "\\."]) |
| 268 |
.pipe_in("Hello. World.") |
| 269 |
.succeeds() |
| 270 |
.stdout_contains("Hello") |
| 271 |
.stdout_contains("World"); |
| 272 |
} |
| 273 |
|
| 274 |
#[test] |
| 275 |
fn test_sentence_regexp_split_behavior() { |
| 276 |
new_ucmd!() |
| 277 |
.args(&["-G", "-w", "50", "-S", "[.!]"]) |
| 278 |
.pipe_in("One sentence. Two sentence!") |
| 279 |
.succeeds() |
| 280 |
.stdout_contains("One sentence") |
| 281 |
.stdout_contains("Two sentence"); |
| 282 |
} |
| 283 |
|
| 284 |
#[test] |
| 285 |
fn test_sentence_regexp_empty_match_failure() { |
| 286 |
new_ucmd!() |
| 287 |
.args(&["-G", "-S", "^"]) |
| 288 |
.fails() |
| 289 |
.stderr_contains("A regular expression cannot match a length zero string"); |
| 290 |
} |
| 291 |
|
| 292 |
#[test] |
| 293 |
fn test_sentence_regexp_newlines_are_spaces() { |
| 294 |
new_ucmd!() |
| 295 |
.args(&["-G", "-S", "\\."]) |
| 296 |
.pipe_in("Start of\nsentence.") |
| 297 |
.succeeds() |
| 298 |
.stdout_contains("Start of sentence"); |
| 299 |
} |
| 300 |
|
| 301 |
#[test] |
| 302 |
fn test_sentence_regexp_invalid_syntax_failure() { |
| 303 |
new_ucmd!() |
| 304 |
.args(&["-S", "^["]) |
| 305 |
.fails() |
| 306 |
.stderr_contains("Invalid regexp"); |
| 307 |
} |
| 308 |
|
| 309 |
#[test] |
| 310 |
fn test_gnu_mode_dumb_format() { |
| 311 |
// Test GNU mode (dumb format) - the default mode without -G flag |
| 312 |
new_ucmd!().pipe_in("a b").succeeds().stdout_only( |
| 313 |
" a b\n a b\n", |
| 314 |
); |
| 315 |
|
| 316 |
new_ucmd!() |
| 317 |
.pipe_in("2a") |
| 318 |
.succeeds() |
| 319 |
.stdout_only(format!("{}2 a\n", " ".repeat(35))); |
| 320 |
} |
| 321 |
|
| 322 |
#[test] |
| 323 |
fn test_gnu_compatibility_narrow_width() { |
| 324 |
new_ucmd!() |
| 325 |
.args(&["-w", "2"]) |
| 326 |
.pipe_in("qux") |
| 327 |
.succeeds() |
| 328 |
.stdout_only(" qux\n"); |
| 329 |
} |
| 330 |
|
| 331 |
#[test] |
| 332 |
fn test_gnu_compatibility_truncation_width() { |
| 333 |
new_ucmd!() |
| 334 |
.args(&["-w", "10"]) |
| 335 |
.pipe_in("foo bar") |
| 336 |
.succeeds() |
| 337 |
.stdout_only(" / bar\n foo/\n"); |
| 338 |
} |
| 339 |
|
| 340 |
#[test] |
| 341 |
fn test_unicode_padding_alignment() { |
| 342 |
let input = "a\né"; |
| 343 |
new_ucmd!() |
| 344 |
.args(&["-w", "10"]) |
| 345 |
.pipe_in(input) |
| 346 |
.succeeds() |
| 347 |
.stdout_only(" a\n é\n"); |
| 348 |
} |
| 349 |
|
| 350 |
#[test] |
| 351 |
fn test_gnu_compat_numeric_token_with_emoji_produces_no_index() { |
| 352 |
// GNU ptx produces no output for this input in default mode. |
| 353 |
new_ucmd!() |
| 354 |
.pipe_in("012345678901234567890123456789🛠\n") |
| 355 |
.succeeds() |
| 356 |
.no_output(); |
| 357 |
} |
| 358 |
|
| 359 |
#[test] |
| 360 |
fn test_unicode_truncation_alignment() { |
| 361 |
new_ucmd!() |
| 362 |
.args(&["-w", "10"]) |
| 363 |
.pipe_in("föö bar") |
| 364 |
.succeeds() |
| 365 |
.stdout_only(" / bar\n föö/\n"); |
| 366 |
} |
| 367 |
|
| 368 |
#[test] |
| 369 |
fn test_unicode_in_after_chunk_does_not_panic() { |
| 370 |
// Regression test for a panic in get_output_chunks() when the computed |
| 371 |
// max_after_size used byte lengths but the output was assembled as chars. |
| 372 |
// The emoji is multibyte in UTF-8 and previously could trigger: |
| 373 |
// `assertion failed: max_after_size >= after.len()`. |
| 374 |
new_ucmd!() |
| 375 |
.pipe_in("We've got +11 more G of 1.70. 🛠\n") |
| 376 |
.succeeds() |
| 377 |
.stdout_contains("We've got +11"); |
| 378 |
} |
| 379 |
|
| 380 |
#[test] |
| 381 |
fn test_unicode_in_before_chunk_does_not_panic() { |
| 382 |
// Regression test for issue #10893: a panic in get_output_chunks() when the |
| 383 |
// computed max_before_size used char counts but the assert compared against |
| 384 |
// before.len() (byte length). A multibyte char in the before chunk could |
| 385 |
// trigger: `assertion failed: max_before_size >= before.len()`. |
| 386 |
new_ucmd!() |
| 387 |
.args(&["-w", "10"]) |
| 388 |
.pipe_in("éé word\n") |
| 389 |
.succeeds() |
| 390 |
.no_stderr(); |
| 391 |
} |
| 392 |
|
| 393 |
#[test] |
| 394 |
fn test_unicode_tail_chunk_sizing() { |
| 395 |
// The tail chunk budget (max_tail_size) subtracts the size of the before |
| 396 |
// chunk. It must use the before chunk's char count, not its byte length, |
| 397 |
// otherwise a multibyte before chunk shrinks the tail too much and drops a |
| 398 |
// word that fits. Here "cc" wraps into the tail before "aé bé KEY"; with the |
| 399 |
// byte-based budget it was dropped. |
| 400 |
new_ucmd!() |
| 401 |
.args(&["-w", "20"]) |
| 402 |
.pipe_in("aé bé KEY cc dd ee ff gg\n") |
| 403 |
.succeeds() |
| 404 |
.stdout_contains("cc/ aé"); |
| 405 |
} |
| 406 |
|
| 407 |
#[test] |
| 408 |
fn test_duplicate_input_files() { |
| 409 |
new_ucmd!() |
| 410 |
.args(&["one_word", "one_word"]) |
| 411 |
.succeeds() |
| 412 |
.stdout_is(" rust\n rust\n"); |
| 413 |
} |
| 414 |
|
| 415 |
#[test] |
| 416 |
fn test_narrow_width_with_long_reference_no_panic() { |
| 417 |
new_ucmd!() |
| 418 |
.args(&["-w", "1", "-A"]) |
| 419 |
.pipe_in("content") |
| 420 |
.succeeds() |
| 421 |
.stdout_only(":1 content\n"); |
| 422 |
} |
| 423 |
|
| 424 |
#[test] |
| 425 |
fn test_typeset_mode_default_width_100() { |
| 426 |
new_ucmd!() |
| 427 |
.args(&["-t"]) |
| 428 |
.pipe_in("bar\n") |
| 429 |
.succeeds() |
| 430 |
.stdout_only(format!("{}bar\n", " ".repeat(53))); |
| 431 |
} |
| 432 |
|
| 433 |
#[test] |
| 434 |
fn test_typeset_mode_w_overrides_t() { |
| 435 |
new_ucmd!() |
| 436 |
.args(&["-t", "-w", "10"]) |
| 437 |
.pipe_in("bar\n") |
| 438 |
.succeeds() |
| 439 |
.stdout_only(format!("{}bar\n", " ".repeat(8))); |
| 440 |
} |
| 441 |
|
| 442 |
#[test] |
| 443 |
fn test_default_width_72() { |
| 444 |
new_ucmd!() |
| 445 |
.pipe_in("bar\n") |
| 446 |
.succeeds() |
| 447 |
.stdout_only(format!("{}bar\n", " ".repeat(39))); |
| 448 |
} |
| 449 |
|
| 450 |
#[test] |
| 451 |
fn test_invalid_regex_word_trailing_backslash() { |
| 452 |
new_ucmd!().args(&["-W", "bar\\"]).succeeds().no_stderr(); |
| 453 |
} |
| 454 |
|
| 455 |
#[test] |
| 456 |
fn test_invalid_regex_word_unclosed_group() { |
| 457 |
new_ucmd!().args(&["-W", "(wrong"]).succeeds().no_stderr(); |
| 458 |
} |
| 459 |
|
| 460 |
#[test] |
| 461 |
fn test_missing_file_error_contains_filename() { |
| 462 |
new_ucmd!() |
| 463 |
.arg("zxc") |
| 464 |
.fails() |
| 465 |
.stderr_is("ptx: 'zxc': No such file or directory\n"); |
| 466 |
} |
| 467 |
|
| 468 |
#[test] |
| 469 |
fn test_sentence_regex_trailing_backslash() { |
| 470 |
// GNU treats a trailing lone backslash as a literal one instead of erroring. |
| 471 |
new_ucmd!() |
| 472 |
.args(&["-S", "paris\\"]) |
| 473 |
.pipe_in("") |
| 474 |
.succeeds() |
| 475 |
.no_output(); |
| 476 |
new_ucmd!() |
| 477 |
.args(&["-S", "london\\\\\\"]) |
| 478 |
.pipe_in("") |
| 479 |
.succeeds() |
| 480 |
.no_output(); |
| 481 |
} |
| 482 |
|
| 483 |
#[test] |
| 484 |
fn test_invalid_utf8_input_is_not_an_error() { |
| 485 |
new_ucmd!() |
| 486 |
.pipe_in(b"ab\xFFcd\n".to_vec()) |
| 487 |
.succeeds() |
| 488 |
.no_stderr(); |
| 489 |
} |