Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor current tests and start to use assert_cmd #24

Merged
merged 23 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
47f09f9
tests/gnu/arch: change to assert_cli as CLI assertion util
Jul 12, 2018
80d4ed9
tests/gnu/base32: change to assert_cli as CLI assertion util
Jul 12, 2018
b0cc44b
tests: use new_cli macro to simplify creating assert command
Jul 12, 2018
6061fcd
tests/gnu/base64: change to assert_cli as CLI assertion util
Jul 12, 2018
7547355
tests: set tests/fixtures/util as current dir for assert cli
Jul 13, 2018
abdb172
tests/posix/cat: change to assert_cli as CLI assertion util
Jul 13, 2018
a4d6e33
tests: revert setting the default dir but provide a macro to get fixt…
Jul 13, 2018
105839f
tests: use assert_cmd and predicates instead of assert_cli
Jul 13, 2018
95b3766
tests: update assert_cmd and predicates and fix API changes
mssun Jul 20, 2018
a3fb6ec
tests/posix/test_head: start to use assert_cmd
mssun Jul 20, 2018
b36360d
tests/posix/test_head: forgot to assert success status
mssun Jul 20, 2018
f72f8bd
tests/posix/chmod: rewrite chmod testcases
Jul 31, 2018
a31a9ad
tests/posix/cat: delete unused mod
Jul 31, 2018
ead136e
tests/posix/sleep: rewrite sleep test cases
Jul 31, 2018
d085c75
Merge master changes into tests/refactor
Jul 31, 2018
126b03b
tests/posix/echo: rewrite echo with assert_cmd
Jul 31, 2018
1d28625
tests/posix/false: rewrite false with assert_cmd
Jul 31, 2018
2be4106
tests/posix/true: rewrite true with assert_cmd
Jul 31, 2018
48fd4cd
tests/posix/sleep: relax diff time
Jul 31, 2018
e5f8f17
tests/posix/sh: rewrite testcases of sh using assert_cmd
Jul 31, 2018
3de2bf7
CI: fix coverage script for libmesabox refactor
Jul 31, 2018
2a54f58
tests: delete tests framework and unused macros
Jul 31, 2018
0c9293a
CI: polish and simplify run-cov script
Aug 1, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tests: update assert_cmd and predicates and fix API changes
  • Loading branch information
mssun committed Jul 20, 2018
commit 95b37667700a489ae02156595669ff51964f5948
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ rustyline = { git = "https://github.com/kkawakam/rustyline", optional = true }
tempfile = "3.0.2"
libc = "0.2.40"
lazy_static = "1.0.1"
assert_cmd = "0.5.0"
predicates = "0.5.1"
assert_cmd = "0.6.0"
predicates = "0.5.2"

