Skip to content

Commit

Permalink
Add a test to confirm spk rm works with legacy version tags
Browse files Browse the repository at this point in the history
How the version number is specified on the command line shouldn't
matter, as long as it would be considered equal to how the version
exists on disk, e.g., "1.0" == "1.0.0".

Signed-off-by: J Robert Ray <jrray@jrray.org>
  • Loading branch information
jrray committed Feb 26, 2024
1 parent ff285dd commit 91abfd9
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/spk-cli/group2/src/cmd_remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ use spk_schema::spec_ops::WithVersion;
use spk_schema::{BuildIdent, VersionIdent};
use spk_storage as storage;

#[cfg(test)]
#[path = "./cmd_remove_test.rs"]
mod cmd_remove_test;

/// Remove a package from a repository
#[derive(Args)]
#[clap(visible_alias = "rm")]
Expand Down
144 changes: 144 additions & 0 deletions crates/spk-cli/group2/src/cmd_remove_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright (c) Sony Pictures Imageworks, et al.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/imageworks/spk

use clap::Parser;
use futures::prelude::*;
use relative_path::RelativePathBuf;
use rstest::rstest;
use spfs::config::Remote;
use spfs::storage::EntryType;
use spfs::RemoteAddress;
use spk_schema::foundation::ident_component::Component;
use spk_schema::ident_ops::VerbatimTagStrategy;
use spk_schema::recipe;
use spk_solve::spec;
use spk_storage::fixtures::*;
use spk_storage::RepositoryHandle;

use super::{Remove, Run};

#[derive(Parser)]
struct Opt {
#[clap(flatten)]
remove: Remove,
}

/// When the legacy-spk-version-tags feature is enabled, and when a package
/// is published with a non-normalized version tag, `spk rm` is expected to
/// successfully delete the package.
#[rstest]
#[tokio::test]
async fn test_remove_succeeds_for_package_saved_with_legacy_version_tag(
#[values("1", "1.0", "1.0.0", "1.0.0.0", "1.0.0.0.0")] version_to_publish: &str,
#[values("1", "1.0", "1.0.0", "1.0.0.0", "1.0.0.0.0")] version_to_delete: &str,
) {
let mut rt = spfs_runtime_with_tag_strategy::<VerbatimTagStrategy>().await;
let remote_repo = spfsrepo().await;

rt.add_remote_repo(
"origin",
Remote::Address(RemoteAddress {
address: remote_repo.address().clone(),
}),
)
.unwrap();

let recipe = recipe!({"pkg": format!("my-local-pkg/{version_to_publish}")});
rt.tmprepo.publish_recipe(&recipe).await.unwrap();
let spec = spec!({"pkg": format!("my-local-pkg/{version_to_publish}/BGSHW3CN")});
rt.tmprepo
.publish_package(
&spec,
&vec![(Component::Run, empty_layer_digest())]
.into_iter()
.collect(),
)
.await
.unwrap();

// Confirm that the tags were created with the legacy version tag strategy.
for (tag_path, entry_type_filter) in [
(
"spk/spec/my-local-pkg",
Box::new(|tag: &_| matches!(tag, Ok(EntryType::Tag(tag)) if tag == version_to_publish))
as Box<dyn for<'a> Fn(&'a Result<EntryType, spfs::Error>) -> bool>,
),
(
"spk/pkg/my-local-pkg",
Box::new(|tag| matches!(tag, Ok(EntryType::Folder(tag)) if tag == version_to_publish)),
),
]
.iter()
{
match &*rt.tmprepo {
RepositoryHandle::SPFSWithVerbatimTags(spfs) => {
assert!(
spfs.ls_tags(&RelativePathBuf::from(tag_path))
.filter(|tag| { future::ready(entry_type_filter(tag)) })
.next()
.await
.is_some(),
"expected \"{tag_path}/{version_to_publish}\" tag to be found"
);
}
_ => panic!("expected SPFSWithVerbatimTags"),
}
}

match &*rt.tmprepo {
RepositoryHandle::SPFSWithVerbatimTags(spfs) => {
assert!(
spfs.ls_tags(&RelativePathBuf::from("spk/spec/my-local-pkg"))
.filter(|tag| {
future::ready(
matches!(tag, Ok(EntryType::Tag(tag)) if tag == version_to_publish),
)
})
.next()
.await
.is_some(),
"expected \"{version_to_publish}\" tag to be found"
);
}
_ => panic!("expected SPFSWithVerbatimTags"),
}

let mut opt = Opt::try_parse_from([
"remove",
"--legacy-spk-version-tags",
"--yes",
&format!("my-local-pkg/{version_to_delete}"),
])
.unwrap();
opt.remove.run().await.unwrap();

// Confirm that the tags are now gone.
for (tag_path, entry_type_filter) in [
(
"spk/spec/my-local-pkg",
Box::new(|tag: &_| matches!(tag, Ok(EntryType::Tag(tag)) if tag == version_to_publish))
as Box<dyn for<'a> Fn(&'a Result<EntryType, spfs::Error>) -> bool>,
),
(
"spk/pkg/my-local-pkg",
Box::new(|tag| matches!(tag, Ok(EntryType::Folder(tag)) if tag == version_to_publish)),
),
]
.iter()
{
match &*rt.tmprepo {
RepositoryHandle::SPFSWithVerbatimTags(spfs) => {
assert!(
spfs.ls_tags(&RelativePathBuf::from(tag_path))
.filter(|tag| { future::ready(entry_type_filter(tag)) })
.next()
.await
.is_none(),
"expected \"{tag_path}/{version_to_publish}\" tag to be gone"
);
}
_ => panic!("expected SPFSWithVerbatimTags"),
}
}
}

0 comments on commit 91abfd9

Please sign in to comment.