Skip to content

Commit 046451b

Browse files
committed
Add test test_logfile_format().
1 parent 7bdae13 commit 046451b

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5515,6 +5515,7 @@ dependencies = [
55155515
"libc",
55165516
"panic_abort",
55175517
"panic_unwind",
5518+
"rand",
55185519
"std",
55195520
]
55205521

library/test/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ core = { path = "../core" }
1010
panic_unwind = { path = "../panic_unwind" }
1111
panic_abort = { path = "../panic_abort" }
1212
libc = { version = "0.2.150", default-features = false }
13+
14+
[dev-dependencies]
15+
rand = { version = "0.8.5" }

library/test/src/tests.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use rand::RngCore;
2+
use std::fs;
3+
use std::path::PathBuf;
4+
15
use super::*;
26

37
use crate::{
@@ -39,6 +43,36 @@ impl TestOpts {
3943
}
4044
}
4145

46+
// These implementations of TempDir and tmpdir are forked from rust/library/std/src/sys_common/io.rs.
47+
struct TempDir(PathBuf);
48+
49+
impl TempDir {
50+
fn join(&self, path: &str) -> PathBuf {
51+
let TempDir(ref p) = *self;
52+
p.join(path)
53+
}
54+
}
55+
56+
impl Drop for TempDir {
57+
fn drop(&mut self) {
58+
let TempDir(ref p) = *self;
59+
let result = fs::remove_dir_all(p);
60+
// Avoid panicking while panicking as this causes the process to
61+
// immediately abort, without displaying test results.
62+
if !thread::panicking() {
63+
result.unwrap();
64+
}
65+
}
66+
}
67+
68+
fn tmpdir() -> TempDir {
69+
let p = env::temp_dir();
70+
let mut r = rand::thread_rng();
71+
let ret = p.join(&format!("rust-{}", r.next_u32()));
72+
fs::create_dir(&ret).unwrap();
73+
TempDir(ret)
74+
}
75+
4276
fn one_ignored_one_unignored_test() -> Vec<TestDescAndFn> {
4377
vec![
4478
TestDescAndFn {
@@ -922,3 +956,42 @@ fn test_dyn_bench_returning_err_fails_when_run_as_test() {
922956
let result = rx.recv().unwrap().result;
923957
assert_eq!(result, TrFailed);
924958
}
959+
960+
#[test]
961+
fn test_logfile_format() {
962+
let desc = TestDescAndFn {
963+
desc: TestDesc {
964+
name: StaticTestName("whatever"),
965+
ignore: false,
966+
ignore_message: None,
967+
source_file: "",
968+
start_line: 0,
969+
start_col: 0,
970+
end_line: 0,
971+
end_col: 0,
972+
should_panic: ShouldPanic::No,
973+
compile_fail: false,
974+
no_run: false,
975+
test_type: TestType::Unknown,
976+
},
977+
testfn: DynTestFn(Box::new(move || Ok(()))),
978+
};
979+
980+
let tmpdir = tmpdir();
981+
let output_path = &tmpdir.join("output.txt");
982+
983+
let opts = TestOpts {
984+
run_tests: true,
985+
logfile: Some(output_path.clone()),
986+
format: OutputFormat::Pretty,
987+
..TestOpts::new()
988+
};
989+
run_tests_console(&opts, vec![desc]).unwrap();
990+
991+
let contents = fs::read_to_string(output_path).expect("`--logfile` did not create file");
992+
993+
// Split output at line breaks to make the comparison platform-agnostic regarding newline style.
994+
let contents_lines = contents.as_str().lines().collect::<Vec<&str>>();
995+
996+
assert_eq!(contents_lines, vec!["ok whatever"]);
997+
}

0 commit comments

Comments
 (0)