[profile.release]
lto = true
Expand Down
10 changes: 5 additions & 5 deletions tests/gnu/test_base32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::process::Command;
fn test_encode() {
let input = "Hello, World!";
new_cmd!()
.with_stdin(input)
.with_stdin().buffer(input)
.assert()
.success()
.stdout("JBSWY3DPFQQFO33SNRSCC===\n")
Expand All @@ -53,7 +53,7 @@ fn test_decode() {
let input = "JBSWY3DPFQQFO33SNRSCC===\n";
new_cmd!()
.arg(decode_param)
.with_stdin(input)
.with_stdin().buffer(input)
.assert()
.success()
.stdout("Hello, World!")
Expand All @@ -66,7 +66,7 @@ fn test_garbage() {
let input = "aGVsbG8sIHdvcmxkIQ==\0";
new_cmd!()
.arg("-d")
.with_stdin(input)
.with_stdin().buffer(input)
.assert()
.failure()
.stdout("")
Expand All @@ -79,7 +79,7 @@ fn test_ignore_garbage() {
let input = "JBSWY\x013DPFQ\x02QFO33SNRSCC===\n";
new_cmd!()
.args(&["-d", ignore_garbage_param])
.with_stdin(input)
.with_stdin().buffer(input)
.assert()
.success()
.stdout("Hello, World!")
Expand All @@ -93,7 +93,7 @@ fn test_wrap() {
let input = "The quick brown fox jumps over the lazy dog.";
new_cmd!()
.args(&[wrap_param, "20"])
.with_stdin(input)
.with_stdin().buffer(input)
.assert()
.success()
.stdout("KRUGKIDROVUWG2ZAMJZG\n653OEBTG66BANJ2W24DT\nEBXXMZLSEB2GQZJANRQX\nU6JAMRXWOLQ=\n")
Expand Down
14 changes: 7 additions & 7 deletions tests/gnu/test_base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::process::Command;
fn test_encode() {
let input = "hello, world!";
new_cmd!()
.with_stdin(input)
.with_stdin().buffer(input)
.assert()
.success()
.stdout("aGVsbG8sIHdvcmxkIQ==\n")
Expand All @@ -53,7 +53,7 @@ fn test_decode() {
let input = "aGVsbG8sIHdvcmxkIQ==";
new_cmd!()
.arg(decode_param)
.with_stdin(input)
.with_stdin().buffer(input)
.assert()
.success()
.stdout("hello, world!")
Expand All @@ -66,7 +66,7 @@ fn test_garbage() {
let input = "aGVsbG8sIHdvcmxkIQ==\0";
new_cmd!()
.arg("-d")
.with_stdin(input)
.with_stdin().buffer(input)
.assert()
.failure()
.stderr(predicate::str::contains("invalid length at 20").from_utf8())
Expand All @@ -79,7 +79,7 @@ fn test_ignore_garbage() {
let input = "aGVsbG8sIHdvcmxkIQ==\0";
new_cmd!()
.args(&["-d", ignore_garbage_param])
.with_stdin(input)
.with_stdin().buffer(input)
.assert()
.success()
.stdout("hello, world!")
Expand All @@ -93,7 +93,7 @@ fn test_wrap() {
let input = "The quick brown fox jumps over the lazy dog.";
new_cmd!()
.args(&[wrap_param, "20"])
.with_stdin(input)
.with_stdin().buffer(input)
.assert()
.success()
.stdout("VGhlIHF1aWNrIGJyb3du\nIGZveCBqdW1wcyBvdmVy\nIHRoZSBsYXp5IGRvZy4=\n")
Expand All @@ -109,7 +109,7 @@ fn test_wrap_no_arg() {
.assert()
.failure()
.stdout("")
.stderr(predicate::str::contains("requires a value but none was supplied").from_utf8());
.stderr(pred_str_contains!("requires a value but none was supplied"));
}
}

Expand All @@ -121,6 +121,6 @@ fn test_wrap_bad_arg() {
.assert()
.failure()
.stdout("")
.stderr(predicate::str::contains("'b' is not a number\n").from_utf8());
.stderr(pred_str_contains!("'b' is not a number\n"));
}
}
25 changes: 24 additions & 1 deletion tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ macro_rules! new_cmd {
}

#[macro_export]
macro_rules! fixtures_path {
macro_rules! fixtures_dir {
() => {{
use std::env;
let mut path = env::current_dir().unwrap();
Expand All @@ -121,3 +121,26 @@ macro_rules! at_and_ucmd {
(ts.fixtures.clone(), ts.ucmd())
}};
}

#[macro_export]
macro_rules! fixtures_path {
($filename:expr) => {{
let mut fixtures_path = fixtures_dir!();
fixtures_path.push($filename);
fixtures_path
}};
}

#[macro_export]
macro_rules! pred_eq_file {
($filename:expr) => {
predicate::path::eq_file(fixtures_path!($filename).as_path())
}
}

#[macro_export]
macro_rules! pred_str_contains {
($str:expr) => {
predicate::str::contains($str).from_utf8()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RE from_utf8

Right now, the only automatic conversions that happen in our API are u8 slice to predicate and str to predicate.

Feel free to open an issue about converting Predicate<str> into Predicate<[u8]>. or any other ideas to simplify the use of the API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this one bother me a lot. I think it could work, but realize it doesn't when compile the code. It took me some time to find out the reason because the documentation of assert_cmd and predicates are mixed. I should check with these two docs.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you open issues on any documentation problems you came across? We're wanting to make sure this is accessible for people starting a first Rust project to create a CLI. My challenge is the curse of knowledge: I'm too familiar with it to see know what pitfalls a new user will run into.

}
}
24 changes: 12 additions & 12 deletions tests/posix/test_cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use std::process::Command;
#[test]
fn test_output_multi_files_print_all_chars() {
new_cmd!()
.current_dir(fixtures_path!())
.current_dir(fixtures_dir!())
.args(&["alpha.txt", "256.txt", "-A", "-n"])
.assert()
.success()
Expand All @@ -59,7 +59,7 @@ fn test_output_multi_files_print_all_chars() {
#[test]
fn test_numbered_lines_no_trailing_newline() {
new_cmd!()
.current_dir(fixtures_path!())
.current_dir(fixtures_dir!())
.args(&["nonewline.txt", "alpha.txt", "-n"])
.assert()
.success()
Expand All @@ -73,7 +73,7 @@ fn test_with_stdin_show_nonprinting() {
for same_param in vec!["-v", "--show-nonprinting"] {
new_cmd!()
.args(&[same_param])
.with_stdin("\t\0\n")
.with_stdin().buffer("\t\0\n")
.assert()
.success()
.stdout("\t^@\n")
Expand All @@ -86,7 +86,7 @@ fn test_with_stdin_show_tabs() {
for same_param in vec!["-T", "--show-tabs"] {
new_cmd!()
.args(&[same_param])
.with_stdin("\t\0\n")
.with_stdin().buffer("\t\0\n")
.assert()
.success()
.stdout("^I\0\n")
Expand All @@ -100,7 +100,7 @@ fn test_with_stdin_show_ends() {
for same_param in vec!["-E", "--show-ends"] {
new_cmd!()
.args(&[same_param,"-"])
.with_stdin("\t\0\n")
.with_stdin().buffer("\t\0\n")
.assert()
.success()
.stdout("\t\0$\n")
Expand All @@ -113,7 +113,7 @@ fn test_with_stdin_show_all() {
for same_param in vec!["-A", "--show-all"] {
new_cmd!()
.args(&[same_param])
.with_stdin("\t\0\n")
.with_stdin().buffer("\t\0\n")
.assert()
.success()
.stdout("^I^@$\n")
Expand All @@ -125,7 +125,7 @@ fn test_with_stdin_show_all() {
fn test_with_stdin_nonprinting_and_endofline() {
new_cmd!()
.args(&["-e"])
.with_stdin("\t\0\n")
.with_stdin().buffer("\t\0\n")
.assert()
.success()
.stdout("\t^@$\n")
Expand All @@ -136,7 +136,7 @@ fn test_with_stdin_nonprinting_and_endofline() {
fn test_with_stdin_nonprinting_and_tabs() {
new_cmd!()
.args(&["-t"])
.with_stdin("\t\0\n")
.with_stdin().buffer("\t\0\n")
.assert()
.success()
.stdout("^I^@\n")
Expand All @@ -148,7 +148,7 @@ fn test_with_stdin_squeeze_blank() {
for same_param in vec!["-s", "--squeeze-blank"] {
new_cmd!()
.args(&[same_param])
.with_stdin("\n\na\n\n\n\n\nb\n\n\n")
.with_stdin().buffer("\n\na\n\n\n\n\nb\n\n\n")
.assert()
.success()
.stdout("\na\n\nb\n\n")
Expand All @@ -161,7 +161,7 @@ fn test_with_stdin_number_non_blank() {
for same_param in vec!["-b", "--number-nonblank"] {
new_cmd!()
.args(&[same_param, "-"])
.with_stdin("\na\nb\n\n\nc")
.with_stdin().buffer("\na\nb\n\n\nc")
.assert()
.success()
.stdout("\n 1\ta\n 2\tb\n\n\n 3\tc")
Expand All @@ -174,7 +174,7 @@ fn test_non_blank_overrides_number() {
for same_param in vec!["-b", "--number-nonblank"] {
new_cmd!()
.args(&[same_param, "-"])
.with_stdin("\na\nb\n\n\nc")
.with_stdin().buffer("\na\nb\n\n\nc")
.assert()
.success()
.stdout("\n 1\ta\n 2\tb\n\n\n 3\tc")
Expand All @@ -187,7 +187,7 @@ fn test_squeeze_blank_before_numbering() {
for same_param in vec!["-s", "--squeeze-blank"] {
new_cmd!()
.args(&[same_param, "-n", "-"])
.with_stdin("a\n\n\nb")
.with_stdin().buffer("a\n\n\nb")
.assert()
.success()
.stdout(" 1\ta\n 2\t\n 3\tb")
Expand Down