-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathmain.rs
More file actions
140 lines (127 loc) · 4.52 KB
/
main.rs
File metadata and controls
140 lines (127 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#![allow(clippy::exit)]
use anyhow::Result;
use std::{
env,
process::{self, Command},
};
use tester::{
ColorConfig, DynTestName, OutputFormat, RunIgnored, ShouldPanic, TestDesc, TestDescAndFn,
TestFn, TestType, run_tests_console,
test::{TestOpts, parse_opts},
};
use tracing_subscriber::FmtSubscriber;
mod differ;
mod runner;
use runner::Runner;
fn main() -> Result<()> {
let subscriber = FmtSubscriber::builder()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.finish();
tracing::subscriber::set_global_default(subscriber).expect("Failed to set global subscriber");
let args: Vec<String> = env::args().collect();
let opts: TestOpts = match parse_opts(&args) {
Some(Ok(o)) => TestOpts {
test_threads: Some(1),
..o
},
Some(Err(e)) => {
eprintln!("Error parsing test options: {e}");
process::exit(1);
}
None => TestOpts {
list: false,
filters: vec![],
filter_exact: false,
force_run_in_process: false,
exclude_should_panic: false,
run_ignored: RunIgnored::No,
run_tests: true,
bench_benchmarks: false,
logfile: None,
nocapture: false,
color: ColorConfig::AutoColor,
format: OutputFormat::Pretty,
test_threads: Some(1),
skip: vec![],
time_options: None,
options: tester::Options {
display_output: true,
panic_abort: true,
},
},
};
// Find the manifest directory at compile time and locate tests in ../tests.
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let base = std::path::Path::new(manifest_dir)
.join("../tests")
.canonicalize()
.expect("Failed to canonicalize tests directory");
tracing::debug!("Using tests directory: {}", base.display());
let runner = Runner::new(base.clone());
let test_cases =
Runner::collect_test_dirs(&base).expect("Failed to collect test case directories");
if test_cases.is_empty() {
eprintln!("No valid tests found in {}", base.display());
process::exit(1);
}
// We build first to ensure that the tests are compiled before running them and to
// passthrough stdout and stderr from cargo to help debugging.
let mut cmd = Command::new("cargo");
let cmd = cmd.arg("build").arg("--release");
runner::forward_features(cmd);
cmd.current_dir(&base)
.stderr(process::Stdio::inherit())
.stdout(process::Stdio::inherit());
tracing::debug!("Running cargo command: {:?}", cmd);
let output = cmd.output().expect("build output");
let exit_code = output.status.code().unwrap_or(-1);
tracing::debug!("Cargo build exited with code {}", exit_code);
if !output.status.success() {
tracing::error!("Cargo build failed");
process::exit(exit_code);
}
let tests: Vec<TestDescAndFn> = test_cases
.into_iter()
.map(|case| {
let test_name = Runner::format_test_name(&case, &base);
TestDescAndFn {
desc: TestDesc {
name: DynTestName(test_name.clone()),
ignore: false,
should_panic: ShouldPanic::No,
allow_fail: false,
test_type: TestType::IntegrationTest,
},
testfn: TestFn::DynTestFn(Box::new({
let runner = runner.clone();
move || {
runner
.run_test_case(&case)
.unwrap_or_else(|e| panic!("{}", e));
}
})),
}
})
.collect();
// If filters are provided that look like paths (contain '/'), convert them to test names
let opts = if opts.filters.iter().any(|f| f.contains('/')) {
let mut new_opts = opts;
new_opts.filters = new_opts
.filters
.into_iter()
.map(|filter| {
if filter.contains('/') {
// Convert path-like filter to test name format
filter.replace('/', "::")
} else {
filter
}
})
.collect();
new_opts
} else {
opts
};
let passed = run_tests_console(&opts, tests).expect("Failed to run tests");
process::exit(if passed { 0 } else { 1 });
}