Skip to content

Commit c181108

Browse files
authored
fix(mimic)!: Remove libtest2-harness from public API (#114)
Planning to reduce the amount of policy in libtest2-harness and realized we should just decouple these packages.
2 parents 6f575b8 + deebe88 commit c181108

7 files changed

Lines changed: 150 additions & 69 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/libtest2-harness/src/harness.rs

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,67 +19,69 @@ impl Harness {
1919
Self { raw, cases: vec![] }
2020
}
2121

22-
pub fn case(mut self, case: impl Case + 'static) -> Self {
22+
pub fn case(&mut self, case: impl Case + 'static) {
2323
self.cases.push(Box::new(case));
24-
self
2524
}
2625

27-
pub fn cases(mut self, cases: impl IntoIterator<Item = impl Case + 'static>) -> Self {
26+
pub fn cases(&mut self, cases: impl IntoIterator<Item = impl Case + 'static>) {
2827
for case in cases {
2928
self.cases.push(Box::new(case));
3029
}
31-
self
3230
}
3331

34-
pub fn main(mut self) -> ! {
35-
let start = std::time::Instant::now();
32+
pub fn main(self) -> ! {
33+
main(self.raw, self.cases)
34+
}
35+
}
3636

37-
let raw = match self.raw {
38-
Ok(raw) => raw,
39-
Err(err) => {
40-
eprintln!("{err}");
41-
std::process::exit(1)
42-
}
43-
};
44-
let mut parser = cli::Parser::new(&raw);
45-
let opts = parse(&mut parser).unwrap_or_else(|err| {
46-
eprintln!("{err}");
47-
std::process::exit(1)
48-
});
37+
const ERROR_EXIT_CODE: i32 = 101;
4938

50-
#[cfg(feature = "color")]
51-
match opts.color {
52-
libtest_lexarg::ColorConfig::AutoColor => anstream::ColorChoice::Auto,
53-
libtest_lexarg::ColorConfig::AlwaysColor => anstream::ColorChoice::Always,
54-
libtest_lexarg::ColorConfig::NeverColor => anstream::ColorChoice::Never,
55-
}
56-
.write_global();
39+
fn main(raw: std::io::Result<Vec<std::ffi::OsString>>, mut cases: Vec<Box<dyn Case>>) -> ! {
40+
let start = std::time::Instant::now();
5741

58-
let mut notifier = notifier(&opts).unwrap_or_else(|err| {
59-
eprintln!("{err}");
60-
std::process::exit(1)
61-
});
62-
discover(&start, &opts, &mut self.cases, notifier.as_mut()).unwrap_or_else(|err| {
42+
let raw = match raw {
43+
Ok(raw) => raw,
44+
Err(err) => {
6345
eprintln!("{err}");
6446
std::process::exit(1)
65-
});
66-
67-
if !opts.list {
68-
match run(&start, &opts, self.cases, notifier.as_mut()) {
69-
Ok(true) => {}
70-
Ok(false) => std::process::exit(ERROR_EXIT_CODE),
71-
Err(e) => {
72-
eprintln!("error: io error when listing tests: {e:?}");
73-
std::process::exit(ERROR_EXIT_CODE)
74-
}
75-
}
7647
}
48+
};
49+
let mut parser = cli::Parser::new(&raw);
50+
let opts = parse(&mut parser).unwrap_or_else(|err| {
51+
eprintln!("{err}");
52+
std::process::exit(1)
53+
});
7754

78-
std::process::exit(0)
55+
#[cfg(feature = "color")]
56+
match opts.color {
57+
libtest_lexarg::ColorConfig::AutoColor => anstream::ColorChoice::Auto,
58+
libtest_lexarg::ColorConfig::AlwaysColor => anstream::ColorChoice::Always,
59+
libtest_lexarg::ColorConfig::NeverColor => anstream::ColorChoice::Never,
7960
}
80-
}
61+
.write_global();
8162

82-
const ERROR_EXIT_CODE: i32 = 101;
63+
let mut notifier = notifier(&opts).unwrap_or_else(|err| {
64+
eprintln!("{err}");
65+
std::process::exit(1)
66+
});
67+
discover(&start, &opts, &mut cases, notifier.as_mut()).unwrap_or_else(|err| {
68+
eprintln!("{err}");
69+
std::process::exit(1)
70+
});
71+
72+
if !opts.list {
73+
match run(&start, &opts, cases, notifier.as_mut()) {
74+
Ok(true) => {}
75+
Ok(false) => std::process::exit(ERROR_EXIT_CODE),
76+
Err(e) => {
77+
eprintln!("error: io error when listing tests: {e:?}");
78+
std::process::exit(ERROR_EXIT_CODE)
79+
}
80+
}
81+
}
82+
83+
std::process::exit(0)
84+
}
8385

8486
fn parse<'p>(parser: &mut cli::Parser<'p>) -> Result<libtest_lexarg::TestOpts, cli::LexError<'p>> {
8587
let mut test_opts = libtest_lexarg::TestOptsBuilder::new();

crates/libtest2-mimic/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ json = ["libtest2-harness/json"]
3030
threads = ["libtest2-harness/threads"]
3131

3232
[dependencies]
33+
libtest-json = { version = "0.0.1", path = "../libtest-json" }
3334
libtest2-harness = { version = "0.0.1", path = "../libtest2-harness" }
3435

3536
[dev-dependencies]

crates/libtest2-mimic/examples/mimic-simple.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ fn main() {
1515

1616
// Tests
1717

18-
fn check_toph(_context: &TestContext) -> RunResult {
18+
fn check_toph(_context: TestContext<'_>) -> RunResult {
1919
Ok(())
2020
}
21-
fn check_katara(_context: &TestContext) -> RunResult {
21+
fn check_katara(_context: TestContext<'_>) -> RunResult {
2222
Ok(())
2323
}
24-
fn check_sokka(_context: &TestContext) -> RunResult {
24+
fn check_sokka(_context: TestContext<'_>) -> RunResult {
2525
Err(RunError::fail("Sokka tripped and fell :("))
2626
}
27-
fn long_computation(context: &TestContext) -> RunResult {
27+
fn long_computation(context: TestContext<'_>) -> RunResult {
2828
context.ignore_for("slow")?;
2929

3030
std::thread::sleep(std::time::Duration::from_secs(1));
3131
Ok(())
3232
}
33-
fn compile_fail_dummy(_context: &TestContext) -> RunResult {
33+
fn compile_fail_dummy(_context: TestContext<'_>) -> RunResult {
3434
Ok(())
3535
}

crates/libtest2-mimic/src/lib.rs

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,51 @@
2424
#![warn(clippy::print_stderr)]
2525
#![warn(clippy::print_stdout)]
2626

27-
pub use libtest2_harness::Harness;
28-
pub use libtest2_harness::RunError;
29-
pub use libtest2_harness::RunResult;
30-
pub use libtest2_harness::TestContext;
31-
pub use libtest2_harness::TestKind;
27+
pub use libtest_json::RunMode;
3228

33-
use libtest2_harness::Case;
34-
use libtest2_harness::Source;
29+
pub struct Harness {
30+
harness: libtest2_harness::Harness,
31+
}
32+
33+
impl Harness {
34+
pub fn with_args(args: impl IntoIterator<Item = impl Into<std::ffi::OsString>>) -> Self {
35+
Self {
36+
harness: libtest2_harness::Harness::with_args(args),
37+
}
38+
}
39+
40+
pub fn with_env() -> Self {
41+
Self {
42+
harness: libtest2_harness::Harness::with_env(),
43+
}
44+
}
45+
46+
pub fn case(mut self, case: Trial) -> Self {
47+
self.harness.case(TrialCase { inner: case });
48+
self
49+
}
50+
51+
pub fn cases(mut self, cases: impl IntoIterator<Item = Trial>) -> Self {
52+
self.harness
53+
.cases(cases.into_iter().map(|c| TrialCase { inner: c }));
54+
self
55+
}
56+
57+
pub fn main(self) -> ! {
58+
self.harness.main()
59+
}
60+
}
3561

3662
pub struct Trial {
3763
name: String,
3864
#[allow(clippy::type_complexity)]
39-
runner: Box<dyn Fn(&TestContext) -> Result<(), RunError> + Send + Sync>,
65+
runner: Box<dyn Fn(TestContext<'_>) -> Result<(), RunError> + Send + Sync>,
4066
}
4167

4268
impl Trial {
4369
pub fn test(
4470
name: impl Into<String>,
45-
runner: impl Fn(&TestContext) -> Result<(), RunError> + Send + Sync + 'static,
71+
runner: impl Fn(TestContext<'_>) -> Result<(), RunError> + Send + Sync + 'static,
4672
) -> Self {
4773
Self {
4874
name: name.into(),
@@ -51,22 +77,71 @@ impl Trial {
5177
}
5278
}
5379

54-
impl Case for Trial {
80+
struct TrialCase {
81+
inner: Trial,
82+
}
83+
84+
impl libtest2_harness::Case for TrialCase {
5585
fn name(&self) -> &str {
56-
&self.name
86+
&self.inner.name
5787
}
58-
fn kind(&self) -> TestKind {
88+
fn kind(&self) -> libtest2_harness::TestKind {
5989
Default::default()
6090
}
61-
fn source(&self) -> Option<&Source> {
91+
fn source(&self) -> Option<&libtest2_harness::Source> {
6292
None
6393
}
64-
fn exclusive(&self, _: &TestContext) -> bool {
94+
fn exclusive(&self, _: &libtest2_harness::TestContext) -> bool {
6595
false
6696
}
6797

68-
fn run(&self, context: &TestContext) -> Result<(), RunError> {
69-
(self.runner)(context)
98+
fn run(
99+
&self,
100+
context: &libtest2_harness::TestContext,
101+
) -> Result<(), libtest2_harness::RunError> {
102+
(self.inner.runner)(TestContext { inner: context }).map_err(|e| e.inner)
103+
}
104+
}
105+
106+
pub type RunResult = Result<(), RunError>;
107+
108+
#[derive(Debug)]
109+
pub struct RunError {
110+
inner: libtest2_harness::RunError,
111+
}
112+
113+
impl RunError {
114+
pub fn with_cause(cause: impl std::error::Error + Send + Sync + 'static) -> Self {
115+
Self {
116+
inner: libtest2_harness::RunError::with_cause(cause),
117+
}
118+
}
119+
120+
pub fn fail(cause: impl std::fmt::Display) -> Self {
121+
Self {
122+
inner: libtest2_harness::RunError::fail(cause),
123+
}
124+
}
125+
}
126+
127+
#[derive(Debug)]
128+
pub struct TestContext<'t> {
129+
inner: &'t libtest2_harness::TestContext,
130+
}
131+
132+
impl<'t> TestContext<'t> {
133+
pub fn ignore(&self) -> Result<(), RunError> {
134+
self.inner.ignore().map_err(|e| RunError { inner: e })
135+
}
136+
137+
pub fn ignore_for(&self, reason: impl std::fmt::Display) -> Result<(), RunError> {
138+
self.inner
139+
.ignore_for(reason)
140+
.map_err(|e| RunError { inner: e })
141+
}
142+
143+
pub fn current_mode(&self) -> RunMode {
144+
self.inner.current_mode()
70145
}
71146
}
72147

crates/libtest2/examples/tidy.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use libtest2::Trial;
44

55
fn main() -> std::io::Result<()> {
66
let tests = collect_tests()?;
7-
libtest2::Harness::with_env().cases(tests).main()
7+
let mut harness = libtest2::Harness::with_env();
8+
harness.cases(tests);
9+
harness.main()
810
}
911

1012
/// Creates one test for each `.rs` file in the current directory or

crates/libtest2/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ impl Case for Trial {
7575
macro_rules! libtest2_main {
7676
( $( $test:path ),* $(,)*) => {
7777
fn main() {
78-
::libtest2::Harness::with_env()
79-
$(.case(::libtest2::Trial::test(::std::stringify!($test), $test)))*
80-
.main();
78+
let mut harness = ::libtest2::Harness::with_env();
79+
$(harness.case(::libtest2::Trial::test(::std::stringify!($test), $test));)*
80+
harness.main();
8181
}
8282
}
8383
}

0 commit comments

Comments
 (0)