Skip to content

Commit 7c18432

Browse files
authored
Merge pull request #24 from mesalock-linux/tests/refactor
Refactor current tests and start to use `assert_cmd`
2 parents 679459a + 0c9293a commit 7c18432

19 files changed

+703
-1206
lines changed

Cargo.lock

Lines changed: 180 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ env_logger = { version = "0.5.11", optional = true }
104104
tempfile = "3.0.2"
105105
libc = "0.2.40"
106106
lazy_static = "1.0.1"
107+
assert_cmd = "0.6.0"
108+
assert_fs = "0.3.0"
109+
predicates = "0.5.2"
107110

108111
[profile.release]
109112
lto = true

ci/libmesabox-cov-rustc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash -e
2+
3+
get_crate_name()
4+
{
5+
while [[ $# -gt 1 ]] ; do
6+
v=$1
7+
case $v in
8+
--crate-name)
9+
echo $2
10+
return
11+
;;
12+
esac
13+
shift
14+
done
15+
}
16+
17+
case $(get_crate_name "$@") in
18+
libmesabox)
19+
EXTRA=$COVERAGE_OPTIONS
20+
;;
21+
*)
22+
;;
23+
esac
24+
25+
exec "$@" $EXTRA
File renamed without changes.

ci/run-cov

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
for path in /usr/lib/llvm-3.8/lib/clang/3.8.[0-9]/lib/linux/; do LLVM_PATH=$path; done
44
export COVERAGE_OPTIONS="-Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Cpasses=insert-gcov-profiling -Zno-landing-pads -L$LLVM_PATH -lclang_rt.profile-x86_64"
5-
export RUSTC_WRAPPER="./ci/cov-rustc"
65
export CARGO_INCREMENTAL=0
76

87
LCOVOPT="--gcov-tool ./ci/llvm-gcov --rc lcov_branch_coverage=1 --rc lcov_excl_line=assert"
@@ -12,25 +11,27 @@ LCOV="/usr/local/bin/lcov"
1211
rm -rf *.info *.gcda *.gcno
1312
cargo clean
1413

15-
# unit tests
16-
cargo rustc --all-features --profile test --lib
17-
rm ./target/debug/mesabox-*.d
18-
./target/debug/mesabox-*
19-
${LCOV} ${LCOVOPT} --capture --directory . --base-directory . -o mesabox.info
14+
# unit tests of libmesabox
15+
export RUSTC_WRAPPER="./ci/libmesabox-cov-rustc"
16+
cargo rustc --package libmesabox --all-features --profile test --lib
17+
rm ./target/debug/libmesabox-*.d
18+
./target/debug/libmesabox-*
19+
${LCOV} ${LCOVOPT} --capture --directory . --base-directory . -o libmesabox.info
2020

2121
# cleanup target
2222
rm -rf *.gcda *.gcno
2323
cargo clean
2424

2525
# integration tests
26+
export RUSTC_WRAPPER="./ci/mesabox-cov-rustc"
2627
cargo rustc --all-features --test tests
2728
rm ./target/debug/tests-*.d
2829
./target/debug/tests-*
2930
${LCOV} ${LCOVOPT} --capture --directory . --base-directory . -o tests.info
3031

3132
# combining and filtering
32-
${LCOV} ${LCOVOPT} --add mesabox.info --add tests.info -o coverage.info
33-
${LCOV} ${LCOVOPT} --extract coverage.info `find "$(cd src; pwd)" -name "*.rs"` -o final.info
33+
${LCOV} ${LCOVOPT} --add libmesabox.info --add tests.info -o coverage.info
34+
${LCOV} ${LCOVOPT} --extract coverage.info `find "$(cd src; pwd)" "$(cd libmesabox; pwd)" -name "*.rs"` -o final.info
3435

3536
# generate report if not in CI
3637
if [[ "$CI" != true ]]; then

tests/gnu/arch.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,37 @@
66
// For a copy, see the LICENSE file.
77
//
88

9-
use util::*;
9+
use std::process::Command;
10+
use assert_cmd::prelude::*;
1011

1112
const NAME: &str = "arch";
1213

1314
#[test]
1415
#[cfg(target_arch = "x86_64")]
1516
fn test_x86_64() {
16-
new_ucmd!()
17-
.succeeds()
18-
.stdout_only("x86_64\n");
17+
new_cmd!()
18+
.assert()
19+
.success()
20+
.stdout("x86_64\n")
21+
.stderr("");
1922
}
2023

2124
#[test]
2225
#[cfg(target_arch = "arm")]
2326
fn test_arm() {
24-
new_ucmd!()
25-
.succeeds()
26-
.stdout_only("arm\n");
27+
new_cmd!()
28+
.assert()
29+
.success()
30+
.stdout("arm\n")
31+
.stderr("");
2732
}
2833

2934
#[test]
3035
#[cfg(target_arch = "aarch64")]
3136
fn test_aarch64() {
32-
new_ucmd!()
33-
.succeeds()
34-
.stdout_only("aarch64\n");
37+
new_cmd!()
38+
.assert()
39+
.success()
40+
.stdout("aarch64\n")
41+
.stderr("");
3542
}

