|
| 1 | +use rand::RngCore; |
| 2 | +use std::fs; |
| 3 | +use std::path::PathBuf; |
| 4 | + |
1 | 5 | use super::*;
|
2 | 6 |
|
3 | 7 | use crate::{
|
@@ -39,6 +43,36 @@ impl TestOpts {
|
39 | 43 | }
|
40 | 44 | }
|
41 | 45 |
|
| 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 | + |
42 | 76 | fn one_ignored_one_unignored_test() -> Vec<TestDescAndFn> {
|
43 | 77 | vec![
|
44 | 78 | TestDescAndFn {
|
@@ -922,3 +956,42 @@ fn test_dyn_bench_returning_err_fails_when_run_as_test() {
|
922 | 956 | let result = rx.recv().unwrap().result;
|
923 | 957 | assert_eq!(result, TrFailed);
|
924 | 958 | }
|
| 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