Skip to content

Commit fbb0165

Browse files
committed
tests(cksum): Regroup common tests under a single macro
1 parent 31bc9be commit fbb0165

9 files changed

Lines changed: 160 additions & 668 deletions
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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_missing_file() {
91+
let ts = TestScenario::new(util_name!());
92+
let at = &ts.fixtures;
93+
94+
at.write("a", "file1\n");
95+
at.write("c", "file3\n");
96+
97+
ts.ucmd()
98+
.args($args)
99+
.args(&["a", "b", "c"])
100+
.fails()
101+
.stdout_contains("a\n")
102+
.stdout_contains("c\n")
103+
.stderr_contains("b: No such file or directory");
104+
}
105+
}
106+
};
107+
}
108+
pub(crate) use test_digest_inner;
109+
110+
macro_rules! test_digest {
111+
($id:ident) => {
112+
crate::common_checksum_tests::test_digest_inner!($id, 0, &[] as &[&str]);
113+
};
114+
($id:ident,$len:literal) => {
115+
crate::common_checksum_tests::test_digest_inner!($id, $len, &[LENGTH_ARG]);
116+
};
117+
}
118+
pub(crate) use test_digest;

tests/by-util/test_b2sum.rs

Lines changed: 3 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,108 +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_check() {
62-
let ts = TestScenario::new(util_name!());
63-
println!("File content='{}'", ts.fixtures.read(INPUT_FILE));
64-
println!("Check file='{}'", ts.fixtures.read(CHECK_FILE));
65-
66-
ts.ucmd()
67-
.args(&[LENGTH_ARG, "--check", CHECK_FILE])
68-
.succeeds()
69-
.no_stderr()
70-
.stdout_is("input.txt: OK\n");
71-
}
72-
73-
#[test]
74-
fn test_zero() {
75-
let ts = TestScenario::new(util_name!());
76-
assert_eq!(
77-
ts.fixtures.read(EXPECTED_FILE),
78-
get_hash!(
79-
ts.ucmd()
80-
.arg(LENGTH_ARG)
81-
.arg("--zero")
82-
.arg(INPUT_FILE)
83-
.succeeds()
84-
.no_stderr()
85-
.stdout_str()
86-
)
87-
);
88-
}
89-
90-
#[test]
91-
fn test_missing_file() {
92-
let ts = TestScenario::new(util_name!());
93-
let at = &ts.fixtures;
94-
95-
at.write("a", "file1\n");
96-
at.write("c", "file3\n");
97-
98-
ts.ucmd()
99-
.args(&[LENGTH_ARG, "a", "b", "c"])
100-
.fails()
101-
.stdout_contains("a\n")
102-
.stdout_contains("c\n")
103-
.stderr_contains("b: No such file or directory");
104-
}
105-
}
106-
};
107-
}
12+
// spell-checker:ignore testf, ntestf
10813

109-
test_digest_with_len! {b2sum, 512}
14+
test_digest! {b2sum, 512}
11015

11116
#[test]
11217
fn test_check_b2sum_length_option_0() {

tests/by-util/test_md5sum.rs

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,102 +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_check() {
57-
let ts = TestScenario::new(util_name!());
58-
println!("File content='{}'", ts.fixtures.read(INPUT_FILE));
59-
println!("Check file='{}'", ts.fixtures.read(CHECK_FILE));
60-
61-
ts.ucmd()
62-
.args(&["--check", CHECK_FILE])
63-
.succeeds()
64-
.no_stderr()
65-
.stdout_is("input.txt: OK\n");
66-
}
67-
68-
#[test]
69-
fn test_zero() {
70-
let ts = TestScenario::new(util_name!());
71-
assert_eq!(
72-
ts.fixtures.read(EXPECTED_FILE),
73-
get_hash!(
74-
ts.ucmd()
75-
.arg("--zero")
76-
.arg(INPUT_FILE)
77-
.succeeds()
78-
.no_stderr()
79-
.stdout_str()
80-
)
81-
);
82-
}
83-
84-
#[test]
85-
fn test_missing_file() {
86-
let ts = TestScenario::new(util_name!());
87-
let at = &ts.fixtures;
88-
89-
at.write("a", "file1\n");
90-
at.write("c", "file3\n");
91-
92-
ts.ucmd()
93-
.args(&["a", "b", "c"])
94-
.fails()
95-
.stdout_contains("a\n")
96-
.stdout_contains("c\n")
97-
.stderr_contains("b: No such file or directory");
98-
}
99-
}
100-
};
101-
}
10+
// spell-checker:ignore testf, ntestf
10211

10312
test_digest! {md5}
10413

0 commit comments

Comments
 (0)