Debian Codegraph @

tests/by-util/test_dir.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 use regex::Regex;
7 use uutests::new_ucmd;
8 use uutests::util::TestScenario;
9 use uutests::util_name;
10
11 /*
12 * As dir use the same functions than ls, we don't have to retest them here.
13 * We just test the default and the long output
14 */
15
16 #[test]
17 fn test_dir() {
18 new_ucmd!().succeeds();
19 }
20
21 #[test]
22 fn test_default_output() {
23 let scene = TestScenario::new(util_name!());
24 let at = &scene.fixtures;
25 at.mkdir("some-dir1");
26 at.touch("some-file1");
27
28 scene.ucmd().succeeds().stdout_contains("some-file1");
29
30 scene
31 .ucmd()
32 .succeeds()
33 .stdout_does_not_match(&Regex::new("[rwx-]{10}.*some-file1$").unwrap());
34 }
35
36 #[test]
37 fn test_default_format_overrides() {
38 let scene = TestScenario::new(util_name!());
39 scene.fixtures.touch("file");
40 for (flag, expected) in [("-1", "file\n"), ("--zero", "file\0")] {
41 scene.ucmd().arg(flag).succeeds().stdout_only(expected);
42 }
43 for flag in ["--full-time", "--dired"] {
44 scene.ucmd().arg(flag).succeeds().stdout_contains("total 0");
45 }
46 }
47
48 #[test]
49 fn test_quoting_defaults() {
50 let scene = TestScenario::new(util_name!());
51 scene.fixtures.touch("a b");
52 scene.ucmd().succeeds().stdout_only("a\\ b\n");
53 scene.ucmd().arg("--zero").succeeds().stdout_only("a b\0");
54 scene
55 .ucmd()
56 .env("QUOTING_STYLE", "literal")
57 .succeeds()
58 .stdout_only("a b\n");
59 scene
60 .ucmd()
61 .env("QUOTING_STYLE", "literal")
62 .arg("-b")
63 .succeeds()
64 .stdout_only("a\\ b\n");
65 scene
66 .ucmd()
67 .arg("--dired")
68 .succeeds()
69 .stdout_contains(" a\\ b\n");
70 }
71
72 #[test]
73 #[cfg(unix)]
74 fn test_literal_quoting_on_terminal() {
75 let scene = TestScenario::new(util_name!());
76 scene.fixtures.touch("a\nb");
77 scene
78 .ucmd()
79 .env("QUOTING_STYLE", "literal")
80 .terminal_simulation(true)
81 .succeeds()
82 .stdout_only("a\r\nb\r\n");
83 }
84
85 #[test]
86 fn test_long_output() {
87 let scene = TestScenario::new(util_name!());
88 let at = &scene.fixtures;
89 at.mkdir("some-dir1");
90 at.touch("some-file1");
91
92 scene
93 .ucmd()
94 .arg("-l")
95 .succeeds()
96 .stdout_contains("some-file1");
97
98 scene
99 .ucmd()
100 .arg("-l")
101 .succeeds()
102 .stdout_matches(&Regex::new("[rwx-]{10}.*some-file1\n$").unwrap());
103 }
104
105 #[test]
106 fn test_invalid_option_exit_code() {
107 new_ucmd!().arg("-/").fails().code_is(2);
108 }
109
110 #[test]
111 fn test_help_shows_dir_not_ls() {
112 let result = new_ucmd!().arg("--help").succeeds();
113 let output = result.stdout_str();
114
115 // Verify help text contains "dir" in the usage line
116 assert!(
117 output.contains("dir [OPTION]"),
118 "Help should show 'dir [OPTION]'"
119 );
120
121 // Verify help text does not incorrectly show "ls"
122 assert!(
123 !output.contains("ls [OPTION]"),
124 "Help should not show 'ls [OPTION]'"
125 );
126 }
127
128 #[test]
129 fn test_version() {
130 new_ucmd!()
131 .arg("--version")
132 .succeeds()
133 .no_stderr()
134 .stdout_is(format!("dir {}\n", uucore::crate_version!()));
135 }
136
137 #[test]
138 #[cfg(target_os = "linux")]
139 fn test_write_error() {
140 let scene = TestScenario::new(util_name!());
141 scene.fixtures.touch("file");
142
143 scene
144 .ucmd()
145 .arg("file")
146 .set_stdout(std::fs::File::create("/dev/full").unwrap())
147 .fails()
148 .stderr_is("dir: write error: No space left on device\n");
149 }
150
151 #[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))]
152 mod diagnostics {
153 use super::*;
154
155 #[cfg(unix)]
156 #[test]
157 fn test_snippet_points_at_the_unknown_unit_of_block_size() {
158 let result = new_ucmd!()
159 .terminal_sim_stderr()
160 .arg("--block-size=1fb")
161 .fails_with_code(2);
162 let stderr = result.stderr_as_displayed();
163
164 // The report names the utility as it was called, not `ls`.
165 assert!(stderr.contains("dir:1:"), "{stderr}");
166 assert!(stderr.contains("not a known unit"), "{stderr}");
167 }
168
169 #[test]
170 fn test_plain_message_when_stderr_is_a_pipe() {
171 new_ucmd!()
172 .arg("--block-size=1fb")
173 .fails_with_code(2)
174 .stderr_is("dir: invalid --block-size argument '1fb'\n");
175 }
176 }