| 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) araba merci efjkow |
| 7 |
|
| 8 |
use regex::Regex; |
| 9 |
use uutests::new_ucmd; |
| 10 |
use uutests::util::TestScenario; |
| 11 |
use uutests::util::UCommand; |
| 12 |
|
| 13 |
#[test] |
| 14 |
fn test_default() { |
| 15 |
new_ucmd!().arg("hi").succeeds().stdout_only("hi\n"); |
| 16 |
} |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn test_no_trailing_newline() { |
| 20 |
new_ucmd!().arg("-n").arg("hi").succeeds().stdout_only("hi"); |
| 21 |
} |
| 22 |
|
| 23 |
#[test] |
| 24 |
fn test_empty_args() { |
| 25 |
new_ucmd!().succeeds().stdout_only("\n"); |
| 26 |
} |
| 27 |
|
| 28 |
#[test] |
| 29 |
fn test_escape_alert() { |
| 30 |
new_ucmd!() |
| 31 |
.args(&["-e", "\\a"]) |
| 32 |
.succeeds() |
| 33 |
.stdout_only("\x07\n"); |
| 34 |
} |
| 35 |
|
| 36 |
#[test] |
| 37 |
fn test_escape_backslash() { |
| 38 |
new_ucmd!() |
| 39 |
.args(&["-e", "\\\\"]) |
| 40 |
.succeeds() |
| 41 |
.stdout_only("\\\n"); |
| 42 |
} |
| 43 |
|
| 44 |
#[test] |
| 45 |
fn test_escape_backspace() { |
| 46 |
new_ucmd!() |
| 47 |
.args(&["-e", "\\b"]) |
| 48 |
.succeeds() |
| 49 |
.stdout_only("\x08\n"); |
| 50 |
} |
| 51 |
|
| 52 |
#[test] |
| 53 |
fn test_escape_carriage_return() { |
| 54 |
new_ucmd!() |
| 55 |
.args(&["-e", "\\r"]) |
| 56 |
.succeeds() |
| 57 |
.stdout_only("\r\n"); |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn test_escape_escape() { |
| 62 |
new_ucmd!() |
| 63 |
.args(&["-e", "\\e"]) |
| 64 |
.succeeds() |
| 65 |
.stdout_only("\x1B\n"); |
| 66 |
} |
| 67 |
|
| 68 |
#[test] |
| 69 |
fn test_escape_form_feed() { |
| 70 |
new_ucmd!() |
| 71 |
.args(&["-e", "\\f"]) |
| 72 |
.succeeds() |
| 73 |
.stdout_only("\x0C\n"); |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn test_escape_hex() { |
| 78 |
new_ucmd!() |
| 79 |
.args(&["-e", "\\x41"]) |
| 80 |
.succeeds() |
| 81 |
.stdout_only("A\n"); |
| 82 |
} |
| 83 |
|
| 84 |
#[test] |
| 85 |
fn test_escape_short_hex() { |
| 86 |
new_ucmd!() |
| 87 |
.args(&["-e", "foo\\xa bar"]) |
| 88 |
.succeeds() |
| 89 |
.stdout_only("foo\n bar\n"); |
| 90 |
} |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn test_escape_no_hex() { |
| 94 |
new_ucmd!() |
| 95 |
.args(&["-e", "foo\\x bar"]) |
| 96 |
.succeeds() |
| 97 |
.stdout_only("foo\\x bar\n"); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn test_escape_one_slash() { |
| 102 |
new_ucmd!() |
| 103 |
.args(&["-e", "foo\\ bar"]) |
| 104 |
.succeeds() |
| 105 |
.stdout_only("foo\\ bar\n"); |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn test_escape_one_slash_multi() { |
| 110 |
new_ucmd!() |
| 111 |
.args(&["-e", "foo\\", "bar"]) |
| 112 |
.succeeds() |
| 113 |
.stdout_only("foo\\ bar\n"); |
| 114 |
} |
| 115 |
|
| 116 |
#[test] |
| 117 |
fn test_escape_newline() { |
| 118 |
new_ucmd!() |
| 119 |
.args(&["-e", "\\na"]) |
| 120 |
.succeeds() |
| 121 |
.stdout_only("\na\n"); |
| 122 |
} |
| 123 |
|
| 124 |
#[test] |
| 125 |
fn test_escape_override() { |
| 126 |
new_ucmd!() |
| 127 |
.args(&["-e", "-E", "\\na"]) |
| 128 |
.succeeds() |
| 129 |
.stdout_only("\\na\n"); |
| 130 |
|
| 131 |
new_ucmd!() |
| 132 |
.args(&["-E", "-e", "\\na"]) |
| 133 |
.succeeds() |
| 134 |
.stdout_only("\na\n"); |
| 135 |
|
| 136 |
new_ucmd!() |
| 137 |
.args(&["-E", "-e", "-n", "\\na"]) |
| 138 |
.succeeds() |
| 139 |
.stdout_only("\na"); |
| 140 |
|
| 141 |
new_ucmd!() |
| 142 |
.args(&["-e", "-E", "-n", "\\na"]) |
| 143 |
.succeeds() |
| 144 |
.stdout_only("\\na"); |
| 145 |
} |
| 146 |
|
| 147 |
#[test] |
| 148 |
fn test_escape_no_further_output() { |
| 149 |
new_ucmd!() |
| 150 |
.args(&["-e", "a\\cb", "c"]) |
| 151 |
.succeeds() |
| 152 |
.stdout_only("a"); |
| 153 |
} |
| 154 |
|
| 155 |
#[test] |
| 156 |
fn test_escape_octal() { |
| 157 |
new_ucmd!() |
| 158 |
.args(&["-e", "\\0100"]) |
| 159 |
.succeeds() |
| 160 |
.stdout_only("@\n"); |
| 161 |
} |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn test_escape_short_octal() { |
| 165 |
new_ucmd!() |
| 166 |
.args(&["-e", "foo\\040bar"]) |
| 167 |
.succeeds() |
| 168 |
.stdout_only("foo bar\n"); |
| 169 |
} |
| 170 |
|
| 171 |
#[test] |
| 172 |
fn test_escape_nul() { |
| 173 |
new_ucmd!() |
| 174 |
.args(&["-e", "foo\\0 bar"]) |
| 175 |
.succeeds() |
| 176 |
.stdout_only("foo\0 bar\n"); |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
fn test_escape_octal_invalid_digit() { |
| 181 |
new_ucmd!() |
| 182 |
.args(&["-e", "foo\\08 bar"]) |
| 183 |
.succeeds() |
| 184 |
.stdout_only("foo\u{0}8 bar\n"); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
fn test_escape_tab() { |
| 189 |
new_ucmd!() |
| 190 |
.args(&["-e", "\\t"]) |
| 191 |
.succeeds() |
| 192 |
.stdout_only("\t\n"); |
| 193 |
} |
| 194 |
|
| 195 |
#[test] |
| 196 |
fn test_escape_vertical_tab() { |
| 197 |
new_ucmd!() |
| 198 |
.args(&["-e", "\\v"]) |
| 199 |
.succeeds() |
| 200 |
.stdout_only("\x0B\n"); |
| 201 |
} |
| 202 |
|
| 203 |
#[test] |
| 204 |
fn test_escape_unicode_and_quote_not_recognized() { |
| 205 |
// `echo -e` has no `\u`, `\U` or `\"` escape, unlike `printf`: the |
| 206 |
// backslash and the character following it are printed as given. |
| 207 |
for arg in [ |
| 208 |
r"\u0041", |
| 209 |
r"\U00000041", |
| 210 |
r"\U0001F600", |
| 211 |
r#"\""#, |
| 212 |
r"a\u0041b", |
| 213 |
r"a\U00000041b", |
| 214 |
r#"a\"b"#, |
| 215 |
r"\u\u", |
| 216 |
] { |
| 217 |
new_ucmd!() |
| 218 |
.args(&["-e", arg]) |
| 219 |
.succeeds() |
| 220 |
.stdout_only(format!("{arg}\n")); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
#[test] |
| 225 |
fn test_escape_malformed_unicode_not_recognized() { |
| 226 |
// Truncated, non-hexadecimal, surrogate and out-of-range payloads are not |
| 227 |
// special either: nothing is consumed and no error is reported. |
| 228 |
for arg in [ |
| 229 |
r"\u", |
| 230 |
r"\U", |
| 231 |
r"\u00", |
| 232 |
r"\U0000004", |
| 233 |
r"\u00zz", |
| 234 |
r"\U000000zz", |
| 235 |
r"\uD800", |
| 236 |
r"\U00110000", |
| 237 |
] { |
| 238 |
new_ucmd!() |
| 239 |
.args(&["-e", arg]) |
| 240 |
.succeeds() |
| 241 |
.stdout_only(format!("{arg}\n")); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
#[test] |
| 246 |
fn test_disable_escapes_unicode_and_quote() { |
| 247 |
let input_str = r#"\u0041 \U00000041 \""#; |
| 248 |
new_ucmd!() |
| 249 |
.arg("-E") |
| 250 |
.arg(input_str) |
| 251 |
.succeeds() |
| 252 |
.stdout_only(format!("{input_str}\n")); |
| 253 |
} |
| 254 |
|
| 255 |
#[test] |
| 256 |
fn test_escape_recognized_sequences_still_expand() { |
| 257 |
// The sequences the GNU manual does list for `echo -e` are unaffected. |
| 258 |
new_ucmd!() |
| 259 |
.args(&["-e", r"\a\b\e\f\n\r\t\v\\"]) |
| 260 |
.succeeds() |
| 261 |
.stdout_only("\x07\x08\x1B\x0C\n\r\t\x0B\\\n"); |
| 262 |
|
| 263 |
new_ucmd!() |
| 264 |
.args(&["-e", r"\0101\101\x41"]) |
| 265 |
.succeeds() |
| 266 |
.stdout_only("AAA\n"); |
| 267 |
|
| 268 |
new_ucmd!() |
| 269 |
.args(&["-e", r"a\cb", "c"]) |
| 270 |
.succeeds() |
| 271 |
.stdout_only("a"); |
| 272 |
} |
| 273 |
|
| 274 |
#[test] |
| 275 |
fn test_disable_escapes() { |
| 276 |
let input_str = "\\a \\\\ \\b \\r \\e \\f \\x41 \\n a\\cb \\u0100 \\t \\v"; |
| 277 |
new_ucmd!() |
| 278 |
.arg("-E") |
| 279 |
.arg(input_str) |
| 280 |
.succeeds() |
| 281 |
.stdout_only(format!("{input_str}\n")); |
| 282 |
} |
| 283 |
|
| 284 |
#[test] |
| 285 |
fn test_hyphen_value() { |
| 286 |
new_ucmd!().arg("-abc").succeeds().stdout_is("-abc\n"); |
| 287 |
} |
| 288 |
|
| 289 |
#[test] |
| 290 |
fn test_multiple_hyphen_values() { |
| 291 |
new_ucmd!() |
| 292 |
.args(&["-abc", "-def", "-edf"]) |
| 293 |
.succeeds() |
| 294 |
.stdout_is("-abc -def -edf\n"); |
| 295 |
} |
| 296 |
|
| 297 |
#[test] |
| 298 |
fn test_hyphen_values_inside_string() { |
| 299 |
new_ucmd!() |
| 300 |
.arg("'\"\n'CXXFLAGS=-g -O2'\n\"'") // spell-checker:disable-line |
| 301 |
.succeeds() |
| 302 |
.stdout_contains("CXXFLAGS"); // spell-checker:disable-line |
| 303 |
} |
| 304 |
|
| 305 |
#[test] |
| 306 |
fn test_hyphen_values_at_start() { |
| 307 |
new_ucmd!() |
| 308 |
.arg("-E") |
| 309 |
.arg("-test") |
| 310 |
.arg("araba") |
| 311 |
.arg("-merci") |
| 312 |
.succeeds() |
| 313 |
.stdout_does_not_contain("-E") |
| 314 |
.stdout_is("-test araba -merci\n"); |
| 315 |
} |
| 316 |
|
| 317 |
#[test] |
| 318 |
fn test_hyphen_values_between() { |
| 319 |
new_ucmd!() |
| 320 |
.arg("test") |
| 321 |
.arg("-E") |
| 322 |
.arg("araba") |
| 323 |
.succeeds() |
| 324 |
.stdout_is("test -E araba\n"); |
| 325 |
|
| 326 |
new_ucmd!() |
| 327 |
.arg("dumdum ") |
| 328 |
.arg("dum dum dum") |
| 329 |
.arg("-e") |
| 330 |
.arg("dum") |
| 331 |
.succeeds() |
| 332 |
.stdout_is("dumdum dum dum dum -e dum\n"); |
| 333 |
} |
| 334 |
|
| 335 |
#[test] |
| 336 |
fn test_double_hyphens_at_start() { |
| 337 |
new_ucmd!().arg("--").succeeds().stdout_only("--\n"); |
| 338 |
new_ucmd!() |
| 339 |
.arg("--") |
| 340 |
.arg("--") |
| 341 |
.succeeds() |
| 342 |
.stdout_only("-- --\n"); |
| 343 |
|
| 344 |
new_ucmd!() |
| 345 |
.arg("--") |
| 346 |
.arg("a") |
| 347 |
.succeeds() |
| 348 |
.stdout_only("-- a\n"); |
| 349 |
|
| 350 |
new_ucmd!() |
| 351 |
.arg("--") |
| 352 |
.arg("a") |
| 353 |
.arg("b") |
| 354 |
.succeeds() |
| 355 |
.stdout_only("-- a b\n"); |
| 356 |
|
| 357 |
new_ucmd!() |
| 358 |
.arg("--") |
| 359 |
.arg("a") |
| 360 |
.arg("b") |
| 361 |
.arg("--") |
| 362 |
.succeeds() |
| 363 |
.stdout_only("-- a b --\n"); |
| 364 |
} |
| 365 |
|
| 366 |
#[test] |
| 367 |
fn test_double_hyphens_after_single_hyphen() { |
| 368 |
new_ucmd!() |
| 369 |
.arg("-") |
| 370 |
.arg("--") |
| 371 |
.succeeds() |
| 372 |
.stdout_only("- --\n"); |
| 373 |
|
| 374 |
new_ucmd!() |
| 375 |
.arg("-") |
| 376 |
.arg("-n") |
| 377 |
.arg("--") |
| 378 |
.succeeds() |
| 379 |
.stdout_only("- -n --\n"); |
| 380 |
|
| 381 |
new_ucmd!() |
| 382 |
.arg("-n") |
| 383 |
.arg("-") |
| 384 |
.arg("--") |
| 385 |
.succeeds() |
| 386 |
.stdout_only("- --"); |
| 387 |
} |
| 388 |
|
| 389 |
#[test] |
| 390 |
fn test_flag_like_arguments_which_are_no_flags() { |
| 391 |
new_ucmd!() |
| 392 |
.arg("-efjkow") |
| 393 |
.arg("--") |
| 394 |
.succeeds() |
| 395 |
.stdout_only("-efjkow --\n"); |
| 396 |
|
| 397 |
new_ucmd!() |
| 398 |
.arg("--") |
| 399 |
.arg("-efjkow") |
| 400 |
.succeeds() |
| 401 |
.stdout_only("-- -efjkow\n"); |
| 402 |
|
| 403 |
new_ucmd!() |
| 404 |
.arg("-efjkow") |
| 405 |
.arg("-n") |
| 406 |
.arg("--") |
| 407 |
.succeeds() |
| 408 |
.stdout_only("-efjkow -n --\n"); |
| 409 |
|
| 410 |
new_ucmd!() |
| 411 |
.arg("-n") |
| 412 |
.arg("--") |
| 413 |
.arg("-efjkow") |
| 414 |
.succeeds() |
| 415 |
.stdout_only("-- -efjkow"); |
| 416 |
} |
| 417 |
|
| 418 |
#[test] |
| 419 |
fn test_backslash_n_last_char_in_last_argument() { |
| 420 |
new_ucmd!() |
| 421 |
.arg("-n") |
| 422 |
.arg("-e") |
| 423 |
.arg("--") |
| 424 |
.arg("foo\n") |
| 425 |
.succeeds() |
| 426 |
.stdout_only("-- foo\n"); |
| 427 |
|
| 428 |
new_ucmd!() |
| 429 |
.arg("-e") |
| 430 |
.arg("--") |
| 431 |
.arg("foo\\n") |
| 432 |
.succeeds() |
| 433 |
.stdout_only("-- foo\n\n"); |
| 434 |
|
| 435 |
new_ucmd!() |
| 436 |
.arg("-n") |
| 437 |
.arg("--") |
| 438 |
.arg("foo\n") |
| 439 |
.succeeds() |
| 440 |
.stdout_only("-- foo\n"); |
| 441 |
|
| 442 |
new_ucmd!() |
| 443 |
.arg("--") |
| 444 |
.arg("foo\n") |
| 445 |
.succeeds() |
| 446 |
.stdout_only("-- foo\n\n"); |
| 447 |
} |
| 448 |
|
| 449 |
#[test] |
| 450 |
fn test_double_hyphens_after_flags() { |
| 451 |
new_ucmd!() |
| 452 |
.arg("-e") |
| 453 |
.arg("--") |
| 454 |
.succeeds() |
| 455 |
.stdout_only("--\n"); |
| 456 |
|
| 457 |
new_ucmd!() |
| 458 |
.arg("-n") |
| 459 |
.arg("-e") |
| 460 |
.arg("--") |
| 461 |
.arg("foo\n") |
| 462 |
.succeeds() |
| 463 |
.stdout_only("-- foo\n"); |
| 464 |
|
| 465 |
new_ucmd!() |
| 466 |
.arg("-ne") |
| 467 |
.arg("--") |
| 468 |
.succeeds() |
| 469 |
.stdout_only("--"); |
| 470 |
|
| 471 |
new_ucmd!() |
| 472 |
.arg("-neE") |
| 473 |
.arg("--") |
| 474 |
.succeeds() |
| 475 |
.stdout_only("--"); |
| 476 |
|
| 477 |
new_ucmd!() |
| 478 |
.arg("-e") |
| 479 |
.arg("--") |
| 480 |
.arg("--") |
| 481 |
.succeeds() |
| 482 |
.stdout_only("-- --\n"); |
| 483 |
|
| 484 |
new_ucmd!() |
| 485 |
.arg("-e") |
| 486 |
.arg("--") |
| 487 |
.arg("a") |
| 488 |
.arg("--") |
| 489 |
.succeeds() |
| 490 |
.stdout_only("-- a --\n"); |
| 491 |
|
| 492 |
new_ucmd!() |
| 493 |
.arg("-n") |
| 494 |
.arg("--") |
| 495 |
.arg("a") |
| 496 |
.succeeds() |
| 497 |
.stdout_only("-- a"); |
| 498 |
|
| 499 |
new_ucmd!() |
| 500 |
.arg("-n") |
| 501 |
.arg("--") |
| 502 |
.arg("a") |
| 503 |
.arg("--") |
| 504 |
.succeeds() |
| 505 |
.stdout_only("-- a --"); |
| 506 |
} |
| 507 |
|
| 508 |
#[test] |
| 509 |
fn test_double_hyphens() { |
| 510 |
new_ucmd!().arg("--").succeeds().stdout_only("--\n"); |
| 511 |
new_ucmd!() |
| 512 |
.arg("--") |
| 513 |
.arg("--") |
| 514 |
.succeeds() |
| 515 |
.stdout_only("-- --\n"); |
| 516 |
|
| 517 |
new_ucmd!() |
| 518 |
.arg("a") |
| 519 |
.arg("--") |
| 520 |
.arg("b") |
| 521 |
.succeeds() |
| 522 |
.stdout_only("a -- b\n"); |
| 523 |
|
| 524 |
new_ucmd!() |
| 525 |
.arg("a") |
| 526 |
.arg("--") |
| 527 |
.arg("b") |
| 528 |
.arg("--") |
| 529 |
.succeeds() |
| 530 |
.stdout_only("a -- b --\n"); |
| 531 |
|
| 532 |
new_ucmd!() |
| 533 |
.arg("a") |
| 534 |
.arg("b") |
| 535 |
.arg("--") |
| 536 |
.arg("--") |
| 537 |
.succeeds() |
| 538 |
.stdout_only("a b -- --\n"); |
| 539 |
} |
| 540 |
|
| 541 |
#[test] |
| 542 |
fn wrapping_octal() { |
| 543 |
// Some odd behavior of GNU. Values of \0400 and greater do not fit in the |
| 544 |
// u8 that we write to stdout. So we test that it wraps: |
| 545 |
// |
| 546 |
// We give it this input: |
| 547 |
// \o501 = 1_0100_0001 (yes, **9** bits) |
| 548 |
// This should be wrapped into: |
| 549 |
// \o101 = 'A' = 0100_0001, |
| 550 |
// because we only write a single character |
| 551 |
new_ucmd!() |
| 552 |
.arg("-e") |
| 553 |
.arg("\\0501") |
| 554 |
.succeeds() |
| 555 |
.stdout_is("A\n"); |
| 556 |
} |
| 557 |
|
| 558 |
#[test] |
| 559 |
fn old_octal_syntax() { |
| 560 |
new_ucmd!() |
| 561 |
.arg("-e") |
| 562 |
.arg("\\1foo") |
| 563 |
.succeeds() |
| 564 |
.stdout_is("\x01foo\n"); |
| 565 |
|
| 566 |
new_ucmd!() |
| 567 |
.arg("-e") |
| 568 |
.arg("\\43foo") |
| 569 |
.succeeds() |
| 570 |
.stdout_is("#foo\n"); |
| 571 |
|
| 572 |
new_ucmd!() |
| 573 |
.arg("-e") |
| 574 |
.arg("\\101 foo") |
| 575 |
.succeeds() |
| 576 |
.stdout_is("A foo\n"); |
| 577 |
|
| 578 |
new_ucmd!() |
| 579 |
.arg("-e") |
| 580 |
.arg("\\1011") |
| 581 |
.succeeds() |
| 582 |
.stdout_is("A1\n"); |
| 583 |
} |
| 584 |
|
| 585 |
#[test] |
| 586 |
fn partial_version_argument() { |
| 587 |
new_ucmd!().arg("--ver").succeeds().stdout_is("--ver\n"); |
| 588 |
} |
| 589 |
|
| 590 |
#[test] |
| 591 |
fn partial_help_argument() { |
| 592 |
new_ucmd!().arg("--he").succeeds().stdout_is("--he\n"); |
| 593 |
} |
| 594 |
|
| 595 |
#[test] |
| 596 |
fn full_version_argument() { |
| 597 |
new_ucmd!() |
| 598 |
.arg("--version") |
| 599 |
.succeeds() |
| 600 |
.stdout_matches(&Regex::new(r"^echo \(uutils coreutils\) (\d+\.\d+\.\d+)\n$").unwrap()); |
| 601 |
} |
| 602 |
|
| 603 |
#[test] |
| 604 |
fn multiple_version_argument() { |
| 605 |
new_ucmd!() |
| 606 |
.arg("--version") |
| 607 |
.arg("--version") |
| 608 |
.succeeds() |
| 609 |
.stdout_is("--version --version\n"); |
| 610 |
} |
| 611 |
|
| 612 |
#[test] |
| 613 |
fn full_help_argument() { |
| 614 |
assert_ne!(new_ucmd!().arg("--help").succeeds().stdout(), b"--help\n"); |
| 615 |
assert_ne!(new_ucmd!().arg("--help").succeeds().stdout(), b"--help"); // This one is just in case. |
| 616 |
} |
| 617 |
|
| 618 |
#[test] |
| 619 |
fn multiple_help_argument() { |
| 620 |
new_ucmd!() |
| 621 |
.arg("--help") |
| 622 |
.arg("--help") |
| 623 |
.succeeds() |
| 624 |
.stdout_is("--help --help\n"); |
| 625 |
} |
| 626 |
|
| 627 |
#[test] |
| 628 |
fn multibyte_escape_unicode() { |
| 629 |
// spell-checker:disable-next-line |
| 630 |
// Tests suggested by kkew3 |
| 631 |
// https://github.com/uutils/coreutils/issues/6741 |
| 632 |
|
| 633 |
// \u{1F602} is: |
| 634 |
// |
| 635 |
// "Face with Tears of Joy" |
| 636 |
// U+1F602 |
| 637 |
// "π" |
| 638 |
|
| 639 |
new_ucmd!() |
| 640 |
.args(&["-e", r"\xf0\x9f\x98\x82"]) |
| 641 |
.succeeds() |
| 642 |
.stdout_only("\u{1F602}\n"); |
| 643 |
|
| 644 |
new_ucmd!() |
| 645 |
.args(&["-e", r"\x41\xf0\x9f\x98\x82\x42"]) |
| 646 |
.succeeds() |
| 647 |
.stdout_only("A\u{1F602}B\n"); |
| 648 |
|
| 649 |
new_ucmd!() |
| 650 |
.args(&["-e", r"\xf0\x41\x9f\x98\x82"]) |
| 651 |
.succeeds() |
| 652 |
.stdout_only_bytes(b"\xF0A\x9F\x98\x82\n"); |
| 653 |
|
| 654 |
new_ucmd!() |
| 655 |
.args(&["-e", r"\x41\xf0\c\x9f\x98\x82"]) |
| 656 |
.succeeds() |
| 657 |
.stdout_only_bytes(b"A\xF0"); |
| 658 |
} |
| 659 |
|
| 660 |
#[test] |
| 661 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 662 |
fn non_utf_8_hex_round_trip() { |
| 663 |
new_ucmd!() |
| 664 |
.args(&["-e", r"\xFF"]) |
| 665 |
.succeeds() |
| 666 |
.stdout_only_bytes(b"\xFF\n"); |
| 667 |
} |
| 668 |
|
| 669 |
#[test] |
| 670 |
fn nine_bit_octal() { |
| 671 |
const RESULT: &[u8] = b"\xFF\n"; |
| 672 |
|
| 673 |
new_ucmd!() |
| 674 |
.args(&["-e", r"\0777"]) |
| 675 |
.succeeds() |
| 676 |
.stdout_only_bytes(RESULT); |
| 677 |
|
| 678 |
new_ucmd!() |
| 679 |
.args(&["-e", r"\777"]) |
| 680 |
.succeeds() |
| 681 |
.stdout_only_bytes(RESULT); |
| 682 |
} |
| 683 |
|
| 684 |
#[test] |
| 685 |
#[cfg(target_family = "unix")] |
| 686 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 687 |
fn non_utf_8() { |
| 688 |
use std::ffi::OsStr; |
| 689 |
use std::os::unix::ffi::OsStrExt; |
| 690 |
|
| 691 |
// ISO-8859-1 encoded text |
| 692 |
// spell-checker:disable |
| 693 |
const INPUT_AND_OUTPUT: &[u8] = |
| 694 |
b"Swer an rehte g\xFCete wendet s\xEEn gem\xFCete, dem volget s\xE6lde und \xEAre."; |
| 695 |
// spell-checker:enable |
| 696 |
|
| 697 |
let os_str = OsStr::from_bytes(INPUT_AND_OUTPUT); |
| 698 |
|
| 699 |
new_ucmd!() |
| 700 |
.arg("-n") |
| 701 |
.arg(os_str) |
| 702 |
.succeeds() |
| 703 |
.stdout_only_bytes(INPUT_AND_OUTPUT); |
| 704 |
} |
| 705 |
|
| 706 |
#[test] |
| 707 |
fn slash_eight_off_by_one() { |
| 708 |
new_ucmd!() |
| 709 |
.args(&["-e", "-n", r"\8"]) |
| 710 |
.succeeds() |
| 711 |
.stdout_only(r"\8"); |
| 712 |
} |
| 713 |
|
| 714 |
#[test] |
| 715 |
fn test_normalized_newlines_stdout_is() { |
| 716 |
let res = new_ucmd!().args(&["-ne", "A\r\nB\nC"]).run(); |
| 717 |
|
| 718 |
res.normalized_newlines_stdout_is("A\r\nB\nC"); |
| 719 |
res.normalized_newlines_stdout_is("A\nB\nC"); |
| 720 |
res.normalized_newlines_stdout_is("A\nB\r\nC"); |
| 721 |
} |
| 722 |
|
| 723 |
#[test] |
| 724 |
fn test_normalized_newlines_stdout_is_fail() { |
| 725 |
new_ucmd!() |
| 726 |
.args(&["-ne", "A\r\nB\nC"]) |
| 727 |
.run() |
| 728 |
.stdout_is("A\r\nB\nC"); |
| 729 |
} |
| 730 |
|
| 731 |
#[test] |
| 732 |
fn test_cmd_result_stdout_check_and_stdout_str_check() { |
| 733 |
let result = new_ucmd!().arg("Hello world").run(); |
| 734 |
|
| 735 |
result.stdout_str_check(|stdout| stdout.ends_with("world\n")); |
| 736 |
result.stdout_check(|stdout| stdout.get(0..2).unwrap().eq(b"He")); |
| 737 |
result.no_stderr(); |
| 738 |
} |
| 739 |
|
| 740 |
#[test] |
| 741 |
#[cfg_attr(wasi_runner, ignore = "WASI: no subprocess spawning")] |
| 742 |
fn test_cmd_result_stderr_check_and_stderr_str_check() { |
| 743 |
let ts = TestScenario::new("echo"); |
| 744 |
|
| 745 |
let result = UCommand::new() |
| 746 |
.arg(format!( |
| 747 |
"{} {} Hello world >&2", |
| 748 |
ts.bin_path.display(), |
| 749 |
ts.util_name |
| 750 |
)) |
| 751 |
.run(); |
| 752 |
|
| 753 |
result.stderr_str_check(|stderr| stderr.ends_with("world\n")); |
| 754 |
result.stderr_check(|stdout| stdout.get(0..2).unwrap().eq(b"He")); |
| 755 |
result.no_stdout(); |
| 756 |
} |
| 757 |
|
| 758 |
#[test] |
| 759 |
fn test_cmd_result_stdout_str_check_when_false_then_panics() { |
| 760 |
new_ucmd!() |
| 761 |
.args(&["-e", "\\f"]) |
| 762 |
.succeeds() |
| 763 |
.stdout_only("\x0C\n"); |
| 764 |
} |
| 765 |
|
| 766 |
#[cfg(unix)] |
| 767 |
#[test] |
| 768 |
fn test_cmd_result_signal_when_normal_exit_then_no_signal() { |
| 769 |
let result = new_ucmd!().run(); |
| 770 |
assert!(result.signal().is_none()); |
| 771 |
} |
| 772 |
|
| 773 |
mod posixly_correct { |
| 774 |
use super::*; |
| 775 |
|
| 776 |
#[test] |
| 777 |
fn ignore_options() { |
| 778 |
for arg in ["--help", "--version", "-E -n 'foo'", "-nE 'foo'"] { |
| 779 |
new_ucmd!() |
| 780 |
.env("POSIXLY_CORRECT", "1") |
| 781 |
.arg(arg) |
| 782 |
.succeeds() |
| 783 |
.stdout_only(format!("{arg}\n")); |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
#[test] |
| 788 |
fn process_n_option() { |
| 789 |
new_ucmd!() |
| 790 |
.env("POSIXLY_CORRECT", "1") |
| 791 |
.args(&["-n", "foo"]) |
| 792 |
.succeeds() |
| 793 |
.stdout_only("foo"); |
| 794 |
|
| 795 |
// ignore -E & process escapes |
| 796 |
new_ucmd!() |
| 797 |
.env("POSIXLY_CORRECT", "1") |
| 798 |
.args(&["-n", "-E", "foo\\cbar"]) |
| 799 |
.succeeds() |
| 800 |
.stdout_only("foo"); |
| 801 |
} |
| 802 |
|
| 803 |
#[test] |
| 804 |
fn process_escapes() { |
| 805 |
new_ucmd!() |
| 806 |
.env("POSIXLY_CORRECT", "1") |
| 807 |
.arg("foo\\n") |
| 808 |
.succeeds() |
| 809 |
.stdout_only("foo\n\n"); |
| 810 |
|
| 811 |
new_ucmd!() |
| 812 |
.env("POSIXLY_CORRECT", "1") |
| 813 |
.arg("foo\\tbar") |
| 814 |
.succeeds() |
| 815 |
.stdout_only("foo\tbar\n"); |
| 816 |
|
| 817 |
new_ucmd!() |
| 818 |
.env("POSIXLY_CORRECT", "1") |
| 819 |
.arg("foo\\ctbar") |
| 820 |
.succeeds() |
| 821 |
.stdout_only("foo"); |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
#[test] |
| 826 |
fn test_child_when_run_with_a_non_blocking_util() { |
| 827 |
new_ucmd!() |
| 828 |
.arg("hello world") |
| 829 |
.run() |
| 830 |
.success() |
| 831 |
.stdout_only("hello world\n"); |
| 832 |
} |
| 833 |
|
| 834 |
// Test basically that most of the methods of UChild are working |
| 835 |
#[test] |
| 836 |
fn test_uchild_when_run_no_wait_with_a_non_blocking_util() { |
| 837 |
let mut child = new_ucmd!().arg("hello world").run_no_wait(); |
| 838 |
|
| 839 |
// check `child.is_alive()` and `child.delay()` is working |
| 840 |
let mut trials = 10; |
| 841 |
while child.is_alive() { |
| 842 |
assert!( |
| 843 |
trials > 0, |
| 844 |
"Assertion failed: child process is still alive." |
| 845 |
); |
| 846 |
|
| 847 |
child.delay(500); |
| 848 |
trials -= 1; |
| 849 |
} |
| 850 |
|
| 851 |
assert!(!child.is_alive()); |
| 852 |
|
| 853 |
// check `child.is_not_alive()` is working |
| 854 |
assert!(child.is_not_alive()); |
| 855 |
|
| 856 |
// check the current output is correct |
| 857 |
std::assert_eq!(child.stdout(), "hello world\n"); |
| 858 |
assert!(child.stderr().is_empty()); |
| 859 |
|
| 860 |
// check the current output of echo is empty. We already called `child.stdout()` and `echo` |
| 861 |
// exited so there's no additional output after the first call of `child.stdout()` |
| 862 |
assert!(child.stdout().is_empty()); |
| 863 |
assert!(child.stderr().is_empty()); |
| 864 |
|
| 865 |
// check that we're still able to access all output of the child process, even after exit |
| 866 |
// and call to `child.stdout()` |
| 867 |
std::assert_eq!(child.stdout_all(), "hello world\n"); |
| 868 |
assert!(child.stderr_all().is_empty()); |
| 869 |
|
| 870 |
// we should be able to call kill without panics, even if the process already exited |
| 871 |
child.make_assertion().is_not_alive(); |
| 872 |
child.kill(); |
| 873 |
|
| 874 |
// we should be able to call wait without panics and apply some assertions |
| 875 |
child.wait().unwrap().code_is(0).no_output(); |
| 876 |
} |
| 877 |
|
| 878 |
#[test] |
| 879 |
fn test_escape_sequence_ctrl_c() { |
| 880 |
new_ucmd!() |
| 881 |
.args(&["-e", "show\\c123"]) |
| 882 |
.run() |
| 883 |
.success() |
| 884 |
.stdout_only("show"); |
| 885 |
} |
| 886 |
|
| 887 |
#[test] |
| 888 |
fn test_emoji_output() { |
| 889 |
new_ucmd!() |
| 890 |
.arg("Hello π World π") |
| 891 |
.succeeds() |
| 892 |
.stdout_only("Hello π World π\n"); |
| 893 |
|
| 894 |
new_ucmd!() |
| 895 |
.args(&["-n", "Status: π― Complete"]) |
| 896 |
.succeeds() |
| 897 |
.stdout_only("Status: π― Complete"); |
| 898 |
|
| 899 |
new_ucmd!() |
| 900 |
.args(&["π¦", "loves", "π", "and", "π"]) |
| 901 |
.succeeds() |
| 902 |
.stdout_only("π¦ loves π and π\n"); |
| 903 |
|
| 904 |
new_ucmd!() |
| 905 |
.arg("π,π,π,π₯") |
| 906 |
.succeeds() |
| 907 |
.stdout_only("π,π,π,π₯\n"); |
| 908 |
|
| 909 |
new_ucmd!() |
| 910 |
.arg("γγγ«γ‘γ―δΈη") |
| 911 |
.succeeds() |
| 912 |
.stdout_only("γγγ«γ‘γ―δΈη\n"); |
| 913 |
} |