| 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 |
use uucore::display::Quotable; |
| 7 |
use uutests::new_ucmd; |
| 8 |
// spell-checker:ignore (ToDO) taaaa tbbbb tcccc |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn test_invalid_arg() { |
| 12 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 13 |
} |
| 14 |
|
| 15 |
#[test] |
| 16 |
fn test_with_tab() { |
| 17 |
new_ucmd!() |
| 18 |
.arg("with-tab.txt") |
| 19 |
.succeeds() |
| 20 |
.stdout_contains(" ") |
| 21 |
.stdout_does_not_contain("\t"); |
| 22 |
} |
| 23 |
|
| 24 |
#[test] |
| 25 |
fn test_with_trailing_tab() { |
| 26 |
new_ucmd!() |
| 27 |
.arg("with-trailing-tab.txt") |
| 28 |
.succeeds() |
| 29 |
.stdout_contains("with tabs=> ") |
| 30 |
.stdout_does_not_contain("\t"); |
| 31 |
} |
| 32 |
|
| 33 |
#[test] |
| 34 |
fn test_with_trailing_tab_i() { |
| 35 |
new_ucmd!() |
| 36 |
.arg("with-trailing-tab.txt") |
| 37 |
.arg("-i") |
| 38 |
.succeeds() |
| 39 |
.stdout_contains(" // with tabs=>\t"); |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn test_with_tab_size() { |
| 44 |
new_ucmd!() |
| 45 |
.arg("with-tab.txt") |
| 46 |
.arg("--tabs=10") |
| 47 |
.succeeds() |
| 48 |
.stdout_contains(" "); |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn test_with_space() { |
| 53 |
new_ucmd!() |
| 54 |
.arg("with-spaces.txt") |
| 55 |
.succeeds() |
| 56 |
.stdout_contains(" return"); |
| 57 |
} |
| 58 |
|
| 59 |
#[test] |
| 60 |
fn test_with_multiple_files() { |
| 61 |
new_ucmd!() |
| 62 |
.arg("with-spaces.txt") |
| 63 |
.arg("with-tab.txt") |
| 64 |
.succeeds() |
| 65 |
.stdout_contains(" return") |
| 66 |
.stdout_contains(" "); |
| 67 |
} |
| 68 |
|
| 69 |
#[test] |
| 70 |
fn test_tabs() { |
| 71 |
for list in ["3 6 9", "3,6,9", "3\t6\t9", ", \t3,\t6 9,"] { |
| 72 |
new_ucmd!() |
| 73 |
.arg("--tabs") |
| 74 |
.arg(list) |
| 75 |
.pipe_in("a\tb\tc\td\te") |
| 76 |
.succeeds() |
| 77 |
.stdout_is("a b c d e"); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn test_multiple_tabs_args() { |
| 83 |
new_ucmd!() |
| 84 |
.args(&["--tabs=3", "--tabs=6", "--tabs=9"]) |
| 85 |
.pipe_in("a\tb\tc\td\te") |
| 86 |
.succeeds() |
| 87 |
.stdout_is("a b c d e"); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn test_tabs_empty_string() { |
| 92 |
new_ucmd!() |
| 93 |
.args(&["--tabs", ""]) |
| 94 |
.pipe_in("a\tb\tc") |
| 95 |
.succeeds() |
| 96 |
.stdout_is("a b c"); |
| 97 |
} |
| 98 |
|
| 99 |
#[test] |
| 100 |
fn test_tabs_comma_only() { |
| 101 |
new_ucmd!() |
| 102 |
.args(&["--tabs", ","]) |
| 103 |
.pipe_in("a\tb\tc") |
| 104 |
.succeeds() |
| 105 |
.stdout_is("a b c"); |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn test_tabs_space_only() { |
| 110 |
new_ucmd!() |
| 111 |
.args(&["--tabs", " "]) |
| 112 |
.pipe_in("a\tb\tc") |
| 113 |
.succeeds() |
| 114 |
.stdout_is("a b c"); |
| 115 |
} |
| 116 |
|
| 117 |
#[test] |
| 118 |
fn test_tabs_slash() { |
| 119 |
new_ucmd!() |
| 120 |
.args(&["--tabs", "/"]) |
| 121 |
.pipe_in("a\tb\tc") |
| 122 |
.succeeds() |
| 123 |
.stdout_is("a b c"); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn test_tabs_plus() { |
| 128 |
new_ucmd!() |
| 129 |
.args(&["--tabs", "+"]) |
| 130 |
.pipe_in("a\tb\tc") |
| 131 |
.succeeds() |
| 132 |
.stdout_is("a b c"); |
| 133 |
} |
| 134 |
|
| 135 |
#[test] |
| 136 |
fn test_tabs_trailing_slash() { |
| 137 |
new_ucmd!() |
| 138 |
.arg("--tabs=1,/5") |
| 139 |
.pipe_in("\ta\tb\tc") |
| 140 |
.succeeds() |
| 141 |
// 0 1 |
| 142 |
// 01234567890 |
| 143 |
.stdout_is(" a b c"); |
| 144 |
} |
| 145 |
|
| 146 |
#[test] |
| 147 |
fn test_tabs_trailing_slash_long_columns() { |
| 148 |
new_ucmd!() |
| 149 |
.arg("--tabs=1,/3") |
| 150 |
.pipe_in("\taaaa\tbbbb\tcccc") |
| 151 |
.succeeds() |
| 152 |
// 0 1 |
| 153 |
// 01234567890123456 |
| 154 |
.stdout_is(" aaaa bbbb cccc"); |
| 155 |
} |
| 156 |
|
| 157 |
#[test] |
| 158 |
fn test_tabs_trailing_plus() { |
| 159 |
new_ucmd!() |
| 160 |
.arg("--tabs=1,+5") |
| 161 |
.pipe_in("\ta\tb\tc") |
| 162 |
.succeeds() |
| 163 |
// 0 1 |
| 164 |
// 012345678901 |
| 165 |
.stdout_is(" a b c"); |
| 166 |
} |
| 167 |
|
| 168 |
#[test] |
| 169 |
fn test_tabs_trailing_plus_long_columns() { |
| 170 |
new_ucmd!() |
| 171 |
.arg("--tabs=1,+3") |
| 172 |
.pipe_in("\taaaa\tbbbb\tcccc") |
| 173 |
.succeeds() |
| 174 |
// 0 1 |
| 175 |
// 012345678901234567 |
| 176 |
.stdout_is(" aaaa bbbb cccc"); |
| 177 |
} |
| 178 |
|
| 179 |
#[test] |
| 180 |
fn test_tabs_must_be_ascending() { |
| 181 |
new_ucmd!() |
| 182 |
.arg("--tabs=1,1") |
| 183 |
.fails() |
| 184 |
.stderr_contains("tab sizes must be ascending"); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
fn test_tabs_cannot_be_zero() { |
| 189 |
new_ucmd!() |
| 190 |
.arg("--tabs=0") |
| 191 |
.fails() |
| 192 |
.stderr_contains("tab size cannot be 0"); |
| 193 |
} |
| 194 |
|
| 195 |
#[test] |
| 196 |
fn test_tabs_keep_last_trailing_specifier() { |
| 197 |
// If there are multiple trailing specifiers, use only the last one |
| 198 |
// before the number. |
| 199 |
new_ucmd!() |
| 200 |
.arg("--tabs=1,+/+/5") |
| 201 |
.pipe_in("\ta\tb\tc") |
| 202 |
.succeeds() |
| 203 |
// 0 1 |
| 204 |
// 01234567890 |
| 205 |
.stdout_is(" a b c"); |
| 206 |
} |
| 207 |
|
| 208 |
#[test] |
| 209 |
fn test_tabs_comma_separated_no_numbers() { |
| 210 |
new_ucmd!() |
| 211 |
.arg("--tabs=+,/,+,/") |
| 212 |
.pipe_in("\ta\tb\tc") |
| 213 |
.succeeds() |
| 214 |
.stdout_is(" a b c"); |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
fn test_tabs_with_specifier_not_at_start() { |
| 219 |
fn run_cmd(arg: &str, expected_prefix: &str, expected_suffix: &str) { |
| 220 |
let expected_msg = format!( |
| 221 |
"{} specifier not at start of number: {}", |
| 222 |
expected_prefix.quote(), |
| 223 |
expected_suffix.quote() |
| 224 |
); |
| 225 |
new_ucmd!().arg(arg).fails().stderr_contains(expected_msg); |
| 226 |
} |
| 227 |
run_cmd("--tabs=1/", "/", "/"); |
| 228 |
run_cmd("--tabs=1/2", "/", "/2"); |
| 229 |
run_cmd("--tabs=1+", "+", "+"); |
| 230 |
run_cmd("--tabs=1+2", "+", "+2"); |
| 231 |
} |
| 232 |
|
| 233 |
#[test] |
| 234 |
fn test_tabs_with_specifier_only_allowed_with_last_value() { |
| 235 |
fn run_cmd(arg: &str, specifier: &str) { |
| 236 |
let expected_msg = format!( |
| 237 |
"{} specifier only allowed with the last value", |
| 238 |
specifier.quote() |
| 239 |
); |
| 240 |
new_ucmd!().arg(arg).fails().stderr_contains(expected_msg); |
| 241 |
} |
| 242 |
run_cmd("--tabs=/1,2,3", "/"); |
| 243 |
run_cmd("--tabs=1,/2,3", "/"); |
| 244 |
new_ucmd!().arg("--tabs=1,2,/3").succeeds(); |
| 245 |
|
| 246 |
run_cmd("--tabs=+1,2,3", "+"); |
| 247 |
run_cmd("--tabs=1,+2,3", "+"); |
| 248 |
new_ucmd!().arg("--tabs=1,2,+3").succeeds(); |
| 249 |
} |
| 250 |
|
| 251 |
#[test] |
| 252 |
fn test_tabs_with_invalid_chars() { |
| 253 |
new_ucmd!() |
| 254 |
.arg("--tabs=x") |
| 255 |
.fails() |
| 256 |
.stderr_contains("tab size contains invalid character(s): 'x'"); |
| 257 |
new_ucmd!() |
| 258 |
.arg("--tabs=1x2") |
| 259 |
.fails() |
| 260 |
.stderr_contains("tab size contains invalid character(s): 'x2'"); |
| 261 |
} |
| 262 |
|
| 263 |
#[test] |
| 264 |
fn test_tabs_with_too_large_size() { |
| 265 |
let arg = format!("--tabs={}", u128::MAX); |
| 266 |
let expected_error = format!("tab stop is too large '{}'", u128::MAX); |
| 267 |
|
| 268 |
new_ucmd!().arg(arg).fails().stderr_contains(expected_error); |
| 269 |
} |
| 270 |
|
| 271 |
#[cfg(all(target_os = "linux", target_pointer_width = "64"))] |
| 272 |
#[cfg_attr( |
| 273 |
wasi_runner, |
| 274 |
ignore = "WASI runner target is not suitable for this address-space-limit regression test" |
| 275 |
)] |
| 276 |
#[test] |
| 277 |
fn test_large_tab_stop_without_tabs_does_not_allocate() { |
| 278 |
use rlimit::Resource; |
| 279 |
|
| 280 |
const AS_LIMIT: u64 = 200 * 1024 * 1024; |
| 281 |
|
| 282 |
new_ucmd!() |
| 283 |
.limit(Resource::AS, AS_LIMIT, AS_LIMIT) |
| 284 |
.arg("--tabs=267672676527678256") |
| 285 |
.pipe_in("hello\n") |
| 286 |
.succeeds() |
| 287 |
.stdout_is("hello\n"); |
| 288 |
} |
| 289 |
|
| 290 |
#[test] |
| 291 |
fn test_tabs_shortcut() { |
| 292 |
new_ucmd!() |
| 293 |
.args(&["-2", "-5", "-7"]) |
| 294 |
.pipe_in("\ta\tb\tc") |
| 295 |
.succeeds() |
| 296 |
// 01234567890 |
| 297 |
.stdout_is(" a b c"); |
| 298 |
} |
| 299 |
|
| 300 |
#[test] |
| 301 |
fn test_comma_separated_tabs_shortcut() { |
| 302 |
new_ucmd!() |
| 303 |
.args(&["-2,5", "-7"]) |
| 304 |
.pipe_in("\ta\tb\tc") |
| 305 |
.succeeds() |
| 306 |
// 01234567890 |
| 307 |
.stdout_is(" a b c"); |
| 308 |
} |
| 309 |
|
| 310 |
#[test] |
| 311 |
fn test_tabs_and_tabs_shortcut_mixed() { |
| 312 |
new_ucmd!() |
| 313 |
.args(&["-2", "--tabs=5", "-7"]) |
| 314 |
.pipe_in("\ta\tb\tc") |
| 315 |
.succeeds() |
| 316 |
// 01234567890 |
| 317 |
.stdout_is(" a b c"); |
| 318 |
} |
| 319 |
|
| 320 |
#[test] |
| 321 |
fn test_ignore_initial_plus() { |
| 322 |
new_ucmd!() |
| 323 |
.args(&["--tabs=+3"]) |
| 324 |
.pipe_in("\ta\tb\tc") |
| 325 |
.succeeds() |
| 326 |
// 01234567890 |
| 327 |
.stdout_is(" a b c"); |
| 328 |
} |
| 329 |
|
| 330 |
#[test] |
| 331 |
fn test_ignore_initial_pluses() { |
| 332 |
new_ucmd!() |
| 333 |
.args(&["--tabs=++3"]) |
| 334 |
.pipe_in("\ta\tb\tc") |
| 335 |
.succeeds() |
| 336 |
// 01234567890 |
| 337 |
.stdout_is(" a b c"); |
| 338 |
} |
| 339 |
|
| 340 |
#[test] |
| 341 |
fn test_ignore_initial_slash() { |
| 342 |
new_ucmd!() |
| 343 |
.args(&["--tabs=/3"]) |
| 344 |
.pipe_in("\ta\tb\tc") |
| 345 |
.succeeds() |
| 346 |
// 01234567890 |
| 347 |
.stdout_is(" a b c"); |
| 348 |
} |
| 349 |
|
| 350 |
#[test] |
| 351 |
fn test_ignore_initial_slashes() { |
| 352 |
new_ucmd!() |
| 353 |
.args(&["--tabs=//3"]) |
| 354 |
.pipe_in("\ta\tb\tc") |
| 355 |
.succeeds() |
| 356 |
// 01234567890 |
| 357 |
.stdout_is(" a b c"); |
| 358 |
} |
| 359 |
|
| 360 |
#[test] |
| 361 |
fn test_ignore_initial_plus_slash_combination() { |
| 362 |
new_ucmd!() |
| 363 |
.args(&["--tabs=+/3"]) |
| 364 |
.pipe_in("\ta\tb\tc") |
| 365 |
.succeeds() |
| 366 |
// 01234567890 |
| 367 |
.stdout_is(" a b c"); |
| 368 |
} |
| 369 |
|
| 370 |
#[test] |
| 371 |
fn test_comma_with_plus_1() { |
| 372 |
new_ucmd!() |
| 373 |
.args(&["--tabs=3,+6"]) |
| 374 |
.pipe_in("\t111\t222\t333") |
| 375 |
.succeeds() |
| 376 |
// 01234567890 |
| 377 |
.stdout_is(" 111 222 333"); |
| 378 |
} |
| 379 |
|
| 380 |
#[test] |
| 381 |
fn test_comma_with_plus_2() { |
| 382 |
new_ucmd!() |
| 383 |
.args(&["--tabs=1,+5"]) |
| 384 |
.pipe_in("\ta\tb\tc") |
| 385 |
.succeeds() |
| 386 |
// 01234567890 |
| 387 |
.stdout_is(" a b c"); |
| 388 |
} |
| 389 |
|
| 390 |
#[test] |
| 391 |
fn test_comma_with_plus_3() { |
| 392 |
new_ucmd!() |
| 393 |
.args(&["--tabs=2,+5"]) |
| 394 |
.pipe_in("a\tb\tc") |
| 395 |
.succeeds() |
| 396 |
// 01234567890 |
| 397 |
.stdout_is("a b c"); |
| 398 |
} |
| 399 |
|
| 400 |
#[test] |
| 401 |
fn test_comma_with_plus_4() { |
| 402 |
new_ucmd!() |
| 403 |
.args(&["--tabs=1,3,+5"]) |
| 404 |
.pipe_in("a\tb\tc") |
| 405 |
.succeeds() |
| 406 |
// 01234567890 |
| 407 |
.stdout_is("a b c"); |
| 408 |
} |
| 409 |
|
| 410 |
#[test] |
| 411 |
fn test_args_override() { |
| 412 |
new_ucmd!() |
| 413 |
.args(&["-i", "-i", "with-trailing-tab.txt"]) |
| 414 |
.succeeds() |
| 415 |
.stdout_is( |
| 416 |
"// !note: file contains significant whitespace |
| 417 |
// * indentation uses <TAB> characters |
| 418 |
int main() { |
| 419 |
// * next line has both a leading & trailing tab |
| 420 |
// with tabs=>\t |
| 421 |
return 0; |
| 422 |
} |
| 423 |
", |
| 424 |
); |
| 425 |
} |
| 426 |
|
| 427 |
#[test] |
| 428 |
fn test_expand_directory() { |
| 429 |
new_ucmd!() |
| 430 |
.args(&["."]) |
| 431 |
.fails() |
| 432 |
.stderr_contains("expand: .: Is a directory"); |
| 433 |
} |
| 434 |
|
| 435 |
#[test] |
| 436 |
fn test_nonexisting_file() { |
| 437 |
new_ucmd!() |
| 438 |
.args(&["nonexistent", "with-spaces.txt"]) |
| 439 |
.fails() |
| 440 |
.stderr_contains("expand: nonexistent: No such file or directory") |
| 441 |
.stdout_contains_line("// !note: file contains significant whitespace"); |
| 442 |
} |
| 443 |
|
| 444 |
#[test] |
| 445 |
#[cfg(all(target_os = "linux", not(target_env = "musl")))] |
| 446 |
#[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")] |
| 447 |
fn test_read_error() { |
| 448 |
new_ucmd!() |
| 449 |
.arg("/proc/self/mem") |
| 450 |
.fails() |
| 451 |
.stderr_contains("expand: /proc/self/mem: Input/output error"); |
| 452 |
} |
| 453 |
|
| 454 |
#[test] |
| 455 |
#[cfg(target_os = "linux")] |
| 456 |
#[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")] |
| 457 |
fn test_expand_non_utf8_paths() { |
| 458 |
use std::os::unix::ffi::OsStringExt; |
| 459 |
use uutests::at_and_ucmd; |
| 460 |
|
| 461 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 462 |
|
| 463 |
let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]); |
| 464 |
std::fs::write(at.plus(&filename), b"hello\tworld\ntest\tline\n").unwrap(); |
| 465 |
|
| 466 |
ucmd.arg(&filename) |
| 467 |
.succeeds() |
| 468 |
.stdout_is("hello world\ntest line\n"); |
| 469 |
} |
| 470 |
|
| 471 |
#[test] |
| 472 |
fn test_wide_multibyte_char_width() { |
| 473 |
// A tab following a wide character must expand to the next tab stop based |
| 474 |
// on the character's display width, not its byte length. Regression for a |
| 475 |
// bug where the UTF-8 sequence length was derived from the leading byte's |
| 476 |
// codepoint value, so 3- and 4-byte characters were mis-measured. |
| 477 |
// U+4E2D (中, 3 bytes, width 2): col 0->2, tab pads 6 spaces to column 8. |
| 478 |
new_ucmd!() |
| 479 |
.pipe_in("\u{4E2D}\t|".as_bytes()) |
| 480 |
.succeeds() |
| 481 |
.stdout_is("\u{4E2D} |"); |
| 482 |
// U+1F600 (😀, 4 bytes, width 2) behaves the same. |
| 483 |
new_ucmd!() |
| 484 |
.pipe_in("\u{1F600}\t|".as_bytes()) |
| 485 |
.succeeds() |
| 486 |
.stdout_is("\u{1F600} |"); |
| 487 |
} |
| 488 |
|
| 489 |
#[test] |
| 490 |
fn test_buffered_reads_new_line_no_tabs_in_first_chunk() { |
| 491 |
// test includes tabs after 1 full chunk of no tabs with new line |
| 492 |
// checks that the tabstop calculation is done to correct column |
| 493 |
new_ucmd!() |
| 494 |
.args(&["new_line_in_chunk.txt"]) |
| 495 |
.succeeds() |
| 496 |
.stdout_is_fixture("new_line_in_chunk_expected.txt"); |
| 497 |
} |