Skip to content

Commit c792c25

Browse files
committed
Use just two return codes to communicate result
1 parent e6015e4 commit c792c25

File tree

1 file changed

+26
-48
lines changed

1 file changed

+26
-48
lines changed

src/libtest/lib.rs

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use std::any::Any;
4747
use std::borrow::Cow;
4848
use std::cmp;
4949
use std::collections::BTreeMap;
50-
use std::convert::{TryFrom, TryInto};
5150
use std::env;
5251
use std::fmt;
5352
use std::fs::File;
@@ -70,6 +69,12 @@ const QUIET_MODE_MAX_COLUMN: usize = 100; // insert a '\n' after 100 tests in qu
7069

7170
const SECONDARY_TEST_INVOKER_VAR: &'static str = "__RUST_TEST_INVOKE";
7271

72+
// Return codes for secondary process.
73+
// Start somewhere other than 0 so we know the return code means what we think
74+
// it means.
75+
const TR_OK: i32 = 50;
76+
const TR_FAILED: i32 = 51;
77+
7378
// to be used by rustc to compile tests in libtest
7479
pub mod test {
7580
pub use crate::{
@@ -712,41 +717,6 @@ pub enum TestResult {
712717

713718
unsafe impl Send for TestResult {}
714719

715-
// Return codes for TestResult.
716-
// Start somewhere other than 0 so we know the return code means what we think
717-
// it means.
718-
const TR_OK: i32 = 50;
719-
const TR_FAILED: i32 = 51;
720-
const TR_IGNORED: i32 = 52;
721-
const TR_ALLOWED_FAIL: i32 = 53;
722-
723-
impl TryFrom<TestResult> for i32 {
724-
type Error = &'static str;
725-
fn try_from(val: TestResult) -> Result<i32, Self::Error> {
726-
Ok(match val {
727-
TrOk => TR_OK,
728-
TrFailed => TR_FAILED,
729-
TrIgnored => TR_IGNORED,
730-
TrAllowedFail => TR_ALLOWED_FAIL,
731-
TrFailedMsg(..) |
732-
TrBench(..) => return Err("can't convert variant to i32"),
733-
})
734-
}
735-
}
736-
737-
impl TryFrom<i32> for TestResult {
738-
type Error = &'static str;
739-
fn try_from(val: i32) -> Result<Self, Self::Error> {
740-
Ok(match val {
741-
TR_OK => TrOk,
742-
TR_FAILED => TrFailed,
743-
TR_IGNORED => TrIgnored,
744-
TR_ALLOWED_FAIL => TrAllowedFail,
745-
_ => return Err("unrecognized return code"),
746-
})
747-
}
748-
}
749-
750720
enum OutputLocation<T> {
751721
Pretty(Box<term::StdoutTerminal>),
752722
Raw(T),
@@ -1566,6 +1536,15 @@ fn calc_result<'a>(desc: &TestDesc,
15661536
}
15671537
}
15681538

1539+
fn get_result_from_exit_code(desc: &TestDesc, code: i32) -> TestResult {
1540+
match (desc.allow_fail, code) {
1541+
(_, TR_OK) => TrOk,
1542+
(true, TR_FAILED) => TrAllowedFail,
1543+
(false, TR_FAILED) => TrFailed,
1544+
(_, _) => TrFailedMsg(format!("got unexpected return code {}", code)),
1545+
}
1546+
}
1547+
15691548
fn run_test_in_process(desc: TestDesc,
15701549
nocapture: bool,
15711550
testfn: Box<dyn FnOnce() + Send>,
@@ -1616,11 +1595,9 @@ fn spawn_test_subprocess(desc: TestDesc, monitor_ch: Sender<MonitorMsg>) {
16161595
formatters::write_stderr_delimiter(&mut test_output, &desc.name);
16171596
test_output.extend_from_slice(&stderr);
16181597

1619-
let result = match (move || {
1598+
let result = match (|| -> Result<TestResult, String> {
16201599
let exit_code = get_exit_code(status)?;
1621-
TestResult::try_from(exit_code).map_err(|_| {
1622-
format!("child process returned unexpected exit code {}", exit_code)
1623-
})
1600+
Ok(get_result_from_exit_code(&desc, exit_code))
16241601
})() {
16251602
Ok(r) => r,
16261603
Err(e) => {
@@ -1645,18 +1622,19 @@ fn run_test_in_spawned_subprocess(desc: TestDesc, testfn: Box<dyn FnOnce() + Sen
16451622

16461623
// We don't support serializing TrFailedMsg, so just
16471624
// print the message out to stderr.
1648-
let test_result = match test_result {
1649-
TrFailedMsg(msg) => {
1650-
eprintln!("{}", msg);
1651-
TrFailed
1652-
}
1653-
_ => test_result,
1654-
};
1625+
if let TrFailedMsg(msg) = &test_result {
1626+
eprintln!("{}", msg);
1627+
}
16551628

16561629
if let Some(info) = panic_info {
16571630
builtin_panic_hook(info);
16581631
}
1659-
process::exit(test_result.try_into().unwrap());
1632+
1633+
if let TrOk = test_result {
1634+
process::exit(TR_OK);
1635+
} else {
1636+
process::exit(TR_FAILED);
1637+
}
16601638
});
16611639
let record_result2 = record_result.clone();
16621640
panic::set_hook(Box::new(move |info| record_result2(Some(&info))));

0 commit comments

Comments
 (0)