Skip to content

Commit 3f92e1d

Browse files
authored
refactor: bump libtest-mimic (#3)
Signed-off-by: xxchan <[email protected]> Signed-off-by: xxchan <[email protected]>
1 parent 426fdd9 commit 3f92e1d

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ async-trait = "0.1"
1616
console = "0.15"
1717
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
1818
glob = "0.3"
19-
libtest-mimic = "0.4"
19+
libtest-mimic = "0.5"
2020
serde = { version = "1.0", features = ["derive"] }
21-
serde_yaml = "0.8"
21+
serde_yaml = "0.9"
2222
similar = "2"
2323
tokio = { version = "1", features = ["rt", "fs"] }
2424

src/test_runner.rs

+60-60
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::fmt;
22
use std::future::Future;
3-
use std::path::Path;
3+
use std::path::{Path, PathBuf};
44

5-
use anyhow::{anyhow, Context, Error, Result};
5+
use anyhow::{anyhow, Context, Result};
66
use console::style;
7-
use libtest_mimic::{run_tests, Arguments, Outcome, Test};
7+
use libtest_mimic::{Arguments, Failed, Trial};
88
use similar::{ChangeTag, TextDiff};
99
use tokio::runtime::Runtime;
1010

@@ -29,7 +29,7 @@ impl fmt::Display for Line {
2929
/// Test runner based on libtest-mimic.
3030
pub fn planner_test_runner<F, Ft, R>(path: impl AsRef<Path>, runner_fn: F) -> Result<()>
3131
where
32-
F: Fn() -> Ft + Send + Sync + 'static,
32+
F: Fn() -> Ft + Send + Sync + 'static + Clone,
3333
Ft: Future<Output = Result<R>> + Send,
3434
R: PlannerTestRunner,
3535
{
@@ -42,81 +42,81 @@ where
4242
let path = entry.context("failed to read glob entry")?;
4343
let filename = path.file_name().context("unable to extract filename")?;
4444
let testname = filename.to_str().context("unable to convert to string")?;
45-
tests.push(Test {
46-
name: testname
45+
46+
let nocapture = args.nocapture;
47+
let runner_fn = runner_fn.clone();
48+
49+
tests.push(Trial::test(
50+
testname
4751
.strip_suffix(TEST_SUFFIX)
4852
.unwrap()
4953
.replace('/', "_"),
50-
kind: "".into(),
51-
is_ignored: false,
52-
is_bench: false,
53-
data: path.clone(),
54-
});
54+
move || run(path, nocapture, runner_fn),
55+
));
5556
}
5657

5758
if tests.is_empty() {
5859
return Err(anyhow!("no test discovered"));
5960
}
6061

62+
libtest_mimic::run(&args, tests).exit();
63+
}
64+
65+
fn run<F, Ft, R>(path: PathBuf, nocapture: bool, runner_fn: F) -> Result<(), Failed>
66+
where
67+
F: Fn() -> Ft + Send + Sync + 'static + Clone,
68+
Ft: Future<Output = Result<R>> + Send,
69+
R: PlannerTestRunner,
70+
{
6171
fn build_runtime() -> Runtime {
6272
tokio::runtime::Builder::new_current_thread()
6373
.enable_all()
6474
.build()
6575
.unwrap()
6676
}
6777

68-
run_tests(&args, tests, move |case| {
69-
let path = case.data.clone();
70-
let runner_fn = &runner_fn;
71-
match build_runtime().block_on(async move {
72-
let mut runner = runner_fn().await?;
73-
let testcases = tokio::fs::read(&path).await?;
74-
let testcases: Vec<TestCase> = serde_yaml::from_slice(&testcases)?;
75-
let testcases = parse_test_cases(testcases)?;
76-
let mut generated_result = String::new();
77-
for testcase in testcases {
78-
let runner_result = runner.run(&testcase).await;
79-
generate_result(&testcase, &runner_result, &mut generated_result)?;
80-
}
81-
let path = {
82-
let mut path = path;
83-
path.set_extension(RESULT_SUFFIX);
84-
path
78+
build_runtime().block_on(async move {
79+
let mut runner = runner_fn().await?;
80+
let testcases = tokio::fs::read(&path).await?;
81+
let testcases: Vec<TestCase> = serde_yaml::from_slice(&testcases)?;
82+
let testcases = parse_test_cases(testcases)?;
83+
let mut generated_result = String::new();
84+
for testcase in testcases {
85+
let runner_result = runner.run(&testcase).await;
86+
generate_result(&testcase, &runner_result, &mut generated_result)?;
87+
}
88+
let path = {
89+
let mut path = path;
90+
path.set_extension(RESULT_SUFFIX);
91+
path
92+
};
93+
let expected_result = tokio::fs::read_to_string(&path).await?;
94+
95+
let diff = TextDiff::from_lines(&generated_result, &expected_result);
96+
97+
for change in diff.iter_all_changes() {
98+
use console::Style;
99+
let (sign, sty) = match change.tag() {
100+
ChangeTag::Delete => ("-", Style::new().red()),
101+
ChangeTag::Insert => ("+", Style::new().green()),
102+
ChangeTag::Equal => (" ", Style::new()),
85103
};
86-
let expected_result = tokio::fs::read_to_string(&path).await?;
87-
88-
let diff = TextDiff::from_lines(&generated_result, &expected_result);
89-
90-
for change in diff.iter_all_changes() {
91-
use console::Style;
92-
let (sign, sty) = match change.tag() {
93-
ChangeTag::Delete => ("-", Style::new().red()),
94-
ChangeTag::Insert => ("+", Style::new().green()),
95-
ChangeTag::Equal => (" ", Style::new()),
96-
};
97-
98-
if args.nocapture {
99-
print!(
100-
"{}{} {}{}",
101-
style(Line(change.old_index())).dim(),
102-
style(Line(change.new_index())).dim(),
103-
sty.apply_to(sign).bold(),
104-
sty.apply_to(change)
105-
);
106-
}
107-
}
108104

109-
if generated_result != expected_result {
110-
Err(anyhow!("test failed"))
111-
} else {
112-
Ok::<_, Error>(())
105+
if nocapture {
106+
print!(
107+
"{}{} {}{}",
108+
style(Line(change.old_index())).dim(),
109+
style(Line(change.new_index())).dim(),
110+
sty.apply_to(sign).bold(),
111+
sty.apply_to(change)
112+
);
113113
}
114-
}) {
115-
Ok(_) => Outcome::Passed,
116-
Err(err) => Outcome::Failed {
117-
msg: Some(format!("{:#}", err)),
118-
},
114+
}
115+
116+
if generated_result != expected_result {
117+
Err(Failed::without_message())
118+
} else {
119+
Ok(())
119120
}
120121
})
121-
.exit();
122122
}

0 commit comments

Comments
 (0)