| 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 (ToDO) Sdivide ading |
| 7 |
|
| 8 |
use jiff::{Timestamp, ToSpan}; |
| 9 |
use regex::Regex; |
| 10 |
use std::fs::metadata; |
| 11 |
use uutests::util::UCommand; |
| 12 |
use uutests::{at_and_ucmd, new_ucmd}; |
| 13 |
|
| 14 |
const DATE_TIME_FORMAT_DEFAULT: &str = "%Y-%m-%d %H:%M"; |
| 15 |
|
| 16 |
fn file_last_modified_time_format(ucmd: &UCommand, path: &str, format: &str) -> String { |
| 17 |
let tmp_dir_path = ucmd.get_full_fixture_path(path); |
| 18 |
metadata(tmp_dir_path) |
| 19 |
.and_then(|meta| meta.modified()) |
| 20 |
.map(|mtime| { |
| 21 |
let dt: Timestamp = mtime.try_into().unwrap(); |
| 22 |
dt.strftime(format).to_string() |
| 23 |
}) |
| 24 |
.unwrap_or_default() |
| 25 |
} |
| 26 |
|
| 27 |
fn file_last_modified_time(ucmd: &UCommand, path: &str) -> String { |
| 28 |
file_last_modified_time_format(ucmd, path, DATE_TIME_FORMAT_DEFAULT) |
| 29 |
} |
| 30 |
|
| 31 |
fn all_minutes(from: Timestamp, to: Timestamp) -> Vec<String> { |
| 32 |
let to = to + 1.minute(); |
| 33 |
let mut vec = vec![]; |
| 34 |
let mut current = from; |
| 35 |
while current < to { |
| 36 |
vec.push(current.strftime(DATE_TIME_FORMAT_DEFAULT).to_string()); |
| 37 |
current += 1.minute(); |
| 38 |
} |
| 39 |
vec |
| 40 |
} |
| 41 |
|
| 42 |
fn valid_last_modified_template_vars(from: Timestamp) -> Vec<Vec<(String, String)>> { |
| 43 |
all_minutes(from, Timestamp::now()) |
| 44 |
.into_iter() |
| 45 |
.map(|time| vec![("{last_modified_time}".to_string(), time)]) |
| 46 |
.collect() |
| 47 |
} |
| 48 |
|
| 49 |
#[test] |
| 50 |
fn test_invalid_flag() { |
| 51 |
new_ucmd!() |
| 52 |
.arg("--invalid-argument") |
| 53 |
.fails_with_code(1) |
| 54 |
.no_stdout(); |
| 55 |
} |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn test_expand_tabs_multibyte_char_is_rejected() { |
| 59 |
for arg in ["-e€", "-e€3"] { |
| 60 |
new_ucmd!() |
| 61 |
.args(&[arg, "test_one_page.log"]) |
| 62 |
.fails_with_code(1) |
| 63 |
.stderr_contains("pr: '-e' extra characters or invalid number in the argument"); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
#[test] |
| 68 |
fn test_number_lines_multibyte_separator_is_rejected() { |
| 69 |
for arg in ["-n€", "-n€5"] { |
| 70 |
new_ucmd!() |
| 71 |
.args(&[arg, "test_one_page.log"]) |
| 72 |
.fails_with_code(1) |
| 73 |
.stderr_contains("pr: '-n' extra characters or invalid number in the argument"); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn test_number_lines_empty_value_is_rejected() { |
| 79 |
new_ucmd!() |
| 80 |
.args(&["--number-lines=", "test_one_page.log"]) |
| 81 |
.fails_with_code(1) |
| 82 |
.stderr_contains("pr: '-n' extra characters or invalid number in the argument"); |
| 83 |
} |
| 84 |
|
| 85 |
#[test] |
| 86 |
fn test_number_lines_without_value_numbers_lines() { |
| 87 |
// A bare -n/--number-lines numbers lines with the default 5-wide, tab format. |
| 88 |
for arg in ["-n", "--number-lines"] { |
| 89 |
new_ucmd!() |
| 90 |
.args(&["-t", arg]) |
| 91 |
.pipe_in("a\nb\n") |
| 92 |
.succeeds() |
| 93 |
.stdout_is(" 1\ta\n 2\tb\n"); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
#[test] |
| 98 |
fn test_without_any_options() { |
| 99 |
let test_file_path = "test_one_page.log"; |
| 100 |
let expected_test_file_path = "test_one_page.log.expected"; |
| 101 |
let mut scenario = new_ucmd!(); |
| 102 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 103 |
scenario |
| 104 |
.args(&[test_file_path]) |
| 105 |
.succeeds() |
| 106 |
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); |
| 107 |
} |
| 108 |
|
| 109 |
#[test] |
| 110 |
fn test_with_numbering_option_with_number_width() { |
| 111 |
let test_file_path = "test_num_page.log"; |
| 112 |
let expected_test_file_path = "test_num_page_2.log.expected"; |
| 113 |
let mut scenario = new_ucmd!(); |
| 114 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 115 |
scenario |
| 116 |
.args(&["-n", "2", test_file_path]) |
| 117 |
.succeeds() |
| 118 |
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn test_with_long_header_option() { |
| 123 |
let whitespace = " ".repeat(21); |
| 124 |
let blank_lines = "\n".repeat(61); |
| 125 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 126 |
let pattern = |
| 127 |
format!("\n\n{datetime_pattern}{whitespace}new file{whitespace}Page 1\n\n\na{blank_lines}"); |
| 128 |
let regex = Regex::new(&pattern).unwrap(); |
| 129 |
new_ucmd!() |
| 130 |
.args(&["-h", "new file"]) |
| 131 |
.pipe_in("a") |
| 132 |
.succeeds() |
| 133 |
.stdout_matches(®ex); |
| 134 |
new_ucmd!() |
| 135 |
.args(&["--header=new file"]) |
| 136 |
.pipe_in("a") |
| 137 |
.succeeds() |
| 138 |
.stdout_matches(®ex); |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn test_with_double_space_option() { |
| 143 |
let test_file_path = "test_one_page.log"; |
| 144 |
let expected_test_file_path = "test_one_page_double_line.log.expected"; |
| 145 |
for arg in ["-d", "--double-space"] { |
| 146 |
let mut scenario = new_ucmd!(); |
| 147 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 148 |
scenario |
| 149 |
.args(&[arg, test_file_path]) |
| 150 |
.succeeds() |
| 151 |
.stdout_is_templated_fixture( |
| 152 |
expected_test_file_path, |
| 153 |
&[("{last_modified_time}", &value)], |
| 154 |
); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
fn test_with_first_line_number_option() { |
| 160 |
let test_file_path = "test_one_page.log"; |
| 161 |
let expected_test_file_path = "test_one_page_first_line.log.expected"; |
| 162 |
let mut scenario = new_ucmd!(); |
| 163 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 164 |
scenario |
| 165 |
.args(&["-N", "5", "-n", test_file_path]) |
| 166 |
.succeeds() |
| 167 |
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); |
| 168 |
} |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn test_with_first_line_number_long_option() { |
| 172 |
let test_file_path = "test_one_page.log"; |
| 173 |
let expected_test_file_path = "test_one_page_first_line.log.expected"; |
| 174 |
let mut scenario = new_ucmd!(); |
| 175 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 176 |
scenario |
| 177 |
.args(&["--first-line-number=5", "-n", test_file_path]) |
| 178 |
.succeeds() |
| 179 |
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); |
| 180 |
} |
| 181 |
|
| 182 |
#[test] |
| 183 |
fn test_with_number_option_with_custom_separator_char() { |
| 184 |
let test_file_path = "test_num_page.log"; |
| 185 |
let expected_test_file_path = "test_num_page_char.log.expected"; |
| 186 |
let mut scenario = new_ucmd!(); |
| 187 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 188 |
scenario |
| 189 |
.args(&["-nc", test_file_path]) |
| 190 |
.succeeds() |
| 191 |
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); |
| 192 |
} |
| 193 |
|
| 194 |
#[test] |
| 195 |
fn test_with_number_option_with_custom_separator_char_and_width() { |
| 196 |
let test_file_path = "test_num_page.log"; |
| 197 |
let expected_test_file_path = "test_num_page_char_one.log.expected"; |
| 198 |
let mut scenario = new_ucmd!(); |
| 199 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 200 |
scenario |
| 201 |
.args(&["-nc1", test_file_path]) |
| 202 |
.succeeds() |
| 203 |
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn test_with_valid_page_ranges() { |
| 208 |
let test_file_path = "test_num_page.log"; |
| 209 |
let mut scenario = new_ucmd!(); |
| 210 |
scenario |
| 211 |
.args(&["--pages=20:5", test_file_path]) |
| 212 |
.fails() |
| 213 |
.stderr_only("pr: invalid --pages argument '20:5'\n"); |
| 214 |
new_ucmd!() |
| 215 |
.args(&["--pages=1:5", test_file_path]) |
| 216 |
.succeeds(); |
| 217 |
new_ucmd!().args(&["--pages=1", test_file_path]).succeeds(); |
| 218 |
new_ucmd!() |
| 219 |
.args(&["--pages=-1:5", test_file_path]) |
| 220 |
.fails() |
| 221 |
.stderr_only("pr: invalid --pages argument '-1:5'\n"); |
| 222 |
new_ucmd!() |
| 223 |
.args(&["--pages=1:-5", test_file_path]) |
| 224 |
.fails() |
| 225 |
.stderr_only("pr: invalid --pages argument '1:-5'\n"); |
| 226 |
new_ucmd!() |
| 227 |
.args(&["--pages=5:1", test_file_path]) |
| 228 |
.fails() |
| 229 |
.stderr_only("pr: invalid --pages argument '5:1'\n"); |
| 230 |
} |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn test_start_page_exceeds_page_count() { |
| 234 |
new_ucmd!() |
| 235 |
.args(&["--pages=2", "hosts.log"]) |
| 236 |
.succeeds() |
| 237 |
.stderr_only("pr: starting page number 2 exceeds page count 1\n"); |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn test_with_page_range() { |
| 242 |
let test_file_path = "test.log"; |
| 243 |
let expected_test_file_path = "test_page_range_1.log.expected"; |
| 244 |
let expected_test_file_path1 = "test_page_range_2.log.expected"; |
| 245 |
for arg in ["--pages=15", "+15"] { |
| 246 |
let mut scenario = new_ucmd!(); |
| 247 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 248 |
scenario |
| 249 |
.args(&[arg, test_file_path]) |
| 250 |
.succeeds() |
| 251 |
.stdout_is_templated_fixture( |
| 252 |
expected_test_file_path, |
| 253 |
&[("{last_modified_time}", &value)], |
| 254 |
); |
| 255 |
} |
| 256 |
for arg in ["--pages=15:17", "+15:17"] { |
| 257 |
let mut scenario = new_ucmd!(); |
| 258 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 259 |
scenario |
| 260 |
.args(&[arg, test_file_path]) |
| 261 |
.succeeds() |
| 262 |
.stdout_is_templated_fixture( |
| 263 |
expected_test_file_path1, |
| 264 |
&[("{last_modified_time}", &value)], |
| 265 |
); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
#[test] |
| 270 |
fn test_with_no_header_trailer_option() { |
| 271 |
let test_file_path = "test_one_page.log"; |
| 272 |
let expected_test_file_path = "test_one_page_no_ht.log.expected"; |
| 273 |
let mut scenario = new_ucmd!(); |
| 274 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 275 |
scenario |
| 276 |
.args(&["-t", test_file_path]) |
| 277 |
.succeeds() |
| 278 |
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); |
| 279 |
} |
| 280 |
|
| 281 |
#[test] |
| 282 |
fn test_with_page_length_option() { |
| 283 |
let test_file_path = "test.log"; |
| 284 |
for (arg, expected) in [ |
| 285 |
("100", "test_page_length.log.expected"), |
| 286 |
("5", "test_page_length1.log.expected"), |
| 287 |
] { |
| 288 |
let mut scenario = new_ucmd!(); |
| 289 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 290 |
scenario |
| 291 |
.args(&["--pages=2:3", "-l", arg, "-n", test_file_path]) |
| 292 |
.succeeds() |
| 293 |
.stdout_is_templated_fixture(expected, &[("{last_modified_time}", &value)]); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
#[test] |
| 298 |
fn test_with_suppress_error_option() { |
| 299 |
let test_file_path = "test_num_page.log"; |
| 300 |
let mut scenario = new_ucmd!(); |
| 301 |
scenario |
| 302 |
.args(&["--pages=20:5", "-r", test_file_path]) |
| 303 |
.fails() |
| 304 |
.no_output(); |
| 305 |
} |
| 306 |
|
| 307 |
#[test] |
| 308 |
fn test_with_stdin() { |
| 309 |
let expected_file_path = "stdin.log.expected"; |
| 310 |
let mut scenario = new_ucmd!(); |
| 311 |
let start = Timestamp::now(); |
| 312 |
scenario |
| 313 |
.pipe_in_fixture("stdin.log") |
| 314 |
.args(&["--pages=1:2", "-n", "-"]) |
| 315 |
.succeeds() |
| 316 |
.stdout_is_templated_fixture_any( |
| 317 |
expected_file_path, |
| 318 |
&valid_last_modified_template_vars(start), |
| 319 |
); |
| 320 |
} |
| 321 |
|
| 322 |
#[test] |
| 323 |
fn test_with_columns() { |
| 324 |
let test_file_path = "column.log"; |
| 325 |
let expected_test_file_path = "column.log.expected"; |
| 326 |
for arg in ["-3", "--columns=3"] { |
| 327 |
let mut scenario = new_ucmd!(); |
| 328 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 329 |
scenario |
| 330 |
.args(&["--pages=3:5", arg, "-n", test_file_path]) |
| 331 |
.succeeds() |
| 332 |
.stdout_is_templated_fixture( |
| 333 |
expected_test_file_path, |
| 334 |
&[("{last_modified_time}", &value)], |
| 335 |
); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
#[test] |
| 340 |
fn test_with_columns_and_across_option() { |
| 341 |
let test_file_path = "column.log"; |
| 342 |
let expected_test_file_path = "column_across.log.expected"; |
| 343 |
let mut scenario = new_ucmd!(); |
| 344 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 345 |
scenario |
| 346 |
.args(&["--pages=3:5", "--columns=3", "-a", "-n", test_file_path]) |
| 347 |
.succeeds() |
| 348 |
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); |
| 349 |
} |
| 350 |
|
| 351 |
#[test] |
| 352 |
fn test_with_columns_across_option_and_column_separator() { |
| 353 |
let test_file_path = "column.log"; |
| 354 |
for (arg, expected) in [ |
| 355 |
("-s|", "column_across_sep.log.expected"), |
| 356 |
("-Sdivide", "column_across_sep1.log.expected"), |
| 357 |
] { |
| 358 |
let mut scenario = new_ucmd!(); |
| 359 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 360 |
scenario |
| 361 |
.args(&[ |
| 362 |
"--pages=3:5", |
| 363 |
"--columns=3", |
| 364 |
arg, |
| 365 |
"-a", |
| 366 |
"-n", |
| 367 |
test_file_path, |
| 368 |
]) |
| 369 |
.succeeds() |
| 370 |
.stdout_is_templated_fixture(expected, &[("{last_modified_time}", &value)]); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
#[test] |
| 375 |
fn test_with_mpr() { |
| 376 |
let test_file_path = "column.log"; |
| 377 |
let test_file_path1 = "hosts.log"; |
| 378 |
let expected_test_file_path = "mpr.log.expected"; |
| 379 |
let expected_test_file_path1 = "mpr1.log.expected"; |
| 380 |
let expected_test_file_path2 = "mpr2.log.expected"; |
| 381 |
let start = Timestamp::now(); |
| 382 |
new_ucmd!() |
| 383 |
.args(&["--pages=1:2", "-m", "-n", test_file_path, test_file_path1]) |
| 384 |
.succeeds() |
| 385 |
.stdout_is_templated_fixture_any( |
| 386 |
expected_test_file_path, |
| 387 |
&valid_last_modified_template_vars(start), |
| 388 |
); |
| 389 |
|
| 390 |
let start = Timestamp::now(); |
| 391 |
new_ucmd!() |
| 392 |
.args(&["--pages=2:4", "-m", "-n", test_file_path, test_file_path1]) |
| 393 |
.succeeds() |
| 394 |
.stdout_is_templated_fixture_any( |
| 395 |
expected_test_file_path1, |
| 396 |
&valid_last_modified_template_vars(start), |
| 397 |
); |
| 398 |
|
| 399 |
let start = Timestamp::now(); |
| 400 |
new_ucmd!() |
| 401 |
.args(&[ |
| 402 |
"--pages=1:2", |
| 403 |
"-l", |
| 404 |
"100", |
| 405 |
"-n", |
| 406 |
"-m", |
| 407 |
test_file_path, |
| 408 |
test_file_path1, |
| 409 |
test_file_path, |
| 410 |
]) |
| 411 |
.succeeds() |
| 412 |
.stdout_is_templated_fixture_any( |
| 413 |
expected_test_file_path2, |
| 414 |
&valid_last_modified_template_vars(start), |
| 415 |
); |
| 416 |
} |
| 417 |
|
| 418 |
#[test] |
| 419 |
fn test_with_mpr_and_columns_options() { |
| 420 |
let test_file_path = "column.log"; |
| 421 |
new_ucmd!() |
| 422 |
.args(&["--columns=2", "-m", "-n", test_file_path]) |
| 423 |
.fails() |
| 424 |
.stderr_only("pr: cannot specify number of columns when printing in parallel\n"); |
| 425 |
|
| 426 |
new_ucmd!() |
| 427 |
.args(&["-a", "-m", "-n", test_file_path]) |
| 428 |
.fails() |
| 429 |
.stderr_only("pr: cannot specify both printing across and printing in parallel\n"); |
| 430 |
} |
| 431 |
|
| 432 |
#[test] |
| 433 |
fn test_with_offset_space_option() { |
| 434 |
let test_file_path = "column.log"; |
| 435 |
let expected_test_file_path = "column_spaces_across.log.expected"; |
| 436 |
let mut scenario = new_ucmd!(); |
| 437 |
let value = file_last_modified_time(&scenario, test_file_path); |
| 438 |
scenario |
| 439 |
.args(&[ |
| 440 |
"-o", |
| 441 |
"5", |
| 442 |
"--pages=3:5", |
| 443 |
"--columns=3", |
| 444 |
"-a", |
| 445 |
"-n", |
| 446 |
test_file_path, |
| 447 |
]) |
| 448 |
.succeeds() |
| 449 |
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]); |
| 450 |
} |
| 451 |
|
| 452 |
#[test] |
| 453 |
fn test_offset_too_large() { |
| 454 |
let arg = "2147483648"; |
| 455 |
new_ucmd!() |
| 456 |
.args(&["-o", arg]) |
| 457 |
.fails_with_code(1) |
| 458 |
.stderr_is(format!( |
| 459 |
"pr: '-o MARGIN' invalid line offset: '{arg}': Value too large for defined data type\n" |
| 460 |
)); |
| 461 |
} |
| 462 |
|
| 463 |
#[test] |
| 464 |
fn test_start_line_number_too_large() { |
| 465 |
let arg = "18446744073709551615"; |
| 466 |
new_ucmd!() |
| 467 |
.args(&["-n", "-N", arg]) |
| 468 |
.fails_with_code(1) |
| 469 |
.stderr_is(format!( |
| 470 |
"pr: '-N NUMBER' invalid starting line number: '{arg}': Value too large for defined data type\n" |
| 471 |
)); |
| 472 |
} |
| 473 |
|
| 474 |
#[test] |
| 475 |
fn test_page_length_too_large() { |
| 476 |
let arg = "9999999999999999999"; |
| 477 |
new_ucmd!() |
| 478 |
.args(&["-l", arg, "-3"]) |
| 479 |
.fails_with_code(1) |
| 480 |
.stderr_is(format!( |
| 481 |
"pr: '-l PAGE_LENGTH' invalid number of lines: '{arg}': Value too large for defined data type\n" |
| 482 |
)); |
| 483 |
} |
| 484 |
|
| 485 |
#[test] |
| 486 |
fn test_number_width_too_large() { |
| 487 |
let arg = "18446744073709551615"; |
| 488 |
new_ucmd!() |
| 489 |
.args(&["-n", arg]) |
| 490 |
.fails_with_code(1) |
| 491 |
.stderr_is(format!( |
| 492 |
"pr: '-n' extra characters or invalid number in the argument: '{arg}': Value too large for defined data type\nTry 'pr --help' for more information.\n" |
| 493 |
)); |
| 494 |
} |
| 495 |
|
| 496 |
#[test] |
| 497 |
fn test_page_width_too_large() { |
| 498 |
let arg = "18446744073709551615"; |
| 499 |
new_ucmd!() |
| 500 |
.args(&["-W", arg]) |
| 501 |
.fails_with_code(1) |
| 502 |
.stderr_is(format!( |
| 503 |
"pr: '-W PAGE_WIDTH' invalid number of characters: '{arg}': Value too large for defined data type\n" |
| 504 |
)); |
| 505 |
} |
| 506 |
|
| 507 |
#[test] |
| 508 |
fn test_column_width_too_large() { |
| 509 |
let arg = "18446744073709551615"; |
| 510 |
new_ucmd!() |
| 511 |
.args(&["-w", arg, "-2"]) |
| 512 |
.fails_with_code(1) |
| 513 |
.stderr_is(format!( |
| 514 |
"pr: '-w PAGE_WIDTH' invalid number of characters: '{arg}': Value too large for defined data type\n" |
| 515 |
)); |
| 516 |
} |
| 517 |
|
| 518 |
#[test] |
| 519 |
fn test_column_count_too_large() { |
| 520 |
let arg = "9999999999999999999"; |
| 521 |
new_ucmd!() |
| 522 |
.args(&["--columns", arg]) |
| 523 |
.fails_with_code(1) |
| 524 |
.stderr_is(format!( |
| 525 |
"pr: invalid number of columns: '{arg}': Value too large for defined data type\n" |
| 526 |
)); |
| 527 |
|
| 528 |
// The legacy -COLUMN operand form behaves the same. |
| 529 |
new_ucmd!() |
| 530 |
.args(&[format!("-{arg}")]) |
| 531 |
.fails_with_code(1) |
| 532 |
.stderr_is(format!( |
| 533 |
"pr: invalid number of columns: '{arg}': Value too large for defined data type\n" |
| 534 |
)); |
| 535 |
} |
| 536 |
|
| 537 |
#[test] |
| 538 |
fn test_large_number_width_does_not_panic() { |
| 539 |
// Widths above u16::MAX used to panic with "Formatting argument out |
| 540 |
// of range"; GNU pads the number out to the full width instead. |
| 541 |
// With -t (no page headers/footers) GNU emits no page padding, so the |
| 542 |
// output is exactly the numbered line. |
| 543 |
new_ucmd!() |
| 544 |
.args(&["-t", "-n", "70000"]) |
| 545 |
.pipe_in("x\n") |
| 546 |
.succeeds() |
| 547 |
.stdout_is(format!("{}1\tx\n", " ".repeat(69999))); |
| 548 |
} |
| 549 |
|
| 550 |
#[test] |
| 551 |
fn test_large_page_width_does_not_panic() { |
| 552 |
// A page width above u16::MAX used to panic in the header layout. |
| 553 |
new_ucmd!() |
| 554 |
.args(&["-W", "200000"]) |
| 555 |
.pipe_in("x\n") |
| 556 |
.succeeds(); |
| 557 |
} |
| 558 |
|
| 559 |
#[cfg(target_os = "linux")] |
| 560 |
#[test] |
| 561 |
fn test_offset_large_value_does_not_abort_under_memory_limit() { |
| 562 |
use rlimit::Resource; |
| 563 |
use std::process::Stdio; |
| 564 |
|
| 565 |
const AS_LIMIT: u64 = 200 * 1024 * 1024; |
| 566 |
|
| 567 |
new_ucmd!() |
| 568 |
.limit(Resource::AS, AS_LIMIT, AS_LIMIT) |
| 569 |
.set_stdout(Stdio::null()) |
| 570 |
.args(&["-t", "-o", "999999999"]) |
| 571 |
.pipe_in("hi\n") |
| 572 |
.succeeds(); |
| 573 |
} |
| 574 |
|
| 575 |
#[test] |
| 576 |
fn test_offset_invalid() { |
| 577 |
new_ucmd!() |
| 578 |
.args(&["--indent=-5"]) |
| 579 |
.fails_with_code(1) |
| 580 |
.stderr_is("pr: '-o MARGIN' invalid line offset: '-5'\n"); |
| 581 |
|
| 582 |
new_ucmd!() |
| 583 |
.args(&["-o", "abc"]) |
| 584 |
.fails_with_code(1) |
| 585 |
.stderr_is("pr: '-o MARGIN' invalid line offset: 'abc'\n"); |
| 586 |
} |
| 587 |
|
| 588 |
#[test] |
| 589 |
fn test_with_date_format() { |
| 590 |
let whitespace = " ".repeat(50); |
| 591 |
let blank_lines = "\n".repeat(61); |
| 592 |
let datetime_pattern = r"\d{4}__\d{10}"; |
| 593 |
let pattern = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\na{blank_lines}"); |
| 594 |
let regex = Regex::new(&pattern).unwrap(); |
| 595 |
new_ucmd!() |
| 596 |
.args(&["-D", "%Y__%s"]) |
| 597 |
.pipe_in("a") |
| 598 |
.succeeds() |
| 599 |
.stdout_matches(®ex); |
| 600 |
|
| 601 |
// "Format" doesn't need to contain any replaceable token. |
| 602 |
let whitespace = " ".repeat(60); |
| 603 |
let blank_lines = "\n".repeat(61); |
| 604 |
new_ucmd!() |
| 605 |
.args(&["-D", "Hello!"]) |
| 606 |
.pipe_in("a") |
| 607 |
.succeeds() |
| 608 |
.stdout_only(format!("\n\nHello!{whitespace}Page 1\n\n\na{blank_lines}")); |
| 609 |
|
| 610 |
// Long option also works |
| 611 |
new_ucmd!() |
| 612 |
.args(&["--date-format=Hello!"]) |
| 613 |
.pipe_in("a") |
| 614 |
.succeeds() |
| 615 |
.stdout_only(format!("\n\nHello!{whitespace}Page 1\n\n\na{blank_lines}")); |
| 616 |
|
| 617 |
// Option takes precedence over environment variables |
| 618 |
new_ucmd!() |
| 619 |
.env("POSIXLY_CORRECT", "1") |
| 620 |
.env("LC_TIME", "POSIX") |
| 621 |
.args(&["--date-format=Hello!"]) |
| 622 |
.pipe_in("a") |
| 623 |
.succeeds() |
| 624 |
.stdout_only(format!("\n\nHello!{whitespace}Page 1\n\n\na{blank_lines}")); |
| 625 |
} |
| 626 |
|
| 627 |
#[test] |
| 628 |
fn test_with_date_format_env() { |
| 629 |
// POSIXLY_CORRECT + LC_ALL/TIME=POSIX uses "%b %e %H:%M %Y" date format |
| 630 |
let whitespace = " ".repeat(49); |
| 631 |
let blank_lines = "\n".repeat(61); |
| 632 |
let datetime_pattern = r"[A-Z][a-z][a-z] [ \d]\d \d\d:\d\d \d{4}"; |
| 633 |
let pattern = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\na{blank_lines}"); |
| 634 |
let regex = Regex::new(&pattern).unwrap(); |
| 635 |
new_ucmd!() |
| 636 |
.env("POSIXLY_CORRECT", "1") |
| 637 |
.env("LC_ALL", "POSIX") |
| 638 |
.pipe_in("a") |
| 639 |
.succeeds() |
| 640 |
.stdout_matches(®ex); |
| 641 |
new_ucmd!() |
| 642 |
.env("POSIXLY_CORRECT", "1") |
| 643 |
.env("LC_TIME", "POSIX") |
| 644 |
.pipe_in("a") |
| 645 |
.succeeds() |
| 646 |
.stdout_matches(®ex); |
| 647 |
|
| 648 |
// But not if POSIXLY_CORRECT/LC_ALL is something else. |
| 649 |
let whitespace = " ".repeat(50); |
| 650 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 651 |
let pattern = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\na{blank_lines}"); |
| 652 |
let regex = Regex::new(&pattern).unwrap(); |
| 653 |
new_ucmd!() |
| 654 |
.env("LC_TIME", "POSIX") |
| 655 |
.pipe_in("a") |
| 656 |
.succeeds() |
| 657 |
.stdout_matches(®ex); |
| 658 |
new_ucmd!() |
| 659 |
.env("POSIXLY_CORRECT", "1") |
| 660 |
.env("LC_TIME", "C") |
| 661 |
.pipe_in("a") |
| 662 |
.succeeds() |
| 663 |
.stdout_matches(®ex); |
| 664 |
} |
| 665 |
|
| 666 |
#[test] |
| 667 |
fn test_with_join_lines_option() { |
| 668 |
let test_file_1 = "hosts.log"; |
| 669 |
let test_file_2 = "test.log"; |
| 670 |
let expected_file_path = "joined.log.expected"; |
| 671 |
let start = Timestamp::now(); |
| 672 |
|
| 673 |
for join_lines_arg in ["-J", "--join-lines"] { |
| 674 |
new_ucmd!() |
| 675 |
.args(&["+1:2", join_lines_arg, "-m", test_file_1, test_file_2]) |
| 676 |
.succeeds() |
| 677 |
.stdout_is_templated_fixture_any( |
| 678 |
expected_file_path, |
| 679 |
&valid_last_modified_template_vars(start), |
| 680 |
); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
#[test] |
| 685 |
fn test_value_for_number_lines() { |
| 686 |
// *5 is of the form [SEP[NUMBER]] so is accepted and succeeds |
| 687 |
new_ucmd!().args(&["-n", "*5", "test.log"]).succeeds(); |
| 688 |
|
| 689 |
// a is of the form [SEP[NUMBER]] so is accepted and succeeds |
| 690 |
new_ucmd!().args(&["-n", "a", "test.log"]).succeeds(); |
| 691 |
|
| 692 |
// foo5.txt is of not the form [SEP[NUMBER]] so is not used as value. |
| 693 |
// Therefore, pr tries to access the file, which does not exist. |
| 694 |
new_ucmd!().args(&["-n", "foo5.txt", "test.log"]).fails(); |
| 695 |
} |
| 696 |
|
| 697 |
#[test] |
| 698 |
fn test_header_formatting_with_custom_date_format() { |
| 699 |
// This test verifies that the header is properly formatted with: |
| 700 |
// - Date/time on the left |
| 701 |
// - Filename centered |
| 702 |
// - "Page X" on the right |
| 703 |
// This matches GNU pr behavior for the time-style test |
| 704 |
|
| 705 |
let test_file_path = "test_one_page.log"; |
| 706 |
|
| 707 |
// Set a specific date format for consistent output |
| 708 |
let output = new_ucmd!() |
| 709 |
.args(&["-D", "+%Y-%m-%d %H:%M:%S %z (%Z)", test_file_path]) |
| 710 |
.succeeds() |
| 711 |
.stdout_move_str(); |
| 712 |
|
| 713 |
// Extract the header line (3rd line of output) |
| 714 |
let lines: Vec<&str> = output.lines().collect(); |
| 715 |
assert!( |
| 716 |
lines.len() >= 5, |
| 717 |
"Output should have at least 5 lines for header" |
| 718 |
); |
| 719 |
|
| 720 |
let header_line = lines[2]; |
| 721 |
|
| 722 |
// The header should be 72 characters wide (default page width) |
| 723 |
assert_eq!(header_line.chars().count(), 72); |
| 724 |
|
| 725 |
// Check that it contains the expected parts |
| 726 |
assert!(header_line.contains(test_file_path)); |
| 727 |
assert!(header_line.contains("Page 1")); |
| 728 |
|
| 729 |
// Verify the filename is roughly centered |
| 730 |
let filename_pos = header_line.find(test_file_path).unwrap(); |
| 731 |
let page_pos = header_line.find("Page 1").unwrap(); |
| 732 |
|
| 733 |
// Filename should be somewhere in the middle third of the line |
| 734 |
assert!(filename_pos > 24 && filename_pos < 48); |
| 735 |
|
| 736 |
// Page should be right-aligned (near the end) |
| 737 |
assert!(page_pos >= 60); |
| 738 |
} |
| 739 |
|
| 740 |
#[test] |
| 741 |
fn test_help() { |
| 742 |
new_ucmd!().arg("--help").succeeds(); |
| 743 |
} |
| 744 |
|
| 745 |
#[test] |
| 746 |
fn test_version() { |
| 747 |
new_ucmd!().arg("--version").succeeds(); |
| 748 |
} |
| 749 |
|
| 750 |
#[cfg(unix)] |
| 751 |
#[test] |
| 752 |
fn test_pr_char_device_dev_null() { |
| 753 |
new_ucmd!().arg("/dev/null").succeeds(); |
| 754 |
} |
| 755 |
|
| 756 |
#[test] |
| 757 |
fn test_b_flag_backwards_compat() { |
| 758 |
// -b is a no-op for backwards compatibility (column-down is now the default) |
| 759 |
new_ucmd!().args(&["-b", "-t"]).pipe_in("a\nb\n").succeeds(); |
| 760 |
} |
| 761 |
|
| 762 |
#[test] |
| 763 |
fn test_page_header_width() { |
| 764 |
let whitespace = " ".repeat(50); |
| 765 |
let blank_lines = "\n".repeat(61); |
| 766 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 767 |
let pattern = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\na{blank_lines}"); |
| 768 |
let regex = Regex::new(&pattern).unwrap(); |
| 769 |
new_ucmd!().pipe_in("a").succeeds().stdout_matches(®ex); |
| 770 |
} |
| 771 |
|
| 772 |
#[test] |
| 773 |
fn test_separator_options_default_values() { |
| 774 |
// -s and -S without arguments should use default values (TAB and space) |
| 775 |
// TODO: verify output matches GNU pr behavior |
| 776 |
new_ucmd!() |
| 777 |
.args(&["-t", "-2", "-s"]) |
| 778 |
.pipe_in("a\nb\n") |
| 779 |
.succeeds(); |
| 780 |
new_ucmd!() |
| 781 |
.args(&["-t", "-2", "-S"]) |
| 782 |
.pipe_in("a\nb\n") |
| 783 |
.succeeds(); |
| 784 |
} |
| 785 |
|
| 786 |
#[test] |
| 787 |
fn test_omit_pagination_option() { |
| 788 |
// -T/--omit-pagination omits headers/trailers and eliminates form feeds |
| 789 |
// TODO: verify output matches GNU pr behavior (form feed elimination) |
| 790 |
new_ucmd!().args(&["-T"]).pipe_in("a\nb\n").succeeds(); |
| 791 |
new_ucmd!() |
| 792 |
.args(&["--omit-pagination"]) |
| 793 |
.pipe_in("a\nb\n") |
| 794 |
.succeeds(); |
| 795 |
} |
| 796 |
|
| 797 |
#[test] |
| 798 |
fn test_form_feed_newlines() { |
| 799 |
// Here we define the expected output. |
| 800 |
// |
| 801 |
// Each page should have the same number of blank lines before the |
| 802 |
// form-feed character. |
| 803 |
let whitespace = " ".repeat(50); |
| 804 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 805 |
let page1 = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\n\n\x0c"); |
| 806 |
let page2 = format!("\n\n{datetime_pattern}{whitespace}Page 2\n\n\n\n\x0c"); |
| 807 |
let pattern = format!("{page1}{page2}"); |
| 808 |
let regex = Regex::new(&pattern).unwrap(); |
| 809 |
|
| 810 |
// Command line: `printf "\f\f" | pr -f`. |
| 811 |
// |
| 812 |
// Escape code `\x0c` in a Rust string literal is the ASCII escape |
| 813 |
// code `\f` for the "form feed" character (which appears like |
| 814 |
// `^L` in the terminal). |
| 815 |
new_ucmd!() |
| 816 |
.arg("-f") |
| 817 |
.pipe_in("\x0c\x0c") |
| 818 |
.succeeds() |
| 819 |
.stdout_matches(®ex); |
| 820 |
} |
| 821 |
|
| 822 |
#[test] |
| 823 |
fn test_new_line_followed_by_form_feed() { |
| 824 |
// Here we define the expected output. |
| 825 |
let whitespace = " ".repeat(50); |
| 826 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 827 |
let pattern = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\nabc\n\x0c"); |
| 828 |
let regex = Regex::new(&pattern).unwrap(); |
| 829 |
|
| 830 |
// Command line: `printf "abc\n\f" | pr -f`. |
| 831 |
new_ucmd!() |
| 832 |
.arg("-f") |
| 833 |
.pipe_in("abc\n\x0c") |
| 834 |
.succeeds() |
| 835 |
.stdout_matches(®ex); |
| 836 |
} |
| 837 |
|
| 838 |
#[test] |
| 839 |
fn test_form_feed_followed_by_new_line() { |
| 840 |
// Here we define the expected output. |
| 841 |
let whitespace = " ".repeat(50); |
| 842 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 843 |
let blank_lines_61 = "\n".repeat(61); |
| 844 |
let blank_lines_60 = "\n".repeat(60); |
| 845 |
let page1 = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\n{blank_lines_61}"); |
| 846 |
let page2 = format!("\n\n{datetime_pattern}{whitespace}Page 2\n\n\nabc\n{blank_lines_60}"); |
| 847 |
let pattern = format!("{page1}{page2}"); |
| 848 |
let regex = Regex::new(&pattern).unwrap(); |
| 849 |
|
| 850 |
// Command line: `printf "\f\nabc" | pr`. |
| 851 |
new_ucmd!() |
| 852 |
.pipe_in("\x0c\nabc") |
| 853 |
.succeeds() |
| 854 |
.stdout_matches(®ex); |
| 855 |
} |
| 856 |
|
| 857 |
#[test] |
| 858 |
fn test_columns() { |
| 859 |
let whitespace = " ".repeat(50); |
| 860 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 861 |
let header = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\n"); |
| 862 |
// TODO Our output still does not match the behavior of GNU |
| 863 |
// pr. The correct output should be: |
| 864 |
// |
| 865 |
// "a\t\t\t\t b\n"; |
| 866 |
// |
| 867 |
let data = "a \tb \n"; |
| 868 |
let blank_lines_60 = "\n".repeat(60); |
| 869 |
let pattern = format!("{header}{data}{blank_lines_60}"); |
| 870 |
let regex = Regex::new(&pattern).unwrap(); |
| 871 |
|
| 872 |
// Command line: `printf "a\nb\n" | pr -2`. |
| 873 |
new_ucmd!() |
| 874 |
.arg("-2") |
| 875 |
.pipe_in("a\nb\n") |
| 876 |
.succeeds() |
| 877 |
.stdout_matches(®ex); |
| 878 |
} |
| 879 |
|
| 880 |
#[test] |
| 881 |
fn test_columns_partly_filled_page() { |
| 882 |
// Three lines do not divide evenly into two columns. GNU pr puts the |
| 883 |
// ceiling of 3 / 2 in the first column and what is left in the second, so |
| 884 |
// "c" has to show up next to "a". |
| 885 |
// |
| 886 |
// Command line: `printf "a\nb\nc\n" | pr -t -2 -w 20`. |
| 887 |
new_ucmd!() |
| 888 |
.args(&["-t", "-2", "-w", "20"]) |
| 889 |
.pipe_in("a\nb\nc\n") |
| 890 |
.succeeds() |
| 891 |
.stdout_is("a \tc \nb \n"); |
| 892 |
} |
| 893 |
|
| 894 |
#[test] |
| 895 |
fn test_columns_partly_filled_page_across() { |
| 896 |
// Same input read across instead of down. |
| 897 |
// |
| 898 |
// Command line: `printf "a\nb\nc\n" | pr -t -a -2 -w 20`. |
| 899 |
new_ucmd!() |
| 900 |
.args(&["-t", "-a", "-2", "-w", "20"]) |
| 901 |
.pipe_in("a\nb\nc\n") |
| 902 |
.succeeds() |
| 903 |
.stdout_is("a \tb \nc \n"); |
| 904 |
} |
| 905 |
|
| 906 |
#[test] |
| 907 |
fn test_columns_fewer_lines_than_columns() { |
| 908 |
// Fewer lines than columns still has to print those lines. |
| 909 |
// |
| 910 |
// Command line: `printf "a\n" | pr -t -3 -w 20`. |
| 911 |
new_ucmd!() |
| 912 |
.args(&["-t", "-3", "-w", "20"]) |
| 913 |
.pipe_in("a\n") |
| 914 |
.succeeds() |
| 915 |
.stdout_is("a \n"); |
| 916 |
|
| 917 |
// Command line: `printf "a\nb\n" | pr -t -3 -w 20`. |
| 918 |
new_ucmd!() |
| 919 |
.args(&["-t", "-3", "-w", "20"]) |
| 920 |
.pipe_in("a\nb\n") |
| 921 |
.succeeds() |
| 922 |
.stdout_is("a \tb \n"); |
| 923 |
} |
| 924 |
|
| 925 |
#[test] |
| 926 |
fn test_columns_last_page() { |
| 927 |
// Nine lines over pages that hold four each leave the ninth alone on page |
| 928 |
// three, which must not come out as an empty page. |
| 929 |
// |
| 930 |
// Command line: `seq 9 | pr -2 -l 12 -w 20 -D DATE`. |
| 931 |
let header = "\n\nDATE Page "; |
| 932 |
let expected = format!( |
| 933 |
"{header}1\n\n\n1 \t3 \n2 \t4 \n{blank}\ |
| 934 |
{header}2\n\n\n5 \t7 \n6 \t8 \n{blank}\ |
| 935 |
{header}3\n\n\n9 \n\n\n\n\n\n\n", |
| 936 |
blank = "\n".repeat(5), |
| 937 |
); |
| 938 |
new_ucmd!() |
| 939 |
.args(&["-2", "-l", "12", "-w", "20", "-D", "DATE"]) |
| 940 |
.pipe_in("1\n2\n3\n4\n5\n6\n7\n8\n9\n") |
| 941 |
.succeeds() |
| 942 |
.stdout_is(expected); |
| 943 |
} |
| 944 |
|
| 945 |
#[test] |
| 946 |
fn test_merge() { |
| 947 |
// Create the two files to merge. |
| 948 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 949 |
at.write("f", "a\n"); |
| 950 |
at.write("g", "b\n"); |
| 951 |
|
| 952 |
let whitespace = " ".repeat(50); |
| 953 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 954 |
let header = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\n"); |
| 955 |
// TODO Our output still does not match the behavior of GNU |
| 956 |
// pr. The correct output should be: |
| 957 |
// |
| 958 |
// "a\t\t\t\t b\n"; |
| 959 |
// |
| 960 |
// and the blank lines should actually be empty lines. |
| 961 |
let data = "a \tb \n"; |
| 962 |
let blank_lines_55 = |
| 963 |
" \t \n".repeat(55); |
| 964 |
let footer = "\n".repeat(5); |
| 965 |
let pattern = format!("{header}{data}{blank_lines_55}{footer}"); |
| 966 |
let regex = Regex::new(&pattern).unwrap(); |
| 967 |
|
| 968 |
// Command line: `(echo "a" > f; echo "b" > g; pr -m f g)`. |
| 969 |
ucmd.args(&["-m", "f", "g"]) |
| 970 |
.succeeds() |
| 971 |
.stdout_matches(®ex); |
| 972 |
} |
| 973 |
|
| 974 |
#[test] |
| 975 |
fn test_merge_one_long_one_short() { |
| 976 |
// Create the two files to merge. |
| 977 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 978 |
at.write("f", "a\na\n"); |
| 979 |
at.write("g", "b\n"); |
| 980 |
|
| 981 |
// Page 1 should have the first line of `f` and the first line of |
| 982 |
// `b` side-by-side. |
| 983 |
let whitespace = " ".repeat(50); |
| 984 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 985 |
let header = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\n"); |
| 986 |
let data = "a \tb \n"; |
| 987 |
let footer = "\n".repeat(5); |
| 988 |
let page1 = format!("{header}{data}{footer}"); |
| 989 |
|
| 990 |
// Page 2 should have just the second line of `f`. |
| 991 |
let header = format!("\n\n{datetime_pattern}{whitespace}Page 2\n\n\n"); |
| 992 |
let data = "a \t \n"; |
| 993 |
let page2 = format!("{header}{data}{footer}"); |
| 994 |
|
| 995 |
let pattern = format!("{page1}{page2}"); |
| 996 |
let regex = Regex::new(&pattern).unwrap(); |
| 997 |
|
| 998 |
// Command line: |
| 999 |
// |
| 1000 |
// printf "a\na\n" > f |
| 1001 |
// printf "b\n" > g |
| 1002 |
// pr -l11 -m f g |
| 1003 |
// |
| 1004 |
// The line length of 11 leaves room for a 5-line header, a 5-line |
| 1005 |
// footer, and one line of data from the input files. The extra |
| 1006 |
// line from the file `f` will be on the second page. |
| 1007 |
ucmd.args(&["-l", "11", "-m", "f", "g"]) |
| 1008 |
.succeeds() |
| 1009 |
.stdout_matches(®ex); |
| 1010 |
} |
| 1011 |
|
| 1012 |
#[test] |
| 1013 |
fn test_simple_expand_tab() { |
| 1014 |
let whitespace = " ".repeat(50); |
| 1015 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 1016 |
let page_1_beginning = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\n"); |
| 1017 |
|
| 1018 |
let output_regex = Regex::new(&format!("{page_1_beginning}hello world\nabc def\n leading\ntrail \n8chars00 \n")).unwrap(); |
| 1019 |
|
| 1020 |
new_ucmd!() |
| 1021 |
.arg("-e") |
| 1022 |
.pipe_in("hello\tworld\nabc\tdef\n\tleading\ntrail\t\n8chars00\t\n") |
| 1023 |
.succeeds() |
| 1024 |
.stdout_matches(&output_regex); |
| 1025 |
} |
| 1026 |
|
| 1027 |
#[test] |
| 1028 |
fn test_expand_tab_at_end_of_short_flag_cluster() { |
| 1029 |
// `-e` closing a cluster of value-less short flags carries no attached argument, so it has |
| 1030 |
// to fall back to the defaults rather than report a missing value. |
| 1031 |
for (arg, expected) in [ |
| 1032 |
("-tre", "oi\n"), |
| 1033 |
("-tre8", "oi\n"), |
| 1034 |
("-tfre", "oi\n\x0c"), |
| 1035 |
("-tfre8", "oi\n\x0c"), |
| 1036 |
] { |
| 1037 |
new_ucmd!() |
| 1038 |
.arg(arg) |
| 1039 |
.pipe_in("oi\n") |
| 1040 |
.succeeds() |
| 1041 |
.stdout_only(expected); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
#[test] |
| 1046 |
fn test_expand_tab_does_not_consume_following_operand() { |
| 1047 |
// A bare `-e` must leave the file operand alone. |
| 1048 |
new_ucmd!() |
| 1049 |
.args(&["-t", "-e"]) |
| 1050 |
.pipe_in("a\tb\n") |
| 1051 |
.succeeds() |
| 1052 |
.stdout_only("a b\n"); |
| 1053 |
} |
| 1054 |
|
| 1055 |
#[test] |
| 1056 |
fn test_simple_expand_tab_with_digit_argument() { |
| 1057 |
let whitespace = " ".repeat(50); |
| 1058 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 1059 |
let page_1_beginning = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\n"); |
| 1060 |
let input = "hello\tworld\nabc\tdef\n\tleading\ntrail\t\n8chars00\t\n"; |
| 1061 |
|
| 1062 |
let test_cases = vec![ |
| 1063 |
("-e2", Regex::new(&format!("{page_1_beginning}hello world\nabc def\n leading\ntrail \n8chars00 \n")).unwrap()), |
| 1064 |
("-e3", Regex::new(&format!("{page_1_beginning}hello world\nabc def\n leading\ntrail \n8chars00 \n")).unwrap()), |
| 1065 |
("-e8", Regex::new(&format!("{page_1_beginning}hello world\nabc def\n leading\ntrail \n8chars00 \n")).unwrap()), |
| 1066 |
("-e10", Regex::new(&format!("{page_1_beginning}hello world\nabc def\n leading\ntrail \n8chars00 \n")).unwrap()), |
| 1067 |
]; |
| 1068 |
for (arg, output_regex) in test_cases { |
| 1069 |
new_ucmd!() |
| 1070 |
.arg(arg) |
| 1071 |
.pipe_in(input) |
| 1072 |
.succeeds() |
| 1073 |
.stdout_matches(&output_regex); |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
#[test] |
| 1078 |
fn test_simple_expand_tab_with_char_argument() { |
| 1079 |
let whitespace = " ".repeat(50); |
| 1080 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 1081 |
let page_1_beginning = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\n"); |
| 1082 |
let input = "hello\tworld\nabc\tdef\n\tleading\ntrail\t\n8chars00\t\n"; |
| 1083 |
|
| 1084 |
let test_cases = vec![ |
| 1085 |
("-ea", Regex::new(&format!("{page_1_beginning}hello world\n bc def\n le ding\ntr il \n8ch rs00 \n")).unwrap()), |
| 1086 |
("-ee", Regex::new(&format!("{page_1_beginning}h llo world\nabc d f\n l ading\ntrail \n8chars00 \n")).unwrap()), |
| 1087 |
]; |
| 1088 |
for (arg, output_regex) in test_cases { |
| 1089 |
new_ucmd!() |
| 1090 |
.arg(arg) |
| 1091 |
.pipe_in(input) |
| 1092 |
.succeeds() |
| 1093 |
.stdout_matches(&output_regex); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
#[test] |
| 1098 |
fn test_simple_expand_tab_with_both_arguments() { |
| 1099 |
// test different variations of what char to expand |
| 1100 |
// a2, e3, t10 |
| 1101 |
let whitespace = " ".repeat(50); |
| 1102 |
let datetime_pattern = r"\d\d\d\d-\d\d-\d\d \d\d:\d\d"; |
| 1103 |
let page_1_beginning = format!("\n\n{datetime_pattern}{whitespace}Page 1\n\n\n"); |
| 1104 |
let input = "hello\tworld\nabc\tdef\n\tleading\ntrail\t\n8chars00\t\n"; |
| 1105 |
|
| 1106 |
let test_cases = vec![ |
| 1107 |
("-ea2", Regex::new(&format!("{page_1_beginning}hello world\n bc def\n le ding\ntr il \n8ch rs00 \n")).unwrap()), |
| 1108 |
("-ee3", Regex::new(&format!("{page_1_beginning}h llo world\nabc d f\n l ading\ntrail \n8chars00 \n")).unwrap()), |
| 1109 |
("-et10", Regex::new(&format!("{page_1_beginning}hello world\nabc def\n leading\n rail \n8chars00 \n")).unwrap()), |
| 1110 |
]; |
| 1111 |
for (arg, output_regex) in test_cases { |
| 1112 |
new_ucmd!() |
| 1113 |
.arg(arg) |
| 1114 |
.pipe_in(input) |
| 1115 |
.succeeds() |
| 1116 |
.stdout_matches(&output_regex); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
/* cSpell:disable */ |
| 1121 |
#[test] |
| 1122 |
fn test_invalid_expand_tab_arguments() { |
| 1123 |
let test_file_path = "empty_test_file"; |
| 1124 |
|
| 1125 |
let test_cases = vec![ |
| 1126 |
// incorrect argument |
| 1127 |
("-esdgjiojiosdgjiogd", "dgjiojiosdgjiogd"), |
| 1128 |
// 2 non digit parameter |
| 1129 |
("-eab", "b"), |
| 1130 |
// non digit after first digit |
| 1131 |
("-e1a", "1a"), |
| 1132 |
// non digit after first digit after allowed input char |
| 1133 |
("-ea1a", "1a"), |
| 1134 |
// > i32 max |
| 1135 |
("-e2147483648", "2147483648"), |
| 1136 |
// > i32 max after allowed input char |
| 1137 |
("-ea2147483648", "2147483648"), |
| 1138 |
]; |
| 1139 |
|
| 1140 |
for (arg, error_msg_field) in test_cases { |
| 1141 |
new_ucmd!() |
| 1142 |
.args(&[arg, test_file_path]) |
| 1143 |
.fails() |
| 1144 |
.stderr_contains(format!("pr: '-e' extra characters or invalid number in the argument: ‘{error_msg_field}’\nTry 'pr --help' for more information.")); |
| 1145 |
} |
| 1146 |
} |
| 1147 |
/* cSpell:enable */ |
| 1148 |
|
| 1149 |
#[test] |
| 1150 |
fn test_expand_tab_does_not_consume_next_argument() { |
| 1151 |
let test_file_path = "empty_test_file"; |
| 1152 |
new_ucmd!().args(&["-e", test_file_path]).succeeds(); |
| 1153 |
new_ucmd!().args(&["-ea", test_file_path]).succeeds(); |
| 1154 |
new_ucmd!().args(&["-ea1", test_file_path]).succeeds(); |
| 1155 |
} |
| 1156 |
|
| 1157 |
#[test] |
| 1158 |
fn test_zero_columns() { |
| 1159 |
new_ucmd!() |
| 1160 |
.arg("--columns=0") |
| 1161 |
.fails_with_code(1) |
| 1162 |
.stderr_contains("pr: invalid --columns argument '0'"); |
| 1163 |
} |
| 1164 |
|
| 1165 |
#[test] |
| 1166 |
fn test_zero_columns_shortcut() { |
| 1167 |
new_ucmd!() |
| 1168 |
.arg("-0") |
| 1169 |
.fails_with_code(1) |
| 1170 |
.stderr_contains("pr: invalid --columns argument '0'"); |
| 1171 |
} |
| 1172 |
|
| 1173 |
#[test] |
| 1174 |
fn test_filename_ending_with_dash_number_is_not_an_option() { |
| 1175 |
for name in ["a-0", "a-b-0", "a-3"] { |
| 1176 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1177 |
at.write(name, "RUST-pr\n"); |
| 1178 |
ucmd.args(&["-t", name]) |
| 1179 |
.succeeds() |
| 1180 |
.stdout_contains("RUST-pr"); |
| 1181 |
} |
| 1182 |
} |
| 1183 |
|
| 1184 |
#[test] |
| 1185 |
fn test_double_dash_terminates_option_parsing() { |
| 1186 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1187 |
at.write("-0", "RUST-pr\n"); |
| 1188 |
ucmd.args(&["-t", "--", "-0"]) |
| 1189 |
.succeeds() |
| 1190 |
.stdout_contains("RUST-pr"); |
| 1191 |
} |
| 1192 |
|
| 1193 |
#[test] |
| 1194 |
fn test_double_dash_shields_expand_tabs_filename() { |
| 1195 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1196 |
at.write("-e", "RUST-pr\n"); |
| 1197 |
ucmd.args(&["-t", "--", "-e"]) |
| 1198 |
.succeeds() |
| 1199 |
.stdout_contains("RUST-pr"); |
| 1200 |
} |
| 1201 |
|
| 1202 |
#[test] |
| 1203 |
fn test_double_dash_shields_number_lines_filename() { |
| 1204 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1205 |
at.write("-n", "first\n"); |
| 1206 |
at.write("data", "second\n"); |
| 1207 |
ucmd.args(&["-t", "--", "-n", "data"]) |
| 1208 |
.succeeds() |
| 1209 |
.stdout_contains("first") |
| 1210 |
.stdout_contains("second"); |
| 1211 |
} |
| 1212 |
|
| 1213 |
#[test] |
| 1214 |
fn test_double_dash_shields_filename_ending_with_dash_zero() { |
| 1215 |
for name in ["a-0", "a-b-0"] { |
| 1216 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1217 |
at.write(name, "RUST-pr\n"); |
| 1218 |
|
| 1219 |
ucmd.args(&["-t", "--", name]) |
| 1220 |
.succeeds() |
| 1221 |
.stdout_contains("RUST-pr\n"); |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
#[test] |
| 1226 |
fn test_zero_expand_tab_width() { |
| 1227 |
let expected = "pr: '-e' extra characters or invalid number in the argument: ‘0’\nTry 'pr --help' for more information.\n"; |
| 1228 |
new_ucmd!() |
| 1229 |
.arg("-e0") |
| 1230 |
.fails_with_code(1) |
| 1231 |
.stderr_only(expected); |
| 1232 |
new_ucmd!() |
| 1233 |
.arg("-eX0") |
| 1234 |
.fails_with_code(1) |
| 1235 |
.stderr_only(expected); |
| 1236 |
} |
| 1237 |
|
| 1238 |
#[test] |
| 1239 |
fn test_zero_column_width() { |
| 1240 |
new_ucmd!() |
| 1241 |
.args(&["-w", "0"]) |
| 1242 |
.fails_with_code(1) |
| 1243 |
.stderr_is("pr: invalid --width argument '0'\n"); |
| 1244 |
} |
| 1245 |
|
| 1246 |
#[test] |
| 1247 |
fn test_zero_page_width() { |
| 1248 |
new_ucmd!() |
| 1249 |
.args(&["-W", "0"]) |
| 1250 |
.fails_with_code(1) |
| 1251 |
.stderr_is("pr: invalid --page-width argument '0'\n"); |
| 1252 |
} |
| 1253 |
|
| 1254 |
#[test] |
| 1255 |
fn test_page_length_ten_implies_omit_header() { |
| 1256 |
// `pr --help` states it twice: a page length of 10 or less implies `-t`. |
| 1257 |
// At exactly 10 the header and trailer were kept and then subtracted from |
| 1258 |
// the page, which left no room for content: `-h` printed empty pages and |
| 1259 |
// `-t` printed nothing at all. |
| 1260 |
new_ucmd!() |
| 1261 |
.args(&["-l", "10", "-h", "hdr"]) |
| 1262 |
.pipe_in("a\nb\nc\n") |
| 1263 |
.succeeds() |
| 1264 |
.stdout_only("a\nb\nc\n"); |
| 1265 |
|
| 1266 |
new_ucmd!() |
| 1267 |
.args(&["-l", "10", "-t"]) |
| 1268 |
.pipe_in("a\nb\nc\n") |
| 1269 |
.succeeds() |
| 1270 |
.stdout_only("a\nb\nc\n"); |
| 1271 |
} |
| 1272 |
|
| 1273 |
#[test] |
| 1274 |
fn test_page_length_eleven_keeps_header() { |
| 1275 |
new_ucmd!() |
| 1276 |
.args(&["-l", "11", "-h", "hdr"]) |
| 1277 |
.pipe_in("a\nb\nc\n") |
| 1278 |
.succeeds() |
| 1279 |
.stdout_contains("hdr"); |
| 1280 |
} |
| 1281 |
|
| 1282 |
#[test] |
| 1283 |
fn test_zero_length() { |
| 1284 |
new_ucmd!() |
| 1285 |
.args(&["-l", "0"]) |
| 1286 |
.fails_with_code(1) |
| 1287 |
.stderr_is("pr: invalid --length argument '0'\n"); |
| 1288 |
} |
| 1289 |
|
| 1290 |
#[test] |
| 1291 |
fn test_zero_pages() { |
| 1292 |
new_ucmd!() |
| 1293 |
.args(&["--pages", "0"]) |
| 1294 |
.fails_with_code(1) |
| 1295 |
.stderr_is("pr: invalid --pages argument '0'\n"); |
| 1296 |
} |
| 1297 |
|
| 1298 |
#[test] |
| 1299 |
fn test_negative_expand_tabs() { |
| 1300 |
new_ucmd!() |
| 1301 |
.arg("-e=-1") |
| 1302 |
.fails_with_code(1) |
| 1303 |
.stderr_is("pr: '-e' extra characters or invalid number in the argument: ‘-1’\nTry 'pr --help' for more information.\n"); |
| 1304 |
} |
| 1305 |
|
| 1306 |
#[cfg(unix)] |
| 1307 |
#[test] |
| 1308 |
fn test_merge_empty_input() { |
| 1309 |
new_ucmd!() |
| 1310 |
.args(&["-m", "/dev/null", "/dev/null"]) |
| 1311 |
.succeeds() |
| 1312 |
.no_output(); |
| 1313 |
} |
| 1314 |
|
| 1315 |
#[test] |
| 1316 |
fn test_missing_file_error_message() { |
| 1317 |
// A nonexistent operand must be reported as "pr: <file>: <message>", like |
| 1318 |
// GNU pr, with the raw "(os error N)" suffix stripped. The exact message |
| 1319 |
// text is platform dependent, so only assert the portable parts. |
| 1320 |
new_ucmd!() |
| 1321 |
.arg("nonexistent_file") |
| 1322 |
.fails_with_code(1) |
| 1323 |
.stderr_contains("pr: nonexistent_file: ") |
| 1324 |
.stderr_does_not_contain("(os error"); |
| 1325 |
} |