Skip to content

Commit d8f8c12

Browse files
committed
restore compatibility with &Path
1 parent e627889 commit d8f8c12

File tree

4 files changed

+85
-15
lines changed

4 files changed

+85
-15
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [0.2.2] - 2023-08-29
4+
5+
### Added
6+
7+
- Restored compatibility with `fn(&Path) -> Result<()>`. The harness now can take either `fn(&Path) -> Result<()>` or `fn(&Utf8Path) -> Result<()>`.
8+
39
## [0.2.1] - 2023-08-29
410

511
### Changed
@@ -52,7 +58,8 @@ There are no functional changes in this release.
5258

5359
(Version 0.1.0 was yanked because of a metadata issue.)
5460

55-
[0.2.0]: https://github.com/nextest-rs/datatest-stable/releases/tag/datatest-stable-0.2.1
61+
[0.2.2]: https://github.com/nextest-rs/datatest-stable/releases/tag/datatest-stable-0.2.2
62+
[0.2.1]: https://github.com/nextest-rs/datatest-stable/releases/tag/datatest-stable-0.2.1
5663
[0.1.3]: https://github.com/nextest-rs/datatest-stable/releases/tag/datatest-stable-0.1.3
5764
[0.1.2]: https://github.com/nextest-rs/datatest-stable/releases/tag/datatest-stable-0.1.2
5865
[0.1.1]: https://github.com/nextest-rs/datatest-stable/releases/tag/datatest-stable-0.1.1

src/lib.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
//!
2727
//! 2. Call the `datatest_stable::harness!(testfn, root, pattern)` macro with the following
2828
//! parameters:
29-
//! * `testfn` - The test function to be executed on each matching input. This function must have
30-
//! the type `fn(&Utf8Path) -> datatest_stable::Result<()>`. (`Utf8Path` is part of the
31-
//! [`camino`](https://docs.rs/camino) library.)
29+
//! * `testfn` - The test function to be executed on each matching input. This function can be one of:
30+
//! * `fn(&Path) -> datatest_stable::Result<()>`
31+
//! * `fn(&Utf8Path) -> datatest_stable::Result<()>` (`Utf8Path` is part of the
32+
//! [`camino`](https://docs.rs/camino) library, and is re-exported here for convenience.)
3233
//! * `root` - The path to the root directory where the input files (fixtures) live. This path is
3334
//! relative to the root of the crate.
3435
//! * `pattern` - the regex used to match against and select each file to be tested.
@@ -42,14 +43,24 @@
4243
//!
4344
//! ```rust
4445
//! use datatest_stable::Utf8Path;
46+
//! use std::path::Path;
4547
//!
46-
//! fn my_test(path: &Utf8Path) -> datatest_stable::Result<()> {
48+
//! fn my_test(path: &Path) -> datatest_stable::Result<()> {
4749
//! // ... write test here
4850
//!
4951
//! Ok(())
5052
//! }
5153
//!
52-
//! datatest_stable::harness!(my_test, "path/to/fixtures", r"^.*/*");
54+
//! fn my_test_utf8(path: &Utf8Path) -> datatest_stable::Result<()> {
55+
//! // ... write test here
56+
//!
57+
//! Ok(())
58+
//! }
59+
//!
60+
//! datatest_stable::harness!(
61+
//! my_test, "path/to/fixtures", r"^.*/*",
62+
//! my_test_utf8, "path/to/fixtures", r"^.*/*,
63+
//! );
5364
//! ```
5465
//!
5566
//! # Minimum supported Rust version (MSRV)
@@ -78,4 +89,4 @@ pub use camino::Utf8Path;
7889

7990
/// Not part of the public API, just used for macros.
8091
#[doc(hidden)]
81-
pub use self::runner::{runner, Requirements};
92+
pub use self::runner::{runner, Requirements, TestFn};

src/runner.rs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) The datatest-stable Contributors
22
// SPDX-License-Identifier: MIT OR Apache-2.0
33

