From db8e31ddc60fdeb5cdd67a39ec805a7239044d89 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 7 Dec 2020 13:31:40 +0100 Subject: Fix: Add unchecked variants of constructors for tests Because the constructors now have side-effects (accessing the filesystem), this patch adds unchecked variants of the constructors, so that tests can be implemented with pathes that do not point to valid filesystem items. Signed-off-by: Matthias Beyer --- src/filestore/artifact.rs | 42 +++++++++++++++++++++--------------------- src/filestore/path.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 21 deletions(-) (limited to 'src/filestore') diff --git a/src/filestore/artifact.rs b/src/filestore/artifact.rs index 3636ab0..c744d82 100644 --- a/src/filestore/artifact.rs +++ b/src/filestore/artifact.rs @@ -77,9 +77,9 @@ mod tests { #[test] fn test_parser_one_letter_name() { - let p = ArtifactPath::new(PathBuf::from("a-1.ext")).unwrap(); - let root = StoreRoot::new(PathBuf::from("/")).unwrap(); - let r = Artifact::parse_path(&root, &p); + let p = ArtifactPath::new_unchecked(PathBuf::from("a-1.ext")); + let root = StoreRoot::new_unchecked(PathBuf::from("/")); + let r = Artifact::parse_path(&root.join_unchecked(&p)); assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r); let (name, version) = r.unwrap(); @@ -90,9 +90,9 @@ mod tests { #[test] fn test_parser_multi_letter_name() { - let p = ArtifactPath::new(PathBuf::from("foo-1.ext")).unwrap(); - let root = StoreRoot::new(PathBuf::from("/")).unwrap(); - let r = Artifact::parse_path(&root, &p); + let p = ArtifactPath::new_unchecked(PathBuf::from("foo-1.ext")); + let root = StoreRoot::new_unchecked(PathBuf::from("/")); + let r = Artifact::parse_path(&root.join_unchecked(&p)); assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r); let (name, version) = r.unwrap(); @@ -103,9 +103,9 @@ mod tests { #[test] fn test_parser_multi_char_version() { - let p = ArtifactPath::new(PathBuf::from("foo-1123.ext")).unwrap(); - let root = StoreRoot::new(PathBuf::from("/")).unwrap(); - let r = Artifact::parse_path(&root, &p); + let p = ArtifactPath::new_unchecked(PathBuf::from("foo-1123.ext")); + let root = StoreRoot::new_unchecked(PathBuf::from("/")); + let r = Artifact::parse_path(&root.join_unchecked(&p)); assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r); let (name, version) = r.unwrap(); @@ -116,9 +116,9 @@ mod tests { #[test] fn test_parser_multi_char_version_dashed() { - let p = ArtifactPath::new(PathBuf::from("foo-1-1-2-3.ext")).unwrap(); - let root = StoreRoot::new(PathBuf::from("/")).unwrap(); - let r = Artifact::parse_path(&root, &p); + let p = ArtifactPath::new_unchecked(PathBuf::from("foo-1-1-2-3.ext")); + let root = StoreRoot::new_unchecked(PathBuf::from("/")); + let r = Artifact::parse_path(&root.join_unchecked(&p)); assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r); let (name, version) = r.unwrap(); @@ -129,9 +129,9 @@ mod tests { #[test] fn test_parser_multi_char_version_dashed_and_dotted() { - let p = ArtifactPath::new(PathBuf::from("foo-1-1.2-3.ext")).unwrap(); - let root = StoreRoot::new(PathBuf::from("/")).unwrap(); - let r = Artifact::parse_path(&root, &p); + let p = ArtifactPath::new_unchecked(PathBuf::from("foo-1-1.2-3.ext")); + let root = StoreRoot::new_unchecked(PathBuf::from("/")); + let r = Artifact::parse_path(&root.join_unchecked(&p)); assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r); let (name, version) = r.unwrap(); @@ -142,9 +142,9 @@ mod tests { #[test] fn test_parser_alnum_version() { - let p = ArtifactPath::new(PathBuf::from("foo-1-1.2a3.ext")).unwrap(); - let root = StoreRoot::new(PathBuf::from("/")).unwrap(); - let r = Artifact::parse_path(&root, &p); + let p = ArtifactPath::new_unchecked(PathBuf::from("foo-1-1.2a3.ext")); + let root = StoreRoot::new_unchecked(PathBuf::from("/")); + let r = Artifact::parse_path(&root.join_unchecked(&p)); assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r); let (name, version) = r.unwrap(); @@ -155,9 +155,9 @@ mod tests { #[test] fn test_parser_package_name_with_number() { - let p = ArtifactPath::new(PathBuf::from("foo2-1-1.2a3.ext")).unwrap(); - let root = StoreRoot::new(PathBuf::from("/")).unwrap(); - let r = Artifact::parse_path(&root, &p); + let p = ArtifactPath::new_unchecked(PathBuf::from("foo2-1-1.2a3.ext")); + let root = StoreRoot::new_unchecked(PathBuf::from("/")); + let r = Artifact::parse_path(&root.join_unchecked(&p)); assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r); let (name, version) = r.unwrap(); diff --git a/src/filestore/path.rs b/src/filestore/path.rs index c691cdb..c9f0c13 100644 --- a/src/filestore/path.rs +++ b/src/filestore/path.rs @@ -25,6 +25,16 @@ impl StoreRoot { } } + /// Unchecked variant of StoreRoot::new() + /// + /// Because StoreRoot::new() accesses the filesystem, this method is necessary to construct an + /// object of StoreRoot for a non-existing path, so that we can test its implementation without + /// the need to create objects on the filesystem. + #[cfg(test)] + pub fn new_unchecked(root: PathBuf) -> Self { + StoreRoot(root) + } + pub fn join<'a>(&'a self, ap: &'a ArtifactPath) -> Result> { let join = self.0.join(&ap.0); @@ -39,6 +49,16 @@ impl StoreRoot { } } + /// Unchecked variant of StoreRoot::join() + /// + /// This function is needed (like StoreRoot::new_unchecked()) to perform a construction of + /// FullArtifactPath like StoreRoot::join() in without its side effects (accessing the + /// filesystem) for testing purposes + #[cfg(test)] + pub fn join_unchecked<'a>(&'a self, ap: &'a ArtifactPath) -> FullArtifactPath<'a> { + FullArtifactPath(self, ap) + } + pub fn is_file(&self, subpath: &Path) -> bool { self.0.join(subpath).is_file() } @@ -82,6 +102,16 @@ impl ArtifactPath { } } + /// Unchecked variant of ArtifactPath::new() + /// + /// Because ArtifactPath::new() accesses the filesystem, this method is necessary to construct an + /// object of ArtifactPath for a non-existing path, so that we can test its implementation without + /// the need to create objects on the filesystem. + #[cfg(test)] + pub fn new_unchecked(root: PathBuf) -> Self { + ArtifactPath(root) + } + pub fn display(&self) -> std::path::Display { self.0.display() } -- cgit v1.2.3