use std::fs;
use std::os::unix::fs::symlink;
use std::path::{Path, PathBuf};
use indoc::indoc;
use tokio::task;
use verctl_core::repo::Repo;
use wiremock::matchers;
use super::{
AddedBy, VersionCommit, added_by, added_by_from, candidate_deletions, file_at, origin_url,
tagged_commit,
};
use crate::test_git::{commit_all, git, init};
fn version_file(raw: &str) -> Option<String> {
let version = raw.trim();
(!version.is_empty()).then(|| version.to_owned())
}
fn fragment(bump: &str, summary: &str) -> String {
indoc::formatdoc! {"
---
demo: {bump}
---
{summary}
"}
}
fn two_candidates(dir: &Path) -> (String, String) {
init(dir);
fs::create_dir(dir.join(".changeset")).unwrap();
fs::write(dir.join("VERSION"), "0.8.0\n").unwrap();
fs::write(dir.join(".changeset/old.md"), fragment("patch", "Old.")).unwrap();
commit_all(dir, "chore: start");
fs::write(dir.join("VERSION"), "0.8.1\n").unwrap();
fs::remove_file(dir.join(".changeset/old.md")).unwrap();
commit_all(dir, "chore: release demo 0.8.1 (#1)");
fs::write(dir.join(".changeset/one.md"), fragment("patch", "One.")).unwrap();
let added = commit_all(dir, "fix: one (#2)");
fs::write(dir.join("VERSION"), "0.8.2-rc.1\n").unwrap();
fs::remove_file(dir.join(".changeset/one.md")).unwrap();
commit_all(dir, "chore: release demo 0.8.2-rc.1 (#3)");
let first_parent = git(dir, &["rev-parse", "HEAD~1"]);
fs::write(dir.join(".changeset/stray.md"), fragment("patch", "Stray.")).unwrap();
commit_all(dir, "docs: stray (#4)");
fs::remove_file(dir.join(".changeset/stray.md")).unwrap();
commit_all(dir, "docs: drop stray (#5)");
fs::write(dir.join(".changeset/two.md"), fragment("minor", "Two.")).unwrap();
commit_all(dir, "feat: two (#6)");
fs::write(dir.join("VERSION"), "0.8.2-rc.2\n").unwrap();
fs::remove_file(dir.join(".changeset/two.md")).unwrap();
commit_all(dir, "chore: release demo 0.8.2-rc.2 (#7)");
(added, first_parent)
}
#[test]
fn candidate_deletions_lists_what_each_candidate_consumed_since_stable() {
let dir = tempfile::TempDir::new().unwrap();
let (_, first_parent) = two_candidates(dir.path());
let deleted = candidate_deletions(
dir.path(),
&dir.path().join("VERSION"),
&version_file,
&dir.path().join(".changeset"),
)
.unwrap();
let names: Vec<(String, String)> = deleted
.iter()
.map(|file| {
(
file.path
.file_name()
.unwrap()
.to_string_lossy()
.into_owned(),
file.text.clone(),
)
})
.collect();
assert_eq!(
names,
[
("two.md".to_owned(), fragment("minor", "Two.")),
("one.md".to_owned(), fragment("patch", "One.")),
]
);
assert_eq!(deleted[1].parent, first_parent);
assert_eq!(deleted[1].path, dir.path().join(".changeset/one.md"));
}
#[test]
fn candidate_deletions_is_empty_on_a_stable_head() {
let dir = tempfile::TempDir::new().unwrap();
two_candidates(dir.path());
fs::write(dir.path().join("VERSION"), "0.8.2\n").unwrap();
commit_all(dir.path(), "chore: release demo 0.8.2 (#8)");
assert_eq!(
candidate_deletions(
dir.path(),
&dir.path().join("VERSION"),
&version_file,
&dir.path().join(".changeset"),
)
.unwrap(),
[]
);
}
#[test]
fn added_by_from_resolves_a_fragment_deleted_at_head() {
let dir = tempfile::TempDir::new().unwrap();
let (added, first_parent) = two_candidates(dir.path());
let path = dir.path().join(".changeset/one.md");
assert_eq!(added_by(dir.path(), &path).unwrap(), None);
assert_eq!(
added_by_from(dir.path(), &path, &first_parent).unwrap(),
Some(AddedBy {
sha: added,
subject: "fix: one (#2)".to_owned(),
})
);
}
#[test]
fn candidate_deletions_fails_at_a_shallow_boundary() {
let dir = tempfile::TempDir::new().unwrap();
two_candidates(dir.path());
let boundary = git(dir.path(), &["rev-parse", "HEAD~2"]);
fs::write(dir.path().join(".git/shallow"), format!("{boundary}\n")).unwrap();
let error = candidate_deletions(
dir.path(),
&dir.path().join("VERSION"),
&version_file,
&dir.path().join(".changeset"),
)
.unwrap_err()
.to_string();
assert!(error.contains(&boundary), "{error}");
assert!(error.contains("shallow"), "{error}");
}
fn tree_names(dir: &Path, sha: &str) -> Vec<String> {
git(dir, &["ls-tree", "-r", "--name-only", sha])
.lines()
.map(str::to_owned)
.collect()
}
fn version_line(raw: &str) -> Option<String> {
raw.lines()
.find_map(|line| line.strip_prefix("version = "))
.map(|version| version.trim_matches('"').to_owned())
}
fn write_version(dir: &Path, version: &str) {
fs::write(
dir.join("Cargo.toml"),
indoc::formatdoc! {r#"
[package]
name = "demo"
version = "{version}"
"#},
)
.unwrap();
}
#[test]
fn a_tag_on_a_later_commit_at_the_same_version_is_the_release() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
write_version(dir.path(), "0.4.6");
let set = commit_all(dir.path(), "chore(release): version packages (#9)");
fs::write(dir.path().join("README"), "later\n").unwrap();
let later = commit_all(dir.path(), "docs: later");
fs::write(dir.path().join("README"), "head\n").unwrap();
commit_all(dir.path(), "docs: head");
let manifest = dir.path().join("Cargo.toml");
let found = tagged_commit(dir.path(), &manifest, &later, &version_line).unwrap();
assert_eq!(
found,
Some(VersionCommit {
sha: later,
depth: 1,
})
);
assert!(
tagged_commit(dir.path(), &manifest, &set, &version_line)
.unwrap()
.is_some()
);
}
#[test]
fn a_tag_on_a_commit_at_another_version_is_not_the_release() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
write_version(dir.path(), "0.4.5");
let older = commit_all(dir.path(), "chore: start");
write_version(dir.path(), "0.4.6");
commit_all(dir.path(), "chore(release): version packages (#9)");
let manifest = dir.path().join("Cargo.toml");
assert_eq!(
tagged_commit(dir.path(), &manifest, &older, &version_line).unwrap(),
None
);
assert_eq!(
tagged_commit(dir.path(), &manifest, &"0".repeat(40), &version_line).unwrap(),
None
);
}
fn write_project(dir: &Path, version: &str) {
fs::write(
dir.join("project.yml"),
indoc::formatdoc! {r#"
name: Demo
targets:
Demo:
settings:
base:
MARKETING_VERSION: "{version}"
"#},
)
.unwrap();
}
fn released_before_the_version_file(dir: &Path) -> String {
init(dir);
write_project(dir, "0.0.16");
let released = commit_all(dir, "chore(release): version packages");
fs::write(dir.join("VERSION"), "0.0.16\n").unwrap();
commit_all(dir, "chore: version the app through a VERSION file");
released
}
fn released_beside_the_version_file(dir: &Path) -> String {
init(dir);
write_project(dir, "0.0.16");
let start = commit_all(dir, "chore: start");
git(dir, &["checkout", "-q", "--detach", &start]);
fs::write(dir.join("publish.yml"), "published\n").unwrap();
let released = commit_all(dir, "ci: publish");
git(dir, &["checkout", "-q", "main"]);
fs::write(dir.join("VERSION"), "0.0.16\n").unwrap();
commit_all(dir, "chore: version the app through a VERSION file");
released
}
fn keep_merged(dir: &Path, sha: &str) {
git(
dir,
&[
"merge",
"-q",
"-s",
"ours",
"-m",
"forkctl: keep published history",
sha,
],
);
}
#[test]
fn a_tag_on_a_commit_before_the_version_file_existed_is_the_release() {
let dir = tempfile::TempDir::new().unwrap();
let released = released_before_the_version_file(dir.path());
let found = tagged_commit(
dir.path(),
&dir.path().join("VERSION"),
&released,
&version_file,
)
.unwrap();
assert_eq!(
found,
Some(VersionCommit {
sha: released,
depth: 1,
})
);
}
#[test]
fn a_merged_tag_on_a_commit_before_the_version_file_existed_is_the_release() {
let dir = tempfile::TempDir::new().unwrap();
let released = released_beside_the_version_file(dir.path());
keep_merged(dir.path(), &released);
let found = tagged_commit(
dir.path(),
&dir.path().join("VERSION"),
&released,
&version_file,
)
.unwrap();
assert_eq!(
found,
Some(VersionCommit {
sha: released,
depth: 0,
})
);
}
#[test]
fn an_unreached_tag_before_the_version_file_existed_is_not_the_release() {
let dir = tempfile::TempDir::new().unwrap();
let released = released_beside_the_version_file(dir.path());
let found = tagged_commit(
dir.path(),
&dir.path().join("VERSION"),
&released,
&version_file,
)
.unwrap();
assert_eq!(found, None);
}
#[test]
fn a_tag_on_a_commit_with_a_directory_at_the_version_path_is_not_the_release() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::create_dir(dir.path().join("VERSION")).unwrap();
fs::write(dir.path().join("VERSION").join("README"), "a directory\n").unwrap();
let tagged = commit_all(dir.path(), "chore: a directory at the path");
fs::remove_dir_all(dir.path().join("VERSION")).unwrap();
fs::write(dir.path().join("VERSION"), "0.0.16\n").unwrap();
commit_all(dir.path(), "chore: version the app through a VERSION file");
let found = tagged_commit(
dir.path(),
&dir.path().join("VERSION"),
&tagged,
&version_file,
)
.unwrap();
assert_eq!(found, None);
}
fn rewritten_and_merged_back(dir: &Path, published_version: &str) -> String {
init(dir);
write_version(dir, "0.0.1");
fs::write(dir.join("publish.yml"), "one\n").unwrap();
let set = commit_all(dir, "chore: start");
write_version(dir, published_version);
fs::write(dir.join("publish.yml"), "two\n").unwrap();
let published = commit_all(dir, "ci: publish");
git(dir, &["checkout", "-q", "--detach", &set]);
fs::write(dir.join("publish.yml"), "three\n").unwrap();
commit_all(dir, "ci: rewritten publish");
keep_merged(dir, &published);
published
}
#[test]
fn a_tag_on_a_merged_commit_at_the_same_version_is_the_release() {
let dir = tempfile::TempDir::new().unwrap();
let published = rewritten_and_merged_back(dir.path(), "0.0.1");
assert_ne!(
git(dir.path(), &["rev-parse", "HEAD^1:publish.yml"]),
git(
dir.path(),
&["rev-parse", &format!("{published}:publish.yml")]
),
"the first-parent commit at the same version changes another file"
);
let found = tagged_commit(
dir.path(),
&dir.path().join("Cargo.toml"),
&published,
&version_line,
)
.unwrap();
assert_eq!(
found,
Some(VersionCommit {
sha: published,
depth: 0,
})
);
}
#[test]
fn a_tag_merged_several_commits_below_head_takes_the_depth_of_the_merge() {
let dir = tempfile::TempDir::new().unwrap();
let published = rewritten_and_merged_back(dir.path(), "0.0.1");
for later in 1..=5 {
fs::write(dir.path().join("README"), format!("later {later}\n")).unwrap();
commit_all(dir.path(), &format!("docs: later {later}"));
}
let found = tagged_commit(
dir.path(),
&dir.path().join("Cargo.toml"),
&published,
&version_line,
)
.unwrap();
assert_eq!(
found,
Some(VersionCommit {
sha: published,
depth: 5,
})
);
}
#[test]
fn a_tag_on_a_merged_commit_at_another_version_is_not_the_release() {
let dir = tempfile::TempDir::new().unwrap();
let published = rewritten_and_merged_back(dir.path(), "0.0.2");
assert_eq!(
tagged_commit(
dir.path(),
&dir.path().join("Cargo.toml"),
&published,
&version_line,
)
.unwrap(),
None
);
}
#[test]
fn a_tag_on_a_commit_head_does_not_contain_is_not_the_release() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
write_version(dir.path(), "0.0.1");
let set = commit_all(dir.path(), "chore: start");
fs::write(dir.path().join("publish.yml"), "two\n").unwrap();
let abandoned = commit_all(dir.path(), "ci: publish");
git(dir.path(), &["checkout", "-q", "--detach", &set]);
fs::write(dir.path().join("publish.yml"), "three\n").unwrap();
commit_all(dir.path(), "ci: rewritten publish");
assert_eq!(
tagged_commit(
dir.path(),
&dir.path().join("Cargo.toml"),
&abandoned,
&version_line,
)
.unwrap(),
None
);
}
#[test]
fn file_at_reads_a_file_at_a_commit_and_nothing_before_it() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join("README"), "start\n").unwrap();
let before = commit_all(dir.path(), "chore: start");
fs::create_dir_all(dir.path().join(".ctl")).unwrap();
fs::write(dir.path().join(".ctl/ver.yaml"), "packages: []\n").unwrap();
let added = commit_all(dir.path(), "chore: add the config");
fs::write(dir.path().join(".ctl/ver.yaml"), "packages: [later]\n").unwrap();
commit_all(dir.path(), "chore: change the config");
let path = Path::new(".ctl/ver.yaml");
assert_eq!(
file_at(dir.path(), &added, path).unwrap().as_deref(),
Some("packages: []\n")
);
assert_eq!(file_at(dir.path(), &before, path).unwrap(), None);
}
#[test]
fn added_by_names_the_commit_that_added_the_file() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join("README"), "start\n").unwrap();
commit_all(dir.path(), "chore: start");
fs::write(dir.path().join("fix.md"), "Fix.\n").unwrap();
let added = commit_all(dir.path(), "fix: the parser (#12)");
fs::write(dir.path().join("README"), "later\n").unwrap();
commit_all(dir.path(), "docs: later");
assert_eq!(
added_by(dir.path(), &dir.path().join("fix.md")).unwrap(),
Some(AddedBy {
sha: added,
subject: "fix: the parser (#12)".into(),
})
);
}
#[test]
fn added_by_follows_a_merge_to_the_commit_that_added_the_file() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join("README"), "start\n").unwrap();
let base = commit_all(dir.path(), "chore: start");
fs::write(dir.path().join("fix.md"), "Fix.\n").unwrap();
let side = commit_all(dir.path(), "fix: the parser (#12)");
git(dir.path(), &["reset", "-q", "--hard", &base]);
fs::write(dir.path().join("README"), "later\n").unwrap();
let main = commit_all(dir.path(), "docs: later (#13)");
fs::write(dir.path().join("fix.md"), "Fix.\n").unwrap();
git(dir.path(), &["add", "-A"]);
let tree = git(dir.path(), &["write-tree"]);
let merge = git(
dir.path(),
&[
"commit-tree",
&tree,
"-p",
&main,
"-p",
&side,
"-m",
"feat: release next (#14)",
],
);
git(dir.path(), &["reset", "-q", "--hard", &merge]);
assert_eq!(
added_by(dir.path(), &dir.path().join("fix.md")).unwrap(),
Some(AddedBy {
sha: side,
subject: "fix: the parser (#12)".into(),
})
);
}
#[test]
fn added_by_names_a_titled_merge_over_an_unnamed_branch_commit() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join("README"), "start\n").unwrap();
let base = commit_all(dir.path(), "chore: start");
fs::write(dir.path().join("fix.md"), "Fix.\n").unwrap();
let side = commit_all(dir.path(), "wip");
git(dir.path(), &["reset", "-q", "--hard", &base]);
fs::write(dir.path().join("fix.md"), "Fix.\n").unwrap();
git(dir.path(), &["add", "-A"]);
let tree = git(dir.path(), &["write-tree"]);
let merge = git(
dir.path(),
&[
"commit-tree",
&tree,
"-p",
&base,
"-p",
&side,
"-m",
"fix: the parser (#12)",
],
);
git(dir.path(), &["reset", "-q", "--hard", &merge]);
assert_eq!(
added_by(dir.path(), &dir.path().join("fix.md")).unwrap(),
Some(AddedBy {
sha: merge,
subject: "fix: the parser (#12)".into(),
})
);
}
#[test]
fn added_by_keeps_the_adding_pull_request_after_an_edit() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join("README"), "start\n").unwrap();
let base = commit_all(dir.path(), "chore: start");
fs::write(dir.path().join("fix.md"), "Fix.\n").unwrap();
let side = commit_all(dir.path(), "wip");
git(dir.path(), &["reset", "-q", "--hard", &base]);
fs::write(dir.path().join("fix.md"), "Fix.\n").unwrap();
git(dir.path(), &["add", "-A"]);
let tree = git(dir.path(), &["write-tree"]);
let added = git(
dir.path(),
&[
"commit-tree",
&tree,
"-p",
&base,
"-p",
&side,
"-m",
"fix: the parser (#12)",
],
);
git(dir.path(), &["reset", "-q", "--hard", &added]);
fs::write(dir.path().join("fix.md"), "Fix, reworded.\n").unwrap();
let edit = commit_all(dir.path(), "docs: reword the fix");
git(dir.path(), &["reset", "-q", "--hard", &added]);
fs::write(dir.path().join("fix.md"), "Fix, reworded.\n").unwrap();
git(dir.path(), &["add", "-A"]);
let tree = git(dir.path(), &["write-tree"]);
let edited = git(
dir.path(),
&[
"commit-tree",
&tree,
"-p",
&added,
"-p",
&edit,
"-m",
"docs: reword the fix (#13)",
],
);
git(dir.path(), &["reset", "-q", "--hard", &edited]);
fs::write(dir.path().join("fix.md"), "Fix, reworded again.\n").unwrap();
commit_all(dir.path(), "docs: reword the fix again (#14)");
assert_eq!(
added_by(dir.path(), &dir.path().join("fix.md")).unwrap(),
Some(AddedBy {
sha: added,
subject: "fix: the parser (#12)".into(),
})
);
}
#[test]
fn added_by_names_nothing_past_a_shallow_boundary() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join("fix.md"), "Fix.\n").unwrap();
commit_all(dir.path(), "fix: the parser (#12)");
fs::write(dir.path().join("README"), "later\n").unwrap();
let boundary = commit_all(dir.path(), "docs: later (#13)");
fs::write(dir.path().join(".git/shallow"), format!("{boundary}\n")).unwrap();
assert_eq!(
git(dir.path(), &["rev-parse", "--is-shallow-repository"]),
"true"
);
assert_eq!(
added_by(dir.path(), &dir.path().join("fix.md")).unwrap(),
None
);
}
#[test]
fn origin_url_from_discovered_repo() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
git(
dir.path(),
&[
"remote",
"add",
"origin",
"https://github.com/victor-software-house/verctl.git",
],
);
commit_all(dir.path(), "init");
assert_eq!(
origin_url(dir.path()).unwrap(),
"https://github.com/victor-software-house/verctl.git"
);
}
#[test]
fn empty_paths_do_not_switch_head() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
commit_all(dir.path(), "init");
let before = git(dir.path(), &["symbolic-ref", "HEAD"]);
let repo_id = Repo {
owner: "victor-software-house".into(),
name: "verctl".into(),
};
let out = super::commit_branch_and_push(
dir.path(),
"verctl/release/main",
"chore",
"unused",
&repo_id,
&[],
)
.unwrap();
assert_eq!(out, super::PushOutcome::Empty);
assert_eq!(git(dir.path(), &["symbolic-ref", "HEAD"]), before);
}
#[test]
fn unexpected_dirty_fails_unless_globbed() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(
dir.path().join("Cargo.toml"),
indoc! {r#"
[package]
version = "1.0.0"
"#},
)
.unwrap();
commit_all(dir.path(), "init");
fs::write(dir.path().join("secret.env"), "TOKEN=leak\n").unwrap();
let allowed = [dir.path().join("Cargo.toml")];
let err = super::assert_only_allowed(dir.path(), &allowed, &[], super::IgnoredFiles::Skip)
.unwrap_err();
assert!(format!("{err:#}").contains("secret.env"), "{err:#}");
let staged = super::assert_only_allowed(
dir.path(),
&allowed,
&["*.env".into()],
super::IgnoredFiles::Skip,
)
.unwrap();
assert_eq!(staged.len(), 1);
assert!(staged[0].ends_with("secret.env"), "{staged:?}");
}
#[test]
fn stage_glob_collects_a_deleted_file() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join("Cargo.lock"), "old\n").unwrap();
fs::write(
dir.path().join("Cargo.toml"),
indoc! {r#"
[package]
version = "1.0.0"
"#},
)
.unwrap();
commit_all(dir.path(), "init");
fs::remove_file(dir.path().join("Cargo.lock")).unwrap();
let staged = super::assert_only_allowed(
dir.path(),
&[dir.path().join("Cargo.toml")],
&["Cargo.lock".into()],
super::IgnoredFiles::Skip,
)
.unwrap();
assert_eq!(staged.len(), 1);
assert!(staged[0].ends_with("Cargo.lock"), "{staged:?}");
assert!(!dir.path().join("Cargo.lock").exists());
}
#[test]
fn stage_glob_collects_files_under_a_tree_not_the_directory() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(
dir.path().join("Cargo.toml"),
indoc! {r#"
[package]
version = "1.0.0"
"#},
)
.unwrap();
commit_all(dir.path(), "init");
fs::create_dir_all(dir.path().join("src")).unwrap();
fs::write(dir.path().join("src/lib.rs"), "fn x() {}\n").unwrap();
let staged = super::assert_only_allowed(
dir.path(),
&[dir.path().join("Cargo.toml")],
&["src/**".into()],
super::IgnoredFiles::Skip,
)
.unwrap();
assert_eq!(staged.len(), 1);
assert!(staged[0].ends_with("src/lib.rs"), "{staged:?}");
assert!(!staged.iter().any(|path| path.ends_with("src")));
}
#[test]
fn ignored_workdir_paths_are_not_unexpected() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join(".gitignore"), "/target\n").unwrap();
fs::write(
dir.path().join("Cargo.toml"),
indoc! {r#"
[package]
version = "1.0.0"
"#},
)
.unwrap();
commit_all(dir.path(), "init");
fs::create_dir_all(dir.path().join("target/debug")).unwrap();
fs::write(dir.path().join("target/debug/verctl"), "bin\n").unwrap();
super::assert_only_allowed(
dir.path(),
&[dir.path().join("Cargo.toml")],
&[],
super::IgnoredFiles::Skip,
)
.unwrap();
}
#[test]
fn stage_ignored_opt_in_collects_gitignored_files() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join(".gitignore"), "/generated.rs\n").unwrap();
fs::write(
dir.path().join("Cargo.toml"),
indoc! {r#"
[package]
version = "1.0.0"
"#},
)
.unwrap();
commit_all(dir.path(), "init");
fs::write(dir.path().join("generated.rs"), "gen\n").unwrap();
let allowed = [dir.path().join("Cargo.toml")];
let off = super::assert_only_allowed(
dir.path(),
&allowed,
&["generated.rs".into()],
super::IgnoredFiles::Skip,
)
.unwrap();
assert!(off.is_empty(), "{off:?}");
let on = super::assert_only_allowed(
dir.path(),
&allowed,
&["generated.rs".into()],
super::IgnoredFiles::Include,
)
.unwrap();
assert_eq!(on.len(), 1);
assert!(on[0].ends_with("generated.rs"), "{on:?}");
}
#[test]
fn stage_ignored_recurses_a_gitignored_directory() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join(".gitignore"), "/dist\n").unwrap();
fs::write(
dir.path().join("Cargo.toml"),
indoc! {r#"
[package]
version = "1.0.0"
"#},
)
.unwrap();
commit_all(dir.path(), "init");
fs::create_dir_all(dir.path().join("dist")).unwrap();
fs::write(dir.path().join("dist/out.js"), "ok\n").unwrap();
let allowed = [dir.path().join("Cargo.toml")];
let staged = super::assert_only_allowed(
dir.path(),
&allowed,
&["dist/**".into()],
super::IgnoredFiles::Include,
)
.unwrap();
assert_eq!(staged.len(), 1);
assert!(staged[0].ends_with("dist/out.js"), "{staged:?}");
}
#[test]
fn stage_ignored_pathspec_does_not_collect_other_ignored_trees() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(
dir.path().join(".gitignore"),
indoc! {"
/dist
/target
"},
)
.unwrap();
fs::write(
dir.path().join("Cargo.toml"),
indoc! {r#"
[package]
version = "1.0.0"
"#},
)
.unwrap();
commit_all(dir.path(), "init");
fs::create_dir_all(dir.path().join("dist")).unwrap();
fs::write(dir.path().join("dist/out.js"), "ok\n").unwrap();
fs::create_dir_all(dir.path().join("target/debug")).unwrap();
fs::write(dir.path().join("target/debug/verctl"), "bin\n").unwrap();
fs::write(dir.path().join("target/CACHEDIR.TAG"), "tag\n").unwrap();
let staged = super::assert_only_allowed(
dir.path(),
&[dir.path().join("Cargo.toml")],
&["dist/**".into()],
super::IgnoredFiles::Include,
)
.unwrap();
assert_eq!(staged.len(), 1, "{staged:?}");
assert!(staged[0].ends_with("dist/out.js"), "{staged:?}");
assert!(
!staged
.iter()
.any(|path| path.to_string_lossy().contains("target")),
"{staged:?}"
);
}
#[test]
fn write_commit_on_branch_removes_a_deleted_staged_path() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join("Cargo.lock"), "old\n").unwrap();
fs::write(
dir.path().join("Cargo.toml"),
indoc! {r#"
[package]
version = "1.0.0"
"#},
)
.unwrap();
commit_all(dir.path(), "init");
fs::remove_file(dir.path().join("Cargo.lock")).unwrap();
let staged = super::assert_only_allowed(
dir.path(),
&[dir.path().join("Cargo.toml")],
&["Cargo.lock".into()],
super::IgnoredFiles::Skip,
)
.unwrap();
let oid = write_commit(dir.path(), &staged).expect("deletion should commit");
let names = tree_names(dir.path(), &oid);
assert!(!names.iter().any(|name| name == "Cargo.lock"), "{names:?}");
assert!(names.iter().any(|name| name == "Cargo.toml"), "{names:?}");
}
#[test]
fn commit_only_lists_given_paths() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(
dir.path().join("Cargo.toml"),
indoc! {r#"
[package]
version = "1.0.0"
"#},
)
.unwrap();
let parent = commit_all(dir.path(), "init");
fs::write(
dir.path().join("Cargo.toml"),
indoc! {r#"
[package]
version = "1.0.1"
"#},
)
.unwrap();
fs::write(
dir.path().join("secret.env"),
indoc! {"
TOKEN=leak
"},
)
.unwrap();
let oid = write_commit(dir.path(), &[dir.path().join("Cargo.toml")]).expect("commit");
let names = tree_names(dir.path(), &oid);
assert!(names.iter().any(|name| name == "Cargo.toml"), "{names:?}");
assert!(!names.iter().any(|name| name == "secret.env"), "{names:?}");
assert_eq!(git(dir.path(), &["rev-parse", "HEAD"]), parent);
}
#[test]
fn dotted_relative_paths_stage() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(
dir.path().join("Cargo.toml"),
indoc! {r#"
[package]
version = "1.0.0"
"#},
)
.unwrap();
commit_all(dir.path(), "init");
fs::write(
dir.path().join("Cargo.toml"),
indoc! {r#"
[package]
version = "1.0.1"
"#},
)
.unwrap();
let oid = write_commit(dir.path(), &[Path::new("./Cargo.toml").to_path_buf()]).expect("commit");
let names = tree_names(dir.path(), &oid);
assert!(names.iter().any(|name| name == "Cargo.toml"), "{names:?}");
}
#[tokio::test]
async fn push_keeps_the_token_out_of_the_error() {
let server = wiremock::MockServer::start().await;
wiremock::Mock::given(matchers::any())
.respond_with(
wiremock::ResponseTemplate::new(401)
.insert_header("WWW-Authenticate", r#"Basic realm="git""#),
)
.mount(&server)
.await;
let (dir, _) = fixture();
git(dir.path(), &["branch", "verctl/release/main"]);
let url = format!("{}/owner/repo.git", server.uri());
let token = "verctl-push-token-5f3a9c";
let error = task::spawn_blocking(move || {
super::push(&dir.path().join(".git"), &url, "verctl/release/main", token)
})
.await
.unwrap()
.unwrap_err();
let message = format!("{error:#}");
assert!(!message.contains(token), "{message}");
let expected = format!("Basic {}", base64(&format!("x-access-token:{token}")));
let requests = server.received_requests().await.unwrap();
assert!(
requests.iter().any(|request| {
request
.headers
.get("authorization")
.is_some_and(|value| value.as_bytes() == expected.as_bytes())
}),
"no request carried the token as Basic credentials"
);
}
fn base64(text: &str) -> String {
const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::new();
for chunk in text.as_bytes().chunks(3) {
let bytes = [
chunk[0],
chunk.get(1).copied().unwrap_or(0),
chunk.get(2).copied().unwrap_or(0),
];
let joined = u32::from(bytes[0]) << 16 | u32::from(bytes[1]) << 8 | u32::from(bytes[2]);
for index in 0..4 {
if index <= chunk.len() {
let sextet = (joined >> (18 - 6 * index)) & 0x3f;
out.push(char::from(ALPHABET[usize::try_from(sextet).unwrap()]));
} else {
out.push('=');
}
}
}
out
}
#[test]
fn the_commit_applies_clean_filters() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join(".gitattributes"), "*.md text\n").unwrap();
fs::write(dir.path().join("CHANGELOG.md"), "old\n").unwrap();
commit_all(dir.path(), "init");
fs::write(dir.path().join("CHANGELOG.md"), "a\r\nb\r\n").unwrap();
let oid = write_commit(dir.path(), &[dir.path().join("CHANGELOG.md")]).expect("commit");
let blob = format!("{oid}:CHANGELOG.md");
assert_eq!(
git(dir.path(), &["cat-file", "-p", &blob]),
indoc! {"
a
b"}
);
}
#[test]
fn a_configured_identity_wins_over_the_fallback() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
git(dir.path(), &["config", "user.name", "Configured Name"]);
git(
dir.path(),
&["config", "user.email", "configured@example.com"],
);
fs::write(dir.path().join("Cargo.toml"), "version = 1\n").unwrap();
commit_all(dir.path(), "init");
fs::write(dir.path().join("Cargo.toml"), "version = 2\n").unwrap();
let oid = write_commit(dir.path(), &[dir.path().join("Cargo.toml")]).expect("commit");
let configured = "Configured Name <configured@example.com>";
assert_eq!(
git(
dir.path(),
&["log", "-1", "--format=%an <%ae>|%cn <%ce>", &oid]
),
format!("{configured}|{configured}")
);
assert_eq!(
git(
dir.path(),
&[
"log",
"-g",
"-1",
"--format=%gn <%ge>",
"refs/heads/verctl/release/main",
],
),
configured
);
}
#[test]
fn a_workdir_found_from_dot_resolves_paths() {
let repo = super::discover(Path::new(".")).unwrap();
let workdir = super::absolute_workdir(&repo).unwrap();
assert!(workdir.is_absolute(), "{}", workdir.display());
assert_eq!(
super::workdir_rel(&workdir, Path::new("./Cargo.toml")).unwrap(),
Path::new("Cargo.toml")
);
}
#[cfg(unix)]
#[test]
fn a_symlinked_workdir_takes_canonical_paths() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
fs::write(dir.path().join("Cargo.toml"), "version = 1\n").unwrap();
commit_all(dir.path(), "init");
fs::write(dir.path().join("Cargo.toml"), "version = 2\n").unwrap();
let links = tempfile::TempDir::new().unwrap();
let link = links.path().join("checkout");
symlink(dir.path(), &link).unwrap();
let canonical = dir.path().canonicalize().unwrap().join("Cargo.toml");
let workdir = super::absolute_workdir(&super::discover(&link).unwrap()).unwrap();
assert_eq!(
super::workdir_rel(&workdir, &canonical).unwrap(),
Path::new("Cargo.toml")
);
assert_eq!(
super::workdir_rel(&workdir, &link.join("Cargo.toml")).unwrap(),
Path::new("Cargo.toml")
);
}
fn write_commit(dir: &Path, paths: &[PathBuf]) -> Option<String> {
let repo = super::discover(dir).unwrap();
let workdir = super::absolute_workdir(&repo).unwrap();
let parent = repo.head_commit().unwrap();
super::write_commit_on_branch(
&repo,
&workdir,
&parent,
"verctl/release/main",
"chore",
paths,
)
.unwrap()
.map(|id| id.to_string())
}
fn stock() -> Vec<String> {
super::candidate_names(None)
}
fn fixture() -> (tempfile::TempDir, String) {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
let sha = commit_all(dir.path(), "init");
(dir, sha)
}
fn add_origin(dir: &Path) {
git(
dir,
&[
"remote",
"add",
"origin",
"https://github.com/victor-software-house/verctl.git",
],
);
}
fn track(dir: &Path, name: &str, sha: &str) {
git(
dir,
&["update-ref", &format!("refs/remotes/origin/{name}"), sha],
);
}
fn set_origin_head(dir: &Path, name: &str) {
git(
dir,
&[
"symbolic-ref",
"refs/remotes/origin/HEAD",
&format!("refs/remotes/origin/{name}"),
],
);
}
fn prove(root: &Path, candidates: &[String]) -> anyhow::Result<()> {
match super::prove_default_history(root, candidates)? {
None => Ok(()),
Some(off) => anyhow::bail!(off),
}
}
#[test]
fn candidate_names_are_base_ref_then_main_master() {
assert_eq!(super::candidate_names(None), ["main", "master"]);
assert_eq!(super::candidate_names(Some("")), ["main", "master"]);
assert_eq!(super::candidate_names(Some(" ")), ["main", "master"]);
assert_eq!(
super::candidate_names(Some("trunk")),
["trunk", "main", "master"]
);
assert_eq!(super::candidate_names(Some("main")), ["main", "master"]);
assert_eq!(
super::candidate_names(Some("release/1.0")),
["release/1.0", "main", "master"]
);
}
#[test]
fn no_repository_refuses() {
let dir = tempfile::TempDir::new().unwrap();
let err = prove(dir.path(), &stock()).unwrap_err();
assert!(
format!("{err:#}").contains("release cannot prove HEAD"),
"{err:#}"
);
assert!(format!("{err:#}").contains("no git repository"), "{err:#}");
}
#[test]
fn no_head_commit_refuses() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
let err = prove(dir.path(), &stock()).unwrap_err();
assert!(format!("{err:#}").contains("HEAD has no commit"), "{err:#}");
}
#[test]
fn a_missing_origin_refuses() {
let (dir, _) = fixture();
let err = prove(dir.path(), &stock()).unwrap_err();
assert!(format!("{err:#}").contains("no `origin` remote"), "{err:#}");
}
#[test]
fn head_on_origin_main_is_ok() {
let (dir, sha) = fixture();
add_origin(dir.path());
track(dir.path(), "main", &sha);
prove(dir.path(), &stock()).unwrap();
}
#[test]
fn head_sha_is_the_peeled_head_commit() {
let (dir, sha) = fixture();
assert_eq!(super::head_sha(dir.path()).unwrap(), sha);
}
#[test]
fn origin_master_without_head_is_ok() {
let (dir, sha) = fixture();
add_origin(dir.path());
track(dir.path(), "master", &sha);
prove(dir.path(), &stock()).unwrap();
}
#[test]
fn origin_head_to_non_main_is_ok() {
let (dir, sha) = fixture();
add_origin(dir.path());
track(dir.path(), "trunk", &sha);
set_origin_head(dir.path(), "trunk");
prove(dir.path(), &stock()).unwrap();
}
#[test]
fn base_ref_trunk_without_origin_head_is_ok() {
let (dir, sha) = fixture();
add_origin(dir.path());
track(dir.path(), "trunk", &sha);
prove(dir.path(), &super::candidate_names(Some("trunk"))).unwrap();
}
#[test]
fn origin_without_default_tracking_ref_fails() {
let (dir, _) = fixture();
add_origin(dir.path());
let err = prove(dir.path(), &stock()).unwrap_err();
assert!(format!("{err:#}").contains("origin exists"), "{err:#}");
}
#[test]
fn origin_trunk_alone_needs_head_or_base_ref() {
let (dir, sha) = fixture();
add_origin(dir.path());
track(dir.path(), "trunk", &sha);
let err = prove(dir.path(), &stock()).unwrap_err();
assert!(format!("{err:#}").contains("origin exists"), "{err:#}");
prove(dir.path(), &super::candidate_names(Some("trunk"))).unwrap();
set_origin_head(dir.path(), "trunk");
prove(dir.path(), &stock()).unwrap();
}
#[test]
fn origin_hotfix_alone_is_not_the_default() {
let (dir, sha) = fixture();
add_origin(dir.path());
track(dir.path(), "hotfix", &sha);
let err = prove(dir.path(), &stock()).unwrap_err();
assert!(format!("{err:#}").contains("origin exists"), "{err:#}");
}
#[test]
fn unpeelable_origin_head_does_not_fall_through_to_main() {
let (dir, sha) = fixture();
add_origin(dir.path());
track(dir.path(), "main", &sha);
set_origin_head(dir.path(), "trunk");
let err = prove(dir.path(), &stock()).unwrap_err();
assert!(format!("{err:#}").contains("origin/trunk"), "{err:#}");
}
#[test]
fn github_https_url_has_no_credentials() {
let url = super::github_https_url("victor-software-house/qctl");
assert_eq!(url, "https://github.com/victor-software-house/qctl.git");
let lower = url.to_ascii_lowercase();
assert!(!lower.contains("bearer"), "{url}");
assert!(!lower.contains("token"), "{url}");
assert!(!lower.contains("x-access-token"), "{url}");
}
#[test]
fn github_git_user_is_x_access_token() {
assert_eq!(super::github_git_user(), "x-access-token");
}
#[test]
fn ensure_origin_head_skips_fetch_when_the_ref_exists() {
let (dir, sha) = fixture();
git(
dir.path(),
&["remote", "add", "origin", "https://127.0.0.1:1/nope.git"],
);
track(dir.path(), "main", &sha);
super::ensure_origin_head(dir.path(), "main", &super::FetchDefault::Origin).unwrap();
assert_eq!(
git(dir.path(), &["symbolic-ref", "refs/remotes/origin/HEAD"]),
"refs/remotes/origin/main"
);
}
#[test]
fn ensure_origin_head_fetches_a_missing_ref_from_origin() {
let upstream_dir = tempfile::TempDir::new().unwrap();
init(upstream_dir.path());
commit_all(upstream_dir.path(), "init");
let work_dir = tempfile::TempDir::new().unwrap();
init(work_dir.path());
commit_all(work_dir.path(), "local");
let url = format!("file://{}", upstream_dir.path().display());
git(work_dir.path(), &["remote", "add", "origin", &url]);
super::ensure_origin_head(work_dir.path(), "main", &super::FetchDefault::Origin).unwrap();
git(
work_dir.path(),
&["rev-parse", "--verify", "refs/remotes/origin/main^{commit}"],
);
assert_eq!(
git(
work_dir.path(),
&["symbolic-ref", "refs/remotes/origin/HEAD"]
),
"refs/remotes/origin/main"
);
}
#[test]
fn a_shallow_clone_gets_the_history_that_holds_the_tagged_commit() {
let upstream = tempfile::TempDir::new().unwrap();
init(upstream.path());
write_version(upstream.path(), "0.0.1");
commit_all(upstream.path(), "chore: start");
write_version(upstream.path(), "0.0.2");
let set = commit_all(upstream.path(), "chore(release): version packages");
fs::write(upstream.path().join("README"), "later\n").unwrap();
commit_all(upstream.path(), "docs: later");
let parent = tempfile::TempDir::new().unwrap();
let url = format!("file://{}", upstream.path().display());
git(
parent.path(),
&["clone", "-q", "--depth", "1", &url, "work"],
);
let work = parent.path().join("work");
let manifest = work.join("Cargo.toml");
let error = tagged_commit(&work, &manifest, &set, &version_line).unwrap_err();
assert!(format!("{error:#}").contains("shallow"), "{error:#}");
super::ensure_full_history(&work, "main", &super::FetchDefault::Origin).unwrap();
let found = tagged_commit(&work, &manifest, &set, &version_line).unwrap();
assert_eq!(found, Some(VersionCommit { sha: set, depth: 1 }));
}
#[test]
fn a_full_clone_is_not_fetched_again() {
let dir = tempfile::TempDir::new().unwrap();
init(dir.path());
commit_all(dir.path(), "init");
git(
dir.path(),
&["remote", "add", "origin", "file:///nonexistent/verctl"],
);
super::ensure_full_history(dir.path(), "main", &super::FetchDefault::Origin).unwrap();
}
#[test]
fn a_pull_request_merge_checkout_compares_against_its_merge_base() {
let upstream = tempfile::TempDir::new().unwrap();
let up = upstream.path();
init(up);
write_version(up, "0.0.1");
commit_all(up, "chore: start");
write_version(up, "0.0.2");
commit_all(up, "chore(release): version packages");
git(up, &["checkout", "-q", "-b", "pr"]);
fs::write(up.join("README"), "change\n").unwrap();
commit_all(up, "docs: change");
git(up, &["checkout", "-q", "--detach", "main"]);
git(
up,
&["merge", "-q", "--no-ff", "pr", "-m", "Merge pr into main"],
);
git(up, &["update-ref", "refs/pull/1/merge", "HEAD"]);
git(up, &["checkout", "-q", "main"]);
write_version(up, "0.0.3");
commit_all(up, "chore(release): version packages");
let work = tempfile::TempDir::new().unwrap();
let dir = work.path();
init(dir);
let url = format!("file://{}", up.display());
git(dir, &["remote", "add", "origin", &url]);
git(
dir,
&[
"fetch",
"-q",
"--depth=1",
"origin",
"+refs/pull/1/merge:refs/remotes/pull/1/merge",
],
);
git(
dir,
&["checkout", "-q", "--detach", "refs/remotes/pull/1/merge"],
);
assert_eq!(git(dir, &["rev-parse", "--is-shallow-repository"]), "true");
let manifest = [Path::new("Cargo.toml")];
let candidates = ["main".to_owned()];
let error = super::files_on_merge_base(dir, &manifest, &candidates).unwrap_err();
assert!(
format!("{error:#}").contains("fetch the default branch"),
"{error:#}"
);
super::ensure_comparable(dir, "main", &super::FetchDefault::Origin).unwrap();
let files = super::files_on_merge_base(dir, &manifest, &candidates).unwrap();
let base = files[0].as_deref().expect("Cargo.toml at the merge-base");
assert_eq!(version_line(base).as_deref(), Some("0.0.2"));
}
#[test]
fn release_candidates_put_the_event_default_after_the_configured_branch() {
assert_eq!(
super::release_candidate_names(None, Some("trunk"), Some("develop")),
["trunk", "main", "master", "develop"]
);
assert_eq!(
super::release_candidate_names(Some("stable"), Some("trunk"), None),
["stable", "trunk", "main", "master"]
);
assert_eq!(
super::release_candidate_names(None, Some("main"), Some("main")),
["main", "master"]
);
assert_eq!(
super::release_candidate_names(None, None, Some("develop")),
["main", "master", "develop"]
);
}
#[test]
fn a_full_clone_with_the_branch_is_not_fetched_for_a_comparison() {
let (dir, sha) = fixture();
git(
dir.path(),
&["remote", "add", "origin", "file:///nonexistent/verctl"],
);
track(dir.path(), "main", &sha);
super::ensure_comparable(dir.path(), "main", &super::FetchDefault::Origin).unwrap();
}
#[test]
fn a_full_clone_without_the_branch_fetches_it_for_a_comparison() {
let upstream = tempfile::TempDir::new().unwrap();
init(upstream.path());
let tip = commit_all(upstream.path(), "init");
let work = tempfile::TempDir::new().unwrap();
init(work.path());
commit_all(work.path(), "local");
let url = format!("file://{}", upstream.path().display());
git(work.path(), &["remote", "add", "origin", &url]);
super::ensure_comparable(work.path(), "main", &super::FetchDefault::Origin).unwrap();
assert_eq!(
git(work.path(), &["rev-parse", "refs/remotes/origin/main"]),
tip
);
}
#[test]
fn fetch_one_branch_does_not_set_depth() {
let src = include_str!("remote.rs");
assert!(
!src.contains("opts.depth"),
"a depth-limited fetch leaves the tip parentless so graph_descendant_of cannot prove ancestry"
);
}
#[test]
fn configured_default_branch_is_only_verctl_default_branch() {
let src = include_str!("remote.rs");
let start = src
.find("pub fn configured_default_branch")
.expect("configured_default_branch");
let rest = &src[start..];
let end = rest
.find("pub fn ensure_origin_head")
.expect("ensure_origin_head");
let body = &rest[..end];
assert!(!body.contains("GITHUB_BASE_REF"), "{body}");
assert!(!body.contains("env_base_ref"), "{body}");
}
#[test]
fn fetched_default_branch_keeps_parents_for_ancestry() {
let upstream_dir = tempfile::TempDir::new().unwrap();
init(upstream_dir.path());
commit_all(upstream_dir.path(), "A");
let work_dir = tempfile::TempDir::new().unwrap();
init(work_dir.path());
let url = format!("file://{}", upstream_dir.path().display());
git(work_dir.path(), &["remote", "add", "origin", &url]);
super::ensure_origin_head(work_dir.path(), "main", &super::FetchDefault::Origin).unwrap();
let first = git(work_dir.path(), &["rev-parse", "refs/remotes/origin/main"]);
git(
work_dir.path(),
&["update-ref", "--no-deref", "HEAD", &first],
);
fs::write(upstream_dir.path().join("b.txt"), "b\n").unwrap();
commit_all(upstream_dir.path(), "B");
fs::write(upstream_dir.path().join("c.txt"), "c\n").unwrap();
commit_all(upstream_dir.path(), "C");
git(
work_dir.path(),
&["update-ref", "-d", "refs/remotes/origin/main"],
);
git(
work_dir.path(),
&["symbolic-ref", "--delete", "refs/remotes/origin/HEAD"],
);
super::ensure_origin_head(work_dir.path(), "main", &super::FetchDefault::Origin).unwrap();
prove(work_dir.path(), &stock()).unwrap();
}
#[test]
fn origin_head_wins_over_main_when_they_differ() {
let (dir, first) = fixture();
add_origin(dir.path());
fs::write(dir.path().join("extra.txt"), "x\n").unwrap();
let second = commit_all(dir.path(), "ahead");
track(dir.path(), "main", &first);
track(dir.path(), "trunk", &second);
set_origin_head(dir.path(), "trunk");
prove(dir.path(), &stock()).unwrap();
}
#[test]
fn head_off_default_history_fails() {
let (dir, first) = fixture();
add_origin(dir.path());
track(dir.path(), "main", &first);
git(dir.path(), &["update-ref", "refs/heads/other", &first]);
git(dir.path(), &["symbolic-ref", "HEAD", "refs/heads/other"]);
fs::write(dir.path().join("extra.txt"), "x\n").unwrap();
let head = commit_all(dir.path(), "ahead");
let off = super::prove_default_history(dir.path(), &stock())
.unwrap()
.unwrap();
assert!(off.contains("not an ancestor"), "{off}");
assert!(off.contains(&head), "{off}");
assert!(off.contains(&first), "{off}");
}
#[test]
fn workflows_at_the_default_tip_match() {
let (dir, _) = fixture();
fs::create_dir_all(dir.path().join(".github/workflows")).unwrap();
fs::write(dir.path().join(".github/workflows/ci.yml"), "on: push\n").unwrap();
let release = commit_all(dir.path(), "chore(release): version packages");
fs::write(dir.path().join("README"), "later\n").unwrap();
let tip = commit_all(dir.path(), "docs: later");
add_origin(dir.path());
track(dir.path(), "main", &tip);
git(dir.path(), &["update-ref", "--no-deref", "HEAD", &release]);
assert_eq!(super::workflows_differ(dir.path()).unwrap(), None);
}
#[test]
fn a_workflow_changed_since_the_release_commit_differs() {
let (dir, _) = fixture();
fs::create_dir_all(dir.path().join(".github/workflows")).unwrap();
fs::write(dir.path().join(".github/workflows/ci.yml"), "on: push\n").unwrap();
let release = commit_all(dir.path(), "chore(release): version packages");
fs::write(
dir.path().join(".github/workflows/ci.yml"),
"on: [push, pull_request]\n",
)
.unwrap();
let tip = commit_all(dir.path(), "ci: run on pull requests");
add_origin(dir.path());
track(dir.path(), "main", &tip);
git(dir.path(), &["update-ref", "--no-deref", "HEAD", &release]);
let differs = super::workflows_differ(dir.path()).unwrap().unwrap();
assert!(differs.contains(".github/workflows/"), "{differs}");
assert!(differs.contains(&release), "{differs}");
assert!(differs.contains(&tip), "{differs}");
let (bare, first) = fixture();
add_origin(bare.path());
fs::create_dir_all(bare.path().join(".github/workflows")).unwrap();
fs::write(bare.path().join(".github/workflows/ci.yml"), "on: push\n").unwrap();
let added = commit_all(bare.path(), "ci: add");
track(bare.path(), "main", &added);
git(bare.path(), &["update-ref", "--no-deref", "HEAD", &first]);
assert!(super::workflows_differ(bare.path()).unwrap().is_some());
}
#[test]
fn the_workflows_compared_are_those_of_the_run_commit() {
let (dir, _) = fixture();
fs::create_dir_all(dir.path().join(".github/workflows")).unwrap();
fs::write(dir.path().join(".github/workflows/ci.yml"), "on: push\n").unwrap();
let release = commit_all(dir.path(), "chore(release): version packages");
fs::write(
dir.path().join(".github/workflows/ci.yml"),
"on: [push, pull_request]\n",
)
.unwrap();
let tip = commit_all(dir.path(), "ci: run on pull requests");
add_origin(dir.path());
track(dir.path(), "main", &tip);
git(dir.path(), &["update-ref", "--no-deref", "HEAD", &release]);
assert_eq!(
super::workflows_differ_at(dir.path(), Some(&tip)).unwrap(),
None
);
let at_head = super::workflows_differ_at(dir.path(), Some(&"0".repeat(40)))
.unwrap()
.unwrap();
assert!(at_head.contains(&format!("at HEAD {release}")), "{at_head}");
let unset = super::workflows_differ_at(dir.path(), None)
.unwrap()
.unwrap();
assert!(unset.contains(&format!("at HEAD {release}")), "{unset}");
let at_run = super::workflows_differ_at(dir.path(), Some(&release))
.unwrap()
.unwrap();
assert!(
at_run.contains(&format!("at GITHUB_SHA {release}")),
"{at_run}"
);
}
#[test]
fn head_behind_origin_main_is_ok() {
let (dir, first) = fixture();
add_origin(dir.path());
fs::write(dir.path().join("extra.txt"), "x\n").unwrap();
let second = commit_all(dir.path(), "ahead");
track(dir.path(), "main", &second);
git(dir.path(), &["update-ref", "--no-deref", "HEAD", &first]);
prove(dir.path(), &stock()).unwrap();
}
#[test]
fn extract_tag_tree_reads_local_annotated_tag_and_preserves_files() {
let (dir, _) = fixture();
fs::write(dir.path().join("Cargo.toml"), "version = \"0.1.0\"\n").unwrap();
commit_all(dir.path(), "release 0.1.0");
git(dir.path(), &["tag", "-a", "v0.1.0", "-m", "v0.1.0"]);
fs::write(dir.path().join("Cargo.toml"), "version = \"0.1.1\"\n").unwrap();
commit_all(dir.path(), "next");
let dest = tempfile::TempDir::new().unwrap();
let extracted = super::extract_tag_tree(
dir.path(),
"v0.1.0",
dest.path(),
&super::FetchDefault::Origin,
)
.unwrap();
assert_eq!(extracted, dest.path());
assert_eq!(
fs::read_to_string(extracted.join("Cargo.toml")).unwrap(),
"version = \"0.1.0\"\n"
);
}
#[test]
fn extract_tag_tree_fetches_missing_tag_in_shallow_clone() {
let upstream = tempfile::TempDir::new().unwrap();
init(upstream.path());
fs::write(upstream.path().join("Cargo.toml"), "version = \"0.1.0\"\n").unwrap();
commit_all(upstream.path(), "v0.1.0");
git(upstream.path(), &["tag", "v0.1.0"]);
fs::write(upstream.path().join("Cargo.toml"), "version = \"0.1.1\"\n").unwrap();
commit_all(upstream.path(), "ahead");
let parent = tempfile::TempDir::new().unwrap();
let url = format!("file://{}", upstream.path().display());
git(
parent.path(),
&["clone", "-q", "--depth", "1", "--no-tags", &url, "work"],
);
let work = parent.path().join("work");
let dest = tempfile::TempDir::new().unwrap();
let extracted =
super::extract_tag_tree(&work, "v0.1.0", dest.path(), &super::FetchDefault::Origin)
.unwrap();
assert_eq!(
fs::read_to_string(extracted.join("Cargo.toml")).unwrap(),
"version = \"0.1.0\"\n"
);
}
#[test]
fn extract_tag_tree_fails_naming_missing_tag() {
let (dir, _) = fixture();
let dest = tempfile::TempDir::new().unwrap();
let error = super::extract_tag_tree(
dir.path(),
"v9.9.9",
dest.path(),
&super::FetchDefault::Origin,
)
.unwrap_err();
let message = format!("{error:#}");
assert!(message.contains("v9.9.9"), "{message}");
}