on 5f6e310 help sign in
bat/tests/integration_tests.rs
672 lines 14 kB history raw
1use assert_cmd::Command;
3fn bat_with_config() -> Command {
4 let mut cmd = Command::cargo_bin("bat").unwrap();
5 cmd.current_dir("tests/examples");
6 cmd.env_remove("PAGER");
7 cmd.env_remove("BAT_PAGER");
8 cmd.env_remove("BAT_CONFIG_PATH");
9 cmd.env_remove("BAT_STYLE");
10 cmd.env_remove("BAT_THEME");
11 cmd.env_remove("BAT_TABS");
12 cmd
15fn bat() -> Command {
16 let mut cmd = bat_with_config();
17 cmd.arg("--no-config");
18 cmd
21#[test]
22fn basic() {
23 bat()
24 .arg("test.txt")
25 .assert()
26 .success()
27 .stdout("hello world\n")
28 .stderr("");
31#[test]
32fn stdin() {
33 bat()
34 .write_stdin("foo\nbar\n")
35 .assert()
36 .success()
37 .stdout("foo\nbar\n");
40#[test]
41fn concatenate() {
42 bat()
43 .arg("test.txt")
44 .arg("test.txt")
45 .assert()
46 .success()
47 .stdout("hello world\nhello world\n");
50#[test]
51fn concatenate_stdin() {
52 bat()
53 .arg("test.txt")
54 .arg("-")
55 .arg("test.txt")
56 .write_stdin("stdin\n")
57 .assert()
58 .success()
59 .stdout("hello world\nstdin\nhello world\n");
62#[test]
63fn concatenate_empty_first() {
64 bat()
65 .arg("empty.txt")
66 .arg("test.txt")
67 .assert()
68 .success()
69 .stdout("hello world\n");
72#[test]
73fn concatenate_empty_last() {
74 bat()
75 .arg("test.txt")
76 .arg("empty.txt")
77 .assert()
78 .success()
79 .stdout("hello world\n");
82#[test]
83fn concatenate_empty_both() {
84 bat()
85 .arg("empty.txt")
86 .arg("empty.txt")
87 .assert()
88 .success()
89 .stdout("");
92#[test]
93fn concatenate_empty_between() {
94 bat()
95 .arg("test.txt")
96 .arg("empty.txt")
97 .arg("test.txt")
98 .assert()
99 .success()
100 .stdout("hello world\nhello world\n");
103#[test]
104fn concatenate_empty_first_and_last() {
105 bat()
106 .arg("empty.txt")
107 .arg("test.txt")
108 .arg("empty.txt")
109 .assert()
110 .success()
111 .stdout("hello world\n");
114#[test]
115fn concatenate_single_line() {
116 bat()
117 .arg("single-line.txt")
118 .arg("single-line.txt")
119 .assert()
120 .success()
121 .stdout("Single LineSingle Line");
124#[test]
125fn concatenate_single_line_empty() {
126 bat()
127 .arg("single-line.txt")
128 .arg("empty.txt")
129 .arg("single-line.txt")
130 .assert()
131 .success()
132 .stdout("Single LineSingle Line");
135#[test]
136fn line_numbers() {
137 bat()
138 .arg("multiline.txt")
139 .arg("--style=numbers")
140 .arg("--decorations=always")
141 .assert()
142 .success()
143 .stdout(" 1 line 1\n 2 line 2\n 3 line 3\n 4 line 4\n");
146#[test]
147fn line_range_2_3() {
148 bat()
149 .arg("multiline.txt")
150 .arg("--line-range=2:3")
151 .assert()
152 .success()
153 .stdout("line 2\nline 3\n");
156#[test]
157fn line_range_first_two() {
158 bat()
159 .arg("multiline.txt")
160 .arg("--line-range=:2")
161 .assert()
162 .success()
163 .stdout("line 1\nline 2\n");
166#[test]
167fn line_range_last_3() {
168 bat()
169 .arg("multiline.txt")
170 .arg("--line-range=2:")
171 .assert()
172 .success()
173 .stdout("line 2\nline 3\nline 4\n");
176#[test]
177fn line_range_multiple() {
178 bat()
179 .arg("multiline.txt")
180 .arg("--line-range=1:2")
181 .arg("--line-range=4:4")
182 .assert()
183 .success()
184 .stdout("line 1\nline 2\nline 4\n");
187#[test]
188fn tabs_numbers() {
189 bat()
190 .arg("tabs.txt")
191 .arg("--tabs=4")
192 .arg("--style=numbers")
193 .arg("--decorations=always")
194 .assert()
195 .success()
196 .stdout(
197 " 1 1 2 3 4
198 2 1 ?
199 3 22 ?
200 4 333 ?
201 5 4444 ?
202 6 55555 ?
203 7 666666 ?
204 8 7777777 ?
205 9 88888888 ?
206",
207 );
210#[test]
211fn tabs_passthrough_wrapped() {
212 bat()
213 .arg("tabs.txt")
214 .arg("--tabs=0")
215 .arg("--style=plain")
216 .arg("--decorations=always")
217 .assert()
218 .success()
219 .stdout(
220 " 1 2 3 4
2211 ?
22222 ?
223333 ?
2244444 ?
22555555 ?
226666666 ?
2277777777 ?
22888888888 ?
229",
230 );
233#[test]
234fn tabs_4_wrapped() {
235 bat()
236 .arg("tabs.txt")
237 .arg("--tabs=4")
238 .arg("--style=plain")
239 .arg("--decorations=always")
240 .assert()
241 .success()
242 .stdout(
243 " 1 2 3 4
2441 ?
24522 ?
246333 ?
2474444 ?
24855555 ?
249666666 ?
2507777777 ?
25188888888 ?
252",
253 );
256#[test]
257fn tabs_8_wrapped() {
258 bat()
259 .arg("tabs.txt")
260 .arg("--tabs=8")
261 .arg("--style=plain")
262 .arg("--decorations=always")
263 .assert()
264 .success()
265 .stdout(
266 " 1 2 3 4
2671 ?
26822 ?
269333 ?
2704444 ?
27155555 ?
272666666 ?
2737777777 ?
27488888888 ?
275",
276 );
279#[test]
280fn tabs_passthrough() {
281 bat()
282 .arg("tabs.txt")
283 .arg("--tabs=0")
284 .arg("--style=plain")
285 .arg("--decorations=always")
286 .assert()
287 .success()
288 .stdout(
289 " 1 2 3 4
2901 ?
29122 ?
292333 ?
2934444 ?
29455555 ?
295666666 ?
2967777777 ?
29788888888 ?
298",
299 );
302#[test]
303fn tabs_4() {
304 bat()
305 .arg("tabs.txt")
306 .arg("--tabs=4")
307 .arg("--style=plain")
308 .arg("--decorations=always")
309 .assert()
310 .success()
311 .stdout(
312 " 1 2 3 4
3131 ?
31422 ?
315333 ?
3164444 ?
31755555 ?
318666666 ?
3197777777 ?
32088888888 ?
321",
322 );
325#[test]
326fn tabs_8() {
327 bat()
328 .arg("tabs.txt")
329 .arg("--tabs=8")
330 .arg("--style=plain")
331 .arg("--decorations=always")
332 .assert()
333 .success()
334 .stdout(
335 " 1 2 3 4
3361 ?
33722 ?
338333 ?
3394444 ?
34055555 ?
341666666 ?
3427777777 ?
34388888888 ?
344",
345 );
348#[test]
349fn fail_non_existing() {
350 bat().arg("non-existing-file").assert().failure();
353#[test]
354fn fail_directory() {
355 bat().arg("sub_directory").assert().failure();
358#[test]
359fn do_not_exit_directory() {
360 bat()
361 .arg("sub_directory")
362 .arg("test.txt")
363 .assert()
364 .stdout("hello world\n")
365 .failure();
368#[test]
369fn pager_basic() {
370 bat()
371 .env("PAGER", "echo pager-output")
372 .arg("--paging=always")
373 .arg("test.txt")
374 .assert()
375 .success()
376 .stdout("pager-output\n");
379#[test]
380fn pager_overwrite() {
381 bat()
382 .env("PAGER", "echo other-pager")
383 .env("BAT_PAGER", "echo pager-output")
384 .arg("--paging=always")
385 .arg("test.txt")
386 .assert()
387 .success()
388 .stdout("pager-output\n");
391#[test]
392fn pager_disable() {
393 bat()
394 .env("PAGER", "echo other-pager")
395 .env("BAT_PAGER", "")
396 .arg("--paging=always")
397 .arg("test.txt")
398 .assert()
399 .success()
400 .stdout("hello world\n");
403#[test]
404fn config_location_test() {
405 bat_with_config()
406 .env("BAT_CONFIG_PATH", "bat.conf")
407 .arg("--config-file")
408 .assert()
409 .success()
410 .stdout("bat.conf\n");
413#[test]
414fn config_read_arguments_from_file() {
415 bat_with_config()
416 .env("BAT_CONFIG_PATH", "bat.conf")
417 .arg("test.txt")
418 .assert()
419 .success()
420 .stdout("dummy-pager-from-config\n");
423#[test]
424fn utf16() {
425 // The output will be converted to UTF-8 with a leading UTF-8 BOM
426 bat()
427 .arg("--plain")
428 .arg("--decorations=always")
429 .arg("test_UTF-16LE.txt")
430 .assert()
431 .success()
432 .stdout(std::str::from_utf8(b"\xEF\xBB\xBFhello world\n").unwrap());
435#[test]
436fn can_print_file_named_cache() {
437 bat_with_config()
438 .arg("cache")
439 .assert()
440 .success()
441 .stdout("test\n")
442 .stderr("");
445#[test]
446fn can_print_file_named_cache_with_additional_argument() {
447 bat_with_config()
448 .arg("cache")
449 .arg("test.txt")
450 .assert()
451 .success()
452 .stdout("test\nhello world\n")
453 .stderr("");
456#[test]
457fn can_print_file_starting_with_cache() {
458 bat_with_config()
459 .arg("cache.c")
460 .assert()
461 .success()
462 .stdout("test\n")
463 .stderr("");
466#[test]
467fn does_not_print_unwanted_file_named_cache() {
468 bat_with_config().arg("cach").assert().failure();
471#[test]
472fn unicode_wrap() {
473 bat_with_config()
474 .arg("unicode-wrap.txt")
475 .arg("--style=numbers,snip")
476 .arg("--decorations=always")
477 .arg("--terminal-width=40")
478 .assert()
479 .success()
480 .stdout(
481 " 1 ビタミンA ビタミンD ビタミンE ビ
482 タミンK ビタミンB1 ビタミンB2 ナ
483 イアシン パントテン酸 ビタミンB6
484 ビタミンB12 葉酸 ビオチン ビタ
485 ミンC
486 2
487 3 고양이 고양이 고양이 고양이 고양이
488 고양이 고양이 고양이 고양이 고양이
489 고양이 고양이 고양이 고양이 고양이
490 고양이 고양이 고양이 고양이 고양이
491 고양이 고양이 고양이 고양이 고양이
492 고양이 고양이 고양이 고양이 고양이
493 고양이 고양이 고양이 고양이 고양이
494 고양이 고양이 고양이 고양이 고양이
495 고양이
496 4
497 5 1 บวก 2 บวก 3 บวก 4 บวก 5 บวก 6 บวก
498 7 บวก 8 บวก 9 บวก 10 บวก 11 บวก 12
499 บวก 13 บวก 14 บวก 15 บวก 16 บวก 17
500 บวก 18 บวก 19 บวก 20
501 6
502 7 Бельгия Болгария Чехия Дания Герман
503 ия Эстония Ирландия Греция Испания
504 Франция Хорватия Италия Кипр Латвия
505 Литва Люксембург Венгрия Мальта Ни
506 дерланды Австрия Польша Португалия
507 Румыния Словения Словакия Финляндия
508 Швеция Великобритания
509",
510 );
513#[test]
514fn snip() {
515 bat()
516 .arg("multiline.txt")
517 .arg("--style=numbers,snip")
518 .arg("--decorations=always")
519 .arg("--line-range=1:2")
520 .arg("--line-range=4:")
521 .arg("--terminal-width=80")
522 .assert()
523 .success()
524 .stdout(
525 " 1 line 1
526 2 line 2
527 ...─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ 8< ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
528 4 line 4
529",
530 );
533#[test]
534fn empty_file_leads_to_empty_output_with_grid_enabled() {
535 bat()
536 .arg("empty.txt")
537 .arg("--style=grid")
538 .arg("--decorations=always")
539 .arg("--terminal-width=80")
540 .assert()
541 .success()
542 .stdout("");
545#[test]
546fn filename_basic() {
547 bat()
548 .arg("test.txt")
549 .arg("--decorations=always")
550 .arg("--style=header")
551 .arg("-r=0:0")
552 .arg("--file-name=foo")
553 .assert()
554 .success()
555 .stdout("File: foo\n")
556 .stderr("");
559#[test]
560fn filename_binary() {
561 bat()
562 .arg("test.binary")
563 .arg("--decorations=always")
564 .arg("--style=header")
565 .arg("-r=0:0")
566 .arg("--file-name=foo")
567 .assert()
568 .success()
569 .stdout("File: foo <BINARY>\n")
570 .stderr("");
573#[test]
574fn filename_stdin() {
575 bat()
576 .arg("--decorations=always")
577 .arg("--style=header")
578 .arg("-r=0:0")
579 .arg("-")
580 .write_stdin("stdin\n")
581 .arg("--file-name=foo")
582 .assert()
583 .success()
584 .stdout("File: foo\n")
585 .stderr("");
588#[test]
589fn filename_stdin_binary() {
590 let vec = vec![0; 1];
591 bat_with_config()
592 .arg("--decorations=always")
593 .arg("--style=header")
594 .write_stdin(vec)
595 .arg("--file-name=foo")
596 .assert()
597 .success()
598 .stdout("File: foo <BINARY>\n")
599 .stderr("");
602#[test]
603fn filename_multiple_ok() {
604 bat()
605 .arg("--decorations=always")
606 .arg("--style=header")
607 .arg("-r=0:0")
608 .arg("test.txt")
609 .arg("--file-name=foo")
610 .arg("single-line.txt")
611 .arg("--file-name=bar")
612 .assert()
613 .success()
614 .stdout("File: foo\nFile: bar\n")
615 .stderr("");
618#[test]
619fn filename_multiple_err() {
620 bat()
621 .arg("--decorations=always")
622 .arg("--style=header")
623 .arg("-r=0:0")
624 .arg("test.txt")
625 .arg("--file-name=foo")
626 .arg("single-line.txt")
627 .assert()
628 .failure();
631#[cfg(target_os = "linux")]
632#[test]
633fn file_with_invalid_utf8_filename() {
634 use std::ffi::OsStr;
635 use std::fs::File;
636 use std::io::Write;
637 use std::os::unix::ffi::OsStrExt;
639 use tempdir::TempDir;
641 let tmp_dir = TempDir::new("bat_test").expect("can create temporary directory");
642 let file_path = tmp_dir
643 .path()
644 .join(OsStr::from_bytes(b"test-invalid-utf8-\xC3(.rs"));
645 {
646 let mut file = File::create(&file_path).expect("can create temporary file");
647 writeln!(file, "dummy content").expect("can write to file");
648 }
650 bat()
651 .arg(file_path.as_os_str())
652 .assert()
653 .success()
654 .stdout("dummy content\n");
657#[test]
658fn do_not_panic_regression_tests() {
659 for filename in &[
660 "issue_28.md",
661 "issue_190.md",
662 "issue_314.hs",
663 "issue_914.rb",
664 "issue_915.vue",
665 ] {
666 bat()
667 .arg("--color=always")
668 .arg(&format!("regression_tests/{}", filename))
669 .assert()
670 .success();
671 }