mod common;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use common::TmpDir;
use ostrya::{
Checksum, CommitOptions, CreateOptions, DirMeta, Error, MutableTree, Ref, RefSelector, Repo,
RepoMode, Summary, SummaryOptions, Xattrs,
};
use ostrya_rt::block_on;
const OWN: &str = "org.example.Own";
const OTHER: &str = "org.example.Other";
fn create(tmp: &TmpDir, collection_id: Option<&str>) -> PathBuf {
let path = tmp.path().join("repo");
let mut opts = CreateOptions::new(RepoMode::BareUser);
if let Some(id) = collection_id {
opts = opts.collection_id(id);
}
block_on(Repo::create(&path, opts)).unwrap();
path
}
fn open(path: &Path) -> Repo {
block_on(Repo::open(path)).unwrap()
}
fn checksum(byte: u8) -> Checksum {
Checksum::from_bytes([byte; 32])
}
async fn put(repo: &Repo, id: &str, name: &str, checksum: &Checksum) {
repo.set_ref_immediate(&Ref::collection(id, name), Some(checksum))
.await
.unwrap();
}
fn put_own_mirror(path: &Path, name: &str, checksum: &Checksum) {
let file = path.join("refs/mirrors").join(OWN).join(name);
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
std::fs::write(file, format!("{}\n", checksum.to_hex())).unwrap();
}
async fn put_local(repo: &Repo, name: &str, checksum: &Checksum) {
repo.set_ref_immediate(&Ref::local(name), Some(checksum))
.await
.unwrap();
}
async fn resolve(repo: &Repo, r: &Ref) -> Option<Checksum> {
repo.resolve_ref_tip(r).await.unwrap()
}
async fn resolve_or_not_found(repo: &Repo, r: &Ref) -> ostrya::Result<Option<Checksum>> {
repo.resolve_ref_tip(r)
.await?
.map(Some)
.ok_or_else(|| Error::RefNotFound(r.to_string()))
}
fn assert_not_found(result: ostrya::Result<Option<Checksum>>, label: &str) {
match result {
Err(Error::RefNotFound(got)) => assert_eq!(got, label),
other => panic!("{label}: {other:?}"),
}
}
fn assert_io(result: ostrya::Result<Option<Checksum>>, kind: ErrorKind) {
match result {
Err(Error::Io(e)) => assert_eq!(e.kind(), kind, "{e}"),
other => panic!("{kind:?}: {other:?}"),
}
}
fn assert_not_a_ref(result: ostrya::Result<Option<Checksum>>, name: &str) {
match result {
Err(Error::NotARef { name: got, reason }) => {
assert_eq!(got, name);
assert_eq!(reason, "is not a regular file", "{name}");
}
other => panic!("{name}: {other:?}"),
}
}
#[test]
fn a_mirror_ref_of_another_collection_resolves() {
let tmp = TmpDir::new("ref-store-mirror");
let repo = open(&create(&tmp, Some(OWN)));
block_on(async {
put(&repo, OTHER, "main", &checksum(1)).await;
put_local(&repo, "main", &checksum(2)).await;
let cref = Ref::collection(OTHER, "main");
assert_eq!(resolve(&repo, &cref).await, Some(checksum(1)));
assert_eq!(
repo.resolve_ref_tip(&cref).await.unwrap(),
Some(checksum(1))
);
});
}
#[test]
fn another_collection_does_not_read_a_local_ref() {
let tmp = TmpDir::new("ref-store-other-local");
let repo = open(&create(&tmp, Some(OWN)));
block_on(async {
put_local(&repo, "x", &checksum(1)).await;
assert_eq!(resolve(&repo, &Ref::collection(OTHER, "x")).await, None);
});
}
#[test]
fn the_own_collection_reads_a_local_ref() {
let tmp = TmpDir::new("ref-store-own-local");
let repo = open(&create(&tmp, Some(OWN)));
block_on(async {
put_local(&repo, "main", &checksum(1)).await;
let cref = Ref::collection(OWN, "main");
assert_eq!(resolve(&repo, &cref).await, Some(checksum(1)));
assert_eq!(
repo.resolve_ref_tip(&cref).await.unwrap(),
Some(checksum(1))
);
});
}
#[test]
fn the_own_collection_ignores_its_mirror_refs() {
let tmp = TmpDir::new("ref-store-own-mirror");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
put_own_mirror(&path, "main", &checksum(1));
put_own_mirror(&path, "only", &checksum(1));
block_on(async {
put_local(&repo, "main", &checksum(2)).await;
assert_eq!(
resolve(&repo, &Ref::collection(OWN, "main")).await,
Some(checksum(2))
);
let only = Ref::collection(OWN, "only");
assert_eq!(resolve(&repo, &only).await, None);
assert_not_found(
resolve_or_not_found(&repo, &only).await,
&format!("@{OWN}:only"),
);
});
}
#[test]
fn a_local_ref_reads_refs_heads_alone() {
let tmp = TmpDir::new("ref-store-local");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
put_own_mirror(&path, "main", &checksum(1));
put_own_mirror(&path, "only-mirror", &checksum(1));
block_on(async {
put_local(&repo, "main", &checksum(2)).await;
assert_eq!(resolve(&repo, &Ref::local("main")).await, Some(checksum(2)));
assert_eq!(resolve(&repo, &Ref::local("only-mirror")).await, None);
});
}
#[test]
fn a_repository_with_no_collection_id_reads_the_mirror_ref_alone() {
let tmp = TmpDir::new("ref-store-no-id");
let repo = open(&create(&tmp, None));
block_on(async {
put_local(&repo, "main", &checksum(1)).await;
let cref = Ref::collection(OWN, "main");
assert_eq!(resolve(&repo, &cref).await, None);
assert_not_found(
resolve_or_not_found(&repo, &cref).await,
&format!("@{OWN}:main"),
);
put(&repo, OWN, "main", &checksum(2)).await;
assert_eq!(resolve(&repo, &cref).await, Some(checksum(2)));
});
}
#[test]
fn an_alias_resolves_to_its_target() {
let tmp = TmpDir::new("ref-store-alias");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
put_local(&repo, "main", &checksum(1)).await;
repo.set_ref_alias_immediate(&Ref::local("current"), &Ref::local("main"))
.await
.unwrap();
assert_eq!(
resolve(&repo, &Ref::local("current")).await,
Some(checksum(1))
);
assert_eq!(
resolve(&repo, &Ref::collection(OWN, "current")).await,
Some(checksum(1))
);
put(&repo, OTHER, "stable", &checksum(2)).await;
let mirror = path.join("refs/mirrors").join(OTHER);
std::os::unix::fs::symlink("stable", mirror.join("current")).unwrap();
assert_eq!(
resolve(&repo, &Ref::collection(OTHER, "current")).await,
Some(checksum(2))
);
});
}
#[test]
fn an_alias_with_an_absent_target_reads_as_an_absent_ref() {
let tmp = TmpDir::new("ref-store-dangling");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
put_local(&repo, "gone", &checksum(1)).await;
let own = path.join("refs/mirrors").join(OWN);
std::fs::create_dir_all(&own).unwrap();
std::os::unix::fs::symlink("missing", own.join("gone")).unwrap();
assert_eq!(
resolve(&repo, &Ref::collection(OWN, "gone")).await,
Some(checksum(1))
);
let other = path.join("refs/mirrors").join(OTHER);
std::fs::create_dir_all(&other).unwrap();
std::os::unix::fs::symlink("missing", other.join("gone")).unwrap();
assert_eq!(resolve(&repo, &Ref::collection(OTHER, "gone")).await, None);
});
}
#[test]
fn an_absent_ref_reads_as_none() {
let tmp = TmpDir::new("ref-store-absent");
let repo = open(&create(&tmp, Some(OWN)));
block_on(async {
for (cref, label) in [
(Ref::collection(OTHER, "absent"), format!("@{OTHER}:absent")),
(Ref::collection(OWN, "absent"), format!("@{OWN}:absent")),
(Ref::local("absent"), "absent".to_owned()),
] {
assert_eq!(repo.resolve_ref_tip(&cref).await.unwrap(), None, "{label}");
assert_not_found(resolve_or_not_found(&repo, &cref).await, &label);
}
});
}
#[test]
fn the_own_collection_reads_a_local_ref_beside_a_mirror_directory_or_file() {
let tmp = TmpDir::new("ref-store-own-not-a-ref");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
put_own_mirror(&path, "a/b", &checksum(2));
put_local(&repo, "a", &checksum(1)).await;
let cref = Ref::collection(OWN, "a");
assert_eq!(
repo.resolve_ref_tip(&cref).await.unwrap(),
Some(checksum(1))
);
put_own_mirror(&path, "f", &checksum(2));
put_local(&repo, "f/g", &checksum(1)).await;
let cref = Ref::collection(OWN, "f/g");
assert_eq!(
repo.resolve_ref_tip(&cref).await.unwrap(),
Some(checksum(1))
);
});
}
#[test]
fn the_own_collection_reads_a_local_ref_whose_mirror_path_is_too_long() {
let tmp = TmpDir::new("ref-store-own-too-long");
let repo = open(&create(&tmp, Some(OWN)));
let mut parts = vec!["a".repeat(200); 20];
parts.push("b".repeat(59));
let name = parts.join("/");
assert_eq!(name.len(), 4079);
block_on(async {
put_local(&repo, &name, &checksum(1)).await;
assert_eq!(
repo.resolve_ref_tip(&Ref::collection(OWN, name.as_str()))
.await
.unwrap(),
Some(checksum(1))
);
assert_io(
repo.resolve_ref_tip(&Ref::collection(OTHER, name.as_str()))
.await,
ErrorKind::InvalidFilename,
);
});
}
#[test]
fn the_own_collection_reads_a_local_ref_beside_a_mirror_ref_that_fails() {
let tmp = TmpDir::new("ref-store-own-fails");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
let own = path.join("refs/mirrors").join(OWN);
std::fs::create_dir_all(&own).unwrap();
std::os::unix::fs::symlink("l", own.join("l")).unwrap();
std::fs::write(own.join("g"), "garbage\n").unwrap();
std::fs::write(own.join("u"), b"\xff\xfe\n").unwrap();
block_on(async {
for name in ["l", "g", "u"] {
put_local(&repo, name, &checksum(1)).await;
let cref = Ref::collection(OWN, name);
assert_eq!(
repo.resolve_ref_tip(&cref).await.unwrap(),
Some(checksum(1)),
"{name}"
);
}
});
}
#[test]
fn a_directory_is_not_a_ref_and_a_path_through_a_file_reads_as_no_ref() {
let tmp = TmpDir::new("ref-store-other-not-a-ref");
let repo = open(&create(&tmp, Some(OWN)));
block_on(async {
put(&repo, OTHER, "a/b", &checksum(2)).await;
put_local(&repo, "a", &checksum(1)).await;
let cref = Ref::collection(OTHER, "a");
assert_not_a_ref(repo.resolve_ref_tip(&cref).await, &cref.to_string());
put(&repo, OTHER, "f", &checksum(2)).await;
put_local(&repo, "f/g", &checksum(1)).await;
assert_eq!(resolve(&repo, &Ref::collection(OTHER, "f/g")).await, None);
put_local(&repo, "d/e", &checksum(1)).await;
assert_not_a_ref(repo.resolve_ref_tip(&Ref::local("d")).await, "d");
let rev = format!("@{OTHER}:a");
assert_not_a_ref(repo.resolve_rev(&rev, false).await, &rev);
let rev = format!("@{OTHER}:f/g");
assert_not_found(repo.resolve_rev(&rev, false).await, &rev);
});
}
#[test]
fn ref_path_has_entry_reads_each_state_of_a_ref_path() {
let tmp = TmpDir::new("ref-store-has-entry");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
let heads = path.join("refs/heads");
let outside = tmp.path().join("outside");
std::fs::create_dir(&outside).unwrap();
block_on(async {
let has = |r: Ref| {
let repo = repo.clone();
async move { repo.ref_path_has_entry(&r).await.unwrap() }
};
assert!(!has(Ref::local("absent")).await);
assert!(!has(Ref::local("absent/below")).await);
assert!(!has(Ref::collection(OTHER, "absent")).await);
put_local(&repo, "file", &checksum(1)).await;
assert!(has(Ref::local("file")).await);
assert!(has(Ref::collection(OWN, "file")).await);
assert!(has(Ref::local("file/below")).await);
std::os::unix::fs::symlink("file", heads.join("alias")).unwrap();
assert!(has(Ref::local("alias")).await);
std::os::unix::fs::symlink("nowhere", heads.join("dangling")).unwrap();
assert!(has(Ref::local("dangling")).await);
assert!(has(Ref::local("dangling/below")).await);
std::fs::create_dir(heads.join("dir")).unwrap();
assert!(has(Ref::local("dir")).await);
assert!(!has(Ref::local("dir/absent")).await);
let fifo = std::process::Command::new("mkfifo")
.arg(heads.join("fifo"))
.status()
.unwrap();
assert!(fifo.success(), "mkfifo failed");
assert!(has(Ref::local("fifo")).await);
assert!(has(Ref::local("fifo/below")).await);
std::fs::create_dir_all(path.join("refs/mirrors")).unwrap();
std::os::unix::fs::symlink(&outside, path.join("refs/mirrors").join(OTHER)).unwrap();
assert!(has(Ref::collection(OTHER, "absent")).await);
std::fs::rename(&heads, path.join("refs/heads.real")).unwrap();
std::os::unix::fs::symlink(&outside, &heads).unwrap();
assert!(has(Ref::local("absent")).await);
assert!(has(Ref::collection(OWN, "absent")).await);
let err = repo
.ref_path_has_entry(&Ref::local("a/../b"))
.await
.unwrap_err();
assert!(matches!(err, Error::InvalidRefspec(_)), "{err:?}");
});
}
#[test]
fn a_resolve_takes_no_update_lock() {
let tmp = TmpDir::new("ref-store-no-lock");
let path = create(&tmp, Some(OWN));
let config = path.join("config");
let mut text = std::fs::read_to_string(&config).unwrap();
text.push_str("lock-timeout-secs=0\n");
std::fs::write(&config, text).unwrap();
let holder = open(&path);
let reader = open(&path);
block_on(async {
put(&holder, OTHER, "a", &checksum(1)).await;
put_local(&holder, "b", &checksum(2)).await;
put_local(&holder, "c", &checksum(3)).await;
let crefs = [
(Ref::collection(OTHER, "a"), checksum(1)),
(Ref::collection(OWN, "b"), checksum(2)),
(Ref::local("c"), checksum(3)),
];
let guard = holder.begin_update().await.unwrap();
for repo in [&holder, &reader] {
for (cref, want) in &crefs {
assert_eq!(
repo.resolve_ref_tip(cref).await.unwrap(),
Some(*want),
"{cref:?}"
);
}
}
let tasks: Vec<_> = crefs
.iter()
.cloned()
.flat_map(|(cref, want)| {
[holder.clone(), reader.clone()].map(|repo| {
let cref = cref.clone();
ostrya_rt::spawn(async move { (repo.resolve_ref_tip(&cref).await, want, cref) })
})
})
.collect();
for task in tasks {
let (result, want, cref) = task.await;
assert_eq!(result.unwrap(), Some(want), "{cref:?}");
}
guard.finish().await.unwrap();
});
}
async fn write_commit(repo: &Repo, subject: &str, parent: Option<Checksum>) -> Checksum {
let txn = repo.transaction().await.unwrap();
let meta = DirMeta {
uid: 0,
gid: 0,
mode: 0o040755,
xattrs: Xattrs::empty(),
};
let mut mtree = MutableTree::new();
mtree.set_metadata_checksum(txn.write_dirmeta(&meta).await.unwrap());
let root = txn.write_mtree(&mut mtree).await.unwrap();
let commit = txn
.write_commit(
CommitOptions {
parent,
subject: Some(subject.to_owned()),
timestamp: Some(1_700_000_000),
..CommitOptions::default()
},
&root,
)
.await
.unwrap();
txn.commit().await.unwrap();
commit
}
#[test]
fn a_revision_reads_a_collection_ref_and_its_ancestry() {
let tmp = TmpDir::new("ref-store-rev");
let repo = open(&create(&tmp, Some(OWN)));
block_on(async {
let first = write_commit(&repo, "first", None).await;
let second = write_commit(&repo, "second", Some(first)).await;
put(&repo, OTHER, "os/x86_64", &second).await;
let rev = format!("@{OTHER}:os/x86_64");
assert_eq!(repo.resolve_rev(&rev, false).await.unwrap(), Some(second));
assert_eq!(
repo.resolve_rev(&format!("{rev}^"), false).await.unwrap(),
Some(first)
);
assert!(matches!(
repo.resolve_rev(&format!("{rev}^^"), false).await,
Err(Error::NoParentCommit(c)) if c == first
));
});
}
#[test]
fn a_revision_of_the_own_collection_reads_the_local_ref() {
let tmp = TmpDir::new("ref-store-rev-own");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
put_own_mirror(&path, "main", &checksum(1));
block_on(async {
put_local(&repo, "main", &checksum(2)).await;
assert_eq!(
repo.resolve_rev(&format!("@{OWN}:main"), false)
.await
.unwrap(),
Some(checksum(2))
);
});
}
#[test]
fn a_revision_of_an_absent_collection_ref_names_the_ref() {
let tmp = TmpDir::new("ref-store-rev-absent");
let repo = open(&create(&tmp, Some(OWN)));
block_on(async {
let rev = format!("@{OTHER}:x");
assert_not_found(repo.resolve_rev(&rev, false).await, &rev);
assert_eq!(repo.resolve_rev(&rev, true).await.unwrap(), None);
for rev in ["@bad:x", "@bad-id.x:y", "@", "@org.example.A:"] {
match repo.resolve_rev(rev, true).await {
Err(Error::InvalidRefspec(got)) => assert_eq!(got, rev),
other => panic!("{rev}: {other:?}"),
}
}
});
}
fn ref_file(path: &Path, rel: &str) -> Option<Checksum> {
match std::fs::read_to_string(path.join(rel)) {
Ok(text) => Some(Checksum::from_hex(text.trim_end()).unwrap()),
Err(e) if e.kind() == ErrorKind::NotFound => None,
Err(e) => panic!("{rel}: {e}"),
}
}
fn cases() -> [(Ref, &'static str); 4] {
[
(Ref::local("a/b"), "refs/heads/a/b"),
(Ref::remote("origin", "a/b"), "refs/remotes/origin/a/b"),
(
Ref::collection(OTHER, "a/b"),
"refs/mirrors/org.example.Other/a/b",
),
(Ref::collection(OWN, "c"), "refs/heads/c"),
]
}
#[test]
fn each_writer_puts_each_case_at_its_path() {
let tmp = TmpDir::new("ref-store-writers");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
for (r, rel) in cases() {
repo.set_ref_immediate(&r, Some(&checksum(1)))
.await
.unwrap();
assert_eq!(ref_file(&path, rel), Some(checksum(1)), "{r}");
assert_eq!(resolve(&repo, &r).await, Some(checksum(1)), "{r}");
let txn = repo.transaction().await.unwrap();
txn.set_ref(&r, Some(&checksum(2)));
txn.commit().await.unwrap();
assert_eq!(ref_file(&path, rel), Some(checksum(2)), "{r}");
assert_eq!(resolve(&repo, &r).await, Some(checksum(2)), "{r}");
let guard = repo.begin_update().await.unwrap();
guard.set_ref(&r, Some(&checksum(3))).await.unwrap();
assert_eq!(guard.read_ref(&r).await.unwrap(), Some(checksum(3)), "{r}");
guard.finish().await.unwrap();
assert_eq!(ref_file(&path, rel), Some(checksum(3)), "{r}");
let guard = repo.begin_update().await.unwrap();
guard.set_ref(&r, None).await.unwrap();
assert_eq!(guard.read_ref(&r).await.unwrap(), None, "{r}");
guard.finish().await.unwrap();
assert_eq!(ref_file(&path, rel), None, "{r}");
}
assert!(!path.join("refs/mirrors").join(OWN).exists());
});
}
#[test]
fn a_parsed_ref_of_the_own_collection_writes_the_local_ref() {
let tmp = TmpDir::new("ref-store-parsed-own");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
let r: Ref = format!("@{OWN}:a").parse().unwrap();
assert_eq!(r, Ref::collection(OWN, "a"));
repo.set_ref_immediate(&r, Some(&checksum(1)))
.await
.unwrap();
assert_eq!(ref_file(&path, "refs/heads/a"), Some(checksum(1)));
assert_eq!(resolve(&repo, &Ref::local("a")).await, Some(checksum(1)));
assert!(!path.join("refs/mirrors").join(OWN).exists());
});
}
#[test]
fn remove_refs_immediate_refuses_a_batch_with_a_mirror_alias() {
let tmp = TmpDir::new("ref-store-remove-alias");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
put(&repo, OTHER, "a/b", &checksum(1)).await;
let mirror = path.join("refs/mirrors").join(OTHER);
std::os::unix::fs::symlink("b", mirror.join("a/c")).unwrap();
let batch = [Ref::collection(OTHER, "a/b"), Ref::collection(OTHER, "a/c")];
match repo.remove_refs_immediate(&batch).await {
Err(Error::RefNotWritable { name, reason }) => {
assert_eq!(name, format!("@{OTHER}:a/c"));
assert_eq!(reason, "names an alias");
}
other => panic!("expected RefNotWritable, got {other:?}"),
}
assert!(mirror.join("a/b").symlink_metadata().unwrap().is_file());
assert!(mirror.join("a/c").symlink_metadata().unwrap().is_symlink());
put_local(&repo, "main", &checksum(2)).await;
put_local(&repo, "other", &checksum(3)).await;
repo.set_ref_alias_immediate(&Ref::local("al"), &Ref::local("main"))
.await
.unwrap();
let batch = [Ref::local("other"), Ref::local("al")];
match repo.remove_refs_immediate(&batch).await {
Err(Error::RefNotWritable { name, reason }) => {
assert_eq!(name, "al");
assert_eq!(reason, "names an alias");
}
other => panic!("expected RefNotWritable, got {other:?}"),
}
assert_eq!(ref_file(&path, "refs/heads/other"), Some(checksum(3)));
let heads = path.join("refs/heads");
assert!(heads.join("al").symlink_metadata().unwrap().is_symlink());
assert_eq!(ref_file(&path, "refs/heads/main"), Some(checksum(2)));
});
}
#[test]
fn remove_refs_immediate_removes_each_ref() {
let tmp = TmpDir::new("ref-store-remove-each");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
let mut batch = Vec::new();
for (r, _) in cases() {
repo.set_ref_immediate(&r, Some(&checksum(1)))
.await
.unwrap();
batch.push(r);
}
batch.push(Ref::local("absent"));
batch.push(Ref::local("a/b"));
repo.remove_refs_immediate(&batch).await.unwrap();
for (r, rel) in cases() {
assert_eq!(ref_file(&path, rel), None, "{r}");
}
for dir in [
"refs/heads/a",
"refs/remotes/origin/a",
"refs/mirrors/org.example.Other/a",
] {
assert!(path.join(dir).is_dir(), "{dir}");
}
assert!(!path.join("refs/mirrors").join(OWN).exists());
});
}
#[test]
fn remove_refs_immediate_refuses_an_invalid_ref_before_any_removal() {
let tmp = TmpDir::new("ref-store-remove-invalid");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
put_local(&repo, "main", &checksum(1)).await;
let batch = [Ref::local("main"), Ref::local("-bad")];
match repo.remove_refs_immediate(&batch).await {
Err(Error::InvalidRefspec(name)) => assert_eq!(name, "-bad"),
other => panic!("expected InvalidRefspec, got {other:?}"),
}
assert_eq!(ref_file(&path, "refs/heads/main"), Some(checksum(1)));
});
}
#[test]
fn remove_refs_immediate_with_an_empty_batch_takes_no_lock() {
let tmp = TmpDir::new("ref-store-remove-empty");
let path = create(&tmp, Some(OWN));
let writer = open(&path);
block_on(async {
put_local(&writer, "main", &checksum(1)).await;
});
let config = path.join("config");
let text = std::fs::read_to_string(&config).unwrap();
let text = text.replacen("[core]\n", "[core]\nlock-timeout-secs=-2\n", 1);
std::fs::write(&config, text).unwrap();
let repo = open(&path);
block_on(async {
repo.remove_refs_immediate(&[]).await.unwrap();
match repo.remove_refs_immediate(&[Ref::local("main")]).await {
Err(Error::InvalidFormat(message)) => {
assert!(
message.contains("lock-timeout-secs -2 is below -1"),
"{message}"
);
}
other => panic!("expected InvalidFormat, got {other:?}"),
}
assert_eq!(ref_file(&path, "refs/heads/main"), Some(checksum(1)));
});
}
const FIRST: &str = "org.example.First";
fn put_file(path: &Path, rel: &str, checksum: &Checksum) {
let file = path.join(rel);
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
std::fs::write(file, format!("{}\n", checksum.to_hex())).unwrap();
}
fn put_oversize(path: &Path, rel: &str) {
let file = path.join(rel);
std::fs::create_dir_all(file.parent().unwrap()).unwrap();
std::fs::write(file, vec![b'a'; 4097]).unwrap();
}
fn assert_over_cap<T: std::fmt::Debug>(result: ostrya::Result<T>, what: &str) {
match result {
Err(Error::InvalidFormat(m)) => {
assert!(m.contains("is larger than its cap"), "{what}: {m}")
}
other => panic!("{what}: {other:?}"),
}
}
#[test]
fn list_refs_where_gives_the_local_refs_then_the_collection_refs() {
let tmp = TmpDir::new("ref-store-where-order");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
put_local(&repo, "b", &checksum(1)).await;
put_local(&repo, "a", &checksum(2)).await;
put_local(&repo, "d/e", &checksum(3)).await;
repo.set_ref_alias_immediate(&Ref::local("cur"), &Ref::local("a"))
.await
.unwrap();
std::os::unix::fs::symlink("missing", path.join("refs/heads/gone")).unwrap();
put(&repo, OTHER, "x", &checksum(4)).await;
put(&repo, OTHER, "a/b", &checksum(5)).await;
put(&repo, FIRST, "z", &checksum(6)).await;
put_own_mirror(&path, "m", &checksum(7));
put_file(&path, "refs/mirrors/nodot/q", &checksum(8));
let all = repo.list_refs_where(|_| true).await.unwrap();
assert_eq!(
all,
vec![
(Ref::local("a"), checksum(2)),
(Ref::local("b"), checksum(1)),
(Ref::local("cur"), checksum(2)),
(Ref::local("d/e"), checksum(3)),
(Ref::collection(FIRST, "z"), checksum(6)),
(Ref::collection(OTHER, "a/b"), checksum(5)),
(Ref::collection(OTHER, "x"), checksum(4)),
]
);
let mut expected: Vec<(Ref, Checksum)> = repo
.list_refs(None)
.await
.unwrap()
.into_iter()
.map(|(name, c)| (Ref::local(name), c))
.collect();
let mut mirrors = repo.list_mirror_refs().await.unwrap();
mirrors.retain(|(id, _, _)| id != OWN && ostrya::is_valid_collection_id(id));
mirrors.sort();
expected.extend(
mirrors
.into_iter()
.map(|(id, name, c)| (Ref::collection(id, name), c)),
);
assert_eq!(all, expected);
let only = repo
.list_refs_where(|r| matches!(r, Ref::Collection { .. }) || r.name() == "b")
.await
.unwrap();
assert_eq!(
only,
vec![
(Ref::local("b"), checksum(1)),
(Ref::collection(FIRST, "z"), checksum(6)),
(Ref::collection(OTHER, "a/b"), checksum(5)),
(Ref::collection(OTHER, "x"), checksum(4)),
]
);
});
}
#[test]
fn list_refs_where_does_not_read_an_entry_that_keep_drops() {
let tmp = TmpDir::new("ref-store-where-keep");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
put_local(&repo, "main", &checksum(1)).await;
put(&repo, OTHER, "x", &checksum(2)).await;
put_oversize(&path, "refs/heads/big");
put_oversize(&path, &format!("refs/mirrors/{OTHER}/big"));
let refs = repo.list_refs_where(|r| r.name() != "big").await.unwrap();
assert_eq!(
refs,
vec![
(Ref::local("main"), checksum(1)),
(Ref::collection(OTHER, "x"), checksum(2)),
]
);
for name in [Ref::local("big"), Ref::collection(OTHER, "big")] {
let want = name.clone();
let refs = repo.list_refs_where(move |r| *r == want).await;
assert_over_cap(refs, &name.to_string());
}
assert_over_cap(repo.list_refs(None).await, "list_refs");
});
}
#[test]
fn list_refs_where_does_not_read_the_own_or_an_invalid_collection() {
let tmp = TmpDir::new("ref-store-where-skip");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
put_local(&repo, "main", &checksum(1)).await;
put(&repo, OTHER, "x", &checksum(2)).await;
let own = format!("refs/mirrors/{OWN}/r");
let entries = [own.as_str(), "refs/mirrors/nodot/r", "refs/mirrors/top"];
for rel in entries {
put_oversize(&path, rel);
}
let seen = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let record = seen.clone();
let refs = repo
.list_refs_where(move |r| {
record.lock().unwrap().push(r.clone());
true
})
.await
.unwrap();
assert_eq!(
refs,
vec![
(Ref::local("main"), checksum(1)),
(Ref::collection(OTHER, "x"), checksum(2)),
]
);
let mut seen = seen.lock().unwrap().clone();
seen.sort_by_key(|r| r.to_string());
assert_eq!(seen, vec![Ref::collection(OTHER, "x"), Ref::local("main")]);
assert_over_cap(repo.list_mirror_refs().await, "list_mirror_refs");
for rel in entries {
put_file(&path, rel, &checksum(3));
}
let mut mirrors = repo.list_mirror_refs().await.unwrap();
mirrors.sort();
assert_eq!(
mirrors,
vec![
("nodot".to_owned(), "r".to_owned(), checksum(3)),
(OTHER.to_owned(), "x".to_owned(), checksum(2)),
(OWN.to_owned(), "r".to_owned(), checksum(3)),
]
);
});
}
fn put_fifo(path: &Path, rel: &str) -> PathBuf {
let fifo = path.join(rel);
std::fs::create_dir_all(fifo.parent().unwrap()).unwrap();
rustix::fs::mknodat(
rustix::fs::CWD,
&fifo,
rustix::fs::FileType::Fifo,
rustix::fs::Mode::from_raw_mode(0o644),
0,
)
.unwrap();
fifo
}
fn without_wait<T: Send + 'static>(
repo: &Repo,
fifos: &[PathBuf],
what: &str,
call: impl FnOnce(Repo) -> T + Send + 'static,
) -> T {
let (tx, rx) = std::sync::mpsc::channel();
let repo = repo.clone();
std::thread::spawn(move || {
let _ = tx.send(call(repo));
});
let timeout = std::time::Duration::from_secs(10);
let Ok(result) = rx.recv_timeout(timeout) else {
for fifo in fifos {
let _ = rustix::fs::open(
fifo,
rustix::fs::OFlags::WRONLY
| rustix::fs::OFlags::NONBLOCK
| rustix::fs::OFlags::CLOEXEC,
rustix::fs::Mode::empty(),
);
}
let _ = rx.recv_timeout(timeout);
panic!("{what}: the open waits on a FIFO");
};
result
}
#[test]
fn a_ref_entry_that_is_a_fifo_is_no_ref() {
let tmp = TmpDir::new("ref-store-fifo");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
put_local(&repo, "main", &checksum(1)).await;
repo.set_ref_immediate(&Ref::remote("o", "x"), Some(&checksum(2)))
.await
.unwrap();
put(&repo, OTHER, "x", &checksum(3)).await;
});
let fifos = [
put_fifo(&path, "refs/heads/fifo"),
put_fifo(&path, "refs/remotes/o/fifo"),
put_fifo(&path, &format!("refs/mirrors/{OTHER}/fifo")),
];
let heads = without_wait(&repo, &fifos, "list_refs", |r| block_on(r.list_refs(None)));
assert_eq!(heads.unwrap(), vec![("main".to_owned(), checksum(1))]);
let remotes = without_wait(&repo, &fifos, "list_remote_refs", |r| {
block_on(r.list_remote_refs())
});
assert_eq!(remotes.unwrap(), vec![("o:x".to_owned(), checksum(2))]);
let mirrors = without_wait(&repo, &fifos, "list_mirror_refs", |r| {
block_on(r.list_mirror_refs())
});
assert_eq!(
mirrors.unwrap(),
vec![(OTHER.to_owned(), "x".to_owned(), checksum(3))]
);
let all = without_wait(&repo, &fifos, "list_refs_where", |r| {
block_on(r.list_refs_where(|_| true))
});
assert_eq!(
all.unwrap(),
vec![
(Ref::local("main"), checksum(1)),
(Ref::collection(OTHER, "x"), checksum(3)),
]
);
let collections = without_wait(&repo, &fifos, "list_collection_refs", |r| {
block_on(r.list_collection_refs())
});
let collections: Vec<_> = collections
.unwrap()
.into_iter()
.map(|e| (e.collection, e.name, e.commit))
.collect();
assert_eq!(
collections,
vec![
(OTHER.to_owned(), "x".to_owned(), checksum(3)),
(OWN.to_owned(), "main".to_owned(), checksum(1)),
]
);
let other_fifo = format!("@{OTHER}:fifo");
for rev in ["fifo", "o:fifo", other_fifo.as_str()] {
let owned = rev.to_owned();
let strict = without_wait(&repo, &fifos, rev, move |r| {
block_on(r.resolve_rev(&owned, false))
});
assert_not_a_ref(strict, rev);
let owned = rev.to_owned();
let lenient = without_wait(&repo, &fifos, rev, move |r| {
block_on(r.resolve_rev(&owned, true))
});
assert_not_a_ref(lenient, rev);
}
for r in [
Ref::local("fifo"),
Ref::remote("o", "fifo"),
Ref::collection(OTHER, "fifo"),
] {
let what = r.to_string();
let tip = without_wait(&repo, &fifos, &what, move |repo| {
block_on(repo.resolve_ref_tip(&r))
});
assert_not_a_ref(tip, &what);
}
}
fn assert_through_alias<T: std::fmt::Debug>(result: ostrya::Result<T>, name: &str) {
match result {
Err(Error::RefNotWritable { name: n, reason }) => {
assert_eq!(n, name);
assert_eq!(reason, "passes through an alias", "{name}");
}
other => panic!("{name}: expected RefNotWritable, got {other:?}"),
}
}
#[test]
fn a_ref_write_below_an_alias_is_refused() {
let tmp = TmpDir::new("ref-store-below-alias");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
let heads = path.join("refs/heads");
block_on(async {
put_local(&repo, "d0/y", &checksum(1)).await;
repo.set_ref_immediate(&Ref::remote("real", "y"), Some(&checksum(1)))
.await
.unwrap();
std::os::unix::fs::symlink("nosuch", heads.join("up")).unwrap();
std::os::unix::fs::symlink("d0", heads.join("up2")).unwrap();
std::os::unix::fs::symlink("real", path.join("refs/remotes/origin")).unwrap();
let refs = [
Ref::local("up/x"),
Ref::local("up2/x"),
Ref::local("up2/y"),
Ref::remote("origin", "x"),
Ref::remote("origin", "y"),
];
for r in &refs {
let name = r.to_string();
for checksum in [Some(checksum(2)), None] {
assert_through_alias(repo.set_ref_immediate(r, checksum.as_ref()).await, &name);
let guard = repo.begin_update().await.unwrap();
assert_through_alias(guard.set_ref(r, checksum.as_ref()).await, &name);
guard.finish().await.unwrap();
}
assert_through_alias(
repo.remove_refs_immediate(std::slice::from_ref(r)).await,
&name,
);
}
let target = Ref::local("d0/y");
for refspec in ["up/al", "up2/al", "origin:al"] {
let alias: Ref = refspec.parse().unwrap();
assert_through_alias(repo.set_ref_alias_immediate(&alias, &target).await, refspec);
let guard = repo.begin_update().await.unwrap();
assert_through_alias(guard.set_ref_alias(&alias, &target).await, refspec);
guard.finish().await.unwrap();
}
assert_eq!(
std::fs::read_dir(heads.join("d0"))
.unwrap()
.map(|e| e.unwrap().file_name())
.collect::<Vec<_>>(),
["y"]
);
assert_eq!(
std::fs::read_dir(path.join("refs/remotes/real"))
.unwrap()
.map(|e| e.unwrap().file_name())
.collect::<Vec<_>>(),
["y"]
);
assert!(!heads.join("nosuch").exists());
assert_eq!(ref_file(&path, "refs/heads/d0/y"), Some(checksum(1)));
for r in [Ref::local("up2/y"), Ref::remote("origin", "y")] {
match repo.resolve_ref_tip(&r).await {
Err(Error::InvalidFormat(m)) => {
assert!(m.contains("passes through a symlink"), "{r}: {m}");
}
other => panic!("{r}: {other:?}"),
}
}
});
}
#[test]
fn an_alias_removal_below_an_alias_is_refused() {
let tmp = TmpDir::new("ref-store-alias-removal-below-alias");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
let heads = path.join("refs/heads");
block_on(async {
put_local(&repo, "d0/y", &checksum(1)).await;
repo.set_ref_alias_immediate(&Ref::local("d0/al"), &Ref::local("d0/y"))
.await
.unwrap();
std::os::unix::fs::symlink("d0", heads.join("up2")).unwrap();
std::os::unix::fs::symlink("nosuch", heads.join("up")).unwrap();
for refspec in ["up2/al", "up/al"] {
let alias = Ref::local(refspec);
assert_through_alias(repo.remove_ref_alias_immediate(&alias).await, refspec);
let guard = repo.begin_update().await.unwrap();
assert_through_alias(guard.remove_ref_alias(&alias).await, refspec);
guard.finish().await.unwrap();
}
assert!(
std::fs::symlink_metadata(heads.join("d0/al"))
.unwrap()
.is_symlink(),
"the alias behind the link stays"
);
assert_eq!(
resolve(&repo, &Ref::local("d0/al")).await,
Some(checksum(1))
);
assert!(!heads.join("nosuch").exists());
});
}
fn link_body(path: &Path, rel: &str) -> String {
std::fs::read_link(path.join(rel))
.unwrap()
.into_os_string()
.into_string()
.unwrap()
}
fn assert_not_writable<T: std::fmt::Debug>(result: ostrya::Result<T>, name: &str, reason: &str) {
match result {
Err(Error::RefNotWritable { name: n, reason: r }) => {
assert_eq!((n.as_str(), r.as_str()), (name, reason));
}
other => panic!("{name}: expected RefNotWritable, got {other:?}"),
}
}
#[test]
fn a_mirror_alias_resolves_to_its_target() {
let tmp = TmpDir::new("ref-store-mirror-alias");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
let rel = format!("refs/mirrors/{OTHER}/defpool/x");
block_on(async {
let commit = write_commit(&repo, "one", None).await;
put(&repo, OTHER, "pool/x", &commit).await;
let alias = Ref::collection(OTHER, "defpool/x");
repo.set_ref_alias_immediate(&alias, &Ref::collection(OTHER, "pool/x"))
.await
.unwrap();
assert_eq!(link_body(&path, &rel), "../pool/x");
assert_eq!(resolve(&repo, &alias).await, Some(commit));
let all = RefSelector::parse(["**"]).unwrap();
assert_eq!(
repo.list_collection_refs_in(OTHER, &all).await.unwrap(),
[
("defpool/x".to_owned(), commit),
("pool/x".to_owned(), commit),
]
);
let listed = repo.list_refs_where(|_| true).await.unwrap();
assert!(listed.contains(&(alias, commit)), "{listed:?}");
repo.regenerate_summary(&SummaryOptions::default())
.await
.unwrap();
let summary = Summary::parse(&repo.read_summary().await.unwrap().unwrap()).unwrap();
let map = summary.collection_map().unwrap();
let refs = map
.iter()
.find(|(id, _)| id == OTHER)
.map(|(_, refs)| refs)
.unwrap_or_else(|| panic!("no {OTHER} in {map:?}"));
let names: Vec<_> = refs.iter().map(|r| (r.name.as_str(), r.commit)).collect();
assert_eq!(names, [("defpool/x", commit), ("pool/x", commit)]);
});
}
#[test]
fn the_guard_writes_a_mirror_alias() {
let tmp = TmpDir::new("ref-store-guard-mirror-alias");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
let rel = format!("refs/mirrors/{OTHER}/defpool/x");
block_on(async {
put(&repo, OTHER, "pool/x", &checksum(1)).await;
let alias = Ref::collection(OTHER, "defpool/x");
let guard = repo.begin_update().await.unwrap();
guard
.set_ref_alias(&alias, &Ref::collection(OTHER, "pool/x"))
.await
.unwrap();
guard.finish().await.unwrap();
assert_eq!(link_body(&path, &rel), "../pool/x");
assert_eq!(resolve(&repo, &alias).await, Some(checksum(1)));
});
}
#[test]
fn an_alias_can_name_a_ref_of_another_namespace() {
let tmp = TmpDir::new("ref-store-alias-across");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
put_local(&repo, "main", &checksum(1)).await;
put(&repo, OTHER, "x", &checksum(2)).await;
let cases = [
(
Ref::collection(OTHER, "al"),
Ref::local("main"),
format!("refs/mirrors/{OTHER}/al"),
"../../heads/main".to_owned(),
checksum(1),
),
(
Ref::local("lx"),
Ref::collection(OTHER, "x"),
"refs/heads/lx".to_owned(),
format!("../mirrors/{OTHER}/x"),
checksum(2),
),
(
Ref::collection(FIRST, "y"),
Ref::collection(OTHER, "x"),
format!("refs/mirrors/{FIRST}/y"),
format!("../{OTHER}/x"),
checksum(2),
),
];
for (alias, target, rel, body, commit) in cases {
repo.set_ref_alias_immediate(&alias, &target).await.unwrap();
assert_eq!(link_body(&path, &rel), body, "{alias}");
assert_eq!(resolve(&repo, &alias).await, Some(commit), "{alias}");
}
});
}
#[test]
fn an_alias_of_the_own_collection_is_a_local_alias() {
let tmp = TmpDir::new("ref-store-own-alias");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
put_local(&repo, "main", &checksum(1)).await;
let alias = Ref::collection(OWN, "al");
repo.set_ref_alias_immediate(&alias, &Ref::collection(OWN, "main"))
.await
.unwrap();
assert_eq!(link_body(&path, "refs/heads/al"), "main");
assert!(!path.join("refs/mirrors").join(OWN).exists());
assert_eq!(resolve(&repo, &Ref::local("al")).await, Some(checksum(1)));
assert_not_writable(
repo.set_ref_alias_immediate(&alias, &Ref::local("al"))
.await,
"al",
"targets an alias",
);
let guard = repo.begin_update().await.unwrap();
assert_not_writable(
guard.set_ref_alias(&alias, &alias).await,
"al",
"targets an alias",
);
guard.finish().await.unwrap();
assert_eq!(link_body(&path, "refs/heads/al"), "main");
});
}
#[test]
fn a_mirror_alias_of_an_alias_is_refused() {
let tmp = TmpDir::new("ref-store-mirror-alias-of-alias");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
let mirror = path.join("refs/mirrors").join(OTHER);
block_on(async {
put(&repo, OTHER, "pool/x", &checksum(1)).await;
repo.set_ref_alias_immediate(
&Ref::collection(OTHER, "a"),
&Ref::collection(OTHER, "pool/x"),
)
.await
.unwrap();
let alias = Ref::collection(OTHER, "c");
let target = Ref::collection(OTHER, "a");
let name = format!("@{OTHER}:c");
assert_not_writable(
repo.set_ref_alias_immediate(&alias, &target).await,
&name,
"targets an alias",
);
let guard = repo.begin_update().await.unwrap();
assert_not_writable(
guard.set_ref_alias(&alias, &target).await,
&name,
"targets an alias",
);
guard.finish().await.unwrap();
assert!(std::fs::symlink_metadata(mirror.join("c")).is_err());
});
}
#[test]
fn the_alias_listing_gives_each_alias_with_its_target() {
use std::os::unix::fs::symlink;
let tmp = TmpDir::new("ref-store-alias-listing");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
let mirrors = path.join("refs/mirrors");
block_on(async {
put_local(&repo, "main", &checksum(1)).await;
repo.set_ref_immediate(&Ref::remote("origin", "main"), Some(&checksum(2)))
.await
.unwrap();
put(&repo, OTHER, "pool/x", &checksum(3)).await;
repo.set_ref_alias_immediate(&Ref::local("al"), &Ref::local("main"))
.await
.unwrap();
repo.set_ref_alias_immediate(&Ref::remote("origin", "ra"), &Ref::remote("origin", "main"))
.await
.unwrap();
repo.set_ref_alias_immediate(
&Ref::collection(OTHER, "defpool/x"),
&Ref::collection(OTHER, "pool/x"),
)
.await
.unwrap();
repo.set_ref_alias_immediate(
&Ref::collection(OTHER, "broken"),
&Ref::collection(OTHER, "gone"),
)
.await
.unwrap();
let other = mirrors.join(OTHER);
symlink("../../../config", other.join("out")).unwrap();
symlink(format!("../{OWN}/x"), other.join("ownt")).unwrap();
symlink("../../heads/-a", other.join("bad")).unwrap();
symlink(format!("../mirrors/{OWN}/x"), path.join("refs/heads/ownl")).unwrap();
std::fs::create_dir_all(mirrors.join("nodots")).unwrap();
symlink(format!("../{OTHER}/pool/x"), mirrors.join("nodots/al")).unwrap();
put_own_mirror(&path, "x", &checksum(4));
symlink("x", mirrors.join(OWN).join("own")).unwrap();
let aliases = repo.list_ref_aliases().await.unwrap();
let got: Vec<(Ref, Option<Ref>, String)> = aliases
.into_iter()
.map(|a| (a.name, a.target, a.body))
.collect();
let mirror = |name: &str| Ref::collection(OTHER, name);
let want = [
(
Ref::collection("nodots", "al"),
Some(mirror("pool/x")),
format!("../{OTHER}/pool/x"),
),
(mirror("bad"), None, "../../heads/-a".to_owned()),
(mirror("broken"), Some(mirror("gone")), "gone".to_owned()),
(
mirror("defpool/x"),
Some(mirror("pool/x")),
"../pool/x".to_owned(),
),
(mirror("out"), None, "../../../config".to_owned()),
(mirror("ownt"), None, format!("../{OWN}/x")),
(
Ref::local("al"),
Some(Ref::local("main")),
"main".to_owned(),
),
(
Ref::remote("origin", "ra"),
Some(Ref::remote("origin", "main")),
"main".to_owned(),
),
(Ref::local("ownl"), None, format!("../mirrors/{OWN}/x")),
];
assert_eq!(got, want);
assert_eq!(resolve(&repo, &mirror("gone")).await, None);
assert_eq!(resolve(&repo, &mirror("broken")).await, None);
});
}
#[test]
fn a_symlink_directly_under_refs_mirrors_fails_the_alias_listing() {
let tmp = TmpDir::new("ref-store-alias-listing-top-link");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
block_on(async {
put(&repo, OTHER, "x", &checksum(1)).await;
std::os::unix::fs::symlink(format!("{OTHER}/x"), path.join("refs/mirrors/al")).unwrap();
match repo.list_ref_aliases().await {
Err(Error::InvalidFormat(m)) => {
assert!(m.contains("passes through a symlink"), "{m}");
}
other => panic!("{other:?}"),
}
});
}
#[test]
fn the_remove_calls_remove_a_mirror_alias() {
let tmp = TmpDir::new("ref-store-remove-mirror-alias");
let path = create(&tmp, Some(OWN));
let repo = open(&path);
let mirror = path.join("refs/mirrors").join(OTHER);
let target = Ref::collection(OTHER, "pool/x");
let rel = format!("refs/mirrors/{OTHER}/pool/x");
block_on(async {
put(&repo, OTHER, "pool/x", &checksum(1)).await;
for by_guard in [false, true] {
let alias = Ref::collection(OTHER, "defpool/x");
repo.set_ref_alias_immediate(&alias, &target).await.unwrap();
if by_guard {
let guard = repo.begin_update().await.unwrap();
guard.remove_ref_alias(&alias).await.unwrap();
guard.finish().await.unwrap();
} else {
repo.remove_ref_alias_immediate(&alias).await.unwrap();
}
assert!(std::fs::symlink_metadata(mirror.join("defpool/x")).is_err());
assert_eq!(ref_file(&path, &rel), Some(checksum(1)));
}
let name = format!("@{OTHER}:pool/x");
assert_not_writable(
repo.remove_ref_alias_immediate(&target).await,
&name,
"names a ref file",
);
let guard = repo.begin_update().await.unwrap();
assert_not_writable(
guard.remove_ref_alias(&target).await,
&name,
"names a ref file",
);
guard.finish().await.unwrap();
assert_eq!(ref_file(&path, &rel), Some(checksum(1)));
});
}