Skip to content

Commit

Permalink
Simple test of tag namespace behavior
Browse files Browse the repository at this point in the history
The test code requires a way to modify the tag namespace of a TempRepo,
but since it can be changed through an Arc, add a `with_tag_namespace`
method to make a duplicate handle to an existing repo but with a different
namespace set. This required putting the TempDir inside an Arc.

Signed-off-by: J Robert Ray <jrray@jrray.org>
  • Loading branch information
jrray committed May 16, 2023
1 parent ab41fe4 commit c6d2b95
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
20 changes: 18 additions & 2 deletions crates/spfs/src/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tempfile::TempDir;
use crate as spfs;

pub enum TempRepo {
FS(Arc<spfs::storage::RepositoryHandle>, TempDir),
FS(Arc<spfs::storage::RepositoryHandle>, Arc<TempDir>),
Tar(Arc<spfs::storage::RepositoryHandle>, TempDir),
Rpc {
repo: Arc<spfs::storage::RepositoryHandle>,
Expand All @@ -32,6 +32,22 @@ impl TempRepo {
Self::Rpc { repo, .. } => Arc::clone(repo),
}
}

pub async fn with_tag_namespace<S>(&self, namespace: S) -> Self
where
S: AsRef<str>,
{
match self {
TempRepo::FS(_, tempdir) => {
let mut repo = spfs::storage::fs::FSRepository::open(tempdir.path().join("repo"))
.await
.unwrap();
repo.set_tag_namespace(Some(namespace.as_ref().into()));
TempRepo::FS(Arc::new(repo.into()), Arc::clone(tempdir))
}
_ => panic!("only TempRepo::FS type supports setting tag namespaces"),
}
}
}

impl std::ops::Deref for TempRepo {
Expand Down Expand Up @@ -122,7 +138,7 @@ pub async fn tmprepo(kind: &str) -> TempRepo {
.await
.unwrap()
.into();
TempRepo::FS(Arc::new(repo), tmpdir)
TempRepo::FS(Arc::new(repo), Arc::new(tmpdir))
}
"tar" => {
let repo = spfs::storage::tar::TarRepository::create(tmpdir.path().join("repo.tar"))
Expand Down
46 changes: 46 additions & 0 deletions crates/spfs/src/storage/tag_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,49 @@ async fn test_rm_tags(
"should fail to remove a removed tag, got {res:?}"
);
}

#[rstest]
#[case::fs(tmprepo("fs"))]
#[tokio::test]
async fn test_tag_in_namespace(
#[case]
#[future]
tmprepo: TempRepo,
) {
init_logging();
let tmprepo = tmprepo.await;

let namespace_name = "test-namespace";
let namespaced_repo = tmprepo.with_tag_namespace(namespace_name).await;

// Create a tag in the namespaced repo.
let tag_name = "a-tag";
let spec = tracking::TagSpec::parse(tag_name).unwrap();
namespaced_repo
.push_tag(&spec, &encoding::EMPTY_DIGEST.into())
.await
.unwrap();

// Listing the tags in the namespaced repo contains the tag we made.
let tags: Vec<_> = namespaced_repo
.ls_tags(&RelativePathBuf::from("/"))
.collect::<Result<Vec<_>>>()
.await
.unwrap();
assert_eq!(tags, vec![EntryType::Tag(tag_name.to_string())]);

// Listing the tags in the non-namespaced repo contains [only] the
// namespace.
let tags: Vec<_> = tmprepo
.ls_tags(&RelativePathBuf::from("/"))
.collect::<Result<Vec<_>>>()
.await
.unwrap();
assert_eq!(
tags,
vec![EntryType::Namespace {
name: namespace_name.to_string(),
depth: 0
}]
);
}

0 comments on commit c6d2b95

Please sign in to comment.