Debian Codegraph @

tests/by-util/test_sum.rs

raw
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 #[cfg(target_os = "linux")]
7 use std::os::unix::ffi::OsStringExt;
8 use uutests::at_and_ucmd;
9 use uutests::new_ucmd;
10
11 #[test]
12 fn test_invalid_arg() {
13 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
14 }
15
16 #[test]
17 fn test_bsd_single_file() {
18 new_ucmd!()
19 .arg("lorem_ipsum.txt")
20 .succeeds()
21 .stdout_only_fixture("bsd_single_file.expected");
22 }
23
24 #[test]
25 fn test_bsd_multiple_files() {
26 new_ucmd!()
27 .arg("lorem_ipsum.txt")
28 .arg("alice_in_wonderland.txt")
29 .succeeds()
30 .stdout_only_fixture("bsd_multiple_files.expected");
31 }
32
33 #[test]
34 fn test_bsd_stdin() {
35 new_ucmd!()
36 .pipe_in_fixture("lorem_ipsum.txt")
37 .succeeds()
38 .stdout_only_fixture("bsd_stdin.expected");
39 }
40
41 #[test]
42 fn test_sysv_single_file() {
43 new_ucmd!()
44 .arg("-s")
45 .arg("lorem_ipsum.txt")
46 .succeeds()
47 .stdout_only_fixture("sysv_single_file.expected");
48 }
49
50 #[test]
51 fn test_sysv_multiple_files() {
52 new_ucmd!()
53 .arg("-s")
54 .arg("lorem_ipsum.txt")
55 .arg("alice_in_wonderland.txt")
56 .succeeds()
57 .stdout_only_fixture("sysv_multiple_files.expected");
58 }
59
60 #[test]
61 fn test_sysv_stdin() {
62 new_ucmd!()
63 .arg("-s")
64 .pipe_in_fixture("lorem_ipsum.txt")
65 .succeeds()
66 .stdout_only_fixture("sysv_stdin.expected");
67 }
68
69 #[test]
70 fn test_invalid_file() {
71 let (at, mut ucmd) = at_and_ucmd!();
72
73 at.mkdir("a");
74
75 ucmd.arg("a").fails().stderr_is("sum: a: Is a directory\n");
76 }
77
78 #[test]
79 fn test_invalid_metadata() {
80 let (_, mut ucmd) = at_and_ucmd!();
81
82 ucmd.arg("b")
83 .fails()
84 .stderr_is("sum: b: No such file or directory\n");
85 }
86
87 #[test]
88 #[cfg(target_os = "linux")]
89 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
90 fn test_sum_non_utf8_paths() {
91 let (at, mut ucmd) = at_and_ucmd!();
92
93 let filename = std::ffi::OsString::from_vec(vec![0xFF, 0xFE]);
94 std::fs::write(at.plus(&filename), b"test content").unwrap();
95
96 ucmd.arg(&filename).succeeds();
97 }
98
99 #[test]
100 fn test_filename_ends_with_slash() {
101 let (at, mut ucmd) = at_and_ucmd!();
102
103 at.touch("a");
104
105 ucmd.arg("a/")
106 .fails_with_code(1)
107 .stderr_is("sum: a/: Not a directory\n");
108 }
109
110 #[cfg(all(unix, not(target_vendor = "apple"), not(target_os = "openbsd")))]
111 #[cfg_attr(wasi_runner, ignore)]
112 #[test]
113 fn test_filename_proc_self_mem() {
114 // https://github.com/uutils/coreutils/issues/12949
115 let result = new_ucmd!().arg("/proc/self/mem").fails_with_code(1);
116
117 let stderr = result.stderr_str();
118
119 let input_output = "sum: /proc/self/mem: Input/output error\n";
120 let io = "sum: /proc/self/mem: I/O error\n";
121
122 assert!(stderr == input_output || stderr == io);
123 }