Skip to content

Commit

Permalink
restore compatibility with &Path
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshowers committed Aug 29, 2023
1 parent e627889 commit d8f8c12
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 15 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
23 changes: 17 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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};
55 changes: 50 additions & 5 deletions src/runner.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -17,22 +19,22 @@ pub fn runner(requirements: &[Requirements]) {

#[doc(hidden)]
pub struct Requirements {
test: fn(&Utf8Path) -> Result<()>,
test: TestFn,
test_name: String,
root: Utf8PathBuf,
pattern: String,
}

impl Requirements {
#[doc(hidden)]
pub fn new(
test: fn(&Utf8Path) -> Result<()>,
pub fn new<P: TestFnPath + ?Sized>(
test: fn(&P) -> Result<()>,
test_name: String,
root: Utf8PathBuf,
pattern: String,
) -> Self {
Self {
test,
test: P::convert(test),
test_name,
root,
pattern,
Expand All @@ -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
Expand All @@ -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),
}
}
}
13 changes: 10 additions & 3 deletions tests/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@

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)?;

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"^.*/*",
);

0 comments on commit d8f8c12

Please sign in to comment.