use assert_cmd::prelude::*;
use assert_fs::prelude::*;
use assert_fs::TempDir;
use predicates::prelude::*;
use conserve::test_fixtures::ScratchArchive;
use conserve::BandId;
use crate::run_conserve;
#[test]
fn delete_both_bands() {
let af = ScratchArchive::new();
af.store_two_versions();
run_conserve()
.args(["delete"])
.args(["-b", "b0000"])
.args(["-b", "b0001"])
.arg(af.path())
.assert()
.success();
assert_eq!(af.list_band_ids().unwrap().len(), 0);
assert_eq!(af.block_dir().block_names().unwrap().count(), 0);
}
#[test]
fn delete_first_version() {
let af = ScratchArchive::new();
af.store_two_versions();
run_conserve()
.args(["delete"])
.args(["-b", "b0"])
.arg(af.path())
.assert()
.success();
assert_eq!(af.list_band_ids().unwrap(), &[BandId::new(&[1])]);
assert_eq!(af.block_dir().block_names().unwrap().count(), 2);
let rd = TempDir::new().unwrap();
run_conserve()
.arg("restore")
.arg(af.path())
.arg(rd.path())
.assert()
.success();
rd.child("hello").assert(predicate::path::is_file());
rd.child("hello").assert(predicate::eq("contents"));
rd.child("subdir/subfile").assert(predicate::eq("contents"));
rd.child("hello2").assert(predicate::eq("contents"));
run_conserve()
.arg("validate")
.arg(af.path())
.assert()
.success();
}
#[test]
fn delete_second_version() {
let af = ScratchArchive::new();
af.store_two_versions();
run_conserve()
.args(["delete"])
.args(["-b", "b1"])
.arg(af.path())
.assert()
.success();
assert_eq!(af.list_band_ids().unwrap(), &[BandId::new(&[0])]);
assert_eq!(af.block_dir().block_names().unwrap().count(), 1);
let rd = TempDir::new().unwrap();
run_conserve()
.arg("restore")
.arg(af.path())
.arg(rd.path())
.assert()
.success();
rd.child("hello").assert(predicate::path::is_file());
rd.child("hello").assert(predicate::eq("contents"));
rd.child("subdir/subfile").assert(predicate::eq("contents"));
rd.child("hello2").assert(predicate::path::exists().not());
run_conserve()
.arg("validate")
.arg(af.path())
.assert()
.success();
}
#[test]
fn delete_nonexistent_band() {
let af = ScratchArchive::new();
let pred_fn = predicate::str::is_match(
r"conserve error: Failed to delete band b0000
caused by: (No such file or directory|The system cannot find the file specified\.) \(os error \d+\)
",
)
.unwrap();
run_conserve()
.args(["delete"])
.args(["-b", "b0000"])
.arg(af.path())
.assert()
.stdout(pred_fn)
.failure();
}