From d8f8c120df19886d497974fbf0ea5edfd160219b Mon Sep 17 00:00:00 2001 From: Rain Date: Tue, 29 Aug 2023 13:13:25 -0700 Subject: [PATCH] restore compatibility with &Path --- CHANGELOG.md | 9 +++++++- src/lib.rs | 23 ++++++++++++++------ src/runner.rs | 55 +++++++++++++++++++++++++++++++++++++++++++----- tests/example.rs | 13 +++++++++--- 4 files changed, 85 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca668a7ca..e33124a58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [0.2.2] - 2023-08-29 + +### Added + +- Restored compatibility with `fn(&Path) -> Result<()>`. The harness now can take either `fn(&Path) -> Result<()>` or `fn(&Utf8Path) -> Result<()>`. + ## [0.2.1] - 2023-08-29 ### Changed @@ -52,7 +58,8 @@ There are no functional changes in this release. (Version 0.1.0 was yanked because of a metadata issue.) -[0.2.0]: https://github.com/nextest-rs/datatest-stable/releases/tag/datatest-stable-0.2.1 +[0.2.2]: https://github.com/nextest-rs/datatest-stable/releases/tag/datatest-stable-0.2.2 +[0.2.1]: https://github.com/nextest-rs/datatest-stable/releases/tag/datatest-stable-0.2.1 [0.1.3]: https://github.com/nextest-rs/datatest-stable/releases/tag/datatest-stable-0.1.3 [0.1.2]: https://github.com/nextest-rs/datatest-stable/releases/tag/datatest-stable-0.1.2 [0.1.1]: https://github.com/nextest-rs/datatest-stable/releases/tag/datatest-stable-0.1.1 diff --git a/src/lib.rs b/src/lib.rs index 8d0017012..500facfc6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,9 +26,10 @@ //! //! 2. Call the `datatest_stable::harness!(testfn, root, pattern)` macro with the following //! parameters: -//! * `testfn` - The test function to be executed on each matching input. This function must have -//! the type `fn(&Utf8Path) -> datatest_stable::Result<()>`. (`Utf8Path` is part of the -//! [`camino`](https://docs.rs/camino) library.) +//! * `testfn` - The test function to be executed on each matching input. This function can be one of: +//! * `fn(&Path) -> datatest_stable::Result<()>` +//! * `fn(&Utf8Path) -> datatest_stable::Result<()>` (`Utf8Path` is part of the +//! [`camino`](https://docs.rs/camino) library, and is re-exported here for convenience.) //! * `root` - The path to the root directory where the input files (fixtures) live. This path is //! relative to the root of the crate. //! * `pattern` - the regex used to match against and select each file to be tested. @@ -42,14 +43,24 @@ //! //! ```rust //! use datatest_stable::Utf8Path; +//! use std::path::Path; //! -//! fn my_test(path: &Utf8Path) -> datatest_stable::Result<()> { +//! fn my_test(path: &Path) -> datatest_stable::Result<()> { //! // ... write test here //! //! Ok(()) //! } //! -//! datatest_stable::harness!(my_test, "path/to/fixtures", r"^.*/*"); +//! fn my_test_utf8(path: &Utf8Path) -> datatest_stable::Result<()> { +//! // ... write test here +//! +//! Ok(()) +//! } +//! +//! datatest_stable::harness!( +//! my_test, "path/to/fixtures", r"^.*/*", +//! my_test_utf8, "path/to/fixtures", r"^.*/*, +//! ); //! ``` //! //! # Minimum supported Rust version (MSRV) @@ -78,4 +89,4 @@ pub use camino::Utf8Path; /// Not part of the public API, just used for macros. #[doc(hidden)] -pub use self::runner::{runner, Requirements}; +pub use self::runner::{runner, Requirements, TestFn}; diff --git a/src/runner.rs b/src/runner.rs index 782555a8a..27bede8b3 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -1,6 +1,8 @@ // Copyright (c) The datatest-stable Contributors // SPDX-License-Identifier: MIT OR Apache-2.0 +use std::path::Path; + use crate::{utils, Result}; use camino::{Utf8Path, Utf8PathBuf}; use libtest_mimic::{Arguments, Trial}; @@ -17,7 +19,7 @@ pub fn runner(requirements: &[Requirements]) { #[doc(hidden)] pub struct Requirements { - test: fn(&Utf8Path) -> Result<()>, + test: TestFn, test_name: String, root: Utf8PathBuf, pattern: String, @@ -25,14 +27,14 @@ pub struct Requirements { impl Requirements { #[doc(hidden)] - pub fn new( - test: fn(&Utf8Path) -> Result<()>, + pub fn new( + test: fn(&P) -> Result<()>, test_name: String, root: Utf8PathBuf, pattern: String, ) -> Self { Self { - test, + test: P::convert(test), test_name, root, pattern, @@ -52,7 +54,9 @@ impl Requirements { let testfn = self.test; let name = utils::derive_test_name(&self.root, &path, &self.test_name); Some(Trial::test(name, move || { - (testfn)(&path).map_err(|err| format!("{:?}", err).into()) + testfn + .call(&path) + .map_err(|err| format!("{:?}", err).into()) })) } else { None @@ -71,3 +75,44 @@ impl Requirements { tests } } + +#[derive(Clone, Copy)] +#[doc(hidden)] +pub enum TestFn { + Path(fn(&Path) -> Result<()>), + Utf8Path(fn(&Utf8Path) -> Result<()>), +} + +mod private { + pub trait Sealed {} +} + +#[doc(hidden)] +pub trait TestFnPath: private::Sealed { + fn convert(f: fn(&Self) -> Result<()>) -> TestFn; +} + +impl private::Sealed for Path {} + +impl TestFnPath for Path { + fn convert(f: fn(&Self) -> Result<()>) -> TestFn { + TestFn::Path(f) + } +} + +impl private::Sealed for Utf8Path {} + +impl TestFnPath for Utf8Path { + fn convert(f: fn(&Self) -> Result<()>) -> TestFn { + TestFn::Utf8Path(f) + } +} + +impl TestFn { + fn call(&self, path: &Utf8Path) -> Result<()> { + match self { + TestFn::Path(f) => f(path.as_ref()), + TestFn::Utf8Path(f) => f(path), + } + } +} diff --git a/tests/example.rs b/tests/example.rs index 51b77b243..4352c887d 100644 --- a/tests/example.rs +++ b/tests/example.rs @@ -3,9 +3,9 @@ use camino::Utf8Path; use datatest_stable::Result; -use std::{fs::File, io::Read}; +use std::{fs::File, io::Read, path::Path}; -fn test_artifact(path: &Utf8Path) -> Result<()> { +fn test_artifact(path: &Path) -> Result<()> { let mut file = File::open(path)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; @@ -13,4 +13,11 @@ fn test_artifact(path: &Utf8Path) -> Result<()> { Ok(()) } -datatest_stable::harness!(test_artifact, "tests/files", r"^.*/*"); +fn test_artifact_utf8(path: &Utf8Path) -> Result<()> { + test_artifact(path.as_ref()) +} + +datatest_stable::harness!( + test_artifact, "tests/files", r"^.*/*", + test_artifact_utf8, "tests/files", r"^.*/*", +);