4+
use std::path::Path;
5+
46
use crate::{utils, Result};
57
use camino::{Utf8Path, Utf8PathBuf};
68
use libtest_mimic::{Arguments, Trial};
@@ -17,22 +19,22 @@ pub fn runner(requirements: &[Requirements]) {
1719

1820
#[doc(hidden)]
1921
pub struct Requirements {
20-
test: fn(&Utf8Path) -> Result<()>,
22+
test: TestFn,
2123
test_name: String,
2224
root: Utf8PathBuf,
2325
pattern: String,
2426
}
2527

2628
impl Requirements {
2729
#[doc(hidden)]
28-
pub fn new(
29-
test: fn(&Utf8Path) -> Result<()>,
30+
pub fn new<P: TestFnPath + ?Sized>(
31+
test: fn(&P) -> Result<()>,
3032
test_name: String,
3133
root: Utf8PathBuf,
3234
pattern: String,
3335
) -> Self {
3436
Self {
35-
test,
37+
test: P::convert(test),
3638
test_name,
3739
root,
3840
pattern,
@@ -52,7 +54,9 @@ impl Requirements {
5254
let testfn = self.test;
5355
let name = utils::derive_test_name(&self.root, &path, &self.test_name);
5456
Some(Trial::test(name, move || {
55-
(testfn)(&path).map_err(|err| format!("{:?}", err).into())
57+
testfn
58+
.call(&path)
59+
.map_err(|err| format!("{:?}", err).into())
5660
}))
5761
} else {
5862
None
@@ -71,3 +75,44 @@ impl Requirements {
7175
tests
7276
}
7377
}
78+
79+
#[derive(Clone, Copy)]
80+
#[doc(hidden)]
81+
pub enum TestFn {
82+
Path(fn(&Path) -> Result<()>),
83+
Utf8Path(fn(&Utf8Path) -> Result<()>),
84+
}
85+
86+
mod private {
87+
pub trait Sealed {}
88+
}
89+
90+
#[doc(hidden)]
91+
pub trait TestFnPath: private::Sealed {
92+
fn convert(f: fn(&Self) -> Result<()>) -> TestFn;
93+
}
94+
95+
impl private::Sealed for Path {}
96+
97+
impl TestFnPath for Path {
98+
fn convert(f: fn(&Self) -> Result<()>) -> TestFn {
99+
TestFn::Path(f)
100+
}
101+
}
102+
103+
impl private::Sealed for Utf8Path {}
104+
105+
impl TestFnPath for Utf8Path {
106+
fn convert(f: fn(&Self) -> Result<()>) -> TestFn {
107+
TestFn::Utf8Path(f)
108+
}
109+
}
110+
111+
impl TestFn {
112+
fn call(&self, path: &Utf8Path) -> Result<()> {
113+
match self {
114+
TestFn::Path(f) => f(path.as_ref()),
115+
TestFn::Utf8Path(f) => f(path),
116+
}
117+
}
118+
}

tests/example.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33

44
use camino::Utf8Path;
55
use datatest_stable::Result;
6-
use std::{fs::File, io::Read};
6+
use std::{fs::File, io::Read, path::Path};
77

8-
fn test_artifact(path: &Utf8Path) -> Result<()> {
8+
fn test_artifact(path: &Path) -> Result<()> {
99
let mut file = File::open(path)?;
1010
let mut contents = String::new();
1111
file.read_to_string(&mut contents)?;
1212

1313
Ok(())
1414
}
1515

16-
datatest_stable::harness!(test_artifact, "tests/files", r"^.*/*");
16+
fn test_artifact_utf8(path: &Utf8Path) -> Result<()> {
17+
test_artifact(path.as_ref())
18+
}
19+
20+
datatest_stable::harness!(
21+
test_artifact, "tests/files", r"^.*/*",
22+
test_artifact_utf8, "tests/files", r"^.*/*",
23+
);

0 commit comments

Comments
 (0)