Skip to content

Commit

Permalink
fix(dir): Rename PathFixture to DirRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 24, 2024
1 parent 23c5326 commit 2da9e27
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
Empty file.
2 changes: 1 addition & 1 deletion crates/snapbox/src/dir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use ops::resolve_dir;
pub use ops::strip_trailing_slash;
#[cfg(feature = "dir")]
pub use ops::Walk;
pub use root::PathFixture;
pub use root::DirRoot;

#[cfg(feature = "dir")]
pub(crate) use ops::canonicalize;
Expand Down
4 changes: 2 additions & 2 deletions crates/snapbox/src/dir/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ impl Iterator for Walk {
}
}

/// Copy a template into a [`PathFixture`][super::PathFixture]
/// Copy a template into a [`DirRoot`][super::DirRoot]
///
/// Note: Generally you'll use [`PathFixture::with_template`][super::PathFixture::with_template] instead.
/// Note: Generally you'll use [`DirRoot::with_template`][super::DirRoot::with_template] instead.
///
/// Note: Ignores `.keep` files
#[cfg(feature = "dir")]
Expand Down
40 changes: 20 additions & 20 deletions crates/snapbox/src/dir/root.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/// Working directory for tests
#[derive(Debug)]
pub struct PathFixture(PathFixtureInner);
pub struct DirRoot(DirRootInner);

#[derive(Debug)]
enum PathFixtureInner {
enum DirRootInner {
None,
Immutable(std::path::PathBuf),
#[cfg(feature = "dir")]
Expand All @@ -15,13 +15,13 @@ enum PathFixtureInner {
},
}

impl PathFixture {
impl DirRoot {
pub fn none() -> Self {
Self(PathFixtureInner::None)
Self(DirRootInner::None)
}

pub fn immutable(target: &std::path::Path) -> Self {
Self(PathFixtureInner::Immutable(target.to_owned()))
Self(DirRootInner::Immutable(target.to_owned()))
}

#[cfg(feature = "dir")]
Expand All @@ -31,15 +31,15 @@ impl PathFixture {
// correctly
let path = crate::dir::canonicalize(temp.path())
.map_err(|e| format!("Failed to canonicalize {}: {}", temp.path().display(), e))?;
Ok(Self(PathFixtureInner::MutableTemp { temp, path }))
Ok(Self(DirRootInner::MutableTemp { temp, path }))
}

#[cfg(feature = "dir")]
pub fn mutable_at(target: &std::path::Path) -> Result<Self, crate::assert::Error> {
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())))
Ok(Self(DirRootInner::MutablePath(target.to_owned())))
}

#[cfg(feature = "dir")]
Expand All @@ -48,10 +48,10 @@ impl PathFixture {
template_root: &std::path::Path,
) -> Result<Self, crate::assert::Error> {
match &self.0 {
PathFixtureInner::None | PathFixtureInner::Immutable(_) => {
DirRootInner::None | DirRootInner::Immutable(_) => {
return Err("Sandboxing is disabled".into());
}
PathFixtureInner::MutablePath(path) | PathFixtureInner::MutableTemp { path, .. } => {
DirRootInner::MutablePath(path) | DirRootInner::MutableTemp { path, .. } => {
crate::debug!(
"Initializing {} from {}",
path.display(),
Expand All @@ -66,38 +66,38 @@ impl PathFixture {

pub fn is_mutable(&self) -> bool {
match &self.0 {
PathFixtureInner::None | PathFixtureInner::Immutable(_) => false,
DirRootInner::None | DirRootInner::Immutable(_) => false,
#[cfg(feature = "dir")]
PathFixtureInner::MutablePath(_) => true,
DirRootInner::MutablePath(_) => true,
#[cfg(feature = "dir")]
PathFixtureInner::MutableTemp { .. } => true,
DirRootInner::MutableTemp { .. } => true,
}
}

pub fn path(&self) -> Option<&std::path::Path> {
match &self.0 {
PathFixtureInner::None => None,
PathFixtureInner::Immutable(path) => Some(path.as_path()),
DirRootInner::None => None,
DirRootInner::Immutable(path) => Some(path.as_path()),
#[cfg(feature = "dir")]
PathFixtureInner::MutablePath(path) => Some(path.as_path()),
DirRootInner::MutablePath(path) => Some(path.as_path()),
#[cfg(feature = "dir")]
PathFixtureInner::MutableTemp { path, .. } => Some(path.as_path()),
DirRootInner::MutableTemp { path, .. } => Some(path.as_path()),
}
}

/// Explicitly close to report errors
pub fn close(self) -> Result<(), std::io::Error> {
match self.0 {
PathFixtureInner::None | PathFixtureInner::Immutable(_) => Ok(()),
DirRootInner::None | DirRootInner::Immutable(_) => Ok(()),
#[cfg(feature = "dir")]
PathFixtureInner::MutablePath(_) => Ok(()),
DirRootInner::MutablePath(_) => Ok(()),
#[cfg(feature = "dir")]
PathFixtureInner::MutableTemp { temp, .. } => temp.close(),
DirRootInner::MutableTemp { temp, .. } => temp.close(),
}
}
}

impl Default for PathFixture {
impl Default for DirRoot {
fn default() -> Self {
Self::none()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/snapbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! [`Output`][std::process::Output].
//!
//! Testing Filesystem Interactions:
//! - [`dir::PathFixture`]: Working directory for tests
//! - [`dir::DirRoot`]: Working directory for tests
//! - [`Assert`]: Diff a directory against files present in a pattern directory
//!
//! You can also build your own version of these with the lower-level building blocks these are
Expand Down
12 changes: 6 additions & 6 deletions crates/trycmd/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ impl Case {

#[cfg(feature = "filesystem")]
if let Mode::Dump(_) = mode {
// Handled as part of PathFixture
// Handled as part of DirRoot
} else {
let fixture_root = self.path.with_extension("out");
if fixture_root.exists() {
Expand Down Expand Up @@ -1016,20 +1016,20 @@ fn fs_context(
cwd: Option<&std::path::Path>,
sandbox: bool,
mode: &crate::Mode,
) -> Result<snapbox::dir::PathFixture, crate::Error> {
) -> Result<snapbox::dir::DirRoot, crate::Error> {
if sandbox {
#[cfg(feature = "filesystem")]
match mode {
crate::Mode::Dump(root) => {
let target = root.join(path.with_extension("out").file_name().unwrap());
let mut context = snapbox::dir::PathFixture::mutable_at(&target)?;
let mut context = snapbox::dir::DirRoot::mutable_at(&target)?;
if let Some(cwd) = cwd {
context = context.with_template(cwd)?;
}
Ok(context)
}
crate::Mode::Fail | crate::Mode::Overwrite => {
let mut context = snapbox::dir::PathFixture::mutable_temp()?;
let mut context = snapbox::dir::DirRoot::mutable_temp()?;
if let Some(cwd) = cwd {
context = context.with_template(cwd)?;
}
Expand All @@ -1040,7 +1040,7 @@ fn fs_context(
Err("Sandboxing is disabled".into())
} else {
Ok(cwd
.map(snapbox::dir::PathFixture::immutable)
.unwrap_or_else(snapbox::dir::PathFixture::none))
.map(snapbox::dir::DirRoot::immutable)
.unwrap_or_else(snapbox::dir::DirRoot::none))
}
}

0 comments on commit 2da9e27

Please sign in to comment.