-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from epage/d
fix(dir): Rename PathFixture to DirRoot
- Loading branch information
Showing
8 changed files
with
129 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +0,0 @@ | ||
/// Working directory for tests | ||
#[derive(Debug)] | ||
pub struct PathFixture(PathFixtureInner); | ||
|
||
#[derive(Debug)] | ||
enum PathFixtureInner { | ||
None, | ||
Immutable(std::path::PathBuf), | ||
#[cfg(feature = "dir")] | ||
MutablePath(std::path::PathBuf), | ||
#[cfg(feature = "dir")] | ||
MutableTemp { | ||
temp: tempfile::TempDir, | ||
path: std::path::PathBuf, | ||
}, | ||
} | ||
|
||
impl PathFixture { | ||
pub fn none() -> Self { | ||
Self(PathFixtureInner::None) | ||
} | ||
|
||
pub fn immutable(target: &std::path::Path) -> Self { | ||
Self(PathFixtureInner::Immutable(target.to_owned())) | ||
} | ||
|
||
#[cfg(feature = "dir")] | ||
pub fn mutable_temp() -> Result<Self, crate::assert::Error> { | ||
let temp = tempfile::tempdir().map_err(|e| e.to_string())?; | ||
// We need to get the `/private` prefix on Mac so variable substitutions work | ||
// 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 })) | ||
} | ||
|
||
#[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()))) | ||
} | ||
|
||
#[cfg(feature = "dir")] | ||
pub fn with_template( | ||
self, | ||
template_root: &std::path::Path, | ||
) -> Result<Self, crate::assert::Error> { | ||
match &self.0 { | ||
PathFixtureInner::None | PathFixtureInner::Immutable(_) => { | ||
return Err("Sandboxing is disabled".into()); | ||
} | ||
PathFixtureInner::MutablePath(path) | PathFixtureInner::MutableTemp { path, .. } => { | ||
crate::debug!( | ||
"Initializing {} from {}", | ||
path.display(), | ||
template_root.display() | ||
); | ||
super::copy_template(template_root, path)?; | ||
} | ||
} | ||
|
||
Ok(self) | ||
} | ||
|
||
pub fn is_mutable(&self) -> bool { | ||
match &self.0 { | ||
PathFixtureInner::None | PathFixtureInner::Immutable(_) => false, | ||
#[cfg(feature = "dir")] | ||
PathFixtureInner::MutablePath(_) => true, | ||
#[cfg(feature = "dir")] | ||
PathFixtureInner::MutableTemp { .. } => true, | ||
} | ||
} | ||
|
||
pub fn path(&self) -> Option<&std::path::Path> { | ||
match &self.0 { | ||
PathFixtureInner::None => None, | ||
PathFixtureInner::Immutable(path) => Some(path.as_path()), | ||
#[cfg(feature = "dir")] | ||
PathFixtureInner::MutablePath(path) => Some(path.as_path()), | ||
#[cfg(feature = "dir")] | ||
PathFixtureInner::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(()), | ||
#[cfg(feature = "dir")] | ||
PathFixtureInner::MutablePath(_) => Ok(()), | ||
#[cfg(feature = "dir")] | ||
PathFixtureInner::MutableTemp { temp, .. } => temp.close(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for PathFixture { | ||
fn default() -> Self { | ||
Self::none() | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
//! Initialize working directories and assert on how they've changed | ||
mod diff; | ||
mod fixture; | ||
mod ops; | ||
mod root; | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub use diff::FileType; | ||
pub use diff::PathDiff; | ||
pub use fixture::PathFixture; | ||
#[cfg(feature = "dir")] | ||
pub use ops::copy_template; | ||
pub use ops::resolve_dir; | ||
pub use ops::strip_trailing_slash; | ||
#[cfg(feature = "dir")] | ||
pub use ops::Walk; | ||
pub use root::DirRoot; | ||
|
||
#[cfg(feature = "dir")] | ||
pub(crate) use ops::canonicalize; | ||
pub(crate) use ops::display_relpath; | ||
pub(crate) use ops::shallow_copy; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/// Working directory for tests | ||
#[derive(Debug)] | ||
pub struct DirRoot(DirRootInner); | ||
|
||
#[derive(Debug)] | ||
enum DirRootInner { | ||
None, | ||
Immutable(std::path::PathBuf), | ||
#[cfg(feature = "dir")] | ||
MutablePath(std::path::PathBuf), | ||
#[cfg(feature = "dir")] | ||
MutableTemp { | ||
temp: tempfile::TempDir, | ||
path: std::path::PathBuf, | ||
}, | ||
} | ||
|
||
impl DirRoot { | ||
pub fn none() -> Self { | ||
Self(DirRootInner::None) | ||
} | ||
|
||
pub fn immutable(target: &std::path::Path) -> Self { | ||
Self(DirRootInner::Immutable(target.to_owned())) | ||
} | ||
|
||
#[cfg(feature = "dir")] | ||
pub fn mutable_temp() -> Result<Self, crate::assert::Error> { | ||
let temp = tempfile::tempdir().map_err(|e| e.to_string())?; | ||
// We need to get the `/private` prefix on Mac so variable substitutions work | ||
// correctly | ||
let path = crate::dir::canonicalize(temp.path()) | ||
.map_err(|e| format!("Failed to canonicalize {}: {}", temp.path().display(), e))?; | ||
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(DirRootInner::MutablePath(target.to_owned()))) | ||
} | ||
|
||
#[cfg(feature = "dir")] | ||
pub fn with_template( | ||
self, | ||
template_root: &std::path::Path, | ||
) -> Result<Self, crate::assert::Error> { | ||
match &self.0 { | ||
DirRootInner::None | DirRootInner::Immutable(_) => { | ||
return Err("Sandboxing is disabled".into()); | ||
} | ||
DirRootInner::MutablePath(path) | DirRootInner::MutableTemp { path, .. } => { | ||
crate::debug!( | ||
"Initializing {} from {}", | ||
path.display(), | ||
template_root.display() | ||
); | ||
super::copy_template(template_root, path)?; | ||
} | ||
} | ||
|
||
Ok(self) | ||
} | ||
|
||
pub fn is_mutable(&self) -> bool { | ||
match &self.0 { | ||
DirRootInner::None | DirRootInner::Immutable(_) => false, | ||
#[cfg(feature = "dir")] | ||
DirRootInner::MutablePath(_) => true, | ||
#[cfg(feature = "dir")] | ||
DirRootInner::MutableTemp { .. } => true, | ||
} | ||
} | ||
|
||
pub fn path(&self) -> Option<&std::path::Path> { | ||
match &self.0 { | ||
DirRootInner::None => None, | ||
DirRootInner::Immutable(path) => Some(path.as_path()), | ||
#[cfg(feature = "dir")] | ||
DirRootInner::MutablePath(path) => Some(path.as_path()), | ||
#[cfg(feature = "dir")] | ||
DirRootInner::MutableTemp { path, .. } => Some(path.as_path()), | ||
} | ||
} | ||
|
||
/// Explicitly close to report errors | ||
pub fn close(self) -> Result<(), std::io::Error> { | ||
match self.0 { | ||
DirRootInner::None | DirRootInner::Immutable(_) => Ok(()), | ||
#[cfg(feature = "dir")] | ||
DirRootInner::MutablePath(_) => Ok(()), | ||
#[cfg(feature = "dir")] | ||
DirRootInner::MutableTemp { temp, .. } => temp.close(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for DirRoot { | ||
fn default() -> Self { | ||
Self::none() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters