Skip to content

Commit 07a0ed8

Browse files
committed
tests(cksum): Regroup common tests under a single macro
1 parent 9846eb8 commit 07a0ed8

9 files changed

Lines changed: 152 additions & 662 deletions
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
macro_rules! test_digest_inner {
11+
($id:ident, $len:literal, $args:expr) => {
12+
mod $id {
13+
use uutests::util::*;
14+
use uutests::util_name;
15+
#[allow(unused)]
16+
static LENGTH_ARG: &'static str = concat!("--length=", stringify!($len));
17+
static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected");
18+
static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile");
19+
static INPUT_FILE: &'static str = "input.txt";
20+
21+
#[test]
22+
fn test_single_file() {
23+
let ts = TestScenario::new(util_name!());
24+
assert_eq!(
25+
ts.fixtures.read(EXPECTED_FILE),
26+
ts.ucmd()
27+
.arg(INPUT_FILE)
28+
.args($args)
29+
.succeeds()
30+
.no_stderr()
31+
.stdout_str()
32+
.split(' ')
33+
.next()
34+
.unwrap()
35+
);
36+
}
37+
38+
#[test]
39+
fn test_stdin() {
40+
let ts = TestScenario::new(util_name!());
41+
assert_eq!(
42+
ts.fixtures.read(EXPECTED_FILE),
43+
ts.ucmd()
44+
.args($args)
45+
.pipe_in_fixture(INPUT_FILE)
46+
.succeeds()
47+
.no_stderr()
48+
.stdout_str()
49+
.split(' ')
50+
.next()
51+
.unwrap()
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($args)
63+
.args(&["--check", CHECK_FILE])
64+
.succeeds()
65+
.no_stderr()
66+
.stdout_is("input.txt: OK\n");
67+
}
68+
69+
#[test]
70+
fn test_zero() {
71+
let ts = TestScenario::new(util_name!());
72+
assert_eq!(
73+
ts.fixtures.read(EXPECTED_FILE),
74+
ts.ucmd()
75+
.args($args)
76+
.arg("--zero")
77+
.arg(INPUT_FILE)
78+
.succeeds()
79+
.no_stderr()
80+
.stdout_str()
81+
.split(' ')
82+
.next()
83+
.unwrap()
84+
);
85+
}
86+
87+
#[test]
88+
fn test_missing_file() {
89+
let ts = TestScenario::new(util_name!());
90+
let at = &ts.fixtures;
91+
92+
at.write("a", "file1\n");
93+
at.write("c", "file3\n");
94+
95+
ts.ucmd()
96+
.args($args)
97+
.args(&["a", "b", "c"])
98+
.fails()
99+
.stdout_contains("a\n")
100+
.stdout_contains("c\n")
101+
.stderr_contains("b: No such file or directory");
102+
}
103+
}
104+
};
105+
}
106+
pub(crate) use test_digest_inner;
107+
108+
macro_rules! test_digest {
109+
($id:ident) => {
110+
crate::common_checksum_tests::test_digest_inner!($id, 0, &[] as &[&str]);
111+
};
112+
($id:ident,$len:literal) => {
113+
crate::common_checksum_tests::test_digest_inner!($id, $len, &[LENGTH_ARG]);
114+
};
115+
}
116+
pub(crate) use test_digest;

tests/by-util/test_b2sum.rs

Lines changed: 2 additions & 97 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;
1112
// 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-
}
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: 1 addition & 92 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;
910
// 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-
}
10211

10312
test_digest! {md5}
10413

0 commit comments

Comments
 (0)