forked from uutils/coreutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pr.rs
484 lines (443 loc) · 15.4 KB
/
test_pr.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore (ToDO) Sdivide
use crate::common::util::{TestScenario, UCommand};
use chrono::{DateTime, Duration, Utc};
use std::fs::metadata;
const DATE_TIME_FORMAT: &str = "%b %d %H:%M %Y";
fn file_last_modified_time(ucmd: &UCommand, path: &str) -> String {
let tmp_dir_path = ucmd.get_full_fixture_path(path);
let file_metadata = metadata(tmp_dir_path);
file_metadata
.map(|i| {
i.modified()
.map(|x| {
let date_time: DateTime<Utc> = x.into();
date_time.format(DATE_TIME_FORMAT).to_string()
})
.unwrap_or_default()
})
.unwrap_or_default()
}
fn all_minutes(from: DateTime<Utc>, to: DateTime<Utc>) -> Vec<String> {
let to = to + Duration::try_minutes(1).unwrap();
let mut vec = vec![];
let mut current = from;
while current < to {
vec.push(current.format(DATE_TIME_FORMAT).to_string());
current += Duration::try_minutes(1).unwrap();
}
vec
}
fn valid_last_modified_template_vars(from: DateTime<Utc>) -> Vec<Vec<(String, String)>> {
all_minutes(from, Utc::now())
.into_iter()
.map(|time| vec![("{last_modified_time}".to_string(), time)])
.collect()
}
#[test]
fn test_invalid_flag() {
new_ucmd!()
.arg("--invalid-argument")
.fails()
.no_stdout()
.code_is(1);
}
#[test]
fn test_without_any_options() {
let test_file_path = "test_one_page.log";
let expected_test_file_path = "test_one_page.log.expected";
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&[test_file_path])
.succeeds()
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]);
}
#[test]
fn test_with_numbering_option_with_number_width() {
let test_file_path = "test_num_page.log";
let expected_test_file_path = "test_num_page_2.log.expected";
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&["-n", "2", test_file_path])
.succeeds()
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]);
}
#[test]
fn test_with_long_header_option() {
let test_file_path = "test_one_page.log";
let expected_test_file_path = "test_one_page_header.log.expected";
let header = "new file";
for args in [&["-h", header][..], &["--header=new file"][..]] {
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(args)
.arg(test_file_path)
.succeeds()
.stdout_is_templated_fixture(
expected_test_file_path,
&[("{last_modified_time}", &value), ("{header}", header)],
);
}
}
#[test]
fn test_with_double_space_option() {
let test_file_path = "test_one_page.log";
let expected_test_file_path = "test_one_page_double_line.log.expected";
for arg in ["-d", "--double-space"] {
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&[arg, test_file_path])
.succeeds()
.stdout_is_templated_fixture(
expected_test_file_path,
&[("{last_modified_time}", &value)],
);
}
}
#[test]
fn test_with_first_line_number_option() {
let test_file_path = "test_one_page.log";
let expected_test_file_path = "test_one_page_first_line.log.expected";
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&["-N", "5", "-n", test_file_path])
.succeeds()
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]);
}
#[test]
fn test_with_first_line_number_long_option() {
let test_file_path = "test_one_page.log";
let expected_test_file_path = "test_one_page_first_line.log.expected";
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&["--first-line-number=5", "-n", test_file_path])
.succeeds()
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]);
}
#[test]
fn test_with_number_option_with_custom_separator_char() {
let test_file_path = "test_num_page.log";
let expected_test_file_path = "test_num_page_char.log.expected";
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&["-nc", test_file_path])
.succeeds()
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]);
}
#[test]
fn test_with_number_option_with_custom_separator_char_and_width() {
let test_file_path = "test_num_page.log";
let expected_test_file_path = "test_num_page_char_one.log.expected";
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&["-nc1", test_file_path])
.succeeds()
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]);
}
#[test]
fn test_with_valid_page_ranges() {
let test_file_path = "test_num_page.log";
let mut scenario = new_ucmd!();
scenario
.args(&["--pages=20:5", test_file_path])
.fails()
.stderr_is("pr: invalid --pages argument '20:5'\n")
.stdout_is("");
new_ucmd!()
.args(&["--pages=1:5", test_file_path])
.succeeds();
new_ucmd!().args(&["--pages=1", test_file_path]).succeeds();
new_ucmd!()
.args(&["--pages=-1:5", test_file_path])
.fails()
.stderr_is("pr: invalid --pages argument '-1:5'\n")
.stdout_is("");
new_ucmd!()
.args(&["--pages=1:-5", test_file_path])
.fails()
.stderr_is("pr: invalid --pages argument '1:-5'\n")
.stdout_is("");
new_ucmd!()
.args(&["--pages=5:1", test_file_path])
.fails()
.stderr_is("pr: invalid --pages argument '5:1'\n")
.stdout_is("");
}
#[test]
fn test_with_page_range() {
let test_file_path = "test.log";
let expected_test_file_path = "test_page_range_1.log.expected";
let expected_test_file_path1 = "test_page_range_2.log.expected";
for arg in ["--pages=15", "+15"] {
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&[arg, test_file_path])
.succeeds()
.stdout_is_templated_fixture(
expected_test_file_path,
&[("{last_modified_time}", &value)],
);
}
for arg in ["--pages=15:17", "+15:17"] {
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&[arg, test_file_path])
.succeeds()
.stdout_is_templated_fixture(
expected_test_file_path1,
&[("{last_modified_time}", &value)],
);
}
}
#[test]
fn test_with_no_header_trailer_option() {
let test_file_path = "test_one_page.log";
let expected_test_file_path = "test_one_page_no_ht.log.expected";
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&["-t", test_file_path])
.succeeds()
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]);
}
#[test]
fn test_with_page_length_option() {
let test_file_path = "test.log";
for (arg, expected) in [
("100", "test_page_length.log.expected"),
("5", "test_page_length1.log.expected"),
] {
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&["--pages=2:3", "-l", arg, "-n", test_file_path])
.succeeds()
.stdout_is_templated_fixture(expected, &[("{last_modified_time}", &value)]);
}
}
#[test]
fn test_with_suppress_error_option() {
let test_file_path = "test_num_page.log";
let mut scenario = new_ucmd!();
scenario
.args(&["--pages=20:5", "-r", test_file_path])
.fails()
.stderr_is("")
.stdout_is("");
}
#[test]
fn test_with_stdin() {
let expected_file_path = "stdin.log.expected";
let mut scenario = new_ucmd!();
let start = Utc::now();
scenario
.pipe_in_fixture("stdin.log")
.args(&["--pages=1:2", "-n", "-"])
.run()
.stdout_is_templated_fixture_any(
expected_file_path,
&valid_last_modified_template_vars(start),
);
}
#[test]
fn test_with_column() {
let test_file_path = "column.log";
let expected_test_file_path = "column.log.expected";
for arg in ["-3", "--column=3"] {
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&["--pages=3:5", arg, "-n", test_file_path])
.succeeds()
.stdout_is_templated_fixture(
expected_test_file_path,
&[("{last_modified_time}", &value)],
);
}
}
#[test]
fn test_with_column_across_option() {
let test_file_path = "column.log";
let expected_test_file_path = "column_across.log.expected";
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&["--pages=3:5", "--column=3", "-a", "-n", test_file_path])
.succeeds()
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]);
}
#[test]
fn test_with_column_across_option_and_column_separator() {
let test_file_path = "column.log";
for (arg, expected) in [
("-s|", "column_across_sep.log.expected"),
("-Sdivide", "column_across_sep1.log.expected"),
] {
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&["--pages=3:5", "--column=3", arg, "-a", "-n", test_file_path])
.succeeds()
.stdout_is_templated_fixture(expected, &[("{last_modified_time}", &value)]);
}
}
#[test]
fn test_with_mpr() {
let test_file_path = "column.log";
let test_file_path1 = "hosts.log";
let expected_test_file_path = "mpr.log.expected";
let expected_test_file_path1 = "mpr1.log.expected";
let expected_test_file_path2 = "mpr2.log.expected";
let start = Utc::now();
new_ucmd!()
.args(&["--pages=1:2", "-m", "-n", test_file_path, test_file_path1])
.succeeds()
.stdout_is_templated_fixture_any(
expected_test_file_path,
&valid_last_modified_template_vars(start),
);
let start = Utc::now();
new_ucmd!()
.args(&["--pages=2:4", "-m", "-n", test_file_path, test_file_path1])
.succeeds()
.stdout_is_templated_fixture_any(
expected_test_file_path1,
&valid_last_modified_template_vars(start),
);
let start = Utc::now();
new_ucmd!()
.args(&[
"--pages=1:2",
"-l",
"100",
"-n",
"-m",
test_file_path,
test_file_path1,
test_file_path,
])
.succeeds()
.stdout_is_templated_fixture_any(
expected_test_file_path2,
&valid_last_modified_template_vars(start),
);
}
#[test]
fn test_with_mpr_and_column_options() {
let test_file_path = "column.log";
new_ucmd!()
.args(&["--column=2", "-m", "-n", test_file_path])
.fails()
.stderr_is("pr: cannot specify number of columns when printing in parallel\n")
.stdout_is("");
new_ucmd!()
.args(&["-a", "-m", "-n", test_file_path])
.fails()
.stderr_is("pr: cannot specify both printing across and printing in parallel\n")
.stdout_is("");
}
#[test]
fn test_with_offset_space_option() {
let test_file_path = "column.log";
let expected_test_file_path = "column_spaces_across.log.expected";
let mut scenario = new_ucmd!();
let value = file_last_modified_time(&scenario, test_file_path);
scenario
.args(&[
"-o",
"5",
"--pages=3:5",
"--column=3",
"-a",
"-n",
test_file_path,
])
.succeeds()
.stdout_is_templated_fixture(expected_test_file_path, &[("{last_modified_time}", &value)]);
}
#[test]
fn test_with_pr_core_utils_tests() {
let test_cases = vec![
("", vec!["0Ft"], vec!["0F"], 0),
("", vec!["0Fnt"], vec!["0F"], 0),
("+3", vec!["0Ft"], vec!["3-0F"], 0),
("+3 -f", vec!["0Ft"], vec!["3f-0F"], 0),
("-a -3", vec!["0Ft"], vec!["a3-0F"], 0),
("-a -3 -f", vec!["0Ft"], vec!["a3f-0F"], 0),
("-a -3 -f", vec!["0Fnt"], vec!["a3f-0F"], 0),
("+3 -a -3 -f", vec!["0Ft"], vec!["3a3f-0F"], 0),
("-l 24", vec!["FnFn"], vec!["l24-FF"], 0),
("-W 20 -l24 -f", vec!["tFFt-ll"], vec!["W20l24f-ll"], 0),
];
for test_case in test_cases {
let (flags, input_file, expected_file, return_code) = test_case;
let mut scenario = new_ucmd!();
let input_file_path = input_file.first().unwrap();
let test_file_path = expected_file.first().unwrap();
let value = file_last_modified_time(&scenario, input_file_path);
let mut arguments: Vec<&str> = flags
.split(' ')
.filter(|i| i.trim() != "")
.collect::<Vec<&str>>();
arguments.extend(input_file.clone());
let scenario_with_args = scenario.args(&arguments);
let scenario_with_expected_status = if return_code == 0 {
scenario_with_args.succeeds()
} else {
scenario_with_args.fails()
};
scenario_with_expected_status.stdout_is_templated_fixture(
test_file_path,
&[
("{last_modified_time}", &value),
("{file_name}", input_file_path),
],
);
}
}
#[test]
fn test_with_join_lines_option() {
let test_file_1 = "hosts.log";
let test_file_2 = "test.log";
let expected_file_path = "joined.log.expected";
let mut scenario = new_ucmd!();
let start = Utc::now();
scenario
.args(&["+1:2", "-J", "-m", test_file_1, test_file_2])
.run()
.stdout_is_templated_fixture_any(
expected_file_path,
&valid_last_modified_template_vars(start),
);
}
#[test]
fn test_value_for_number_lines() {
// *5 is of the form [SEP[NUMBER]] so is accepted and succeeds
new_ucmd!().args(&["-n", "*5", "test.log"]).succeeds();
// a is of the form [SEP[NUMBER]] so is accepted and succeeds
new_ucmd!().args(&["-n", "a", "test.log"]).succeeds();
// foo5.txt is of not the form [SEP[NUMBER]] so is not used as value.
// Therefore, pr tries to access the file, which does not exist.
new_ucmd!().args(&["-n", "foo5.txt", "test.log"]).fails();
}
#[test]
fn test_help() {
new_ucmd!().arg("--help").succeeds();
}
#[test]
fn test_version() {
new_ucmd!().arg("--version").succeeds();
}