Skip to content

Commit bfcfb04

Browse files
committed
test_more: use at_and_ucmd helper macro and add tests
1 parent f9e00d3 commit bfcfb04

File tree

1 file changed

+143
-99
lines changed

1 file changed

+143
-99
lines changed

tests/by-util/test_more.rs

Lines changed: 143 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
//
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
5+
56
use std::io::IsTerminal;
6-
#[cfg(target_family = "unix")]
7-
use uutests::at_and_ucmd;
8-
use uutests::new_ucmd;
9-
use uutests::util::TestScenario;
10-
use uutests::util_name;
7+
8+
use uutests::{at_and_ucmd, new_ucmd, util::TestScenario, util_name};
119

1210
#[test]
13-
fn test_more_no_arg() {
11+
fn test_no_arg() {
1412
if std::io::stdout().is_terminal() {
1513
new_ucmd!().fails().stderr_contains("more: bad usage");
1614
}
@@ -19,38 +17,31 @@ fn test_more_no_arg() {
1917
#[test]
2018
fn test_valid_arg() {
2119
if std::io::stdout().is_terminal() {
22-
let scene = TestScenario::new(util_name!());
23-
let at = &scene.fixtures;
24-
20+
let (at, mut ucmd) = at_and_ucmd!();
2521
let file = "test_file";
2622
at.touch(file);
2723

28-
scene.ucmd().arg(file).arg("-c").succeeds();
29-
scene.ucmd().arg(file).arg("--print-over").succeeds();
24+
ucmd.arg(file).arg("-c").succeeds();
25+
ucmd.arg(file).arg("--clean-print").succeeds();
3026

31-
scene.ucmd().arg(file).arg("-p").succeeds();
32-
scene.ucmd().arg(file).arg("--clean-print").succeeds();
27+
ucmd.arg(file).arg("-p").succeeds();
28+
ucmd.arg(file).arg("--print-over").succeeds();
3329

34-
scene.ucmd().arg(file).arg("-s").succeeds();
35-
scene.ucmd().arg(file).arg("--squeeze").succeeds();
30+
ucmd.arg(file).arg("-s").succeeds();
31+
ucmd.arg(file).arg("--squeeze").succeeds();
3632

37-
scene.ucmd().arg(file).arg("-u").succeeds();
38-
scene.ucmd().arg(file).arg("--plain").succeeds();
33+
ucmd.arg(file).arg("-u").succeeds();
34+
ucmd.arg(file).arg("--plain").succeeds();
3935

40-
scene.ucmd().arg(file).arg("-n").arg("10").succeeds();
41-
scene.ucmd().arg(file).arg("--lines").arg("0").succeeds();
42-
scene.ucmd().arg(file).arg("--number").arg("0").succeeds();
36+
ucmd.arg(file).arg("-n").arg("10").succeeds();
37+
ucmd.arg(file).arg("--lines").arg("0").succeeds();
38+
ucmd.arg(file).arg("--number").arg("0").succeeds();
4339

44-
scene.ucmd().arg(file).arg("-F").arg("10").succeeds();
45-
scene
46-
.ucmd()
47-
.arg(file)
48-
.arg("--from-line")
49-
.arg("0")
50-
.succeeds();
40+
ucmd.arg(file).arg("-F").arg("10").succeeds();
41+
ucmd.arg(file).arg("--from-line").arg("0").succeeds();
5142

52-
scene.ucmd().arg(file).arg("-P").arg("something").succeeds();
53-
scene.ucmd().arg(file).arg("--pattern").arg("-1").succeeds();
43+
ucmd.arg(file).arg("-P").arg("something").succeeds();
44+
ucmd.arg(file).arg("--pattern").arg("-1").succeeds();
5445
}
5546
}
5647

@@ -67,101 +58,147 @@ fn test_invalid_arg() {
6758
}
6859

6960
#[test]
70-
fn test_argument_from_file() {
61+
fn test_from_file_arg() {
7162
if std::io::stdout().is_terminal() {
72-
let scene = TestScenario::new(util_name!());
73-
let at = &scene.fixtures;
74-
63+
let (at, mut ucmd) = at_and_ucmd!();
7564
let file = "test_file";
65+
at.write(file, "1\n2\n3\n4\n5\n");
7666

77-
at.write(file, "1\n2");
78-
79-
// output all lines
80-
scene
81-
.ucmd()
82-
.arg("-F")
67+
// Output all lines
68+
ucmd.arg("-F")
8369
.arg("0")
8470
.arg(file)
8571
.succeeds()
8672
.no_stderr()
8773
.stdout_contains("1")
8874
.stdout_contains("2");
8975

90-
// output only the second line
91-
scene
92-
.ucmd()
93-
.arg("-F")
76+
// Output only the second line
77+
ucmd.arg("-F")
9478
.arg("2")
9579
.arg(file)
9680
.succeeds()
9781
.no_stderr()
9882
.stdout_contains("2")
9983
.stdout_does_not_contain("1");
84+
85+
// Start from beyond the last line
86+
ucmd.arg("-F")
87+
.arg("100")
88+
.arg(file)
89+
.succeeds()
90+
.no_stderr()
91+
.stdout_contains("5")
92+
.stdout_does_not_contain("4");
10093
}
10194
}
10295

10396
#[test]
104-
fn test_more_dir_arg() {
105-
// Run the test only if there's a valid terminal, else do nothing
106-
// Maybe we could capture the error, i.e. "Device not found" in that case
107-
// but I am leaving this for later
97+
fn test_lines_arg() {
10898
if std::io::stdout().is_terminal() {
109-
new_ucmd!()
110-
.arg(".")
99+
let (at, mut ucmd) = at_and_ucmd!();
100+
let file = "test_file";
101+
at.write(file, "1\n2\n3\n4\n5\n");
102+
103+
ucmd.arg("-n")
104+
.arg("2")
105+
.arg(file)
111106
.succeeds()
112-
.stderr_contains("'.' is a directory.");
107+
.no_stderr()
108+
.stdout_contains("1")
109+
.stdout_contains("2")
110+
.stdout_does_not_contain("3");
111+
112+
ucmd.arg("-n").arg("0").arg(file).succeeds().no_stderr();
113+
114+
ucmd.arg("--from-line")
115+
.arg("2")
116+
.arg("--lines")
117+
.arg("2")
118+
.arg(file)
119+
.succeeds()
120+
.no_stderr()
121+
.stdout_contains("2")
122+
.stdout_contains("3")
123+
.stdout_does_not_contain("1")
124+
.stdout_does_not_contain("4");
125+
126+
ucmd.arg("-P")
127+
.arg("3")
128+
.arg("-n")
129+
.arg("2")
130+
.arg(file)
131+
.succeeds()
132+
.no_stderr()
133+
.stdout_contains("3")
134+
.stdout_contains("4")
135+
.stdout_does_not_contain("5");
113136
}
114137
}
115138

116139
#[test]
117-
#[cfg(target_family = "unix")]
118-
fn test_more_invalid_file_perms() {
119-
use std::fs::{Permissions, set_permissions};
120-
use std::os::unix::fs::PermissionsExt;
121-
140+
fn test_display_formatting() {
122141
if std::io::stdout().is_terminal() {
123142
let (at, mut ucmd) = at_and_ucmd!();
124-
let permissions = Permissions::from_mode(0o244);
125-
at.make_file("invalid-perms.txt");
126-
set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap();
127-
ucmd.arg("invalid-perms.txt")
143+
144+
// Test squeeze blank lines
145+
let squeeze_file = "squeeze_test.txt";
146+
at.write(squeeze_file, "Line 1\n\n\n\nLine 2\n\n\nLine 3\n");
147+
148+
ucmd.arg("-s")
149+
.arg("-n")
150+
.arg("3")
151+
.arg(squeeze_file)
128152
.succeeds()
129-
.stderr_contains("permission denied");
153+
.no_stderr()
154+
.stdout_contains("Line 1")
155+
.stdout_contains("Line 2")
156+
.stdout_contains("Line 3");
157+
158+
// Test display modes
159+
let display_file = "display_test.txt";
160+
at.write(display_file, "Test content\nfor display modes.\n");
161+
162+
// Test clean print
163+
ucmd.arg("-c").arg(display_file).succeeds().no_stderr();
164+
165+
// Test print over
166+
ucmd.arg("-p").arg(display_file).succeeds().no_stderr();
167+
168+
// Test both together (should work, last one takes precedence)
169+
ucmd.arg("-c")
170+
.arg("-p")
171+
.arg(display_file)
172+
.succeeds()
173+
.no_stderr();
130174
}
131175
}
132176

133177
#[test]
134-
fn test_more_error_on_single_arg() {
178+
fn test_file_arg() {
179+
// Run the test only if there's a valid terminal, else do nothing
180+
// Maybe we could capture the error, i.e. "Device not found" in that case
181+
// but I am leaving this for later
135182
if std::io::stdout().is_terminal() {
136-
let ts = TestScenario::new("more");
137-
ts.fixtures.mkdir_all("folder");
138-
ts.ucmd()
139-
.arg("folder")
183+
let (at, mut ucmd) = at_and_ucmd!();
184+
185+
// Directory as argument
186+
ucmd.arg(".")
187+
.succeeds()
188+
.stderr_contains("'.' is a directory.");
189+
190+
// Single argument errors
191+
at.mkdir_all("folder");
192+
ucmd.arg("folder")
140193
.succeeds()
141194
.stderr_contains("is a directory");
142-
ts.ucmd()
143-
.arg("file1")
195+
196+
ucmd.arg("nonexistent_file")
144197
.succeeds()
145198
.stderr_contains("No such file or directory");
146-
}
147-
}
148199

149-
#[test]
150-
fn test_more_error_on_multiple_files() {
151-
if std::io::stdout().is_terminal() {
152-
let ts = TestScenario::new("more");
153-
ts.fixtures.mkdir_all("folder");
154-
ts.fixtures.make_file("file1");
155-
ts.ucmd()
156-
.arg("folder")
157-
.arg("file2")
158-
.arg("file1")
159-
.succeeds()
160-
.stderr_contains("folder")
161-
.stderr_contains("file2")
162-
.stdout_contains("file1");
163-
ts.ucmd()
164-
.arg("file2")
200+
// Multiple nonexistent files
201+
ucmd.arg("file2")
165202
.arg("file3")
166203
.succeeds()
167204
.stderr_contains("file2")
@@ -170,19 +207,31 @@ fn test_more_error_on_multiple_files() {
170207
}
171208

172209
#[test]
173-
fn test_more_pattern_found() {
210+
#[cfg(target_family = "unix")]
211+
fn test_invalid_file_perms() {
174212
if std::io::stdout().is_terminal() {
175-
let scene = TestScenario::new(util_name!());
176-
let at = &scene.fixtures;
213+
use std::fs::{Permissions, set_permissions};
214+
use std::os::unix::fs::PermissionsExt;
177215

178-
let file = "test_file";
216+
let (at, mut ucmd) = at_and_ucmd!();
217+
let permissions = Permissions::from_mode(0o244);
218+
at.make_file("invalid-perms.txt");
219+
set_permissions(at.plus("invalid-perms.txt"), permissions).unwrap();
220+
ucmd.arg("invalid-perms.txt")
221+
.succeeds()
222+
.stderr_contains("permission denied");
223+
}
224+
}
179225

226+
#[test]
227+
fn test_pattern_found() {
228+
if std::io::stdout().is_terminal() {
229+
let (at, mut ucmd) = at_and_ucmd!();
230+
let file = "test_file";
180231
at.write(file, "line1\nline2");
181232

182233
// output only the second line "line2"
183-
scene
184-
.ucmd()
185-
.arg("-P")
234+
ucmd.arg("-P")
186235
.arg("line2")
187236
.arg(file)
188237
.succeeds()
@@ -195,17 +244,12 @@ fn test_more_pattern_found() {
195244
#[test]
196245
fn test_more_pattern_not_found() {
197246
if std::io::stdout().is_terminal() {
198-
let scene = TestScenario::new(util_name!());
199-
let at = &scene.fixtures;
200-
247+
let (at, mut ucmd) = at_and_ucmd!();
201248
let file = "test_file";
202-
203249
let file_content = "line1\nline2";
204250
at.write(file, file_content);
205251

206-
scene
207-
.ucmd()
208-
.arg("-P")
252+
ucmd.arg("-P")
209253
.arg("something")
210254
.arg(file)
211255
.succeeds()

0 commit comments

Comments
 (0)