Skip to content

Commit d7ced0c

Browse files
committed
tests(cksum): Regroup common tests under a single macro
1 parent e6c067e commit d7ced0c

9 files changed

Lines changed: 178 additions & 781 deletions
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
//! This file centralizes common tests for standalone checksum uutils:
2+
//! - b2sum
3+
//! - md5sum
4+
//! - sha1sum
5+
//! - sha224sum
6+
//! - sha256sum
7+
//! - sha384sum
8+
//! - sha512sum
9+
10+
// spell-checker:ignore checkfile
11+
12+
macro_rules! test_digest_inner {
13+
($id:ident, $len:literal, $args:expr) => {
14+
mod $id {
15+
use uutests::util::*;
16+
use uutests::util_name;
17+
#[allow(unused)]
18+
static LENGTH_ARG: &'static str = concat!("--length=", stringify!($len));
19+
static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected");
20+
static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile");
21+
static INPUT_FILE: &'static str = "input.txt";
22+
23+
#[test]
24+
fn test_single_file() {
25+
let ts = TestScenario::new(util_name!());
26+
assert_eq!(
27+
ts.fixtures.read(EXPECTED_FILE),
28+
ts.ucmd()
29+
.arg(INPUT_FILE)
30+
.args($args)
31+
.succeeds()
32+
.no_stderr()
33+
.stdout_str()
34+
.split(' ')
35+
.next()
36+
.unwrap()
37+
);
38+
}
39+
40+
#[test]
41+
fn test_stdin() {
42+
let ts = TestScenario::new(util_name!());
43+
assert_eq!(
44+
ts.fixtures.read(EXPECTED_FILE),
45+
ts.ucmd()
46+
.args($args)
47+
.pipe_in_fixture(INPUT_FILE)
48+
.succeeds()
49+
.no_stderr()
50+
.stdout_str()
51+
.split(' ')
52+
.next()
53+
.unwrap()
54+
);
55+
}
56+
57+
#[test]
58+
fn test_check() {
59+
let ts = TestScenario::new(util_name!());
60+
println!("File content='{}'", ts.fixtures.read(INPUT_FILE));
61+
println!("Check file='{}'", ts.fixtures.read(CHECK_FILE));
62+
63+
ts.ucmd()
64+
.args($args)
65+
.args(&["--check", CHECK_FILE])
66+
.succeeds()
67+
.no_stderr()
68+
.stdout_is("input.txt: OK\n");
69+
}
70+
71+
#[test]
72+
fn test_zero() {
73+
let ts = TestScenario::new(util_name!());
74+
assert_eq!(
75+
ts.fixtures.read(EXPECTED_FILE),
76+
ts.ucmd()
77+
.args($args)
78+
.arg("--zero")
79+
.arg(INPUT_FILE)
80+
.succeeds()
81+
.no_stderr()
82+
.stdout_str()
83+
.split(' ')
84+
.next()
85+
.unwrap()
86+
);
87+
}
88+
89+
#[test]
90+
fn test_stdin_with_dash_directory() {
91+
let ts = TestScenario::new(util_name!());
92+
ts.fixtures.mkdir("-");
93+
assert_eq!(
94+
ts.fixtures.read(EXPECTED_FILE),
95+
ts.ucmd()
96+
.args($args)
97+
.pipe_in_fixture(INPUT_FILE)
98+
.succeeds()
99+
.no_stderr()
100+
.stdout_str()
101+
.split(' ')
102+
.next()
103+
.unwrap()
104+
);
105+
}
106+
107+
#[test]
108+
fn test_missing_file() {
109+
let ts = TestScenario::new(util_name!());
110+
let at = &ts.fixtures;
111+
112+
at.write("a", "file1\n");
113+
at.write("c", "file3\n");
114+
115+
ts.ucmd()
116+
.args($args)
117+
.args(&["a", "b", "c"])
118+
.fails()
119+
.stdout_contains("a\n")
120+
.stdout_contains("c\n")
121+
.stderr_contains("b: No such file or directory");
122+
}
123+
}
124+
};
125+
}
126+
pub(crate) use test_digest_inner;
127+
128+
macro_rules! test_digest {
129+
($id:ident) => {
130+
crate::common_checksum_tests::test_digest_inner!($id, 0, &[] as &[&str]);
131+
};
132+
($id:ident,$len:literal) => {
133+
crate::common_checksum_tests::test_digest_inner!($id, $len, &[LENGTH_ARG]);
134+
};
135+
}
136+
pub(crate) use test_digest;

