| 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) defaultcheck nocheck helpb helpz nwordb nwordwordz wordtotal |
| 7 |
|
| 8 |
use uutests::new_ucmd; |
| 9 |
use uutests::util::TestScenario; |
| 10 |
#[cfg(unix)] |
| 11 |
use uutests::util::is_locale_available; |
| 12 |
use uutests::util_name; |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn test_invalid_arg() { |
| 16 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 17 |
} |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn ab_no_args() { |
| 21 |
let scene = TestScenario::new(util_name!()); |
| 22 |
let at = &scene.fixtures; |
| 23 |
at.write("a", "a\nz\n"); |
| 24 |
at.write("b", "b\nz\n"); |
| 25 |
|
| 26 |
scene |
| 27 |
.ucmd() |
| 28 |
.args(&["a", "b"]) |
| 29 |
.succeeds() |
| 30 |
.stdout_is("a\n\tb\n\t\tz\n"); |
| 31 |
} |
| 32 |
|
| 33 |
#[test] |
| 34 |
fn ab_dash_one() { |
| 35 |
let scene = TestScenario::new(util_name!()); |
| 36 |
let at = &scene.fixtures; |
| 37 |
at.write("a", "a\nz\n"); |
| 38 |
at.write("b", "b\nz\n"); |
| 39 |
|
| 40 |
scene |
| 41 |
.ucmd() |
| 42 |
.args(&["a", "b", "-1"]) |
| 43 |
.succeeds() |
| 44 |
.stdout_is("b\n\tz\n"); |
| 45 |
} |
| 46 |
|
| 47 |
#[test] |
| 48 |
fn ab_dash_two() { |
| 49 |
let scene = TestScenario::new(util_name!()); |
| 50 |
let at = &scene.fixtures; |
| 51 |
at.write("a", "a\nz\n"); |
| 52 |
at.write("b", "b\nz\n"); |
| 53 |
|
| 54 |
scene |
| 55 |
.ucmd() |
| 56 |
.args(&["a", "b", "-2"]) |
| 57 |
.succeeds() |
| 58 |
.stdout_is("a\n\tz\n"); |
| 59 |
} |
| 60 |
|
| 61 |
#[test] |
| 62 |
fn ab_dash_three() { |
| 63 |
let scene = TestScenario::new(util_name!()); |
| 64 |
let at = &scene.fixtures; |
| 65 |
at.write("a", "a\nz\n"); |
| 66 |
at.write("b", "b\nz\n"); |
| 67 |
|
| 68 |
scene |
| 69 |
.ucmd() |
| 70 |
.args(&["a", "b", "-3"]) |
| 71 |
.succeeds() |
| 72 |
.stdout_is("a\n\tb\n"); |
| 73 |
} |
| 74 |
|
| 75 |
#[test] |
| 76 |
fn a_empty() { |
| 77 |
let scene = TestScenario::new(util_name!()); |
| 78 |
let at = &scene.fixtures; |
| 79 |
at.write("a", "a\nz\n"); |
| 80 |
at.touch("empty"); |
| 81 |
scene |
| 82 |
.ucmd() |
| 83 |
.args(&["a", "empty"]) |
| 84 |
.succeeds() |
| 85 |
.stdout_is("a\nz\n"); |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn empty_empty() { |
| 90 |
let scene = TestScenario::new(util_name!()); |
| 91 |
let at = &scene.fixtures; |
| 92 |
at.touch("empty"); |
| 93 |
scene |
| 94 |
.ucmd() |
| 95 |
.args(&["empty", "empty"]) |
| 96 |
.succeeds() |
| 97 |
.no_output(); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn total() { |
| 102 |
let scene = TestScenario::new(util_name!()); |
| 103 |
let at = &scene.fixtures; |
| 104 |
at.write("a", "a\nz\n"); |
| 105 |
at.write("b", "b\nz\n"); |
| 106 |
scene |
| 107 |
.ucmd() |
| 108 |
.args(&["--total", "a", "b"]) |
| 109 |
.succeeds() |
| 110 |
.stdout_is("a\n\tb\n\t\tz\n1\t1\t1\ttotal\n"); |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn total_with_suppressed_regular_output() { |
| 115 |
let scene = TestScenario::new(util_name!()); |
| 116 |
let at = &scene.fixtures; |
| 117 |
at.write("a", "a\nz\n"); |
| 118 |
at.write("b", "b\nz\n"); |
| 119 |
scene |
| 120 |
.ucmd() |
| 121 |
.args(&["--total", "-123", "a", "b"]) |
| 122 |
.succeeds() |
| 123 |
.stdout_is("1\t1\t1\ttotal\n"); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn repeated_flags() { |
| 128 |
let scene = TestScenario::new(util_name!()); |
| 129 |
let at = &scene.fixtures; |
| 130 |
at.write("a", "a\nz\n"); |
| 131 |
at.write("b", "b\nz\n"); |
| 132 |
scene |
| 133 |
.ucmd() |
| 134 |
.args(&["--total", "-123123", "--total", "a", "b"]) |
| 135 |
.succeeds() |
| 136 |
.stdout_is("1\t1\t1\ttotal\n"); |
| 137 |
} |
| 138 |
|
| 139 |
#[test] |
| 140 |
fn total_with_output_delimiter() { |
| 141 |
let scene = TestScenario::new(util_name!()); |
| 142 |
let at = &scene.fixtures; |
| 143 |
at.write("a", "a\nz\n"); |
| 144 |
at.write("b", "b\nz\n"); |
| 145 |
scene |
| 146 |
.ucmd() |
| 147 |
.args(&["--total", "--output-delimiter=word", "a", "b"]) |
| 148 |
.succeeds() |
| 149 |
.stdout_is("a\nwordb\nwordwordz\n1word1word1wordtotal\n"); |
| 150 |
} |
| 151 |
|
| 152 |
#[test] |
| 153 |
fn output_delimiter() { |
| 154 |
let scene = TestScenario::new(util_name!()); |
| 155 |
let at = &scene.fixtures; |
| 156 |
at.write("a", "a\nz\n"); |
| 157 |
at.write("b", "b\nz\n"); |
| 158 |
scene |
| 159 |
.ucmd() |
| 160 |
.args(&["--output-delimiter=word", "a", "b"]) |
| 161 |
.succeeds() |
| 162 |
.stdout_is("a\nwordb\nwordwordz\n"); |
| 163 |
} |
| 164 |
|
| 165 |
#[test] |
| 166 |
fn output_delimiter_hyphen_one() { |
| 167 |
let scene = TestScenario::new(util_name!()); |
| 168 |
let at = &scene.fixtures; |
| 169 |
at.write("a", "a\nz\n"); |
| 170 |
at.write("b", "b\nz\n"); |
| 171 |
scene |
| 172 |
.ucmd() |
| 173 |
.args(&["--output-delimiter", "-1", "a", "b"]) |
| 174 |
.succeeds() |
| 175 |
.stdout_is("a\n-1b\n-1-1z\n"); |
| 176 |
} |
| 177 |
|
| 178 |
#[test] |
| 179 |
fn output_delimiter_hyphen_help() { |
| 180 |
let scene = TestScenario::new(util_name!()); |
| 181 |
let at = &scene.fixtures; |
| 182 |
at.write("a", "a\nz\n"); |
| 183 |
at.write("b", "b\nz\n"); |
| 184 |
scene |
| 185 |
.ucmd() |
| 186 |
.args(&["--output-delimiter", "--help", "a", "b"]) |
| 187 |
.succeeds() |
| 188 |
.stdout_is("a\n--helpb\n--help--helpz\n"); |
| 189 |
} |
| 190 |
|
| 191 |
#[test] |
| 192 |
fn output_delimiter_multiple_identical() { |
| 193 |
let scene = TestScenario::new(util_name!()); |
| 194 |
let at = &scene.fixtures; |
| 195 |
at.write("a", "a\nz\n"); |
| 196 |
at.write("b", "b\nz\n"); |
| 197 |
scene |
| 198 |
.ucmd() |
| 199 |
.args(&[ |
| 200 |
"--output-delimiter=word", |
| 201 |
"--output-delimiter=word", |
| 202 |
"a", |
| 203 |
"b", |
| 204 |
]) |
| 205 |
.succeeds() |
| 206 |
.stdout_is("a\nwordb\nwordwordz\n"); |
| 207 |
} |
| 208 |
|
| 209 |
#[test] |
| 210 |
fn output_delimiter_multiple_different() { |
| 211 |
let scene = TestScenario::new(util_name!()); |
| 212 |
let at = &scene.fixtures; |
| 213 |
at.write("a", "a\nz\n"); |
| 214 |
at.write("b", "b\nz\n"); |
| 215 |
scene |
| 216 |
.ucmd() |
| 217 |
.args(&[ |
| 218 |
"--output-delimiter=word", |
| 219 |
"--output-delimiter=other", |
| 220 |
"a", |
| 221 |
"b", |
| 222 |
]) |
| 223 |
.fails() |
| 224 |
.no_stdout() |
| 225 |
.stderr_contains("multiple") |
| 226 |
.stderr_contains("output") |
| 227 |
.stderr_contains("delimiters"); |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
#[ignore = "This is too weird; deviate intentionally."] |
| 232 |
fn output_delimiter_multiple_different_prevents_help() { |
| 233 |
let scene = TestScenario::new(util_name!()); |
| 234 |
let at = &scene.fixtures; |
| 235 |
at.write("a", "a\nz\n"); |
| 236 |
at.write("b", "b\nz\n"); |
| 237 |
scene |
| 238 |
.ucmd() |
| 239 |
.args(&[ |
| 240 |
"--output-delimiter=word", |
| 241 |
"--output-delimiter=other", |
| 242 |
"--help", |
| 243 |
"a", |
| 244 |
"b", |
| 245 |
]) |
| 246 |
.fails() |
| 247 |
.no_stdout() |
| 248 |
.stderr_contains("multiple") |
| 249 |
.stderr_contains("output") |
| 250 |
.stderr_contains("delimiters"); |
| 251 |
} |
| 252 |
|
| 253 |
#[test] |
| 254 |
fn output_delimiter_nul() { |
| 255 |
let scene = TestScenario::new(util_name!()); |
| 256 |
let at = &scene.fixtures; |
| 257 |
at.write("a", "a\nz\n"); |
| 258 |
at.write("b", "b\nz\n"); |
| 259 |
scene |
| 260 |
.ucmd() |
| 261 |
.args(&["--output-delimiter=", "a", "b"]) |
| 262 |
.succeeds() |
| 263 |
.stdout_is("a\n\0b\n\0\0z\n"); |
| 264 |
} |
| 265 |
|
| 266 |
#[test] |
| 267 |
fn zero_terminated() { |
| 268 |
let scene = TestScenario::new(util_name!()); |
| 269 |
let at = &scene.fixtures; |
| 270 |
|
| 271 |
at.write("a_nul", "a\0z\0"); |
| 272 |
at.write("b_nul", "b\0z\0"); |
| 273 |
for param in ["-z", "--zero-terminated"] { |
| 274 |
scene |
| 275 |
.ucmd() |
| 276 |
.args(&[param, "a_nul", "b_nul"]) |
| 277 |
.succeeds() |
| 278 |
.stdout_is("a\0\tb\0\t\tz\0"); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
#[test] |
| 283 |
fn zero_terminated_provided_multiple_times() { |
| 284 |
let scene = TestScenario::new(util_name!()); |
| 285 |
let at = &scene.fixtures; |
| 286 |
|
| 287 |
at.write("a_nul", "a\0z\0"); |
| 288 |
at.write("b_nul", "b\0z\0"); |
| 289 |
for param in ["-z", "--zero-terminated"] { |
| 290 |
scene |
| 291 |
.ucmd() |
| 292 |
.args(&[param, param, param, "a_nul", "b_nul"]) |
| 293 |
.succeeds() |
| 294 |
.stdout_is("a\0\tb\0\t\tz\0"); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
#[test] |
| 299 |
fn zero_terminated_with_total() { |
| 300 |
let scene = TestScenario::new(util_name!()); |
| 301 |
let at = &scene.fixtures; |
| 302 |
|
| 303 |
at.write("a_nul", "a\0z\0"); |
| 304 |
at.write("b_nul", "b\0z\0"); |
| 305 |
|
| 306 |
for param in ["-z", "--zero-terminated"] { |
| 307 |
scene |
| 308 |
.ucmd() |
| 309 |
.args(&[param, "--total", "a_nul", "b_nul"]) |
| 310 |
.succeeds() |
| 311 |
.stdout_is("a\0\tb\0\t\tz\x001\t1\t1\ttotal\0"); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
#[test] |
| 316 |
fn check_order() { |
| 317 |
let scene = TestScenario::new(util_name!()); |
| 318 |
let at = &scene.fixtures; |
| 319 |
at.write("bad_order_1", "e\nd\nb\na\n"); |
| 320 |
at.write("bad_order_2", "e\nc\nb\na\n"); |
| 321 |
scene |
| 322 |
.ucmd() |
| 323 |
.args(&["--check-order", "bad_order_1", "bad_order_2"]) |
| 324 |
.fails() |
| 325 |
.stdout_is("\t\te\n") |
| 326 |
.stderr_is("comm: file 2 is not in sorted order\n"); |
| 327 |
} |
| 328 |
|
| 329 |
#[test] |
| 330 |
fn nocheck_order() { |
| 331 |
let scene = TestScenario::new(util_name!()); |
| 332 |
let at = &scene.fixtures; |
| 333 |
at.write("bad_order_1", "e\nd\nb\na\n"); |
| 334 |
at.write("bad_order_2", "e\nc\nb\na\n"); |
| 335 |
scene |
| 336 |
.ucmd() |
| 337 |
.args(&["--nocheck-order", "bad_order_1", "bad_order_2"]) |
| 338 |
.succeeds() |
| 339 |
.stdout_is("\t\te\n\tc\n\tb\n\ta\nd\nb\na\n") |
| 340 |
.no_stderr(); |
| 341 |
} |
| 342 |
|
| 343 |
// when neither --check-order nor --no-check-order is provided, |
| 344 |
// stderr and the error code behaves like check order, but stdout |
| 345 |
// behaves like nocheck_order. However with some quirks detailed below. |
| 346 |
#[test] |
| 347 |
fn defaultcheck_order() { |
| 348 |
let scene = TestScenario::new(util_name!()); |
| 349 |
let at = &scene.fixtures; |
| 350 |
at.write("a", "a\n"); |
| 351 |
at.write("bad_order_1", "e\nd\nb\na\n"); |
| 352 |
scene |
| 353 |
.ucmd() |
| 354 |
.args(&["a", "bad_order_1"]) |
| 355 |
.fails() |
| 356 |
.stdout_is("a\n\te\n\td\n\tb\n\ta\n") |
| 357 |
.stderr_is( |
| 358 |
"comm: file 2 is not in sorted order\n\ |
| 359 |
comm: input is not in sorted order\n", |
| 360 |
); |
| 361 |
} |
| 362 |
|
| 363 |
// * the first: if both files are not in order, the default behavior is the only |
| 364 |
// behavior that will provide an error message |
| 365 |
// * the second: if two rows are paired but are out of order, |
| 366 |
// it won't matter if all rows in the two files are exactly the same. |
| 367 |
// This is specified in the documentation |
| 368 |
#[test] |
| 369 |
fn defaultcheck_order_identical_bad_order_files() { |
| 370 |
let scene = TestScenario::new(util_name!()); |
| 371 |
let at = &scene.fixtures; |
| 372 |
at.write("bad_order_1", "e\nd\nb\na\n"); |
| 373 |
scene |
| 374 |
.ucmd() |
| 375 |
.args(&["bad_order_1", "bad_order_1"]) |
| 376 |
.succeeds() |
| 377 |
.stdout_is("\t\te\n\t\td\n\t\tb\n\t\ta\n"); |
| 378 |
scene |
| 379 |
.ucmd() |
| 380 |
.arg("--check-order") |
| 381 |
.args(&["bad_order_1", "bad_order_1"]) |
| 382 |
.fails() |
| 383 |
.stdout_is("\t\te\n") |
| 384 |
.stderr_is("comm: file 1 is not in sorted order\n"); |
| 385 |
} |
| 386 |
|
| 387 |
// * the third: (it is not know whether this is a bug or not) |
| 388 |
// for the first incident, and only the first incident, |
| 389 |
// where both lines are different and one or both file lines being |
| 390 |
// compared are out of order from the preceding line, |
| 391 |
// it is ignored and no errors occur. |
| 392 |
// * the fourth: (it is not known whether this is a bug or not) |
| 393 |
// there are additional, not-yet-understood circumstances where an out-of-order |
| 394 |
// pair is ignored and is not counted against the 1 maximum out-of-order line. |
| 395 |
#[test] |
| 396 |
fn unintuitive_default_behavior_1() { |
| 397 |
let scene = TestScenario::new(util_name!()); |
| 398 |
let at = &scene.fixtures; |
| 399 |
at.write("defaultcheck_unintuitive_1", "m\nh\nn\no\nc\np\n"); |
| 400 |
at.write("defaultcheck_unintuitive_2", "m\nh\nn\no\np\n"); |
| 401 |
// Here, GNU does not fail, but uutils does |
| 402 |
scene |
| 403 |
.ucmd() |
| 404 |
.args(&["defaultcheck_unintuitive_1", "defaultcheck_unintuitive_2"]) |
| 405 |
.fails() |
| 406 |
.stdout_is("\t\tm\n\t\th\n\t\tn\n\t\to\nc\n\t\tp\n"); |
| 407 |
} |
| 408 |
|
| 409 |
#[test] |
| 410 |
fn no_arguments() { |
| 411 |
new_ucmd!().fails().no_stdout(); |
| 412 |
} |
| 413 |
|
| 414 |
#[test] |
| 415 |
fn one_argument() { |
| 416 |
new_ucmd!().arg("a").fails().no_stdout(); |
| 417 |
} |
| 418 |
|
| 419 |
#[test] |
| 420 |
fn test_no_such_file() { |
| 421 |
new_ucmd!() |
| 422 |
.args(&["bogus_file_1", "bogus_file_2"]) |
| 423 |
.fails() |
| 424 |
.stderr_only("comm: bogus_file_1: No such file or directory\n"); |
| 425 |
} |
| 426 |
|
| 427 |
#[test] |
| 428 |
fn test_is_dir() { |
| 429 |
let scene = TestScenario::new(util_name!()); |
| 430 |
let at = &scene.fixtures; |
| 431 |
|
| 432 |
scene |
| 433 |
.ucmd() |
| 434 |
.args(&[".", "."]) |
| 435 |
.fails() |
| 436 |
.stderr_only("comm: .: Is a directory\n"); |
| 437 |
|
| 438 |
at.mkdir("dir"); |
| 439 |
scene |
| 440 |
.ucmd() |
| 441 |
.args(&["dir", "."]) |
| 442 |
.fails() |
| 443 |
.stderr_only("comm: dir: Is a directory\n"); |
| 444 |
|
| 445 |
at.touch("file"); |
| 446 |
scene |
| 447 |
.ucmd() |
| 448 |
.args(&[".", "file"]) |
| 449 |
.fails() |
| 450 |
.stderr_only("comm: .: Is a directory\n"); |
| 451 |
|
| 452 |
at.touch("file"); |
| 453 |
scene |
| 454 |
.ucmd() |
| 455 |
.args(&["file", "."]) |
| 456 |
.fails() |
| 457 |
.stderr_only("comm: .: Is a directory\n"); |
| 458 |
} |
| 459 |
|
| 460 |
#[test] |
| 461 |
fn test_sorted() { |
| 462 |
let scene = TestScenario::new(util_name!()); |
| 463 |
let at = &scene.fixtures; |
| 464 |
at.write("comm1", "1\n3"); |
| 465 |
at.write("comm2", "3\n2"); |
| 466 |
scene |
| 467 |
.ucmd() |
| 468 |
.args(&["comm1", "comm2"]) |
| 469 |
.fails_with_code(1) |
| 470 |
.stdout_is("1\n\t\t3\n\t2\n") |
| 471 |
.stderr_is("comm: file 2 is not in sorted order\ncomm: input is not in sorted order\n"); |
| 472 |
} |
| 473 |
|
| 474 |
#[test] |
| 475 |
fn test_sorted_check_order() { |
| 476 |
let expected_stderr = "comm: file 2 is not in sorted order\n"; |
| 477 |
|
| 478 |
let scene = TestScenario::new(util_name!()); |
| 479 |
let at = &scene.fixtures; |
| 480 |
at.write("comm1", "1\n3"); |
| 481 |
at.write("comm2", "3\n2"); |
| 482 |
scene |
| 483 |
.ucmd() |
| 484 |
.arg("--check-order") |
| 485 |
.args(&["comm1", "comm2"]) |
| 486 |
.fails_with_code(1) |
| 487 |
.stdout_is("1\n\t\t3\n") |
| 488 |
.stderr_is(expected_stderr); |
| 489 |
} |
| 490 |
|
| 491 |
#[test] |
| 492 |
fn test_both_inputs_out_of_order() { |
| 493 |
let scene = TestScenario::new(util_name!()); |
| 494 |
let at = &scene.fixtures; |
| 495 |
at.write("file_a", "3\n1\n0\n"); |
| 496 |
at.write("file_b", "3\n2\n0\n"); |
| 497 |
|
| 498 |
scene |
| 499 |
.ucmd() |
| 500 |
.args(&["file_a", "file_b"]) |
| 501 |
.fails_with_code(1) |
| 502 |
.stdout_is("\t\t3\n1\n0\n\t2\n\t0\n") |
| 503 |
.stderr_is( |
| 504 |
"comm: file 1 is not in sorted order\n\ |
| 505 |
comm: file 2 is not in sorted order\n\ |
| 506 |
comm: input is not in sorted order\n", |
| 507 |
); |
| 508 |
} |
| 509 |
|
| 510 |
#[test] |
| 511 |
fn test_both_inputs_out_of_order_last_pair() { |
| 512 |
let scene = TestScenario::new(util_name!()); |
| 513 |
let at = &scene.fixtures; |
| 514 |
at.write("file_a", "3\n1\n"); |
| 515 |
at.write("file_b", "3\n2\n"); |
| 516 |
|
| 517 |
scene |
| 518 |
.ucmd() |
| 519 |
.args(&["file_a", "file_b"]) |
| 520 |
.fails_with_code(1) |
| 521 |
.stdout_is("\t\t3\n1\n\t2\n") |
| 522 |
.stderr_is( |
| 523 |
"comm: file 1 is not in sorted order\n\ |
| 524 |
comm: file 2 is not in sorted order\n\ |
| 525 |
comm: input is not in sorted order\n", |
| 526 |
); |
| 527 |
} |
| 528 |
|
| 529 |
#[test] |
| 530 |
fn test_first_input_out_of_order_extended() { |
| 531 |
let scene = TestScenario::new(util_name!()); |
| 532 |
let at = &scene.fixtures; |
| 533 |
at.write("file_a", "0\n3\n1\n"); |
| 534 |
at.write("file_b", "2\n3\n"); |
| 535 |
|
| 536 |
scene |
| 537 |
.ucmd() |
| 538 |
.args(&["file_a", "file_b"]) |
| 539 |
.fails_with_code(1) |
| 540 |
.stdout_is("0\n\t2\n\t\t3\n1\n") |
| 541 |
.stderr_is( |
| 542 |
"comm: file 1 is not in sorted order\n\ |
| 543 |
comm: input is not in sorted order\n", |
| 544 |
); |
| 545 |
} |
| 546 |
|
| 547 |
#[test] |
| 548 |
fn test_out_of_order_input_nocheck() { |
| 549 |
let scene = TestScenario::new(util_name!()); |
| 550 |
let at = &scene.fixtures; |
| 551 |
|
| 552 |
// Create input files |
| 553 |
at.write("file_a", "1\n3\n"); |
| 554 |
at.write("file_b", "3\n2\n"); |
| 555 |
|
| 556 |
scene |
| 557 |
.ucmd() |
| 558 |
.arg("--nocheck-order") |
| 559 |
.args(&["file_a", "file_b"]) |
| 560 |
.succeeds() |
| 561 |
.stdout_is("1\n\t\t3\n\t2\n") |
| 562 |
.no_stderr(); |
| 563 |
} |
| 564 |
|
| 565 |
#[test] |
| 566 |
fn test_both_inputs_out_of_order_but_identical() { |
| 567 |
let scene = TestScenario::new(util_name!()); |
| 568 |
let at = &scene.fixtures; |
| 569 |
|
| 570 |
at.write("file_a", "2\n1\n0\n"); |
| 571 |
at.write("file_b", "2\n1\n0\n"); |
| 572 |
|
| 573 |
scene |
| 574 |
.ucmd() |
| 575 |
.args(&["file_a", "file_b"]) |
| 576 |
.succeeds() |
| 577 |
.stdout_is("\t\t2\n\t\t1\n\t\t0\n") |
| 578 |
.no_stderr(); |
| 579 |
} |
| 580 |
|
| 581 |
#[test] |
| 582 |
fn test_comm_arg_error() { |
| 583 |
let scene = TestScenario::new(util_name!()); |
| 584 |
|
| 585 |
// Test extra argument error case |
| 586 |
scene |
| 587 |
.ucmd() |
| 588 |
.args(&["a", "b", "no-such"]) |
| 589 |
.fails() |
| 590 |
.code_is(1) |
| 591 |
.stderr_contains("error: unexpected argument 'no-such' found") |
| 592 |
.stderr_contains("Usage: comm [OPTION]... FILE1 FILE2") |
| 593 |
.stderr_contains("For more information, try '--help'."); |
| 594 |
// Test extra argument error case |
| 595 |
scene |
| 596 |
.ucmd() |
| 597 |
.args(&["a"]) |
| 598 |
.fails() |
| 599 |
.code_is(1) |
| 600 |
.stderr_is("error: the following required arguments were not provided:\n <FILE2>\n\nUsage: comm [OPTION]... FILE1 FILE2\n\nFor more information, try '--help'.\n"); |
| 601 |
} |
| 602 |
|
| 603 |
#[test] |
| 604 |
fn comm_emoji_sorted_inputs() { |
| 605 |
let scene = TestScenario::new(util_name!()); |
| 606 |
let at = &scene.fixtures; |
| 607 |
|
| 608 |
at.write("file1", "š\nš¦\n"); |
| 609 |
at.write("file2", "š¦\nšŖ½\n"); |
| 610 |
|
| 611 |
scene |
| 612 |
.ucmd() |
| 613 |
.args(&["file1", "file2"]) |
| 614 |
.env("LC_ALL", "C.UTF-8") |
| 615 |
.succeeds() |
| 616 |
.stdout_only("š\n\t\tš¦\n\tšŖ½\n"); |
| 617 |
} |
| 618 |
|
| 619 |
#[test] |
| 620 |
fn test_comm_eintr_handling() { |
| 621 |
// Test that comm properly handles EINTR (ErrorKind::Interrupted) during file comparison |
| 622 |
// This verifies the signal interruption retry logic in are_files_identical function |
| 623 |
let scene = TestScenario::new(util_name!()); |
| 624 |
let at = &scene.fixtures; |
| 625 |
|
| 626 |
// Create test files with identical content |
| 627 |
let test_content = "line1\nline2\nline3\n"; |
| 628 |
at.write("file1", test_content); |
| 629 |
at.write("file2", test_content); |
| 630 |
|
| 631 |
// Test that comm can handle interrupted reads during file comparison |
| 632 |
// The EINTR handling should retry and complete successfully |
| 633 |
scene |
| 634 |
.ucmd() |
| 635 |
.args(&["file1", "file2"]) |
| 636 |
.succeeds() |
| 637 |
.stdout_contains("line1") // Check that content is present (comm adds tabs for identical lines) |
| 638 |
.stdout_contains("line2") |
| 639 |
.stdout_contains("line3"); |
| 640 |
|
| 641 |
// Create test files with identical content |
| 642 |
let test_content = "line1\nline2\nline3\n"; |
| 643 |
at.write("file1", test_content); |
| 644 |
at.write("file2", test_content); |
| 645 |
|
| 646 |
// Test that comm can handle interrupted reads during file comparison |
| 647 |
// The EINTR handling should retry and complete successfully |
| 648 |
scene |
| 649 |
.ucmd() |
| 650 |
.args(&["file1", "file2"]) |
| 651 |
.succeeds() |
| 652 |
.stdout_contains("line1") // Check that content is present (comm adds tabs for identical lines) |
| 653 |
.stdout_contains("line2") |
| 654 |
.stdout_contains("line3"); |
| 655 |
} |
| 656 |
|
| 657 |
#[test] |
| 658 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 659 |
fn test_output_lossy_utf8() { |
| 660 |
let scene = TestScenario::new(util_name!()); |
| 661 |
let at = &scene.fixtures; |
| 662 |
|
| 663 |
// Create files with invalid UTF-8 |
| 664 |
// A: \xfe\n\xff\n |
| 665 |
// B: \xff\n\xfe\n |
| 666 |
at.write_bytes("a", b"\xfe\n\xff\n"); |
| 667 |
at.write_bytes("b", b"\xff\n\xfe\n"); |
| 668 |
|
| 669 |
// GNU comm output (and uutils with fix): |
| 670 |
// \xfe\n (col 1) |
| 671 |
// \t\t\xff\n (col 3) |
| 672 |
// \t\xfe\n (col 2) |
| 673 |
// Hex: fe 0a 09 09 ff 0a 09 fe 0a |
| 674 |
|
| 675 |
scene |
| 676 |
.ucmd() |
| 677 |
.args(&["a", "b"]) |
| 678 |
.fails() // Fails because of unsorted input |
| 679 |
.stdout_is_bytes(b"\xfe\n\t\t\xff\n\t\xfe\n"); |
| 680 |
} |
| 681 |
|
| 682 |
#[test] |
| 683 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 684 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 685 |
fn test_comm_anonymous_pipes() { |
| 686 |
use std::{io::Write, os::fd::AsRawFd, process}; |
| 687 |
|
| 688 |
let scene = TestScenario::new(util_name!()); |
| 689 |
|
| 690 |
// Open two anonymous pipes |
| 691 |
let (comm1_reader, mut comm1_writer) = std::io::pipe().unwrap(); |
| 692 |
let (comm2_reader, mut comm2_writer) = std::io::pipe().unwrap(); |
| 693 |
|
| 694 |
// comm reads the data in chunks |
| 695 |
// make content large enough, so that at least two chunks are read |
| 696 |
// default buffer size is 8192, so with 6 characters (5 digits + \n) per line we need to write at least 1366 lines |
| 697 |
|
| 698 |
// write 1500 lines into comm1: 00000\n00001\n...01500\n |
| 699 |
let mut content = String::new(); |
| 700 |
for i in 0..1500 { |
| 701 |
content.push_str(&format!("{i:05}\n")); |
| 702 |
} |
| 703 |
assert!(comm1_writer.write_all(content.as_bytes()).is_ok()); |
| 704 |
drop(comm1_writer); |
| 705 |
|
| 706 |
// write into comm2: 00000\n00001\n...01500\n99999\n |
| 707 |
content.push_str("99999\n"); |
| 708 |
assert!(comm2_writer.write_all(content.as_bytes()).is_ok()); |
| 709 |
drop(comm2_writer); |
| 710 |
|
| 711 |
// run comm, showing unique lines in second input |
| 712 |
let comm1_fd = format!("/proc/{}/fd/{}", process::id(), comm1_reader.as_raw_fd()); |
| 713 |
let comm2_fd = format!("/proc/{}/fd/{}", process::id(), comm2_reader.as_raw_fd()); |
| 714 |
scene |
| 715 |
.ucmd() |
| 716 |
.args(&["-13", &comm1_fd, &comm2_fd]) |
| 717 |
.succeeds() |
| 718 |
.stdout_is("99999\n"); |
| 719 |
} |
| 720 |
|
| 721 |
#[test] |
| 722 |
#[cfg(all(target_os = "linux", not(target_env = "musl")))] |
| 723 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 724 |
fn test_read_error() { |
| 725 |
new_ucmd!() |
| 726 |
.arg("/proc/self/mem") |
| 727 |
.arg("/dev/null") |
| 728 |
.fails() |
| 729 |
.stderr_contains("comm: /proc/self/mem: Input/output error"); |
| 730 |
|
| 731 |
new_ucmd!() |
| 732 |
.arg("/dev/null") |
| 733 |
.arg("/proc/self/mem") |
| 734 |
.fails() |
| 735 |
.stderr_contains("comm: /proc/self/mem: Input/output error"); |
| 736 |
} |
| 737 |
|
| 738 |
#[test] |
| 739 |
#[cfg(unix)] |
| 740 |
#[cfg_attr(wasi_runner, ignore = "WASI: the guest does not inherit LC_ALL")] |
| 741 |
fn test_locale_collation() { |
| 742 |
// In a UTF-8 locale the collation puts `a1` before `a-b` and the byte order |
| 743 |
// puts them the other way round. Reading a file `sort` produced with a byte |
| 744 |
// comparison rejects it as unsorted and drops the line the two files share. |
| 745 |
let locale = "en_US.UTF-8"; |
| 746 |
if !is_locale_available(locale) { |
| 747 |
return; |
| 748 |
} |
| 749 |
let scene = TestScenario::new(util_name!()); |
| 750 |
scene.fixtures.write("f1", "a1\na-b\n"); |
| 751 |
scene.fixtures.write("f2", "a-b\n"); |
| 752 |
|
| 753 |
scene |
| 754 |
.ucmd() |
| 755 |
.env("LC_ALL", locale) |
| 756 |
.args(&["-12", "f1", "f2"]) |
| 757 |
.succeeds() |
| 758 |
.stdout_only("a-b\n"); |
| 759 |
|
| 760 |
scene |
| 761 |
.ucmd() |
| 762 |
.env("LC_ALL", locale) |
| 763 |
.args(&["-23", "f1", "f2"]) |
| 764 |
.succeeds() |
| 765 |
.stdout_only("a1\n"); |
| 766 |
} |
| 767 |
|
| 768 |
#[test] |
| 769 |
#[cfg(unix)] |
| 770 |
fn test_c_locale_still_orders_by_bytes() { |
| 771 |
let scene = TestScenario::new(util_name!()); |
| 772 |
scene.fixtures.write("f1", "a-b\na1\n"); |
| 773 |
scene.fixtures.write("f2", "a-b\n"); |
| 774 |
|
| 775 |
scene |
| 776 |
.ucmd() |
| 777 |
.env("LC_ALL", "C") |
| 778 |
.args(&["-12", "f1", "f2"]) |
| 779 |
.succeeds() |
| 780 |
.stdout_only("a-b\n"); |
| 781 |
} |
| 782 |
|
| 783 |
#[test] |
| 784 |
#[cfg(target_os = "linux")] |
| 785 |
fn test_comm_write_error_dev_full() { |
| 786 |
use std::fs::OpenOptions; |
| 787 |
let scene = TestScenario::new(util_name!()); |
| 788 |
scene.fixtures.write("a", "a\n"); |
| 789 |
let dev_full = OpenOptions::new().write(true).open("/dev/full").unwrap(); |
| 790 |
scene |
| 791 |
.ucmd() |
| 792 |
.args(&["a", "a"]) |
| 793 |
.set_stdout(dev_full) |
| 794 |
.fails() |
| 795 |
.stderr_is("comm: write error: No space left on device\n"); |
| 796 |
} |