tests/gnu/base32.rs

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// Copyright (c) 2018, The MesaLock Linux Project Contributors
33
// All rights reserved.
4-
//
4+
//
55
// This work is licensed under the terms of the BSD 3-Clause License.
66
// For a copy, see the LICENSE file.
77
//
@@ -32,86 +32,97 @@
3232
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3333
//
3434

35-
use util::*;
35+
use assert_cmd::prelude::*;
36+
use predicates::prelude::*;
37+
use std::process::Command;
3638

3739
const NAME: &str = "base32";
3840

3941
#[test]
4042
fn test_encode() {
4143
let input = "Hello, World!";
42-
new_ucmd!()
43-
.pipe_in(input)
44-
.succeeds()
45-
.stdout_only("JBSWY3DPFQQFO33SNRSCC===\n");
44+
new_cmd!()
45+
.with_stdin().buffer(input)
46+
.assert()
47+
.success()
48+
.stdout("JBSWY3DPFQQFO33SNRSCC===\n")
49+
.stderr("");
4650
}
4751

4852
#[test]
4953
fn test_decode() {
5054
for decode_param in vec!["-d", "--decode"] {
5155
let input = "JBSWY3DPFQQFO33SNRSCC===\n";
52-
new_ucmd!()
56+
new_cmd!()
5357
.arg(decode_param)
54-
.pipe_in(input)
55-
.succeeds()
56-
.stdout_only("Hello, World!");
58+
.with_stdin().buffer(input)
59+
.assert()
60+
.success()
61+
.stdout("Hello, World!")
62+
.stderr("");
5763
}
5864
}
5965

6066
#[test]
6167
fn test_garbage() {
6268
let input = "aGVsbG8sIHdvcmxkIQ==\0";
63-
new_ucmd!()
69+
new_cmd!()
6470
.arg("-d")
65-
.pipe_in(input)
66-
.fails()
67-
.no_stdout()
68-
.stderr_contains("invalid length at 16");
71+
.with_stdin().buffer(input)
72+
.assert()
73+
.failure()
74+
.stdout("")
75+
.stderr(predicate::str::contains("invalid length at 16").from_utf8());
6976
}
7077

7178
#[test]
7279
fn test_ignore_garbage() {
7380
for ignore_garbage_param in vec!["-i", "--ignore-garbage"] {
7481
let input = "JBSWY\x013DPFQ\x02QFO33SNRSCC===\n";
75-
new_ucmd!()
76-
.arg("-d")
77-
.arg(ignore_garbage_param)
78-
.pipe_in(input)
79-
.succeeds()
80-
.stdout_only("Hello, World!");
82+
new_cmd!()
83+
.args(&["-d", ignore_garbage_param])
84+
.with_stdin().buffer(input)
85+
.assert()
86+
.success()
87+
.stdout("Hello, World!")
88+
.stderr("");
8189
}
8290
}
8391

8492
#[test]
8593
fn test_wrap() {
8694
for wrap_param in vec!["-w", "--wrap"] {
8795
let input = "The quick brown fox jumps over the lazy dog.";
88-
new_ucmd!()
89-
.arg(wrap_param)
90-
.arg("20")
91-
.pipe_in(input)
92-
.succeeds()
93-
.stdout_only("KRUGKIDROVUWG2ZAMJZG\n653OEBTG66BANJ2W24DT\nEBXXMZLSEB2GQZJANRQX\nU6JAMRXWOLQ=\n");
96+
new_cmd!()
97+
.args(&[wrap_param, "20"])
98+
.with_stdin().buffer(input)
99+
.assert()
100+
.success()
101+
.stdout("KRUGKIDROVUWG2ZAMJZG\n653OEBTG66BANJ2W24DT\nEBXXMZLSEB2GQZJANRQX\nU6JAMRXWOLQ=\n")
102+
.stderr("");
94103
}
95104
}
96105

97106
#[test]
98107
fn test_wrap_no_arg() {
99108
for wrap_param in vec!["-w", "--wrap"] {
100-
new_ucmd!()
109+
new_cmd!()
101110
.arg(wrap_param)
102-
.fails()
103-
.no_stdout()
104-
.stderr_contains("requires a value but none was supplied\n");
111+
.assert()
112+
.failure()
113+
.stdout("")
114+
.stderr(predicate::str::contains("requires a value but none was supplied\n").from_utf8());
105115
}
106116
}
107117

108118
#[test]
109119
fn test_wrap_bad_arg() {
110120
for wrap_param in vec!["-w", "--wrap"] {
111-
new_ucmd!()
112-
.arg(wrap_param).arg("b")
113-
.fails()
114-
.no_stdout()
115-
.stderr_contains("'b' is not a number\n");
121+
new_cmd!()
122+
.args(&[wrap_param, "b"])
123+
.assert()
124+
.failure()
125+
.stdout("")
126+
.stderr(predicate::str::contains("'b' is not a number\n").from_utf8());
116127
}
117128
}

0 commit comments

Comments
 (0)