tests/by-util/test_b2sum.rs

Lines changed: 3 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -5,125 +5,13 @@
55

66
use rstest::rstest;
77

8+
use crate::common_checksum_tests::test_digest;
89
use uutests::new_ucmd;
910
use uutests::util::TestScenario;
1011
use uutests::util_name;
11-
// spell-checker:ignore checkfile, testf, ntestf
12-
macro_rules! get_hash(
13-
($str:expr) => (
14-
$str.split(' ').collect::<Vec<&str>>()[0]
15-
);
16-
);
17-
18-
macro_rules! test_digest_with_len {
19-
($id:ident, $size:expr) => {
20-
mod $id {
21-
use uutests::util::*;
22-
use uutests::util_name;
23-
static LENGTH_ARG: &'static str = concat!("--length=", stringify!($size));
24-
static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected");
25-
static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile");
26-
static INPUT_FILE: &'static str = "input.txt";
27-
28-
#[test]
29-
fn test_single_file() {
30-
let ts = TestScenario::new(util_name!());
31-
assert_eq!(
32-
ts.fixtures.read(EXPECTED_FILE),
33-
get_hash!(
34-
ts.ucmd()
35-
.arg(LENGTH_ARG)
36-
.arg(INPUT_FILE)
37-
.succeeds()
38-
.no_stderr()
39-
.stdout_str()
40-
)
41-
);
42-
}
43-
44-
#[test]
45-
fn test_stdin() {
46-
let ts = TestScenario::new(util_name!());
47-
assert_eq!(
48-
ts.fixtures.read(EXPECTED_FILE),
49-
get_hash!(
50-
ts.ucmd()
51-
.arg(LENGTH_ARG)
52-
.pipe_in_fixture(INPUT_FILE)
53-
.succeeds()
54-
.no_stderr()
55-
.stdout_str()
56-
)
57-
);
58-
}
59-
60-
#[test]
61-
fn test_stdin_with_dash_directory() {
62-
let ts = TestScenario::new(util_name!());
63-
ts.fixtures.mkdir("-");
64-
assert_eq!(
65-
ts.fixtures.read(EXPECTED_FILE),
66-
get_hash!(
67-
ts.ucmd()
68-
.arg(LENGTH_ARG)
69-
.pipe_in_fixture(INPUT_FILE)
70-
.succeeds()
71-
.no_stderr()
72-
.stdout_str()
73-
)
74-
);
75-
}
76-
77-
#[test]
78-
fn test_check() {
79-
let ts = TestScenario::new(util_name!());
80-
println!("File content='{}'", ts.fixtures.read(INPUT_FILE));
81-
println!("Check file='{}'", ts.fixtures.read(CHECK_FILE));
82-
83-
ts.ucmd()
84-
.args(&[LENGTH_ARG, "--check", CHECK_FILE])
85-
.succeeds()
86-
.no_stderr()
87-
.stdout_is("input.txt: OK\n");
88-
}
89-
90-
#[test]
91-
fn test_zero() {
92-
let ts = TestScenario::new(util_name!());
93-
assert_eq!(
94-
ts.fixtures.read(EXPECTED_FILE),
95-
get_hash!(
96-
ts.ucmd()
97-
.arg(LENGTH_ARG)
98-
.arg("--zero")
99-
.arg(INPUT_FILE)
100-
.succeeds()
101-
.no_stderr()
102-
.stdout_str()
103-
)
104-
);
105-
}
106-
107-
#[test]
108-
fn test_missing_file() {
109-
let ts = TestScenario::new(util_name!());
110-
let at = &ts.fixtures;
111-
112-
at.write("a", "file1\n");
113-
at.write("c", "file3\n");
114-
115-
ts.ucmd()
116-
.args(&[LENGTH_ARG, "a", "b", "c"])
117-
.fails()
118-
.stdout_contains("a\n")
119-
.stdout_contains("c\n")
120-
.stderr_contains("b: No such file or directory");
121-
}
122-
}
123-
};
124-
}
12+
// spell-checker:ignore testf, ntestf
12513

