Skip to content

Commit e7d0f78

Browse files
committed
Move to ui_test which supports our testing scheme out of the box
1 parent 7c8eba7 commit e7d0f78

File tree

5 files changed

+32
-79
lines changed

5 files changed

+32
-79
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
/target
1+
target
22
Cargo.lock

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ default = ["tracing-log"]
2222
[dev-dependencies]
2323
tracing = "0.1"
2424
glob = "0.3"
25-
assert_cmd = "1"
25+
ui_test = "0.7"
2626
log = "0.4"
2727

2828
[[test]]

test_dependencies/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "test_dependencies"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
tracing = "0.1"
8+
glob = "0.3"
9+
log = "0.4"
10+
tracing-subscriber = { version = "0.3", default-features = false, features = ["registry", "fmt", "std"] }
11+
tracing-tree = { path = ".." }
12+
tracing-log = "0.1"

test_dependencies/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/ui.rs

Lines changed: 17 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,19 @@
1-
use assert_cmd::prelude::*;
1+
use ui_test::{color_eyre::Result, run_tests, Config, Mode, OutputConflictHandling};
22

3-
use std::process::Command;
4-
5-
// Timings are flaky, so tests would spuriously fail.
6-
// Thus we replace all `/([0-9]+)ms/` with underscores
7-
fn replace_ms(data: &[u8]) -> Vec<u8> {
8-
let mut skip = false;
9-
let mut seen_s = false;
10-
let mut v: Vec<u8> = data
11-
.iter()
12-
.rev()
13-
.filter_map(|&b| match (b, skip, seen_s) {
14-
(b'0'..=b'9', true, _) => None,
15-
(_, true, _) => {
16-
skip = false;
17-
Some(b)
18-
}
19-
(b's', _, _) => {
20-
seen_s = true;
21-
Some(b)
22-
}
23-
(b'm', _, true) => {
24-
seen_s = false;
25-
skip = true;
26-
Some(b)
27-
}
28-
_ => Some(b),
29-
})
30-
.collect();
31-
v.reverse();
32-
v
33-
}
34-
35-
fn main() {
36-
for entry in glob::glob("examples/*.rs").expect("Failed to read glob pattern") {
37-
let entry = entry.unwrap();
38-
let mut cmd = Command::cargo_bin(entry.with_extension("").to_str().unwrap()).unwrap();
39-
let output = cmd.unwrap();
40-
let stderr = entry.with_extension("stderr");
41-
let stdout = entry.with_extension("stdout");
42-
43-
if std::env::args().any(|arg| arg == "--bless") {
44-
if output.stderr.is_empty() {
45-
let _ = std::fs::remove_file(stderr);
46-
} else {
47-
std::fs::write(stderr, replace_ms(&output.stderr)).unwrap();
48-
}
49-
if output.stdout.is_empty() {
50-
let _ = std::fs::remove_file(stdout);
51-
} else {
52-
std::fs::write(stdout, replace_ms(&output.stdout)).unwrap();
53-
}
54-
} else {
55-
if output.stderr.is_empty() {
56-
assert!(
57-
!stderr.exists(),
58-
"{} exists but there was no stderr output",
59-
stderr.display()
60-
);
61-
} else {
62-
assert!(
63-
std::fs::read(&stderr).unwrap() == replace_ms(&output.stderr),
64-
"{} is not the expected output, rerun the test with `cargo test -- -- --bless`",
65-
stderr.display()
66-
);
67-
}
68-
if output.stdout.is_empty() {
69-
assert!(!stdout.exists());
70-
} else {
71-
assert!(
72-
std::fs::read(&stdout).unwrap() == replace_ms(&output.stdout),
73-
"{} is not the expected output, rerun the test with `cargo test -- -- --bless`",
74-
stdout.display()
75-
);
76-
}
77-
}
78-
}
3+
fn main() -> Result<()> {
4+
let mut config = Config::default();
5+
config.root_dir = "examples".into();
6+
config.dependencies_crate_manifest_path = Some("test_dependencies/Cargo.toml".into());
7+
config.args.push("--cfg".into());
8+
config.args.push("feature=\"tracing-log\"".into());
9+
config.out_dir = Some("target/ui_test".into());
10+
config.mode = Mode::Run { exit_code: 0 };
11+
config.stdout_filter("[0-9]+(ms|s|m)", "$1");
12+
config.stderr_filter("[0-9]+(ms|s|m)", "$1");
13+
config.output_conflict_handling = if std::env::args().any(|arg| arg == "--bless") {
14+
OutputConflictHandling::Bless
15+
} else {
16+
OutputConflictHandling::Error
17+
};
18+
run_tests(config)
7919
}

0 commit comments

Comments
 (0)