-
Notifications
You must be signed in to change notification settings - Fork 40
Fix handling of ignore
in wait() and wait_with_all()
#82
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
Changes from all commits
5e77f2b
9aa8698
61e0a80
2c7fc8c
7a8dbd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,13 +151,126 @@ fn test_pipe() { | |
let wc_cmd = "wc"; | ||
assert!(run_cmd!(ls | $wc_cmd).is_ok()); | ||
|
||
// test pipefail | ||
assert!(run_cmd!(false | true).is_err()); | ||
assert!(run_fun!(false | true).is_err()); | ||
assert!(run_fun!(ignore false | true).is_ok()); | ||
set_pipefail(false); | ||
assert!(run_fun!(false | true).is_ok()); | ||
set_pipefail(true); | ||
// test `ignore` command and pipefail mode | ||
// FIXME: make set_pipefail() thread safe, then move this to a separate test_ignore_and_pipefail() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you thought about using sealed_test to make the tests work ? Or we can make the CI to run tests sequentially There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could do that, but i think it’s possible to fix the thread unsafety, and doing so would make the API more reliable. i’ll do that in a separate patch. |
||
struct TestCase { | ||
/// Run the test case, returning whether the result `.is_ok()`. | ||
code: fn() -> bool, | ||
/// Stringified version of `code`, for identifying assertion failures. | ||
code_str: &'static str, | ||
/// Do we expect `.is_ok()` when pipefail is on? | ||
expected_ok_pipefail_on: bool, | ||
/// Do we expect `.is_ok()` when pipefail is off? | ||
expected_ok_pipefail_off: bool, | ||
} | ||
/// Make a function for [TestCase::code]. | ||
/// | ||
/// Usage: `code!((macro!(command)).extra)` | ||
/// - `(macro!(command)).extra` is an expression of type CmdResult | ||
macro_rules! code { | ||
(($macro:tt $bang:tt ($($command:tt)+)) $($after:tt)*) => { | ||
|| $macro$bang($($command)+)$($after)*.is_ok() | ||
}; | ||
} | ||
/// Make a string for [TestCase::code_str]. | ||
/// | ||
/// Usage: `code_str!((macro!(command)).extra)` | ||
/// - `(macro!(command)).extra` is an expression of type CmdResult | ||
macro_rules! code_str { | ||
(($macro:tt $bang:tt ($($command:tt)+)) $($after:tt)*) => { | ||
stringify!($macro$bang($($command)+)$($after)*.is_ok()) | ||
}; | ||
} | ||
/// Make a [TestCase]. | ||
/// Usage: `test_case!(true/false, true/false, (macro!(command)).extra)` | ||
/// - the first `true/false` is TestCase::expected_ok_pipefail_on | ||
/// - the second `true/false` is TestCase::expected_ok_pipefail_off | ||
/// - `(macro!(command)).extra` is an expression of type CmdResult | ||
macro_rules! test_case { | ||
($expected_ok_pipefail_on:expr, $expected_ok_pipefail_off:expr, ($macro:tt $bang:tt ($($command:tt)+)) $($after:tt)*) => { | ||
TestCase { | ||
code: code!(($macro $bang ($($command)+)) $($after)*), | ||
code_str: code_str!(($macro $bang ($($command)+)) $($after)*), | ||
expected_ok_pipefail_on: $expected_ok_pipefail_on, | ||
expected_ok_pipefail_off: $expected_ok_pipefail_off, | ||
} | ||
}; | ||
} | ||
/// Generate test cases for the given entry point. | ||
/// For each test case, every entry point should yield the same results. | ||
macro_rules! test_cases_for_entry_point { | ||
(($macro:tt $bang:tt (...)) $($after:tt)*) => { | ||
&[ | ||
// Use result of last command in pipeline, if all others exit successfully. | ||
test_case!(true, true, ($macro $bang (true)) $($after)*), | ||
test_case!(false, false, ($macro $bang (false)) $($after)*), | ||
test_case!(true, true, ($macro $bang (true | true)) $($after)*), | ||
test_case!(false, false, ($macro $bang (true | false)) $($after)*), | ||
// Use failure of other commands, if pipefail is on. | ||
test_case!(false, true, ($macro $bang (false | true)) $($after)*), | ||
// Use failure of last command in pipeline. | ||
test_case!(false, false, ($macro $bang (false | false)) $($after)*), | ||
// Ignore all failures, when using `ignore` command. | ||
test_case!(true, true, ($macro $bang (ignore true)) $($after)*), | ||
test_case!(true, true, ($macro $bang (ignore false)) $($after)*), | ||
test_case!(true, true, ($macro $bang (ignore true | true)) $($after)*), | ||
test_case!(true, true, ($macro $bang (ignore true | false)) $($after)*), | ||
test_case!(true, true, ($macro $bang (ignore false | true)) $($after)*), | ||
test_case!(true, true, ($macro $bang (ignore false | false)) $($after)*), | ||
] | ||
}; | ||
} | ||
|
||
let test_cases: &[&[TestCase]] = &[ | ||
test_cases_for_entry_point!((run_cmd!(...))), | ||
test_cases_for_entry_point!((run_fun!(...)).map(|_stdout| ())), | ||
test_cases_for_entry_point!((spawn!(...)).unwrap().wait()), | ||
test_cases_for_entry_point!((spawn_with_output!(...)).unwrap().wait_with_all().0), | ||
test_cases_for_entry_point!((spawn_with_output!(...)) | ||
.unwrap() | ||
.wait_with_output() | ||
.map(|_stdout| ())), | ||
test_cases_for_entry_point!((spawn_with_output!(...)) | ||
.unwrap() | ||
.wait_with_raw_output(&mut vec![])), | ||
// FIXME: wait_with_pipe() is currently busted | ||
// test_cases_for_entry_point!((spawn_with_output!(...)) | ||
// .unwrap() | ||
// .wait_with_pipe(&mut |_stdout| {})), | ||
]; | ||
|
||
macro_rules! check_eq { | ||
($left:expr, $right:expr, $($rest:tt)+) => {{ | ||
let left = $left; | ||
let right = $right; | ||
if left != right { | ||
eprintln!("assertion failed ({} != {}): {}", left, right, format!($($rest)+)); | ||
false | ||
} else { | ||
true | ||
} | ||
}}; | ||
} | ||
|
||
let mut ok = true; | ||
for case in test_cases.iter().flat_map(|items| items.iter()) { | ||
ok &= check_eq!( | ||
(case.code)(), | ||
case.expected_ok_pipefail_on, | ||
"{} when pipefail is on", | ||
case.code_str | ||
); | ||
set_pipefail(false); | ||
ok &= check_eq!( | ||
(case.code)(), | ||
case.expected_ok_pipefail_off, | ||
"{} when pipefail is off", | ||
case.code_str | ||
); | ||
set_pipefail(true); | ||
} | ||
|
||
assert!(ok); | ||
} | ||
|
||
#[test] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is precise and thanks for the clean up.