126-
test_digest_with_len! {b2sum, 512}
14+
test_digest! {b2sum, 512}
12715

12816
#[test]
12917
fn test_check_b2sum_length_option_0() {

tests/by-util/test_md5sum.rs

Lines changed: 2 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -3,118 +3,11 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6+
use crate::common_checksum_tests::test_digest;
67
use uutests::new_ucmd;
78
use uutests::util::TestScenario;
89
use uutests::util_name;
9-
// spell-checker:ignore checkfile, testf, ntestf
10-
macro_rules! get_hash(
11-
($str:expr) => (
12-
$str.split(' ').collect::<Vec<&str>>()[0]
13-
);
14-
);
15-
16-
macro_rules! test_digest {
17-
($id:ident) => {
18-
mod $id {
19-
use uutests::util::*;
20-
use uutests::util_name;
21-
static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected");
22-
static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile");
23-
static INPUT_FILE: &'static str = "input.txt";
24-
25-
#[test]
26-
fn test_single_file() {
27-
let ts = TestScenario::new(util_name!());
28-
assert_eq!(
29-
ts.fixtures.read(EXPECTED_FILE),
30-
get_hash!(
31-
ts.ucmd()
32-
.arg(INPUT_FILE)
33-
.succeeds()
34-
.no_stderr()
35-
.stdout_str()
36-
)
37-
);
38-
}
39-
40-
#[test]
41-
fn test_stdin() {
42-
let ts = TestScenario::new(util_name!());
43-
assert_eq!(
44-
ts.fixtures.read(EXPECTED_FILE),
45-
get_hash!(
46-
ts.ucmd()
47-
.pipe_in_fixture(INPUT_FILE)
48-
.succeeds()
49-
.no_stderr()
50-
.stdout_str()
51-
)
52-
);
53-
}
54-
55-
#[test]
56-
fn test_stdin_with_dash_directory() {
57-
let ts = TestScenario::new(util_name!());
58-
ts.fixtures.mkdir("-");
59-
assert_eq!(
60-
ts.fixtures.read(EXPECTED_FILE),
61-
get_hash!(
62-
ts.ucmd()
63-
.pipe_in_fixture(INPUT_FILE)
64-
.succeeds()
65-
.no_stderr()
66-
.stdout_str()
67-
)
68-
);
69-
}
70-
71-
#[test]
72-
fn test_check() {
73-
let ts = TestScenario::new(util_name!());
74-
println!("File content='{}'", ts.fixtures.read(INPUT_FILE));
75-
println!("Check file='{}'", ts.fixtures.read(CHECK_FILE));
76-
77-
ts.ucmd()
78-
.args(&["--check", CHECK_FILE])
79-
.succeeds()
80-
.no_stderr()
81-
.stdout_is("input.txt: OK\n");
82-
}
83-
84-
#[test]
85-
fn test_zero() {
86-
let ts = TestScenario::new(util_name!());
87-
assert_eq!(
88-
ts.fixtures.read(EXPECTED_FILE),
89-
get_hash!(
90-
ts.ucmd()
91-
.arg("--zero")
92-
.arg(INPUT_FILE)
93-
.succeeds()
94-
.no_stderr()
95-
.stdout_str()
96-
)
97-
);
98-
}
99-
100-
#[test]
101-
fn test_missing_file() {
102-
let ts = TestScenario::new(util_name!());
103-
let at = &ts.fixtures;
104-
105-
at.write("a", "file1\n");
106-
at.write("c", "file3\n");
107-
108-
ts.ucmd()
109-
.args(&["a", "b", "c"])
110-
.fails()
111-
.stdout_contains("a\n")
112-
.stdout_contains("c\n")
113-
.stderr_contains("b: No such file or directory");
114-
}
115-
}
116-
};
117-
}
10+
// spell-checker:ignore testf, ntestf
11811

11912
test_digest! {md5}
12013

0 commit comments

Comments
 (0)