| 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) autoformat nocheck FILENUM |
| 7 |
|
| 8 |
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))] |
| 9 |
use std::fs::OpenOptions; |
| 10 |
#[cfg(unix)] |
| 11 |
use std::{ffi::OsStr, os::unix::ffi::OsStrExt}; |
| 12 |
#[cfg(windows)] |
| 13 |
use std::{ffi::OsString, os::windows::ffi::OsStringExt}; |
| 14 |
use uutests::new_ucmd; |
| 15 |
use uutests::util::TestScenario; |
| 16 |
use uutests::util_name; |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn test_invalid_arg() { |
| 20 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 21 |
} |
| 22 |
|
| 23 |
#[test] |
| 24 |
fn empty_files() { |
| 25 |
new_ucmd!() |
| 26 |
.arg("empty.txt") |
| 27 |
.arg("empty.txt") |
| 28 |
.succeeds() |
| 29 |
.stdout_only(""); |
| 30 |
|
| 31 |
new_ucmd!() |
| 32 |
.arg("empty.txt") |
| 33 |
.arg("fields_1.txt") |
| 34 |
.succeeds() |
| 35 |
.stdout_only(""); |
| 36 |
|
| 37 |
new_ucmd!() |
| 38 |
.arg("fields_1.txt") |
| 39 |
.arg("empty.txt") |
| 40 |
.succeeds() |
| 41 |
.stdout_only(""); |
| 42 |
} |
| 43 |
|
| 44 |
#[test] |
| 45 |
fn empty_intersection() { |
| 46 |
new_ucmd!() |
| 47 |
.arg("fields_1.txt") |
| 48 |
.arg("fields_2.txt") |
| 49 |
.arg("-2") |
| 50 |
.arg("2") |
| 51 |
.succeeds() |
| 52 |
.stdout_only(""); |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn default_arguments() { |
| 57 |
new_ucmd!() |
| 58 |
.arg("fields_1.txt") |
| 59 |
.arg("fields_2.txt") |
| 60 |
.succeeds() |
| 61 |
.stdout_only_fixture("default.expected"); |
| 62 |
} |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn only_whitespace_separators_merge() { |
| 66 |
new_ucmd!() |
| 67 |
.arg("contiguous_separators.txt") |
| 68 |
.arg("-") |
| 69 |
.pipe_in(" a ,c ") |
| 70 |
.succeeds() |
| 71 |
.stdout_only("a ,,,b ,c \n"); |
| 72 |
|
| 73 |
new_ucmd!() |
| 74 |
.arg("contiguous_separators.txt") |
| 75 |
.arg("-t") |
| 76 |
.arg(",") |
| 77 |
.arg("-") |
| 78 |
.pipe_in(" a ,c ") |
| 79 |
.succeeds() |
| 80 |
.stdout_only(" a ,,,b,c \n"); |
| 81 |
} |
| 82 |
|
| 83 |
#[test] |
| 84 |
fn different_fields() { |
| 85 |
new_ucmd!() |
| 86 |
.arg("fields_2.txt") |
| 87 |
.arg("fields_4.txt") |
| 88 |
.arg("-j") |
| 89 |
.arg("2") |
| 90 |
.succeeds() |
| 91 |
.stdout_only_fixture("different_fields.expected"); |
| 92 |
|
| 93 |
new_ucmd!() |
| 94 |
.arg("fields_2.txt") |
| 95 |
.arg("fields_4.txt") |
| 96 |
.arg("-1") |
| 97 |
.arg("2") |
| 98 |
.arg("-2") |
| 99 |
.arg("2") |
| 100 |
.succeeds() |
| 101 |
.stdout_only_fixture("different_fields.expected"); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn different_field() { |
| 106 |
new_ucmd!() |
| 107 |
.arg("fields_2.txt") |
| 108 |
.arg("fields_3.txt") |
| 109 |
.arg("-2") |
| 110 |
.arg("2") |
| 111 |
.succeeds() |
| 112 |
.stdout_only_fixture("different_field.expected"); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn out_of_bounds_fields() { |
| 117 |
new_ucmd!() |
| 118 |
.arg("fields_1.txt") |
| 119 |
.arg("fields_4.txt") |
| 120 |
.arg("-1") |
| 121 |
.arg("3") |
| 122 |
.arg("-2") |
| 123 |
.arg("5") |
| 124 |
.succeeds() |
| 125 |
.stdout_only_fixture("out_of_bounds_fields.expected"); |
| 126 |
|
| 127 |
new_ucmd!() |
| 128 |
.arg("fields_1.txt") |
| 129 |
.arg("fields_4.txt") |
| 130 |
.arg("-j") |
| 131 |
.arg("100000000000000000000") // > usize::MAX for 64 bits |
| 132 |
.succeeds() |
| 133 |
.stdout_only_fixture("out_of_bounds_fields.expected"); |
| 134 |
} |
| 135 |
|
| 136 |
#[test] |
| 137 |
fn unpaired_lines() { |
| 138 |
new_ucmd!() |
| 139 |
.arg("fields_2.txt") |
| 140 |
.arg("fields_3.txt") |
| 141 |
.arg("-a") |
| 142 |
.arg("1") |
| 143 |
.succeeds() |
| 144 |
.stdout_only_fixture("fields_2.txt"); |
| 145 |
|
| 146 |
new_ucmd!() |
| 147 |
.arg("fields_3.txt") |
| 148 |
.arg("fields_2.txt") |
| 149 |
.arg("-1") |
| 150 |
.arg("2") |
| 151 |
.arg("-a") |
| 152 |
.arg("2") |
| 153 |
.succeeds() |
| 154 |
.stdout_only_fixture("unpaired_lines.expected"); |
| 155 |
|
| 156 |
new_ucmd!() |
| 157 |
.arg("fields_3.txt") |
| 158 |
.arg("fields_2.txt") |
| 159 |
.arg("-1") |
| 160 |
.arg("2") |
| 161 |
.arg("-a") |
| 162 |
.arg("1") |
| 163 |
.arg("-a") |
| 164 |
.arg("2") |
| 165 |
.succeeds() |
| 166 |
.stdout_only_fixture("unpaired_lines_outer.expected"); |
| 167 |
} |
| 168 |
|
| 169 |
#[test] |
| 170 |
fn suppress_joined() { |
| 171 |
new_ucmd!() |
| 172 |
.arg("fields_3.txt") |
| 173 |
.arg("fields_2.txt") |
| 174 |
.arg("-1") |
| 175 |
.arg("2") |
| 176 |
.arg("-v") |
| 177 |
.arg("2") |
| 178 |
.succeeds() |
| 179 |
.stdout_only_fixture("suppress_joined.expected"); |
| 180 |
|
| 181 |
new_ucmd!() |
| 182 |
.arg("fields_3.txt") |
| 183 |
.arg("fields_2.txt") |
| 184 |
.arg("-1") |
| 185 |
.arg("2") |
| 186 |
.arg("-a") |
| 187 |
.arg("1") |
| 188 |
.arg("-v") |
| 189 |
.arg("2") |
| 190 |
.succeeds() |
| 191 |
.stdout_only_fixture("suppress_joined_outer.expected"); |
| 192 |
} |
| 193 |
|
| 194 |
#[test] |
| 195 |
fn case_insensitive() { |
| 196 |
new_ucmd!() |
| 197 |
.arg("capitalized.txt") |
| 198 |
.arg("fields_3.txt") |
| 199 |
.arg("-i") |
| 200 |
.succeeds() |
| 201 |
.stdout_only_fixture("case_insensitive.expected"); |
| 202 |
} |
| 203 |
|
| 204 |
#[test] |
| 205 |
fn semicolon_separated() { |
| 206 |
new_ucmd!() |
| 207 |
.arg("semicolon_fields_1.txt") |
| 208 |
.arg("semicolon_fields_2.txt") |
| 209 |
.arg("-t") |
| 210 |
.arg(";") |
| 211 |
.succeeds() |
| 212 |
.stdout_only_fixture("semicolon_separated.expected"); |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn new_line_separated() { |
| 217 |
new_ucmd!() |
| 218 |
.arg("-") |
| 219 |
.arg("fields_2.txt") |
| 220 |
.arg("-t") |
| 221 |
.arg("") |
| 222 |
.pipe_in("1 a\n1 b\n8 h\n") |
| 223 |
.succeeds() |
| 224 |
.stdout_only("1 a\n8 h\n"); |
| 225 |
} |
| 226 |
|
| 227 |
#[test] |
| 228 |
fn tab_multi_character() { |
| 229 |
new_ucmd!() |
| 230 |
.arg("semicolon_fields_1.txt") |
| 231 |
.arg("semicolon_fields_2.txt") |
| 232 |
.arg("-t") |
| 233 |
.arg("ab") |
| 234 |
.fails() |
| 235 |
.stderr_is("join: multi-character tab 'ab'\n"); |
| 236 |
} |
| 237 |
|
| 238 |
#[test] |
| 239 |
fn tab_hyphen_leading_as_separate_arg() { |
| 240 |
// A hyphen-leading separator value passed as its own argument (not |
| 241 |
// attached with `-t-x`/`=`) must not be mistaken for a new, |
| 242 |
// unrecognized flag. |
| 243 |
new_ucmd!() |
| 244 |
.arg("semicolon_fields_1.txt") |
| 245 |
.arg("semicolon_fields_2.txt") |
| 246 |
.arg("-t") |
| 247 |
.arg("-x") |
| 248 |
.fails() |
| 249 |
.stderr_is("join: multi-character tab '-x'\n"); |
| 250 |
} |
| 251 |
|
| 252 |
#[test] |
| 253 |
fn default_format() { |
| 254 |
new_ucmd!() |
| 255 |
.arg("fields_1.txt") |
| 256 |
.arg("fields_2.txt") |
| 257 |
.arg("-o") |
| 258 |
.arg("1.1 2.2") |
| 259 |
.succeeds() |
| 260 |
.stdout_only_fixture("default.expected"); |
| 261 |
|
| 262 |
new_ucmd!() |
| 263 |
.arg("fields_1.txt") |
| 264 |
.arg("fields_2.txt") |
| 265 |
.arg("-o") |
| 266 |
.arg("0 2.2") |
| 267 |
.succeeds() |
| 268 |
.stdout_only_fixture("default.expected"); |
| 269 |
} |
| 270 |
|
| 271 |
#[test] |
| 272 |
fn repeated_o_accumulates_fields() { |
| 273 |
// GNU lets -o repeat; the fields accumulate in the order they appear, |
| 274 |
// here giving the reverse of the default format. |
| 275 |
new_ucmd!() |
| 276 |
.arg("fields_1.txt") |
| 277 |
.arg("fields_2.txt") |
| 278 |
.arg("-o") |
| 279 |
.arg("2.2") |
| 280 |
.arg("-o") |
| 281 |
.arg("1.1") |
| 282 |
.succeeds() |
| 283 |
.stdout_only("a 1\nb 2\nc 3\ne 5\nh 8\n"); |
| 284 |
} |
| 285 |
|
| 286 |
#[test] |
| 287 |
fn repeated_o_ignores_auto_when_mixed() { |
| 288 |
// When -o is repeated and not every value is `auto`, each `auto` is |
| 289 |
// ignored: only the explicit fields are printed, repeats included. |
| 290 |
new_ucmd!() |
| 291 |
.arg("fields_1.txt") |
| 292 |
.arg("fields_2.txt") |
| 293 |
.arg("-o") |
| 294 |
.arg("auto") |
| 295 |
.arg("-o") |
| 296 |
.arg("2.2 2.2 1.1") |
| 297 |
.succeeds() |
| 298 |
.stdout_only("a a 1\nb b 2\nc c 3\ne e 5\nh h 8\n"); |
| 299 |
} |
| 300 |
|
| 301 |
#[test] |
| 302 |
fn repeated_o_auto_stays_auto() { |
| 303 |
// `auto` still applies when every -o value is `auto`. |
| 304 |
new_ucmd!() |
| 305 |
.arg("fields_1.txt") |
| 306 |
.arg("fields_2.txt") |
| 307 |
.arg("-o") |
| 308 |
.arg("auto") |
| 309 |
.arg("-o") |
| 310 |
.arg("auto") |
| 311 |
.succeeds() |
| 312 |
.stdout_only_fixture("default.expected"); |
| 313 |
} |
| 314 |
|
| 315 |
#[test] |
| 316 |
fn unpaired_lines_format() { |
| 317 |
new_ucmd!() |
| 318 |
.arg("fields_2.txt") |
| 319 |
.arg("fields_3.txt") |
| 320 |
.arg("-a") |
| 321 |
.arg("2") |
| 322 |
.arg("-o") |
| 323 |
.arg("1.2 1.1 2.4 2.3 2.2 0") |
| 324 |
.succeeds() |
| 325 |
.stdout_only_fixture("unpaired_lines_format.expected"); |
| 326 |
} |
| 327 |
|
| 328 |
#[test] |
| 329 |
fn autoformat() { |
| 330 |
new_ucmd!() |
| 331 |
.arg("fields_2.txt") |
| 332 |
.arg("different_lengths.txt") |
| 333 |
.arg("-o") |
| 334 |
.arg("auto") |
| 335 |
.succeeds() |
| 336 |
.stdout_only_fixture("autoformat.expected"); |
| 337 |
|
| 338 |
new_ucmd!() |
| 339 |
.arg("-") |
| 340 |
.arg("fields_2.txt") |
| 341 |
.arg("-o") |
| 342 |
.arg("auto") |
| 343 |
.pipe_in("1 x y z\n2 p") |
| 344 |
.succeeds() |
| 345 |
.stdout_only("1 x y z a\n2 p b\n"); |
| 346 |
|
| 347 |
new_ucmd!() |
| 348 |
.arg("-") |
| 349 |
.arg("fields_2.txt") |
| 350 |
.arg("-a") |
| 351 |
.arg("1") |
| 352 |
.arg("-o") |
| 353 |
.arg("auto") |
| 354 |
.arg("-e") |
| 355 |
.arg(".") |
| 356 |
.pipe_in("1 x y z\n2 p\n99 a b\n") |
| 357 |
.succeeds() |
| 358 |
.stdout_only("1 x y z a\n2 p . . b\n99 a b . .\n"); |
| 359 |
} |
| 360 |
|
| 361 |
#[test] |
| 362 |
fn empty_format() { |
| 363 |
new_ucmd!() |
| 364 |
.arg("fields_1.txt") |
| 365 |
.arg("fields_2.txt") |
| 366 |
.arg("-o") |
| 367 |
.arg("") |
| 368 |
.fails() |
| 369 |
.stderr_is("join: invalid file number in field spec: ''\n"); |
| 370 |
} |
| 371 |
|
| 372 |
#[test] |
| 373 |
fn empty_key() { |
| 374 |
new_ucmd!() |
| 375 |
.arg("fields_1.txt") |
| 376 |
.arg("empty.txt") |
| 377 |
.arg("-j") |
| 378 |
.arg("2") |
| 379 |
.arg("-a") |
| 380 |
.arg("1") |
| 381 |
.arg("-e") |
| 382 |
.arg("x") |
| 383 |
.succeeds() |
| 384 |
.stdout_only_fixture("empty_key.expected"); |
| 385 |
} |
| 386 |
|
| 387 |
#[test] |
| 388 |
fn missing_format_fields() { |
| 389 |
new_ucmd!() |
| 390 |
.arg("fields_2.txt") |
| 391 |
.arg("different_lengths.txt") |
| 392 |
.arg("-o") |
| 393 |
.arg("0 1.2 2.4") |
| 394 |
.arg("-e") |
| 395 |
.arg("x") |
| 396 |
.succeeds() |
| 397 |
.stdout_only_fixture("missing_format_fields.expected"); |
| 398 |
} |
| 399 |
|
| 400 |
#[test] |
| 401 |
fn empty_fields_use_empty_filler() { |
| 402 |
let ts = TestScenario::new(util_name!()); |
| 403 |
let at = &ts.fixtures; |
| 404 |
|
| 405 |
// A blank line splits into a single empty field, so the join field is |
| 406 |
// present but zero length rather than missing. |
| 407 |
at.write("blank", "hello\n\n\n"); |
| 408 |
|
| 409 |
ts.ucmd() |
| 410 |
.args(&["-e", "EMPTY", "blank", "blank"]) |
| 411 |
.succeeds() |
| 412 |
.stdout_only("hello\nEMPTY\nEMPTY\nEMPTY\nEMPTY\n"); |
| 413 |
|
| 414 |
// The same holds for a line containing only whitespace. |
| 415 |
at.write("spaces", " \n"); |
| 416 |
|
| 417 |
ts.ucmd() |
| 418 |
.args(&["-e", "EMPTY", "-o", "0,1.1", "spaces", "spaces"]) |
| 419 |
.succeeds() |
| 420 |
.stdout_only("EMPTY EMPTY\n"); |
| 421 |
|
| 422 |
// A field between two adjacent separators is also present but empty. |
| 423 |
at.write("gap", "a,,b\n"); |
| 424 |
|
| 425 |
ts.ucmd() |
| 426 |
.args(&[ |
| 427 |
"-t", |
| 428 |
",", |
| 429 |
"-e", |
| 430 |
"EMPTY", |
| 431 |
"-o", |
| 432 |
"0,1.1,1.2,1.3", |
| 433 |
"gap", |
| 434 |
"gap", |
| 435 |
]) |
| 436 |
.succeeds() |
| 437 |
.stdout_only("a,a,EMPTY,b\n"); |
| 438 |
|
| 439 |
ts.ucmd() |
| 440 |
.args(&["-t", ",", "-e", "EMPTY", "gap", "gap"]) |
| 441 |
.succeeds() |
| 442 |
.stdout_only("a,EMPTY,b,EMPTY,b\n"); |
| 443 |
} |
| 444 |
|
| 445 |
#[test] |
| 446 |
fn empty_fields_kept_without_empty_filler() { |
| 447 |
let ts = TestScenario::new(util_name!()); |
| 448 |
let at = &ts.fixtures; |
| 449 |
|
| 450 |
at.write("gap", "a,,b\n"); |
| 451 |
|
| 452 |
// Without -e an empty field stays empty. |
| 453 |
ts.ucmd() |
| 454 |
.args(&["-t", ",", "-o", "0,1.1,1.2,1.3", "gap", "gap"]) |
| 455 |
.succeeds() |
| 456 |
.stdout_only("a,a,,b\n"); |
| 457 |
|
| 458 |
// Passing an empty string to -e is equivalent to not passing it at all. |
| 459 |
ts.ucmd() |
| 460 |
.args(&["-t", ",", "-e", "", "-o", "0,1.1,1.2,1.3", "gap", "gap"]) |
| 461 |
.succeeds() |
| 462 |
.stdout_only("a,a,,b\n"); |
| 463 |
} |
| 464 |
|
| 465 |
#[test] |
| 466 |
fn nocheck_order() { |
| 467 |
new_ucmd!() |
| 468 |
.arg("fields_1.txt") |
| 469 |
.arg("fields_2.txt") |
| 470 |
.arg("--nocheck-order") |
| 471 |
.succeeds() |
| 472 |
.stdout_only_fixture("default.expected"); |
| 473 |
} |
| 474 |
|
| 475 |
#[test] |
| 476 |
fn wrong_line_order() { |
| 477 |
let ts = TestScenario::new(util_name!()); |
| 478 |
new_ucmd!() |
| 479 |
.arg("fields_2.txt") |
| 480 |
.arg("fields_4.txt") |
| 481 |
.fails() |
| 482 |
.stdout_contains("7 g f 4 fg") |
| 483 |
.stderr_is(format!( |
| 484 |
"{0}: fields_4.txt:5: is not sorted: 11 g 5 gh\n{0}: input is not in sorted order\n", |
| 485 |
ts.util_name |
| 486 |
)); |
| 487 |
|
| 488 |
new_ucmd!() |
| 489 |
.arg("--check-order") |
| 490 |
.arg("fields_2.txt") |
| 491 |
.arg("fields_4.txt") |
| 492 |
.fails() |
| 493 |
.stdout_does_not_contain("7 g f 4 fg") |
| 494 |
.stderr_is(format!( |
| 495 |
"{0}: fields_4.txt:5: is not sorted: 11 g 5 gh\n", |
| 496 |
ts.util_name |
| 497 |
)); |
| 498 |
} |
| 499 |
|
| 500 |
#[test] |
| 501 |
fn both_files_wrong_line_order() { |
| 502 |
let ts = TestScenario::new(util_name!()); |
| 503 |
new_ucmd!() |
| 504 |
.arg("fields_4.txt") |
| 505 |
.arg("fields_5.txt") |
| 506 |
.fails() |
| 507 |
.stdout_contains("5 e 3 ef") |
| 508 |
.stderr_is(format!( |
| 509 |
"{0}: fields_5.txt:4: is not sorted: 3\n{0}: fields_4.txt:5: is not sorted: 11 g 5 gh\n{0}: input is not in sorted order\n", |
| 510 |
ts.util_name |
| 511 |
)); |
| 512 |
|
| 513 |
new_ucmd!() |
| 514 |
.arg("--check-order") |
| 515 |
.arg("fields_4.txt") |
| 516 |
.arg("fields_5.txt") |
| 517 |
.fails() |
| 518 |
.stdout_does_not_contain("5 e 3 ef") |
| 519 |
.stderr_is(format!( |
| 520 |
"{0}: fields_5.txt:4: is not sorted: 3\n", |
| 521 |
ts.util_name |
| 522 |
)); |
| 523 |
} |
| 524 |
|
| 525 |
#[test] |
| 526 |
fn headers() { |
| 527 |
new_ucmd!() |
| 528 |
.arg("header_1.txt") |
| 529 |
.arg("header_2.txt") |
| 530 |
.arg("--header") |
| 531 |
.succeeds() |
| 532 |
.stdout_only_fixture("header.expected"); |
| 533 |
} |
| 534 |
|
| 535 |
#[test] |
| 536 |
fn headers_autoformat() { |
| 537 |
new_ucmd!() |
| 538 |
.arg("header_1.txt") |
| 539 |
.arg("header_2.txt") |
| 540 |
.arg("--header") |
| 541 |
.arg("-o") |
| 542 |
.arg("auto") |
| 543 |
.succeeds() |
| 544 |
.stdout_only_fixture("header_autoformat.expected"); |
| 545 |
} |
| 546 |
|
| 547 |
#[test] |
| 548 |
fn single_file_with_header() { |
| 549 |
new_ucmd!() |
| 550 |
.arg("capitalized.txt") |
| 551 |
.arg("empty.txt") |
| 552 |
.arg("--header") |
| 553 |
.succeeds() |
| 554 |
.stdout_is("A 1\n"); |
| 555 |
|
| 556 |
new_ucmd!() |
| 557 |
.arg("empty.txt") |
| 558 |
.arg("capitalized.txt") |
| 559 |
.arg("--header") |
| 560 |
.succeeds() |
| 561 |
.stdout_is("A 1\n"); |
| 562 |
} |
| 563 |
|
| 564 |
#[test] |
| 565 |
fn non_line_feeds() { |
| 566 |
new_ucmd!() |
| 567 |
.arg("non-line_feeds_1.txt") |
| 568 |
.arg("non-line_feeds_2.txt") |
| 569 |
.succeeds() |
| 570 |
.stdout_only_fixture("non-line_feeds.expected"); |
| 571 |
} |
| 572 |
|
| 573 |
#[test] |
| 574 |
fn non_unicode() { |
| 575 |
new_ucmd!() |
| 576 |
.arg("non-unicode_1.bin") |
| 577 |
.arg("non-unicode_2.bin") |
| 578 |
.succeeds() |
| 579 |
.stdout_only_fixture("non-unicode.expected"); |
| 580 |
|
| 581 |
#[cfg(unix)] |
| 582 |
{ |
| 583 |
let non_utf8_byte: u8 = 167; |
| 584 |
new_ucmd!() |
| 585 |
.arg("-t") |
| 586 |
.arg(OsStr::from_bytes(&[non_utf8_byte])) |
| 587 |
.arg("non-unicode_1.bin") |
| 588 |
.arg("non-unicode_2.bin") |
| 589 |
.succeeds() |
| 590 |
.stdout_only_fixture("non-unicode_sep.expected"); |
| 591 |
|
| 592 |
new_ucmd!() |
| 593 |
.arg("-t") |
| 594 |
.arg(OsStr::from_bytes(&[non_utf8_byte, non_utf8_byte])) |
| 595 |
.arg("non-unicode_1.bin") |
| 596 |
.arg("non-unicode_2.bin") |
| 597 |
.fails() |
| 598 |
.stderr_is("join: non-UTF-8 multi-byte tab\n"); |
| 599 |
} |
| 600 |
|
| 601 |
#[cfg(windows)] |
| 602 |
{ |
| 603 |
let invalid_utf16: OsString = OsStringExt::from_wide(&[0xD800]); |
| 604 |
new_ucmd!() |
| 605 |
.arg("-t") |
| 606 |
.arg(&invalid_utf16) |
| 607 |
.arg("non-unicode_1.bin") |
| 608 |
.arg("non-unicode_2.bin") |
| 609 |
.fails() |
| 610 |
.stderr_is( |
| 611 |
"join: unprintable field separators are only supported on unix-like platforms\n", |
| 612 |
); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
#[test] |
| 617 |
fn multibyte_sep() { |
| 618 |
new_ucmd!() |
| 619 |
.arg("-tยง") |
| 620 |
.arg("multibyte_sep_1.txt") |
| 621 |
.arg("multibyte_sep_2.txt") |
| 622 |
.succeeds() |
| 623 |
.stdout_only_fixture("multibyte_sep.expected"); |
| 624 |
} |
| 625 |
|
| 626 |
#[test] |
| 627 |
fn null_field_separators() { |
| 628 |
new_ucmd!() |
| 629 |
.arg("-t") |
| 630 |
.arg("\\0") |
| 631 |
.arg("non-unicode_1.bin") |
| 632 |
.arg("non-unicode_2.bin") |
| 633 |
.succeeds() |
| 634 |
.stdout_only_fixture("null-sep.expected"); |
| 635 |
} |
| 636 |
|
| 637 |
#[test] |
| 638 |
fn null_line_endings() { |
| 639 |
new_ucmd!() |
| 640 |
.arg("-z") |
| 641 |
.arg("non-unicode_1.bin") |
| 642 |
.arg("non-unicode_2.bin") |
| 643 |
.succeeds() |
| 644 |
.stdout_only_fixture("z.expected"); |
| 645 |
} |
| 646 |
|
| 647 |
#[test] |
| 648 |
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "netbsd"))] |
| 649 |
fn test_full() { |
| 650 |
let dev_full = OpenOptions::new().write(true).open("/dev/full").unwrap(); |
| 651 |
new_ucmd!() |
| 652 |
.arg("fields_1.txt") |
| 653 |
.arg("fields_2.txt") |
| 654 |
.set_stdout(dev_full) |
| 655 |
.fails() |
| 656 |
.stderr_contains("No space left on device"); |
| 657 |
} |
| 658 |
|
| 659 |
#[test] |
| 660 |
#[cfg(target_os = "linux")] |
| 661 |
fn test_join_non_utf8_paths() { |
| 662 |
use std::fs::File; |
| 663 |
use std::io::Write; |
| 664 |
|
| 665 |
let ts = TestScenario::new(util_name!()); |
| 666 |
let test_dir = ts.fixtures.subdir.as_path(); |
| 667 |
|
| 668 |
// Create files directly with non-UTF-8 names |
| 669 |
let file1_bytes = b"test_\xFF\xFE_1.txt"; |
| 670 |
let file2_bytes = b"test_\xFF\xFE_2.txt"; |
| 671 |
|
| 672 |
#[cfg(unix)] |
| 673 |
{ |
| 674 |
use std::os::unix::ffi::OsStrExt; |
| 675 |
let file1_name = OsStr::from_bytes(file1_bytes); |
| 676 |
let file2_name = OsStr::from_bytes(file2_bytes); |
| 677 |
|
| 678 |
let mut file1 = File::create(test_dir.join(file1_name)).unwrap(); |
| 679 |
file1.write_all(b"a 1\n").unwrap(); |
| 680 |
|
| 681 |
let mut file2 = File::create(test_dir.join(file2_name)).unwrap(); |
| 682 |
file2.write_all(b"a 2\n").unwrap(); |
| 683 |
|
| 684 |
ts.ucmd() |
| 685 |
.arg(file1_name) |
| 686 |
.arg(file2_name) |
| 687 |
.succeeds() |
| 688 |
.stdout_only("a 1 2\n"); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
#[test] |
| 693 |
fn join_emoji_delim_inner_key() { |
| 694 |
let ts = TestScenario::new(util_name!()); |
| 695 |
let at = &ts.fixtures; |
| 696 |
|
| 697 |
at.write("file1", "a๐ฟb\n"); |
| 698 |
at.write("file2", "u๐ฟb\n"); |
| 699 |
|
| 700 |
ts.ucmd() |
| 701 |
.args(&["-t๐ฟ", "-1", "2", "-2", "2", "file1", "file2"]) |
| 702 |
.succeeds() |
| 703 |
.stdout_only("b๐ฟa๐ฟu\n"); |
| 704 |
} |
| 705 |
|
| 706 |
#[cfg(unix)] |
| 707 |
#[test] |
| 708 |
fn test_locale_collation() { |
| 709 |
let ts = TestScenario::new(util_name!()); |
| 710 |
let at = &ts.fixtures; |
| 711 |
|
| 712 |
at.write("f1.sorted", "abc:d 2\nab:d 1\n"); |
| 713 |
at.write("f2.sorted", "abc:d y\nab:d x\n"); |
| 714 |
|
| 715 |
ts.ucmd() |
| 716 |
.env("LC_ALL", "en_US.UTF-8") |
| 717 |
.arg("--check-order") |
| 718 |
.arg("f1.sorted") |
| 719 |
.arg("f2.sorted") |
| 720 |
.succeeds() |
| 721 |
.stdout_contains("abc:d 2 y") |
| 722 |
.stdout_contains("ab:d 1 x"); |
| 723 |
} |
| 724 |
|
| 725 |
#[test] |
| 726 |
fn test_incompatible_fields_reports_exact_field_number() { |
| 727 |
// An out-of-range field clamps to usize::MAX, which used to overflow the |
| 728 |
// one-based increment. Field numbers past 2^53 also used to be rounded on |
| 729 |
// their way through the localization layer. |
| 730 |
// |
| 731 |
// `parse_field_number` uses `usize`, so a value at or above `usize::MAX` |
| 732 |
// saturates to it. The saturation ceiling is therefore pointer-width |
| 733 |
// dependent, and the expected text is built from `usize::MAX` rather than a |
| 734 |
// hard-coded 64-bit literal. |
| 735 |
let max_field = usize::MAX.to_string(); |
| 736 |
|
| 737 |
// A small field number takes the i64 number path through the localization |
| 738 |
// layer and is platform-independent. |
| 739 |
new_ucmd!() |
| 740 |
.args(&["-j", "3", "-1", "5", "/dev/null", "/dev/null"]) |
| 741 |
.fails() |
| 742 |
.stderr_contains("incompatible join fields 3, 5"); |
| 743 |
|
| 744 |
// Values at or above `usize::MAX` saturate to `usize::MAX` on every |
| 745 |
// platform; the localization layer must carry that ceiling as an exact |
| 746 |
// decimal string rather than rounding it through Fluent's f64-backed number |
| 747 |
// type. |
| 748 |
for field in ["18446744073709551615", "99999999999999999999999"] { |
| 749 |
new_ucmd!() |
| 750 |
.args(&["-j", field, "-1", "5", "/dev/null", "/dev/null"]) |
| 751 |
.fails() |
| 752 |
.stderr_contains(format!("incompatible join fields {max_field}, 5")); |
| 753 |
} |
| 754 |
|
| 755 |
// A value above f64 precision (2^53) but below `usize::MAX` is reported |
| 756 |
// exactly on 64-bit (where `usize` holds it); on 32-bit it saturates to |
| 757 |
// `usize::MAX` like the cases above. |
| 758 |
#[cfg(target_pointer_width = "64")] |
| 759 |
let expected_above: String = "9007199254740993".to_string(); |
| 760 |
#[cfg(not(target_pointer_width = "64"))] |
| 761 |
let expected_above: String = max_field.clone(); |
| 762 |
new_ucmd!() |
| 763 |
.args(&[ |
| 764 |
"-j", |
| 765 |
"9007199254740993", |
| 766 |
"-1", |
| 767 |
"5", |
| 768 |
"/dev/null", |
| 769 |
"/dev/null", |
| 770 |
]) |
| 771 |
.fails() |
| 772 |
.stderr_contains(format!("incompatible join fields {expected_above}, 5")); |
| 773 |
} |
| 774 |
|
| 775 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 776 |
mod diagnostics { |
| 777 |
use super::*; |
| 778 |
|
| 779 |
#[cfg(unix)] |
| 780 |
#[test] |
| 781 |
fn test_snippet_points_at_the_failing_field_of_a_list() { |
| 782 |
let result = new_ucmd!() |
| 783 |
.terminal_sim_stderr() |
| 784 |
.args(&["-o", "1.2,2.x", "/dev/null", "/dev/null"]) |
| 785 |
.fails_with_code(1); |
| 786 |
|
| 787 |
// The first field is fine; only the second one is at fault. |
| 788 |
assert_eq!( |
| 789 |
result.stderr_as_displayed(), |
| 790 |
"\ |
| 791 |
join: invalid field number: 'x' |
| 792 |
โญโ[ join:1:13 ] |
| 793 |
โ |
| 794 |
1 โ join -o 1.2,2.x /dev/null /dev/null |
| 795 |
โ โโโ |
| 796 |
โ |
| 797 |
โ Help: an output field is FILENUM.FIELD, as in -o 1.2,2.1; 0 stands for the join field |
| 798 |
โโโโฏ" |
| 799 |
); |
| 800 |
} |
| 801 |
|
| 802 |
#[cfg(unix)] |
| 803 |
#[test] |
| 804 |
fn test_snippet_points_inside_a_glued_short_option() { |
| 805 |
let result = new_ucmd!() |
| 806 |
.terminal_sim_stderr() |
| 807 |
.args(&["-o1.2,0.4", "/dev/null", "/dev/null"]) |
| 808 |
.fails_with_code(1); |
| 809 |
let stderr = result.stderr_as_displayed(); |
| 810 |
|
| 811 |
assert!(stderr.contains("join:1:12"), "{stderr}"); |
| 812 |
assert!(stderr.contains("invalid field specifier"), "{stderr}"); |
| 813 |
} |
| 814 |
|
| 815 |
#[test] |
| 816 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 817 |
new_ucmd!() |
| 818 |
.args(&["-o", "1.2,2.x", "/dev/null", "/dev/null"]) |
| 819 |
.fails_with_code(1) |
| 820 |
.stderr_is("join: invalid field number: 'x'\n"); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
#[test] |
| 825 |
fn test_hyphen_leading_field_number_is_reported_as_invalid() { |
| 826 |
// GNU hands the argument after the option to that option even when it |
| 827 |
// starts with a hyphen, so it is reported as an invalid field number |
| 828 |
// rather than as an unknown option. |
| 829 |
for opt in ["-1", "-2", "-j"] { |
| 830 |
new_ucmd!() |
| 831 |
.args(&[opt, "-1", "empty.txt", "empty.txt"]) |
| 832 |
.fails() |
| 833 |
.stderr_contains("invalid field number: '-1'"); |
| 834 |
} |
| 835 |
} |