Skip to content

Commit

Permalink
fix(dir): Rename path feature to dir
Browse files Browse the repository at this point in the history
Cherry pick 5fc67de (assert-rs#298)
  • Loading branch information
epage committed May 15, 2024
1 parent b0553d4 commit 61a751f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
6 changes: 4 additions & 2 deletions crates/snapbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ default = ["color-auto", "diff"]
harness = ["dep:libtest-mimic", "dep:ignore"]
## Smarter binary file detection
detect-encoding = ["dep:content_inspector"]
## Snapshotting of paths
path = ["dep:tempfile", "dep:walkdir", "dep:dunce", "detect-encoding", "dep:filetime"]
## Snapshotting of directories
dir = ["dep:tempfile", "dep:walkdir", "dep:dunce", "detect-encoding", "dep:filetime"]
## Deprecated since 0.5.11, replaced with `dir`
path = ["dir"]
## Snapshotting of commands
cmd = ["dep:os_pipe", "dep:wait-timeout", "dep:libc", "dep:windows-sys"]
## Building of examples for snapshotting
Expand Down
2 changes: 1 addition & 1 deletion crates/snapbox/src/assert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl Assert {
}

/// # Directory Assertions
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
impl Assert {
#[track_caller]
pub fn subset_eq(
Expand Down
4 changes: 2 additions & 2 deletions crates/snapbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ pub fn assert_matches(pattern: impl Into<crate::Data>, actual: impl Into<crate::
/// let expected_root = "tests/snapshots/output.txt";
/// snapbox::assert_subset_eq(expected_root, output_root);
/// ```
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
#[track_caller]
pub fn assert_subset_eq(
expected_root: impl Into<std::path::PathBuf>,
Expand All @@ -218,7 +218,7 @@ pub fn assert_subset_eq(
/// let expected_root = "tests/snapshots/output.txt";
/// snapbox::assert_subset_matches(expected_root, output_root);
/// ```
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
#[track_caller]
pub fn assert_subset_matches(
pattern_root: impl Into<std::path::PathBuf>,
Expand Down
48 changes: 24 additions & 24 deletions crates/snapbox/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use crate::current_dir;
#[doc(inline)]
pub use crate::current_rs;

#[cfg(feature = "path")]
#[cfg(feature = "dir")]
use crate::filter::{Filter as _, FilterMatches, FilterNewlines, FilterPaths};

/// Working directory for tests
Expand All @@ -18,9 +18,9 @@ pub struct PathFixture(PathFixtureInner);
enum PathFixtureInner {
None,
Immutable(std::path::PathBuf),
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
MutablePath(std::path::PathBuf),
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
MutableTemp {
temp: tempfile::TempDir,
path: std::path::PathBuf,
Expand All @@ -36,7 +36,7 @@ impl PathFixture {
Self(PathFixtureInner::Immutable(target.to_owned()))
}

#[cfg(feature = "path")]
#[cfg(feature = "dir")]
pub fn mutable_temp() -> crate::assert::Result<Self> {
let temp = tempfile::tempdir().map_err(|e| e.to_string())?;
// We need to get the `/private` prefix on Mac so variable substitutions work
Expand All @@ -46,15 +46,15 @@ impl PathFixture {
Ok(Self(PathFixtureInner::MutableTemp { temp, path }))
}

#[cfg(feature = "path")]
#[cfg(feature = "dir")]
pub fn mutable_at(target: &std::path::Path) -> crate::assert::Result<Self> {
let _ = std::fs::remove_dir_all(target);
std::fs::create_dir_all(target)
.map_err(|e| format!("Failed to create {}: {}", target.display(), e))?;
Ok(Self(PathFixtureInner::MutablePath(target.to_owned())))
}

#[cfg(feature = "path")]
#[cfg(feature = "dir")]
pub fn with_template(self, template_root: &std::path::Path) -> crate::assert::Result<Self> {
match &self.0 {
PathFixtureInner::None | PathFixtureInner::Immutable(_) => {
Expand All @@ -76,9 +76,9 @@ impl PathFixture {
pub fn is_mutable(&self) -> bool {
match &self.0 {
PathFixtureInner::None | PathFixtureInner::Immutable(_) => false,
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
PathFixtureInner::MutablePath(_) => true,
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
PathFixtureInner::MutableTemp { .. } => true,
}
}
Expand All @@ -87,9 +87,9 @@ impl PathFixture {
match &self.0 {
PathFixtureInner::None => None,
PathFixtureInner::Immutable(path) => Some(path.as_path()),
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
PathFixtureInner::MutablePath(path) => Some(path.as_path()),
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
PathFixtureInner::MutableTemp { path, .. } => Some(path.as_path()),
}
}
Expand All @@ -98,9 +98,9 @@ impl PathFixture {
pub fn close(self) -> Result<(), std::io::Error> {
match self.0 {
PathFixtureInner::None | PathFixtureInner::Immutable(_) => Ok(()),
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
PathFixtureInner::MutablePath(_) => Ok(()),
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
PathFixtureInner::MutableTemp { temp, .. } => temp.close(),
}
}
Expand Down Expand Up @@ -139,7 +139,7 @@ impl PathDiff {
/// Report differences between `actual_root` and `pattern_root`
///
/// Note: Requires feature flag `path`
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
pub fn subset_eq_iter(
pattern_root: impl Into<std::path::PathBuf>,
actual_root: impl Into<std::path::PathBuf>,
Expand All @@ -149,7 +149,7 @@ impl PathDiff {
Self::subset_eq_iter_inner(pattern_root, actual_root)
}

#[cfg(feature = "path")]
#[cfg(feature = "dir")]
pub(crate) fn subset_eq_iter_inner(
expected_root: std::path::PathBuf,
actual_root: std::path::PathBuf,
Expand Down Expand Up @@ -212,7 +212,7 @@ impl PathDiff {
/// Report differences between `actual_root` and `pattern_root`
///
/// Note: Requires feature flag `path`
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
pub fn subset_matches_iter(
pattern_root: impl Into<std::path::PathBuf>,
actual_root: impl Into<std::path::PathBuf>,
Expand All @@ -223,7 +223,7 @@ impl PathDiff {
Self::subset_matches_iter_inner(pattern_root, actual_root, substitutions, true)
}

#[cfg(feature = "path")]
#[cfg(feature = "dir")]
pub(crate) fn subset_matches_iter_inner(
expected_root: std::path::PathBuf,
actual_root: std::path::PathBuf,
Expand Down Expand Up @@ -471,12 +471,12 @@ impl std::fmt::Display for FileType {
/// Recursively walk a path
///
/// Note: Ignores `.keep` files
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
pub struct Walk {
inner: walkdir::IntoIter,
}

#[cfg(feature = "path")]
#[cfg(feature = "dir")]
impl Walk {
pub fn new(path: &std::path::Path) -> Self {
Self {
Expand All @@ -485,7 +485,7 @@ impl Walk {
}
}

#[cfg(feature = "path")]
#[cfg(feature = "dir")]
impl Iterator for Walk {
type Item = Result<std::path::PathBuf, std::io::Error>;

Expand All @@ -509,7 +509,7 @@ impl Iterator for Walk {
/// Note: Generally you'll use [`PathFixture::with_template`] instead.
///
/// Note: Ignores `.keep` files
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
pub fn copy_template(
source: impl AsRef<std::path::Path>,
dest: impl AsRef<std::path::Path>,
Expand Down Expand Up @@ -577,7 +577,7 @@ fn shallow_copy(source: &std::path::Path, dest: &std::path::Path) -> crate::asse
Ok(())
}

#[cfg(feature = "path")]
#[cfg(feature = "dir")]
fn copy_stats(
source_meta: &std::fs::Metadata,
dest: &std::path::Path,
Expand All @@ -588,7 +588,7 @@ fn copy_stats(
Ok(())
}

#[cfg(not(feature = "path"))]
#[cfg(not(feature = "dir"))]
fn copy_stats(
_source_meta: &std::fs::Metadata,
_dest: &std::path::Path,
Expand Down Expand Up @@ -624,11 +624,11 @@ pub fn resolve_dir(
}

fn canonicalize(path: &std::path::Path) -> Result<std::path::PathBuf, std::io::Error> {
#[cfg(feature = "path")]
#[cfg(feature = "dir")]
{
dunce::canonicalize(path)
}
#[cfg(not(feature = "path"))]
#[cfg(not(feature = "dir"))]
{
// Hope for the best
Ok(strip_trailing_slash(path).to_owned())
Expand Down

0 comments on commit 61a751f

Please sign in to comment.