#![allow(clippy::disallowed_methods)]
mod support;
use std::fs;
use std::io::Write;
use std::process::Stdio;
use support::fixture::{cargo_package, oakum, path_prefixed_by, plain_repo, versioned, Fixture};
use httpmock::prelude::*;
fn temp_repo(label: &str) -> Fixture {
let root = plain_repo("version", label);
fs::create_dir(root.join(".git")).expect("fixture .git");
write_config(&root, "");
root
}
fn write_config(root: &std::path::Path, rest: &str) {
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(root.join(".changeset/_config.toml"), versioned(rest)).expect("config");
}
fn write_patch_changeset(root: &std::path::Path, name: &str) {
write_changeset(root, name, "patch");
}
fn write_changeset(root: &std::path::Path, name: &str, level: &str) {
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/one.md"),
format!("---\n{name}: {level}\n---\n\n{level} {name}\n"),
)
.expect("changeset");
}
fn cargo_core_app_exact_pin(root: &std::path::Path) {
fs::create_dir_all(root.join("crates/core/src")).unwrap();
fs::create_dir_all(root.join("crates/app/src")).unwrap();
fs::write(
root.join("Cargo.toml"),
"[workspace]\nresolver = \"2\"\nmembers = [\"crates/core\", \"crates/app\"]\n",
)
.unwrap();
fs::write(
root.join("crates/core/Cargo.toml"),
"[package]\nname = \"core\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/core/src/lib.rs"), "").unwrap();
fs::write(
root.join("crates/app/Cargo.toml"),
"[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2021\"\n[dependencies]\ncore = { path = \"../core\", version = \"=0.1.0\" }\n",
)
.unwrap();
fs::write(root.join("crates/app/src/lib.rs"), "").unwrap();
}
fn inheriting_cargo_workspace(root: &std::path::Path, with_pin: bool) {
fs::create_dir_all(root.join("crates/lib/src")).unwrap();
fs::create_dir_all(root.join("crates/app/src")).unwrap();
let pin = if with_pin {
"[workspace.dependencies]\nlib = { path = \"crates/lib\", version = \"^0.1.0\" }\n"
} else {
""
};
fs::write(
root.join("Cargo.toml"),
format!(
"[workspace]\nresolver = \"2\"\nmembers = [\"crates/lib\", \"crates/app\"]\n[workspace.package]\nversion = \"0.1.0\"\n{pin}"
),
)
.unwrap();
fs::write(
root.join("crates/lib/Cargo.toml"),
"[package]\nname = \"lib\"\nversion.workspace = true\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/lib/src/lib.rs"), "").unwrap();
fs::write(
root.join("crates/app/Cargo.toml"),
"[package]\nname = \"app\"\nversion.workspace = true\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/app/src/lib.rs"), "").unwrap();
}
fn assert_consumed(root: &std::path::Path) {
assert!(
!root.join(".changeset/one.md").exists(),
"consumed bump file should be deleted"
);
}
fn assert_no_changelog(root: &std::path::Path) {
assert!(
!root.join("CHANGELOG.md").exists(),
"empty or none plan should not write CHANGELOG.md"
);
}
fn assert_changelog(path: &std::path::Path, version: &str, section: &str, note: &str) {
let text = fs::read_to_string(path).unwrap_or_else(|err| panic!("{}: {err}", path.display()));
let heading = format!("## {version} (");
let Some(after_heading) = text.split_once(&heading).map(|(_, rest)| rest) else {
panic!("{} should contain `{heading}`: {text}", path.display());
};
assert!(
looks_like_ymd(&after_heading[..after_heading.len().min(10)]),
"{} heading date should be YYYY-MM-DD: {text}",
path.display()
);
let pinned = format!(
"## {version} ({})\n\n### {section}\n\n{note}\n",
&after_heading[..10]
);
assert!(
text.contains(&pinned),
"{} should contain `{pinned}`: {text}",
path.display()
);
assert!(
text.starts_with("# Changelog\n\n"),
"{} should start with `# Changelog`: {text}",
path.display()
);
assert!(
text.contains(&format!(
"Generated by oakum {}.\n",
env!("CARGO_PKG_VERSION")
)),
"{} should stamp the tool version: {text}",
path.display()
);
assert!(
!text.contains("<sub>"),
"{} should not use HTML dates: {text}",
path.display()
);
assert!(
!text
.lines()
.any(|line| line.starts_with('_') && line.ends_with('_') && line.len() > 2),
"{} should not use an italic-only date line: {text}",
path.display()
);
assert!(
!text.contains("\n\n\n"),
"{} should not have extra blanks: {text}",
path.display()
);
assert!(
text.ends_with('\n') && !text.ends_with("\n\n"),
"{} should end with one newline: {text:?}",
path.display()
);
}
fn looks_like_ymd(text: &str) -> bool {
let bytes = text.as_bytes();
bytes.len() == 10
&& bytes[4] == b'-'
&& bytes[7] == b'-'
&& bytes[0..4].iter().all(u8::is_ascii_digit)
&& bytes[5..7].iter().all(u8::is_ascii_digit)
&& bytes[8..10].iter().all(u8::is_ascii_digit)
}
#[test]
fn a_summary_nobody_can_receive_is_not_success() {
let root = temp_repo("dead-stdout");
cargo_package(&root, "demo", "0.1.0");
write_changeset(&root, "demo", "minor");
let writer = support::dead_stdout();
let output = oakum(&root)
.arg("version")
.stdout(writer)
.stderr(Stdio::piped())
.output()
.expect("run");
assert_eq!(output.status.code(), Some(2), "{output:?}");
let manifest = fs::read_to_string(root.join("Cargo.toml")).expect("manifest");
assert!(
manifest.contains("version = \"0.2.0\""),
"the write landed: {manifest}"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains(
"files written, but the summary of what changed could not be delivered to stdout"
),
"{stderr}"
);
}
#[test]
fn version_says_what_it_wrote() {
let root = temp_repo("says-what-it-wrote");
cargo_package(&root, "demo", "0.1.0");
write_changeset(&root, "demo", "minor");
let output = oakum(&root).arg("version").output().expect("run");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(output.status.success(), "stderr: {stderr}");
assert!(stdout.contains("versioned:"), "{stdout}");
assert!(
stdout.ends_with('\n') && !stdout.ends_with("\n\n"),
"written as-is, no newline added or lost: {stdout:?}"
);
assert!(
stdout.contains("demo (cargo) 0.1.0 -> 0.2.0"),
"the package, and both versions: {stdout}"
);
assert!(stdout.contains("wrote Cargo.toml"), "{stdout}");
assert!(stdout.contains("wrote CHANGELOG.md"), "{stdout}");
assert!(
stdout.contains("consumed .changeset/one.md"),
"the bump file it deleted is a file it touched: {stdout}"
);
}
#[test]
fn version_says_which_package_triggered_a_cascade() {
let root = temp_repo("cascade-named");
cargo_core_app_exact_pin(&root);
write_changeset(&root, "core", "minor");
let output = oakum(&root).arg("version").output().expect("run");
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(output.status.success(), "stderr: {stderr}");
assert!(
stdout.contains("(cascaded from core (cargo))"),
"the trigger is named: {stdout}"
);
assert!(
!stdout.contains("core (cargo) 0.1.0 -> 0.2.0 (cascaded"),
"the package named by intent is not itself a cascade: {stdout}"
);
}
#[test]
fn version_says_so_when_it_changed_nothing() {
let root = temp_repo("says-nothing-changed");
cargo_package(&root, "demo", "0.1.0");
let output = oakum(&root).arg("version").output().expect("run");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(output.status.success());
assert!(stdout.contains("versioned nothing"), "{stdout}");
assert!(stdout.contains("no file changed"), "{stdout}");
assert!(!stdout.contains("wrote "), "{stdout}");
}
#[test]
fn an_include_naming_no_package_refuses_before_planning() {
let root = temp_repo("unknown-include");
cargo_package(&root, "demo", "0.1.0");
write_config(&root, "include = [\"ghost\"]\n");
write_patch_changeset(&root, "demo");
let before = fs::read_to_string(root.join("Cargo.toml")).unwrap();
let output = oakum(&root).arg("version").output().expect("run");
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(output.status.code(), Some(1), "stderr: {stderr}");
assert!(
stderr.contains("unknown package name in include/exclude: ghost"),
"{stderr}"
);
assert_eq!(fs::read_to_string(root.join("Cargo.toml")).unwrap(), before);
assert!(root.join(".changeset/one.md").exists());
}
#[test]
fn empty_plan_writes_nothing() {
let root = temp_repo("empty");
cargo_package(&root, "demo", "0.1.0");
let before = fs::read_to_string(root.join("Cargo.toml")).unwrap();
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(fs::read_to_string(root.join("Cargo.toml")).unwrap(), before);
assert!(!root.join("Cargo.lock").exists());
assert_no_changelog(&root);
}
#[test]
fn version_bumps_the_package_and_the_lockfile_row() {
let root = temp_repo("bump-lock");
cargo_package(&root, "demo", "0.1.0");
fs::write(
root.join("Cargo.lock"),
"# This file is automatically @generated by Cargo.\nversion = 4\n\n[[package]]\nname = \"demo\"\nversion = \"0.1.0\"\n",
)
.expect("lock");
write_patch_changeset(&root, "demo");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let toml = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(toml.contains("version = \"0.1.1\""), "{toml}");
let lock = fs::read_to_string(root.join("Cargo.lock")).unwrap();
assert!(
lock.contains("name = \"demo\"\nversion = \"0.1.1\""),
"{lock}"
);
assert_consumed(&root);
assert_changelog(&root.join("CHANGELOG.md"), "0.1.1", "Fixed", "patch demo");
}
#[test]
fn version_rewrites_workspace_table_and_member_version() {
let root = temp_repo("inherit");
fs::create_dir_all(root.join("crates/core/src")).unwrap();
fs::create_dir_all(root.join("crates/app/src")).unwrap();
fs::write(
root.join("Cargo.toml"),
"[workspace]\nresolver = \"2\"\nmembers = [\"crates/core\", \"crates/app\"]\n[workspace.dependencies]\ncore = { path = \"crates/core\", version = \"^0.1.0\" }\n",
)
.unwrap();
fs::write(
root.join("crates/core/Cargo.toml"),
"[package]\nname = \"core\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/core/src/lib.rs"), "").unwrap();
fs::write(
root.join("crates/app/Cargo.toml"),
"[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2021\"\n[dependencies]\ncore = { workspace = true }\n",
)
.unwrap();
fs::write(root.join("crates/app/src/lib.rs"), "").unwrap();
fs::write(
root.join("Cargo.lock"),
"# This file is automatically @generated by Cargo.\nversion = 4\n\n[[package]]\nname = \"core\"\nversion = \"0.1.0\"\n\n[[package]]\nname = \"app\"\nversion = \"0.1.0\"\ndependencies = [\n \"core\",\n]\n",
)
.unwrap();
write_patch_changeset(&root, "core");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let workspace = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(
workspace.contains("core = { path = \"crates/core\", version = \"^0.1.1\" }"),
"{workspace}"
);
let core = fs::read_to_string(root.join("crates/core/Cargo.toml")).unwrap();
assert!(core.contains("version = \"0.1.1\""), "{core}");
let app = fs::read_to_string(root.join("crates/app/Cargo.toml")).unwrap();
assert!(app.contains("core = { workspace = true }"), "{app}");
assert!(app.contains("version = \"0.1.0\""), "{app}");
let lock = fs::read_to_string(root.join("Cargo.lock")).unwrap();
assert!(
lock.contains("name = \"core\"\nversion = \"0.1.1\""),
"{lock}"
);
assert_consumed(&root);
assert_changelog(
&root.join("crates/core/CHANGELOG.md"),
"0.1.1",
"Fixed",
"patch core",
);
assert!(!root.join("CHANGELOG.md").exists());
assert!(!root.join("crates/app/CHANGELOG.md").exists());
}
#[test]
fn version_rewrites_package_and_workspace_table_in_the_same_file() {
let root = temp_repo("pkg-ws-root");
fs::create_dir_all(root.join("src")).unwrap();
fs::create_dir_all(root.join("app/src")).unwrap();
fs::write(
root.join("Cargo.toml"),
"[package]\nname = \"core\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[workspace]\nresolver = \"2\"\nmembers = [\".\", \"app\"]\n[workspace.dependencies]\ncore = { path = \".\", version = \"^0.1.0\" }\n",
)
.unwrap();
fs::write(root.join("src/lib.rs"), "").unwrap();
fs::write(
root.join("app/Cargo.toml"),
"[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2021\"\n[dependencies]\ncore = { workspace = true }\n",
)
.unwrap();
fs::write(root.join("app/src/lib.rs"), "").unwrap();
write_patch_changeset(&root, "core");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let toml = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(toml.contains("version = \"0.1.1\""), "{toml}");
assert!(
toml.contains("core = { path = \".\", version = \"^0.1.1\" }"),
"{toml}"
);
assert_consumed(&root);
assert_changelog(&root.join("CHANGELOG.md"), "0.1.1", "Fixed", "patch core");
}
#[test]
fn version_bumps_workspace_inherited_package_version() {
let root = temp_repo("ws-pkg-ver");
fs::create_dir_all(root.join("crates/demo/src")).unwrap();
fs::write(
root.join("Cargo.toml"),
"[workspace]\nresolver = \"2\"\nmembers = [\"crates/demo\"]\n[workspace.package]\nversion = \"0.1.0\"\n",
)
.unwrap();
fs::write(
root.join("crates/demo/Cargo.toml"),
"[package]\nname = \"demo\"\nversion.workspace = true\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/demo/src/lib.rs"), "").unwrap();
write_patch_changeset(&root, "demo");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let workspace = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(
workspace.contains("[workspace.package]\nversion = \"0.1.1\""),
"{workspace}"
);
let member = fs::read_to_string(root.join("crates/demo/Cargo.toml")).unwrap();
assert!(member.contains("version.workspace = true"), "{member}");
assert!(!member.contains("version = \"0.1.1\""), "{member}");
assert_consumed(&root);
assert_changelog(
&root.join("crates/demo/CHANGELOG.md"),
"0.1.1",
"Fixed",
"patch demo",
);
}
#[test]
fn version_folds_workspace_package_version_with_a_pin_on_the_same_file() {
let root = temp_repo("ws-pkg-pin");
fs::create_dir_all(root.join("src")).unwrap();
fs::create_dir_all(root.join("app/src")).unwrap();
fs::write(
root.join("Cargo.toml"),
"[package]\nname = \"core\"\nversion.workspace = true\nedition = \"2021\"\n\n[workspace]\nresolver = \"2\"\nmembers = [\".\", \"app\"]\n[workspace.package]\nversion = \"0.1.0\"\n[workspace.dependencies]\ncore = { path = \".\", version = \"^0.1.0\" }\n",
)
.unwrap();
fs::write(root.join("src/lib.rs"), "").unwrap();
fs::write(
root.join("app/Cargo.toml"),
"[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2021\"\n[dependencies]\ncore = { workspace = true }\n",
)
.unwrap();
fs::write(root.join("app/src/lib.rs"), "").unwrap();
write_patch_changeset(&root, "core");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let toml = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(toml.contains("version.workspace = true"), "{toml}");
assert!(
toml.contains("[workspace.package]\nversion = \"0.1.1\""),
"{toml}"
);
assert!(
toml.contains("core = { path = \".\", version = \"^0.1.1\" }"),
"{toml}"
);
assert_consumed(&root);
assert_changelog(&root.join("CHANGELOG.md"), "0.1.1", "Fixed", "patch core");
}
#[test]
fn version_folds_workspace_package_version_with_a_pin_on_a_virtual_root() {
let root = temp_repo("ws-pkg-virtual-pin");
inheriting_cargo_workspace(&root, true);
fs::write(
root.join("crates/app/Cargo.toml"),
"[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2021\"\n[dependencies]\nlib = { workspace = true }\n",
)
.unwrap();
write_patch_changeset(&root, "lib");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let workspace = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(
workspace.contains("[workspace.package]\nversion = \"0.1.1\""),
"{workspace}"
);
assert!(
workspace.contains("lib = { path = \"crates/lib\", version = \"^0.1.1\" }"),
"{workspace}"
);
let lib = fs::read_to_string(root.join("crates/lib/Cargo.toml")).unwrap();
assert!(lib.contains("version.workspace = true"), "{lib}");
assert_consumed(&root);
assert_changelog(
&root.join("crates/lib/CHANGELOG.md"),
"0.1.1",
"Fixed",
"patch lib",
);
assert!(!root.join("CHANGELOG.md").exists());
}
#[test]
fn version_bumps_two_inheritors_that_share_the_workspace_version() {
let root = temp_repo("ws-pkg-two");
inheriting_cargo_workspace(&root, false);
fs::create_dir_all(root.join(".changeset")).unwrap();
fs::write(
root.join(".changeset/one.md"),
"---\nlib: patch\napp: patch\n---\n\npatch both\n",
)
.unwrap();
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let workspace = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(
workspace.contains("[workspace.package]\nversion = \"0.1.1\""),
"{workspace}"
);
assert_consumed(&root);
assert_changelog(
&root.join("crates/lib/CHANGELOG.md"),
"0.1.1",
"Fixed",
"patch both",
);
assert_changelog(
&root.join("crates/app/CHANGELOG.md"),
"0.1.1",
"Fixed",
"patch both",
);
}
#[test]
fn version_refuses_inheritors_that_need_different_workspace_versions() {
let root = temp_repo("ws-pkg-conflict");
inheriting_cargo_workspace(&root, false);
let before = fs::read_to_string(root.join("Cargo.toml")).unwrap();
fs::create_dir_all(root.join(".changeset")).unwrap();
fs::write(
root.join(".changeset/one.md"),
"---\nlib: patch\napp: minor\n---\n\nmixed\n",
)
.unwrap();
let output = oakum(&root).arg("version").output().expect("run");
assert!(
!output.status.success(),
"expected inheritor conflict: {}",
String::from_utf8_lossy(&output.stdout)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("inherits [workspace.package].version"),
"{stderr}"
);
assert_eq!(fs::read_to_string(root.join("Cargo.toml")).unwrap(), before);
assert!(root.join(".changeset/one.md").exists());
assert_no_changelog(&root);
}
#[test]
fn version_refuses_an_unplanned_inheritor_of_the_workspace_version() {
let root = temp_repo("ws-pkg-sibling");
inheriting_cargo_workspace(&root, false);
let before = fs::read_to_string(root.join("Cargo.toml")).unwrap();
fs::write(
root.join("Cargo.lock"),
"# This file is automatically @generated by Cargo.\nversion = 4\n\n[[package]]\nname = \"app\"\nversion = \"0.1.0\"\n\n[[package]]\nname = \"lib\"\nversion = \"0.1.0\"\n",
)
.unwrap();
write_patch_changeset(&root, "lib");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
!output.status.success(),
"expected unplanned inheritor: {}",
String::from_utf8_lossy(&output.stdout)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("app inherits [workspace.package].version and is not in the plan"),
"{stderr}"
);
assert_eq!(fs::read_to_string(root.join("Cargo.toml")).unwrap(), before);
let lock = fs::read_to_string(root.join("Cargo.lock")).unwrap();
assert!(
lock.contains("name = \"app\"\nversion = \"0.1.0\""),
"{lock}"
);
assert!(
lock.contains("name = \"lib\"\nversion = \"0.1.0\""),
"{lock}"
);
assert!(root.join(".changeset/one.md").exists());
assert!(!root.join("crates/lib/CHANGELOG.md").exists());
assert!(!root.join("crates/app/CHANGELOG.md").exists());
}
#[test]
fn version_retargets_a_published_range_on_the_dependent() {
let root = temp_repo("published-range");
fs::create_dir_all(root.join("crates/core/src")).unwrap();
fs::create_dir_all(root.join("crates/app/src")).unwrap();
fs::write(
root.join("Cargo.toml"),
"[workspace]\nresolver = \"2\"\nmembers = [\"crates/core\", \"crates/app\"]\n",
)
.unwrap();
fs::write(
root.join("crates/core/Cargo.toml"),
"[package]\nname = \"core\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/core/src/lib.rs"), "").unwrap();
fs::write(
root.join("crates/app/Cargo.toml"),
"[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2021\"\n[dependencies]\ncore = { path = \"../core\", version = \"^0.1.0\" }\n",
)
.unwrap();
fs::write(root.join("crates/app/src/lib.rs"), "").unwrap();
write_patch_changeset(&root, "core");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let app = fs::read_to_string(root.join("crates/app/Cargo.toml")).unwrap();
assert!(
app.contains("core = { path = \"../core\", version = \"^0.1.1\" }"),
"{app}"
);
assert!(app.contains("version = \"0.1.0\""), "{app}");
assert_consumed(&root);
assert_changelog(
&root.join("crates/core/CHANGELOG.md"),
"0.1.1",
"Fixed",
"patch core",
);
assert!(!root.join("crates/app/CHANGELOG.md").exists());
}
#[test]
fn version_writes_a_changed_line_for_a_cascaded_package() {
let root = temp_repo("cascade-changelog");
cargo_core_app_exact_pin(&root);
write_changeset(&root, "core", "minor");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_consumed(&root);
assert_changelog(
&root.join("crates/core/CHANGELOG.md"),
"0.2.0",
"Added",
"minor core",
);
let core = fs::read_to_string(root.join("crates/core/CHANGELOG.md")).unwrap();
assert!(!core.contains("Updated"), "{core}");
assert!(!core.contains("### Changed"), "{core}");
assert_changelog(
&root.join("crates/app/CHANGELOG.md"),
"0.1.1",
"Changed",
"Updated core to 0.2.0",
);
let app = fs::read_to_string(root.join("crates/app/CHANGELOG.md")).unwrap();
assert!(!app.contains("minor core"), "{app}");
assert!(!app.contains("### Fixed"), "{app}");
assert!(!app.contains("### Added"), "{app}");
assert!(!app.contains("### Dependencies"), "{app}");
}
#[test]
fn version_notes_file_replaces_a_cascaded_changed_line() {
let root = temp_repo("cascade-notes-file");
cargo_core_app_exact_pin(&root);
write_changeset(&root, "core", "minor");
fs::write(root.join("release-notes.md"), "workflow notes\n").unwrap();
let output = oakum(&root)
.args(["version", "--notes-file", "release-notes.md"])
.output()
.expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_consumed(&root);
let app = fs::read_to_string(root.join("crates/app/CHANGELOG.md")).unwrap();
assert!(app.contains(")\n\nworkflow notes\n"), "{app}");
assert!(!app.contains("Updated"), "{app}");
assert!(!app.contains("### Changed"), "{app}");
}
#[test]
fn version_template_skips_the_cascaded_changed_line() {
let root = temp_repo("cascade-template");
cargo_core_app_exact_pin(&root);
fs::create_dir_all(root.join(".changeset")).unwrap();
fs::write(
root.join(".changeset/_config.toml"),
versioned("template = \"## {{ version }} ({{ date }})\\n\\n{{ source }}\\n\"\n"),
)
.unwrap();
write_changeset(&root, "core", "minor");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_consumed(&root);
let app = fs::read_to_string(root.join("crates/app/CHANGELOG.md")).unwrap();
assert!(app.contains(")\n\ncascade\n"), "{app}");
assert!(!app.contains("Updated"), "{app}");
assert!(!app.contains("### Changed"), "{app}");
let core = fs::read_to_string(root.join("crates/core/CHANGELOG.md")).unwrap();
assert!(core.contains(")\n\nintent\n"), "{core}");
}
#[test]
fn unretargetable_lockfile_leaves_manifests_untouched() {
let root = temp_repo("bad-lock");
cargo_package(&root, "demo", "0.1.0");
let before = fs::read_to_string(root.join("Cargo.toml")).unwrap();
fs::write(
root.join("Cargo.lock"),
"# This file is automatically @generated by Cargo.\nversion = 4\npackage = 4\n",
)
.expect("lock");
write_patch_changeset(&root, "demo");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
!output.status.success(),
"expected lock retarget to fail: {}",
String::from_utf8_lossy(&output.stdout)
);
assert_eq!(fs::read_to_string(root.join("Cargo.toml")).unwrap(), before);
assert_eq!(
fs::read_to_string(root.join("Cargo.lock")).unwrap(),
"# This file is automatically @generated by Cargo.\nversion = 4\npackage = 4\n"
);
assert!(root.join(".changeset/one.md").exists());
assert_no_changelog(&root);
}
#[test]
fn version_bumps_an_npm_package_json() {
let root = temp_repo("npm");
fs::write(
root.join("package.json"),
"{\n \"name\": \"demo\",\n \"version\": \"0.1.0\"\n}\n",
)
.expect("package.json");
write_patch_changeset(&root, "demo");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let json = fs::read_to_string(root.join("package.json")).unwrap();
assert!(json.contains("\"version\": \"0.1.1\""), "{json}");
assert_consumed(&root);
assert_changelog(&root.join("CHANGELOG.md"), "0.1.1", "Fixed", "patch demo");
}
#[test]
fn version_retargets_the_cargo_lock_under_the_workspace_root() {
let root = temp_repo("nested-lock");
let rust = root.join("rust");
fs::create_dir_all(&rust).expect("rust");
cargo_package(&rust, "demo", "0.1.0");
fs::write(
rust.join("Cargo.lock"),
"# This file is automatically @generated by Cargo.\nversion = 4\n\n[[package]]\nname = \"demo\"\nversion = \"0.1.0\"\n",
)
.expect("lock");
fs::write(
root.join("Cargo.lock"),
"# decoy\nversion = 4\n\n[[package]]\nname = \"demo\"\nversion = \"9.9.9\"\n",
)
.expect("decoy");
write_patch_changeset(&root, "demo");
let output = oakum(&rust).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let lock = fs::read_to_string(rust.join("Cargo.lock")).unwrap();
assert!(
lock.contains("name = \"demo\"\nversion = \"0.1.1\""),
"{lock}"
);
let decoy = fs::read_to_string(root.join("Cargo.lock")).unwrap();
assert!(decoy.contains("version = \"9.9.9\""), "{decoy}");
assert_consumed(&root);
assert_changelog(&rust.join("CHANGELOG.md"), "0.1.1", "Fixed", "patch demo");
assert!(!root.join("CHANGELOG.md").exists());
}
#[test]
fn version_bumps_a_nested_npm_package_json() {
let root = temp_repo("nested-npm");
let pkg = root.join("packages/demo");
fs::create_dir_all(&pkg).expect("packages/demo");
fs::write(
pkg.join("package.json"),
"{\n \"name\": \"demo\",\n \"version\": \"0.1.0\"\n}\n",
)
.expect("package.json");
fs::write(
root.join("package.json"),
"{\n \"name\": \"decoy\",\n \"version\": \"9.9.9\"\n}\n",
)
.expect("decoy");
write_patch_changeset(&root, "demo");
let output = oakum(&pkg).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let json = fs::read_to_string(pkg.join("package.json")).unwrap();
assert!(json.contains("\"version\": \"0.1.1\""), "{json}");
let decoy = fs::read_to_string(root.join("package.json")).unwrap();
assert!(decoy.contains("\"version\": \"9.9.9\""), "{decoy}");
assert_consumed(&root);
assert_changelog(&pkg.join("CHANGELOG.md"), "0.1.1", "Fixed", "patch demo");
assert!(!root.join("CHANGELOG.md").exists());
}
#[test]
fn second_version_run_does_not_bump_again() {
let root = temp_repo("twice");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
let first = oakum(&root).arg("version").output().expect("run");
assert!(
first.status.success(),
"stderr: {}",
String::from_utf8_lossy(&first.stderr)
);
assert_consumed(&root);
let first_log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert_changelog(&root.join("CHANGELOG.md"), "0.1.1", "Fixed", "patch demo");
let second = oakum(&root).arg("version").output().expect("run");
assert!(
second.status.success(),
"stderr: {}",
String::from_utf8_lossy(&second.stderr)
);
let toml = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(toml.contains("version = \"0.1.1\""), "{toml}");
assert!(!toml.contains("version = \"0.1.2\""), "{toml}");
assert_eq!(
fs::read_to_string(root.join("CHANGELOG.md")).unwrap(),
first_log
);
}
#[test]
fn none_bump_file_is_consumed_and_writes_nothing() {
let root = temp_repo("none");
cargo_package(&root, "demo", "0.1.0");
let before = fs::read_to_string(root.join("Cargo.toml")).unwrap();
fs::create_dir_all(root.join(".changeset")).unwrap();
fs::write(
root.join(".changeset/one.md"),
"---\ndemo: none\n---\n\nno release\n",
)
.unwrap();
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(fs::read_to_string(root.join("Cargo.toml")).unwrap(), before);
assert_consumed(&root);
assert_no_changelog(&root);
}
#[test]
fn version_consumes_every_loaded_bump_file() {
let root = temp_repo("two-files");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
fs::write(
root.join(".changeset/two.md"),
"---\ndemo: minor\n---\n\nminor demo\n",
)
.unwrap();
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let toml = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(toml.contains("version = \"0.2.0\""), "{toml}");
assert_consumed(&root);
assert!(
!root.join(".changeset/two.md").exists(),
"second consumed bump file should be deleted"
);
assert_changelog(&root.join("CHANGELOG.md"), "0.2.0", "Added", "minor demo");
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.contains("### Fixed\n\npatch demo\n"), "{log}");
}
#[test]
fn version_leaves_instruction_files() {
let root = temp_repo("skip-files");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
fs::write(root.join(".changeset/README.md"), "keep\n").unwrap();
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_consumed(&root);
assert_eq!(
fs::read_to_string(root.join(".changeset/README.md")).unwrap(),
"keep\n"
);
assert_changelog(&root.join("CHANGELOG.md"), "0.1.1", "Fixed", "patch demo");
}
#[test]
fn version_refuses_a_malformed_bump_file_and_writes_nothing() {
let root = temp_repo("malformed-file");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
fs::write(root.join(".changeset/bad.md"), "not a changeset\n").unwrap();
let manifest_before = fs::read_to_string(root.join("Cargo.toml")).unwrap();
let output = oakum(&root).arg("version").output().expect("run");
assert!(
!output.status.success(),
"a malformed bump file must stop version; stdout: {}",
String::from_utf8_lossy(&output.stdout)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("`bad.md` is not a bump file"),
"the malformed file is named: {stderr}"
);
assert_eq!(
fs::read_to_string(root.join("Cargo.toml")).unwrap(),
manifest_before,
"no manifest write beside a refusal"
);
assert!(
root.join(".changeset/one.md").is_file(),
"the valid bump file is not consumed"
);
assert_no_changelog(&root);
}
#[test]
fn version_prepends_a_section_onto_an_existing_changelog() {
let root = temp_repo("changelog-prepend");
cargo_package(&root, "demo", "0.1.0");
fs::write(
root.join("CHANGELOG.md"),
"# Changelog\n\n## [Unreleased]\n\n## 0.1.0 (2026-01-01)\n\n### Added\n\nold\n",
)
.unwrap();
write_patch_changeset(&root, "demo");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_changelog(&root.join("CHANGELOG.md"), "0.1.1", "Fixed", "patch demo");
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.contains("## [Unreleased]\n"), "{log}");
assert!(
log.contains("## 0.1.0 (2026-01-01)\n\n### Added\n\nold\n"),
"{log}"
);
assert_consumed(&root);
}
#[test]
fn version_refuses_a_changelog_without_the_title() {
let root = temp_repo("changelog-refuse");
cargo_package(&root, "demo", "0.1.0");
let before = fs::read_to_string(root.join("Cargo.toml")).unwrap();
fs::write(root.join("CHANGELOG.md"), "notes\n").unwrap();
write_patch_changeset(&root, "demo");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
!output.status.success(),
"expected refuse: {}",
String::from_utf8_lossy(&output.stdout)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("CHANGELOG.md does not start with `# Changelog`"),
"{stderr}"
);
assert_eq!(fs::read_to_string(root.join("Cargo.toml")).unwrap(), before);
assert_eq!(
fs::read_to_string(root.join("CHANGELOG.md")).unwrap(),
"notes\n"
);
assert!(root.join(".changeset/one.md").exists());
}
#[test]
fn version_refuses_a_changelog_with_a_bom() {
let root = temp_repo("changelog-bom");
cargo_package(&root, "demo", "0.1.0");
let before = fs::read_to_string(root.join("Cargo.toml")).unwrap();
fs::write(root.join("CHANGELOG.md"), "\u{FEFF}# Changelog\n").unwrap();
write_patch_changeset(&root, "demo");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
!output.status.success(),
"expected refuse: {}",
String::from_utf8_lossy(&output.stdout)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("BOM"), "{stderr}");
assert_eq!(fs::read_to_string(root.join("Cargo.toml")).unwrap(), before);
assert_eq!(
fs::read_to_string(root.join("CHANGELOG.md")).unwrap(),
"\u{FEFF}# Changelog\n"
);
assert!(root.join(".changeset/one.md").exists());
}
#[test]
fn version_renders_a_configured_changelog_template() {
let root = temp_repo("changelog-template");
cargo_package(&root, "demo", "0.1.0");
fs::create_dir_all(root.join(".changeset")).unwrap();
fs::write(
root.join(".changeset/_config.toml"),
versioned("template = \"## {{ version }} ({{ date }})\\n\\n{{ notes[0] }}\\n\"\n"),
)
.unwrap();
write_patch_changeset(&root, "demo");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.starts_with("# Changelog\n\n## 0.1.1 ("), "{log}");
assert!(log.contains(")\n\npatch demo\n"), "{log}");
assert!(!log.contains("### Fixed"), "{log}");
assert!(
log.contains(&format!(
"Generated by oakum {}.\n",
env!("CARGO_PKG_VERSION")
)),
"{log}"
);
assert_consumed(&root);
}
#[test]
fn version_renders_a_changelog_template_file() {
let root = temp_repo("changelog-template-file");
cargo_package(&root, "demo", "0.1.0");
fs::create_dir_all(root.join(".changeset")).unwrap();
fs::write(
root.join("notes.md"),
"## {{ version }} ({{ date }})\n\n{{ notes[0] }}\n",
)
.unwrap();
fs::write(
root.join(".changeset/_config.toml"),
versioned("template = { file = \"notes.md\" }\n"),
)
.unwrap();
write_patch_changeset(&root, "demo");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.starts_with("# Changelog\n\n## 0.1.1 ("), "{log}");
assert!(log.contains(")\n\npatch demo\n"), "{log}");
assert!(!log.contains("### Fixed"), "{log}");
assert!(
log.contains(&format!(
"Generated by oakum {}.\n",
env!("CARGO_PKG_VERSION")
)),
"{log}"
);
assert_consumed(&root);
}
#[test]
fn version_uses_notes_file_instead_of_bump_notes() {
let root = temp_repo("notes-file");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
fs::write(root.join("release-notes.md"), "workflow notes\n").unwrap();
let output = oakum(&root)
.args(["version", "--notes-file", "release-notes.md"])
.output()
.expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.starts_with("# Changelog\n\n## 0.1.1 ("), "{log}");
assert!(log.contains(")\n\nworkflow notes\n"), "{log}");
assert!(!log.contains("patch demo"), "{log}");
assert!(!log.contains("### Fixed"), "{log}");
assert_consumed(&root);
}
#[test]
fn version_keeps_leading_blank_lines_from_notes_file() {
let root = temp_repo("notes-leading-blanks");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
fs::write(root.join("release-notes.md"), "\n\nworkflow notes\n").unwrap();
let output = oakum(&root)
.args(["version", "--notes-file", "release-notes.md"])
.output()
.expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.contains(")\n\n\n\nworkflow notes\n"), "{log}");
assert_consumed(&root);
}
#[test]
fn version_reads_notes_from_stdin() {
let root = temp_repo("notes-stdin");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
let mut child = oakum(&root)
.args(["version", "--notes-file", "-"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("spawn");
child
.stdin
.as_mut()
.expect("stdin")
.write_all(b"piped notes\n")
.expect("write");
let output = child.wait_with_output().expect("wait");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.contains(")\n\npiped notes\n"), "{log}");
assert!(!log.contains("patch demo"), "{log}");
assert_consumed(&root);
}
#[test]
fn version_strips_bom_from_stdin_notes() {
let root = temp_repo("notes-stdin-bom");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
let mut child = oakum(&root)
.args(["version", "--notes-file", "-"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("spawn");
child
.stdin
.as_mut()
.expect("stdin")
.write_all("\u{FEFF}piped notes\n".as_bytes())
.expect("write");
let output = child.wait_with_output().expect("wait");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.contains(")\n\npiped notes\n"), "{log}");
assert!(!log.contains('\u{FEFF}'), "{log}");
assert_consumed(&root);
}
#[test]
fn version_refuses_empty_stdin_notes() {
let root = temp_repo("notes-stdin-empty");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
let mut child = oakum(&root)
.args(["version", "--notes-file", "-"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("spawn");
drop(child.stdin.take());
let output = child.wait_with_output().expect("wait");
assert!(!output.status.success());
let err = String::from_utf8_lossy(&output.stderr);
assert!(err.contains("`--notes-file -` produced no notes"), "{err}");
assert!(!root.join("CHANGELOG.md").exists());
assert!(root.join(".changeset/one.md").exists());
}
#[test]
fn version_refuses_newline_only_stdin_notes() {
let root = temp_repo("notes-stdin-newline");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
let mut child = oakum(&root)
.args(["version", "--notes-file", "-"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("spawn");
child
.stdin
.as_mut()
.expect("stdin")
.write_all(b"\n")
.expect("write");
let output = child.wait_with_output().expect("wait");
assert!(!output.status.success());
let err = String::from_utf8_lossy(&output.stderr);
assert!(err.contains("`--notes-file -` produced no notes"), "{err}");
assert!(!root.join("CHANGELOG.md").exists());
assert!(root.join(".changeset/one.md").exists());
}
#[test]
fn version_does_not_read_stdin_without_notes_file() {
let root = temp_repo("notes-no-stdin");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
let mut child = oakum(&root)
.arg("version")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("spawn");
child
.stdin
.as_mut()
.expect("stdin")
.write_all(b"should ignore\n")
.expect("write");
let output = child.wait_with_output().expect("wait");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.contains("### Fixed\n\npatch demo\n"), "{log}");
assert!(!log.contains("should ignore"), "{log}");
assert_consumed(&root);
}
#[test]
fn version_empty_notes_file_writes_heading_only() {
let root = temp_repo("notes-empty");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
fs::write(root.join("release-notes.md"), "").unwrap();
let output = oakum(&root)
.args(["version", "--notes-file", "release-notes.md"])
.output()
.expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.contains("## 0.1.1 ("), "{log}");
assert!(!log.contains("patch demo"), "{log}");
assert!(!log.contains("### "), "{log}");
assert_consumed(&root);
}
#[test]
fn version_strips_bom_from_notes_file() {
let root = temp_repo("notes-bom");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
fs::write(root.join("release-notes.md"), "\u{FEFF}workflow notes\n").unwrap();
let output = oakum(&root)
.args(["version", "--notes-file", "release-notes.md"])
.output()
.expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.contains(")\n\nworkflow notes\n"), "{log}");
assert!(!log.contains('\u{FEFF}'), "{log}");
assert_consumed(&root);
}
#[test]
fn version_refuses_a_notes_file_outside_the_repo() {
let root = temp_repo("notes-escape");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
let output = oakum(&root)
.args(["version", "--notes-file", "../secret.md"])
.output()
.expect("run");
assert!(!output.status.success());
let err = String::from_utf8_lossy(&output.stderr);
assert!(err.contains("failed to resolve --notes-file"), "{err}");
assert!(err.contains("outside the repository"), "{err}");
assert!(!err.contains("template"), "{err}");
assert!(!root.join("CHANGELOG.md").exists());
assert!(root.join(".changeset/one.md").exists());
}
#[test]
fn version_refuses_a_missing_notes_file() {
let root = temp_repo("notes-missing");
cargo_package(&root, "demo", "0.1.0");
write_patch_changeset(&root, "demo");
let output = oakum(&root)
.args(["version", "--notes-file", "release-notes.md"])
.output()
.expect("run");
assert!(!output.status.success());
let err = String::from_utf8_lossy(&output.stderr);
assert!(err.contains("failed to resolve --notes-file"), "{err}");
assert!(!err.contains("template"), "{err}");
assert!(!root.join("CHANGELOG.md").exists());
assert!(root.join(".changeset/one.md").exists());
}
#[test]
fn version_renders_notes_file_through_a_template() {
let root = temp_repo("notes-template");
cargo_package(&root, "demo", "0.1.0");
fs::create_dir_all(root.join(".changeset")).unwrap();
fs::write(
root.join(".changeset/_config.toml"),
versioned("template = \"## {{ version }} ({{ date }})\\n\\n{{ notes[0] }}\\n\"\n"),
)
.unwrap();
write_patch_changeset(&root, "demo");
fs::write(root.join("release-notes.md"), "templated notes\n").unwrap();
let output = oakum(&root)
.args(["version", "--notes-file", "release-notes.md"])
.output()
.expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.contains(")\n\ntemplated notes\n"), "{log}");
assert!(!log.contains("patch demo"), "{log}");
assert_consumed(&root);
}
#[test]
fn tool_version_mismatch_refuses() {
let root = temp_repo("toolver");
cargo_package(&root, "demo", "0.1.0");
fs::create_dir_all(root.join(".changeset")).expect("dir");
fs::write(
root.join(".changeset/_config.toml"),
"tool-version = \"9.9.9\"\n",
)
.expect("config");
let output = oakum(&root).arg("version").output().expect("run");
assert!(!output.status.success());
let err = String::from_utf8_lossy(&output.stderr);
assert!(
err.contains("tool-version") && err.contains("upgrade"),
"stderr: {err}"
);
}
#[test]
fn bumping_cargo_oakum_writes_tool_version_in_lockstep() {
let root = temp_repo("self-host-toolver");
cargo_package(&root, "oakum", "0.1.0");
fs::create_dir_all(root.join(".changeset")).expect("dir");
fs::write(
root.join(".changeset/_config.toml"),
versioned("change-files = true\nconventional-commits = false\n"),
)
.expect("config");
write_patch_changeset(&root, "oakum");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let toml = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(toml.contains("version = \"0.1.1\""), "{toml}");
let config = fs::read_to_string(root.join(".changeset/_config.toml")).unwrap();
assert!(
config.contains("tool-version = \"0.1.1\""),
"self-host must bump tool-version with the oakum member: {config}"
);
assert!(
config.contains("conventional-commits = false"),
"must preserve other config bytes: {config}"
);
assert_consumed(&root);
}
#[test]
fn bumping_a_non_oakum_package_leaves_tool_version() {
let root = temp_repo("other-pkg-toolver");
cargo_package(&root, "demo", "0.1.0");
fs::create_dir_all(root.join(".changeset")).expect("dir");
let pinned = versioned("change-files = true\nconventional-commits = false\n");
fs::write(root.join(".changeset/_config.toml"), &pinned).expect("config");
write_patch_changeset(&root, "demo");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let config = fs::read_to_string(root.join(".changeset/_config.toml")).unwrap();
assert_eq!(config, pinned, "tool-version rewrite is oakum-member only");
assert_consumed(&root);
}
#[test]
fn no_config_refuses_version_and_writes_nothing() {
let root = temp_repo("no-config");
fs::remove_file(root.join(".changeset/_config.toml")).expect("drop config");
cargo_package(&root, "oakum", "0.1.0");
write_patch_changeset(&root, "oakum");
let manifest_before = fs::read_to_string(root.join("Cargo.toml")).unwrap();
let output = oakum(&root).arg("version").output().expect("run");
assert!(
!output.status.success(),
"defaults must not bump; stdout: {}",
String::from_utf8_lossy(&output.stdout)
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains(
"unverified: `.changeset/_config.toml` not found; run `oakum init` or `oakum migrate`"
),
"{stderr}"
);
assert!(
!root.join(".changeset/_config.toml").exists(),
"a refusal writes no config"
);
assert_eq!(
fs::read_to_string(root.join("Cargo.toml")).unwrap(),
manifest_before
);
assert!(root.join(".changeset/one.md").is_file(), "bump file kept");
assert_no_changelog(&root);
}
#[test]
fn a_token_in_the_environment_does_not_call_github() {
let root = temp_repo("token-stays-local");
cargo_package(&root, "demo", "0.1.0");
fs::create_dir_all(root.join(".changeset")).expect("dir");
fs::write(root.join(".changeset/_config.toml"), versioned("")).expect("config");
write_patch_changeset(&root, "demo");
let server = MockServer::start();
let hit = server.mock(|when, then| {
when.any_request();
then.status(500).body("version must not call GitHub");
});
let output = oakum(&root)
.arg("version")
.env("GITHUB_API_URL", server.base_url())
.env("GITHUB_TOKEN", "token")
.env("GH_TOKEN", "token")
.env("GITHUB_REPOSITORY", "oakoss/oakum")
.output()
.expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
hit.assert_calls(0);
let toml = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(toml.contains("version = \"0.1.1\""), "{toml}");
assert!(root.join("CHANGELOG.md").exists());
assert!(!root.join(".changeset/one.md").exists());
}
#[test]
fn version_writes_declared_extra_files_for_claude_plugin_shape() {
let root = temp_repo("extra-files-pr26");
let pkg = root.join("plugins/review-cycle");
fs::create_dir_all(pkg.join(".claude-plugin")).expect("plugin dir");
fs::create_dir_all(root.join(".claude-plugin")).expect("marketplace dir");
fs::write(
pkg.join("package.json"),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\",\n \"private\": true\n}\n",
)
.expect("package.json");
fs::write(
pkg.join(".claude-plugin/plugin.json"),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\",\n \"description\": \"Review cycle\"\n}\n",
)
.expect("plugin.json");
fs::write(
root.join(".claude-plugin/marketplace.json"),
"{\n \"name\": \"oakoss\",\n \"plugins\": [\n {\n \"name\": \"other\",\n \"source\": \"./plugins/other\",\n \"version\": \"1.0.0\"\n },\n {\n \"name\": \"review-cycle\",\n \"source\": \"./plugins/review-cycle\",\n \"version\": \"0.14.0\"\n }\n ]\n}\n",
)
.expect("marketplace.json");
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned(
r#"
[private-packages]
version = true
[[packages.review-cycle.extra-files]]
path = ".claude-plugin/plugin.json"
format = "json"
key = "version"
[[packages.review-cycle.extra-files]]
path = "/.claude-plugin/marketplace.json"
format = "json"
key = "plugins.{name=review-cycle}.version"
"#,
),
)
.expect("config");
write_changeset(&root, "review-cycle", "minor");
let output = oakum(&pkg).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(
fs::read_to_string(pkg.join("package.json")).unwrap(),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.15.0\",\n \"private\": true\n}\n"
);
assert_eq!(
fs::read_to_string(pkg.join(".claude-plugin/plugin.json")).unwrap(),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.15.0\",\n \"description\": \"Review cycle\"\n}\n"
);
assert_eq!(
fs::read_to_string(root.join(".claude-plugin/marketplace.json")).unwrap(),
"{\n \"name\": \"oakoss\",\n \"plugins\": [\n {\n \"name\": \"other\",\n \"source\": \"./plugins/other\",\n \"version\": \"1.0.0\"\n },\n {\n \"name\": \"review-cycle\",\n \"source\": \"./plugins/review-cycle\",\n \"version\": \"0.15.0\"\n }\n ]\n}\n"
);
assert_consumed(&root);
}
#[test]
fn version_extra_files_error_when_marketplace_name_missing() {
let root = temp_repo("extra-files-nomatch");
let pkg = root.join("plugins/review-cycle");
fs::create_dir_all(pkg.join(".claude-plugin")).expect("plugin dir");
fs::create_dir_all(root.join(".claude-plugin")).expect("marketplace dir");
fs::write(
pkg.join("package.json"),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\"\n}\n",
)
.expect("package.json");
fs::write(
pkg.join(".claude-plugin/plugin.json"),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\"\n}\n",
)
.expect("plugin.json");
fs::write(
root.join(".claude-plugin/marketplace.json"),
"{\n \"plugins\": [\n {\n \"name\": \"other\",\n \"version\": \"1.0.0\"\n }\n ]\n}\n",
)
.expect("marketplace.json");
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned(
r#"
[[packages.review-cycle.extra-files]]
path = "/.claude-plugin/marketplace.json"
format = "json"
key = "plugins.{name=review-cycle}.version"
"#,
),
)
.expect("config");
write_changeset(&root, "review-cycle", "minor");
let output = oakum(&pkg).arg("version").output().expect("run");
assert!(
!output.status.success(),
"expected failure: {}",
String::from_utf8_lossy(&output.stdout)
);
let err = format!(
"{}{}",
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout)
);
assert!(err.contains("found no element"), "{err}");
assert_eq!(
fs::read_to_string(pkg.join("package.json")).unwrap(),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\"\n}\n",
"failed version must not leave partial writes"
);
}
#[test]
fn version_extra_files_error_when_marketplace_name_ambiguous() {
let root = temp_repo("extra-files-ambiguous");
let pkg = root.join("plugins/review-cycle");
fs::create_dir_all(&pkg).expect("pkg");
fs::create_dir_all(root.join(".claude-plugin")).expect("marketplace dir");
fs::write(
pkg.join("package.json"),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\"\n}\n",
)
.expect("package.json");
fs::write(
root.join(".claude-plugin/marketplace.json"),
"{\n \"plugins\": [\n {\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\"\n },\n {\n \"name\": \"review-cycle\",\n \"version\": \"9.9.9\"\n }\n ]\n}\n",
)
.expect("marketplace.json");
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned(
r#"
[[packages.review-cycle.extra-files]]
path = "/.claude-plugin/marketplace.json"
format = "json"
key = "plugins.{name=review-cycle}.version"
"#,
),
)
.expect("config");
write_changeset(&root, "review-cycle", "minor");
let output = oakum(&pkg).arg("version").output().expect("run");
assert!(!output.status.success());
let err = format!(
"{}{}",
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout)
);
assert!(err.contains("more than one"), "{err}");
assert_eq!(
fs::read_to_string(pkg.join("package.json")).unwrap(),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\"\n}\n",
"failed version must not leave partial writes"
);
}
#[test]
fn version_extra_files_error_when_file_missing() {
let root = temp_repo("extra-files-missing");
let pkg = root.join("plugins/review-cycle");
fs::create_dir_all(&pkg).expect("pkg");
fs::write(
pkg.join("package.json"),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\"\n}\n",
)
.expect("package.json");
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned(
r#"
[[packages.review-cycle.extra-files]]
path = ".claude-plugin/plugin.json"
format = "json"
key = "version"
"#,
),
)
.expect("config");
write_changeset(&root, "review-cycle", "minor");
let output = oakum(&pkg).arg("version").output().expect("run");
assert!(!output.status.success());
let err = format!(
"{}{}",
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout)
);
assert!(
err.contains("missing") || err.contains("extra-files"),
"{err}"
);
assert_eq!(
fs::read_to_string(pkg.join("package.json")).unwrap(),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\"\n}\n",
"failed version must not leave partial writes"
);
}
#[test]
fn version_extra_files_compose_shared_marketplace_across_packages() {
let root = temp_repo("extra-files-shared");
fs::create_dir_all(root.join("crates/alpha/src")).unwrap();
fs::create_dir_all(root.join("crates/beta/src")).unwrap();
fs::create_dir_all(root.join(".claude-plugin")).unwrap();
fs::write(
root.join("Cargo.toml"),
"[workspace]\nresolver = \"2\"\nmembers = [\"crates/alpha\", \"crates/beta\"]\n",
)
.unwrap();
fs::write(
root.join("crates/alpha/Cargo.toml"),
"[package]\nname = \"alpha\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/alpha/src/lib.rs"), "").unwrap();
fs::write(
root.join("crates/beta/Cargo.toml"),
"[package]\nname = \"beta\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/beta/src/lib.rs"), "").unwrap();
fs::write(
root.join(".claude-plugin/marketplace.json"),
"{\n \"plugins\": [\n {\n \"name\": \"alpha\",\n \"version\": \"0.1.0\"\n },\n {\n \"name\": \"beta\",\n \"version\": \"0.1.0\"\n }\n ]\n}\n",
)
.unwrap();
fs::create_dir_all(root.join(".changeset")).unwrap();
fs::write(
root.join(".changeset/_config.toml"),
versioned(
r#"
[[packages.alpha.extra-files]]
path = "../../.claude-plugin/marketplace.json"
format = "json"
key = "plugins.{name=alpha}.version"
[[packages.beta.extra-files]]
path = "/.claude-plugin/marketplace.json"
format = "json"
key = "plugins.{name=beta}.version"
"#,
),
)
.unwrap();
fs::write(
root.join(".changeset/one.md"),
"---\nalpha: patch\nbeta: patch\n---\n\nshared marketplace\n",
)
.unwrap();
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_eq!(
fs::read_to_string(root.join(".claude-plugin/marketplace.json")).unwrap(),
"{\n \"plugins\": [\n {\n \"name\": \"alpha\",\n \"version\": \"0.1.1\"\n },\n {\n \"name\": \"beta\",\n \"version\": \"0.1.1\"\n }\n ]\n}\n"
);
assert_consumed(&root);
}
#[test]
fn version_extra_files_only_bumped_package_writes_shared_marketplace() {
let root = temp_repo("extra-files-one-bump");
fs::create_dir_all(root.join("crates/alpha/src")).unwrap();
fs::create_dir_all(root.join("crates/beta/src")).unwrap();
fs::create_dir_all(root.join(".claude-plugin")).unwrap();
fs::write(
root.join("Cargo.toml"),
"[workspace]\nresolver = \"2\"\nmembers = [\"crates/alpha\", \"crates/beta\"]\n",
)
.unwrap();
fs::write(
root.join("crates/alpha/Cargo.toml"),
"[package]\nname = \"alpha\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/alpha/src/lib.rs"), "").unwrap();
fs::write(
root.join("crates/beta/Cargo.toml"),
"[package]\nname = \"beta\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/beta/src/lib.rs"), "").unwrap();
fs::write(
root.join(".claude-plugin/marketplace.json"),
"{\n \"plugins\": [\n {\n \"name\": \"alpha\",\n \"version\": \"0.1.0\"\n },\n {\n \"name\": \"beta\",\n \"version\": \"0.1.0\"\n }\n ]\n}\n",
)
.unwrap();
fs::create_dir_all(root.join(".changeset")).unwrap();
fs::write(
root.join(".changeset/_config.toml"),
versioned(
r#"
[[packages.alpha.extra-files]]
path = "/.claude-plugin/marketplace.json"
format = "json"
key = "plugins.{name=alpha}.version"
[[packages.beta.extra-files]]
path = "/.claude-plugin/marketplace.json"
format = "json"
key = "plugins.{name=beta}.version"
"#,
),
)
.unwrap();
write_changeset(&root, "alpha", "patch");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let market = fs::read_to_string(root.join(".claude-plugin/marketplace.json")).unwrap();
assert!(
market.contains("\"name\": \"alpha\"") && market.contains("\"version\": \"0.1.1\""),
"{market}"
);
assert!(
market.contains("\"name\": \"beta\"") && market.contains("\"version\": \"0.1.0\""),
"{market}"
);
assert_consumed(&root);
}
#[test]
fn version_extra_files_error_when_plain_key_missing() {
let root = temp_repo("extra-files-missing-key");
let pkg = root.join("plugins/review-cycle");
fs::create_dir_all(pkg.join(".claude-plugin")).expect("plugin dir");
fs::write(
pkg.join("package.json"),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\"\n}\n",
)
.expect("package.json");
fs::write(
pkg.join(".claude-plugin/plugin.json"),
"{\n \"name\": \"review-cycle\"\n}\n",
)
.expect("plugin.json");
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned(
r#"
[[packages.review-cycle.extra-files]]
path = ".claude-plugin/plugin.json"
format = "json"
key = "version"
"#,
),
)
.expect("config");
write_changeset(&root, "review-cycle", "minor");
let output = oakum(&pkg).arg("version").output().expect("run");
assert!(!output.status.success());
let err = format!(
"{}{}",
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout)
);
assert!(err.contains("does not exist"), "{err}");
assert_eq!(
fs::read_to_string(pkg.join("package.json")).unwrap(),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\"\n}\n",
"failed version must not leave partial writes"
);
}
#[test]
fn version_extra_files_error_when_path_escapes_repository() {
let root = temp_repo("extra-files-escape");
let pkg = root.join("plugins/review-cycle");
fs::create_dir_all(&pkg).expect("pkg");
fs::write(
pkg.join("package.json"),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\"\n}\n",
)
.expect("package.json");
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned(
r#"
[[packages.review-cycle.extra-files]]
path = "../../../outside.json"
format = "json"
key = "version"
"#,
),
)
.expect("config");
write_changeset(&root, "review-cycle", "minor");
let output = oakum(&pkg).arg("version").output().expect("run");
assert!(!output.status.success());
let err = format!(
"{}{}",
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout)
);
assert!(err.contains("escapes the repository"), "{err}");
assert_eq!(
fs::read_to_string(pkg.join("package.json")).unwrap(),
"{\n \"name\": \"review-cycle\",\n \"version\": \"0.14.0\"\n}\n"
);
}
#[test]
fn version_refuses_an_unpublishable_package_named_by_intent() {
let root = temp_repo("private-refuse");
fs::write(
root.join("Cargo.toml"),
"[package]\nname = \"internal\"\nversion = \"0.1.0\"\nedition = \"2021\"\npublish = false\n\n[workspace]\n",
)
.expect("Cargo.toml");
fs::create_dir_all(root.join("src")).expect("src");
fs::write(root.join("src/lib.rs"), "").expect("lib.rs");
write_patch_changeset(&root, "internal");
let output = oakum(&root).arg("version").output().expect("run");
assert!(!output.status.success());
let err = String::from_utf8_lossy(&output.stderr);
assert!(err.contains("not version-managed"), "{err}");
assert!(err.contains("private-packages"), "{err}");
let manifest = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(manifest.contains("version = \"0.1.0\""), "{manifest}");
}
#[test]
fn version_bumps_unpublishable_when_private_packages_version_is_true() {
let root = temp_repo("private-opt-in");
fs::write(
root.join("Cargo.toml"),
"[package]\nname = \"internal\"\nversion = \"0.1.0\"\nedition = \"2021\"\npublish = false\n\n[workspace]\n",
)
.expect("Cargo.toml");
fs::create_dir_all(root.join("src")).expect("src");
fs::write(root.join("src/lib.rs"), "").expect("lib.rs");
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned("[private-packages]\nversion = true\n"),
)
.expect("config");
write_patch_changeset(&root, "internal");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let manifest = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(manifest.contains("version = \"0.1.1\""), "{manifest}");
assert_consumed(&root);
}
#[test]
fn version_honors_include_and_excludes_siblings() {
let root = temp_repo("include-exclude");
fs::create_dir_all(root.join("crates/alpha/src")).unwrap();
fs::create_dir_all(root.join("crates/beta/src")).unwrap();
fs::write(
root.join("Cargo.toml"),
"[workspace]\nresolver = \"2\"\nmembers = [\"crates/alpha\", \"crates/beta\"]\n",
)
.unwrap();
fs::write(
root.join("crates/alpha/Cargo.toml"),
"[package]\nname = \"alpha\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/alpha/src/lib.rs"), "").unwrap();
fs::write(
root.join("crates/beta/Cargo.toml"),
"[package]\nname = \"beta\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/beta/src/lib.rs"), "").unwrap();
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned("include = [\"alpha\"]\n"),
)
.expect("config");
fs::write(
root.join(".changeset/one.md"),
"---\nalpha: patch\nbeta: patch\n---\n\nboth\n",
)
.expect("changeset");
let output = oakum(&root).arg("version").output().expect("run");
assert!(!output.status.success());
let err = String::from_utf8_lossy(&output.stderr);
assert!(err.contains("beta"), "{err}");
assert!(err.contains("not version-managed"), "{err}");
}
#[test]
fn version_bumps_only_included_package() {
let root = temp_repo("include-only");
fs::create_dir_all(root.join("crates/alpha/src")).unwrap();
fs::create_dir_all(root.join("crates/beta/src")).unwrap();
fs::write(
root.join("Cargo.toml"),
"[workspace]\nresolver = \"2\"\nmembers = [\"crates/alpha\", \"crates/beta\"]\n",
)
.unwrap();
fs::write(
root.join("crates/alpha/Cargo.toml"),
"[package]\nname = \"alpha\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/alpha/src/lib.rs"), "").unwrap();
fs::write(
root.join("crates/beta/Cargo.toml"),
"[package]\nname = \"beta\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/beta/src/lib.rs"), "").unwrap();
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned("include = [\"alpha\"]\n"),
)
.expect("config");
write_patch_changeset(&root, "alpha");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert!(fs::read_to_string(root.join("crates/alpha/Cargo.toml"))
.unwrap()
.contains("version = \"0.1.1\""));
assert!(fs::read_to_string(root.join("crates/beta/Cargo.toml"))
.unwrap()
.contains("version = \"0.1.0\""));
assert_consumed(&root);
}
#[test]
fn version_drops_cascade_into_an_excluded_dependent() {
let root = temp_repo("exclude-cascade");
fs::create_dir_all(root.join("crates/core/src")).unwrap();
fs::create_dir_all(root.join("crates/app/src")).unwrap();
fs::write(
root.join("Cargo.toml"),
"[workspace]\nresolver = \"2\"\nmembers = [\"crates/core\", \"crates/app\"]\n",
)
.unwrap();
fs::write(
root.join("crates/core/Cargo.toml"),
"[package]\nname = \"core\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/core/src/lib.rs"), "").unwrap();
fs::write(
root.join("crates/app/Cargo.toml"),
"[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2021\"\n[dependencies]\ncore = { path = \"../core\", version = \"=0.1.0\" }\n",
)
.unwrap();
fs::write(root.join("crates/app/src/lib.rs"), "").unwrap();
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned("exclude = [\"app\"]\n"),
)
.expect("config");
write_patch_changeset(&root, "core");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert!(fs::read_to_string(root.join("crates/core/Cargo.toml"))
.unwrap()
.contains("version = \"0.1.1\""));
assert!(
fs::read_to_string(root.join("crates/app/Cargo.toml"))
.unwrap()
.contains("version = \"0.1.0\""),
"excluded dependent must not cascade"
);
assert!(
fs::read_to_string(root.join("crates/app/Cargo.toml"))
.unwrap()
.contains("version = \"=0.1.1\""),
"excluded dependent's pin on core must retarget"
);
assert_consumed(&root);
}
#[test]
fn version_does_not_cascade_through_an_excluded_intermediate() {
let root = temp_repo("exclude-mid-cascade");
for name in ["core", "mid", "leaf"] {
fs::create_dir_all(root.join(format!("crates/{name}/src"))).unwrap();
}
fs::write(
root.join("Cargo.toml"),
"[workspace]\nresolver = \"2\"\nmembers = [\"crates/core\", \"crates/mid\", \"crates/leaf\"]\n",
)
.unwrap();
fs::write(
root.join("crates/core/Cargo.toml"),
"[package]\nname = \"core\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.unwrap();
fs::write(root.join("crates/core/src/lib.rs"), "").unwrap();
fs::write(
root.join("crates/mid/Cargo.toml"),
"[package]\nname = \"mid\"\nversion = \"0.1.0\"\nedition = \"2021\"\n[[bin]]\nname = \"mid\"\npath = \"src/main.rs\"\n[dependencies]\ncore = { path = \"../core\", version = \"=0.1.0\" }\n",
)
.unwrap();
fs::write(root.join("crates/mid/src/main.rs"), "fn main() {}\n").unwrap();
fs::write(
root.join("crates/leaf/Cargo.toml"),
"[package]\nname = \"leaf\"\nversion = \"0.1.0\"\nedition = \"2021\"\n[[bin]]\nname = \"leaf\"\npath = \"src/main.rs\"\n[dependencies]\nmid = { path = \"../mid\", version = \"=0.1.0\" }\n",
)
.unwrap();
fs::write(root.join("crates/leaf/src/main.rs"), "fn main() {}\n").unwrap();
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned("exclude = [\"mid\"]\n"),
)
.expect("config");
write_patch_changeset(&root, "core");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert!(fs::read_to_string(root.join("crates/core/Cargo.toml"))
.unwrap()
.contains("version = \"0.1.1\""));
assert!(fs::read_to_string(root.join("crates/mid/Cargo.toml"))
.unwrap()
.contains("version = \"0.1.0\""));
assert!(
fs::read_to_string(root.join("crates/leaf/Cargo.toml"))
.unwrap()
.contains("version = \"0.1.0\""),
"leaf must not bump through excluded mid"
);
assert_consumed(&root);
}
#[test]
fn version_refuses_unknown_include_name() {
let root = temp_repo("unknown-include");
cargo_package(&root, "demo", "0.1.0");
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned("include = [\"ghost\"]\n"),
)
.expect("config");
write_patch_changeset(&root, "demo");
let output = oakum(&root).arg("version").output().expect("run");
assert!(!output.status.success());
let err = String::from_utf8_lossy(&output.stderr);
assert!(err.contains("ghost"), "{err}");
}
#[test]
fn version_stores_publish_command_without_running_it() {
let root = temp_repo("publish-command-idle");
cargo_package(&root, "demo", "0.1.0");
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/_config.toml"),
versioned("[packages.demo]\npublish-command = \"false --this-must-not-run\"\n"),
)
.expect("config");
write_patch_changeset(&root, "demo");
let shim = root.join("shim");
fs::create_dir_all(&shim).expect("shim");
let marker = root.join("publish-command-ran");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let script = shim.join("false");
fs::write(
&script,
format!("#!/bin/sh\ntouch '{}'\nexit 1\n", marker.display()),
)
.expect("shim script");
fs::set_permissions(&script, fs::Permissions::from_mode(0o755)).expect("chmod");
}
let mut cmd = oakum(&root);
cmd.arg("version");
if shim.exists() {
let path = path_prefixed_by(&shim);
cmd.env("PATH", path);
}
let output = cmd.output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert!(!marker.exists(), "publish-command must not spawn in v0");
let manifest = fs::read_to_string(root.join("Cargo.toml")).unwrap();
assert!(manifest.contains("version = \"0.1.1\""), "{manifest}");
assert_consumed(&root);
}
#[test]
fn a_note_opening_with_a_section_heading_lands_there_not_under_the_level() {
let root = temp_repo("note-section");
cargo_package(&root, "demo", "0.1.0");
fs::create_dir_all(root.join(".changeset")).expect("changeset");
fs::write(
root.join(".changeset/one.md"),
"---\ndemo: patch\n---\n\n### Changed\n\nArchived; no further releases.\n",
)
.expect("changeset");
let output = oakum(&root).arg("version").output().expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
assert_changelog(
&root.join("CHANGELOG.md"),
"0.1.1",
"Changed",
"Archived; no further releases.",
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(!log.contains("### Fixed"), "{log}");
}
#[test]
fn a_template_that_reads_changes_gets_the_commit_pr_and_author() {
let root = temp_repo("template-links");
fs::remove_dir(root.join(".git")).expect("fake .git");
support::fixture::git(&root, &["init"]);
support::fixture::git(
&root,
&["remote", "add", "origin", "git@github.com:acme/demo.git"],
);
cargo_package(&root, "demo", "0.1.0");
write_config(
&root,
"template = \"## {{ version }}\\n\\n{% for c in changes %}- {{ c.section }}/{{ c.level }}: {{ c.note }} ([#{{ c.pr.number }}]({{ c.pr.url }}), [{{ c.commit.short }}]({{ c.commit.url }}), thanks {{ c.author.name }})\\n{% endfor %}\"\n",
);
write_patch_changeset(&root, "demo");
commit_as(&root, "Ada", "fix: patch demo (#42)");
fs::write(
root.join(".changeset/one.md"),
"---\ndemo: patch\n---\n\npatch demo, reworded\n",
)
.expect("edit the first bump file");
commit_as(&root, "Bob", "chore: reword (#50)");
fs::write(
root.join(".changeset/two.md"),
"---\ndemo: minor\n---\n\n### Security\n\nRotate the signing key.\n",
)
.expect("second bump file");
commit_as(&root, "Cy", "feat: rotate (#43)");
let output = oakum(&root)
.arg("version")
.env_remove("GITHUB_REPOSITORY")
.output()
.expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let first = rev_parse(&root, "HEAD~2");
let second = rev_parse(&root, "HEAD");
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(
log.contains(&format!(
"- Fixed/patch: patch demo, reworded ([#42](https://github.com/acme/demo/pull/42), [{}](https://github.com/acme/demo/commit/{first}), thanks Ada)\n",
&first[..7]
)),
"the adding commit wins over a later edit of the file: {log}"
);
assert!(
log.contains(&format!(
"- Security/minor: Rotate the signing key. ([#43](https://github.com/acme/demo/pull/43), [{}](https://github.com/acme/demo/commit/{second}), thanks Cy)\n",
&second[..7]
)),
"each file carries its own commit: {log}"
);
assert!(!log.contains("#50") && !log.contains("Bob"), "{log}");
}
fn commit_as(root: &std::path::Path, author: &str, subject: &str) {
support::fixture::git(root, &["add", "-A"]);
support::fixture::git(
root,
&[
"-c",
&format!("user.name={author}"),
"-c",
&format!("user.email={}@example.com", author.to_ascii_lowercase()),
"-c",
"commit.gpgsign=false",
"commit",
"--no-verify",
"-m",
subject,
],
);
}
fn rev_parse(root: &std::path::Path, rev: &str) -> String {
String::from_utf8(support::fixture::git_output(root, &["rev-parse", rev]).stdout)
.unwrap()
.trim()
.to_owned()
}
const GUIDE_TEMPLATE: &str = "## {{ version }} ({{ date }})\n{% for c in changes %}\n- {{ c.note }}{% if c.pr and c.pr.url %} ([#{{ c.pr.number }}]({{ c.pr.url }})){% endif %}{% if c.author %} by {{ c.author.name }}{% endif %}\n{%- endfor %}\n";
#[test]
fn the_guides_example_template_renders_single_blank_lines_and_links_only_on_github() {
let root = temp_repo("template-guide");
fs::remove_dir(root.join(".git")).expect("fake .git");
support::fixture::git(&root, &["init"]);
support::fixture::git(
&root,
&[
"remote",
"add",
"origin",
"https://github.com/acme/demo.git",
],
);
cargo_package(&root, "demo", "0.1.0");
write_config(&root, "template = { file = \"entry.jinja\" }\n");
fs::write(root.join("entry.jinja"), GUIDE_TEMPLATE).expect("template");
write_patch_changeset(&root, "demo");
support::fixture::git(&root, &["add", "-A"]);
support::fixture::git(
&root,
&[
"-c",
"user.name=Ada",
"-c",
"user.email=ada@example.com",
"-c",
"commit.gpgsign=false",
"commit",
"--no-verify",
"-m",
"fix: patch demo (#7)",
],
);
let output = oakum(&root)
.arg("version")
.env_remove("GITHUB_REPOSITORY")
.output()
.expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
let entry = log
.split("## 0.1.1 (")
.nth(1)
.and_then(|rest| rest.split_once('\n').map(|(_, body)| body))
.expect("entry body");
assert!(
entry.starts_with("\n- patch demo ([#7](https://github.com/acme/demo/pull/7)) by Ada\n\n"),
"one blank line after the heading, the bullet, one blank before the footer: {log:?}"
);
assert!(
!log.contains("\n\n\n"),
"no consecutive blank lines: {log:?}"
);
}
#[test]
fn the_guides_example_template_off_github_writes_no_link() {
let root = temp_repo("template-guide-plain");
fs::remove_dir(root.join(".git")).expect("fake .git");
support::fixture::git(&root, &["init"]);
cargo_package(&root, "demo", "0.1.0");
write_config(&root, "template = { file = \"entry.jinja\" }\n");
fs::write(root.join("entry.jinja"), GUIDE_TEMPLATE).expect("template");
write_patch_changeset(&root, "demo");
support::fixture::git(&root, &["add", "-A"]);
support::fixture::git(
&root,
&[
"-c",
"user.name=Ada",
"-c",
"user.email=ada@example.com",
"-c",
"commit.gpgsign=false",
"commit",
"--no-verify",
"-m",
"fix: patch demo (#9)",
],
);
let output = oakum(&root)
.arg("version")
.env_remove("GITHUB_REPOSITORY")
.output()
.expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.contains("\n- patch demo by Ada\n"), "{log:?}");
assert!(!log.contains("None"), "no null rendered as a link: {log:?}");
}
#[test]
fn a_template_that_reads_changes_still_renders_without_a_github_remote_or_a_commit() {
let root = temp_repo("template-links-plain");
fs::remove_dir(root.join(".git")).expect("fake .git");
support::fixture::git(&root, &["init"]);
cargo_package(&root, "demo", "0.1.0");
write_config(
&root,
"template = \"## {{ version }}\\n\\n{% for c in changes %}- {{ c.file }}: {{ c.note }}{% if c.commit %} committed{% endif %}{% if repo %} on github{% endif %}{% endfor %}\\n\"\n",
);
write_patch_changeset(&root, "demo");
let output = oakum(&root)
.arg("version")
.env_remove("GITHUB_REPOSITORY")
.output()
.expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.contains("- one.md: patch demo\n"), "{log}");
assert!(
!log.contains("committed") && !log.contains("on github"),
"{log}"
);
}
#[test]
fn a_malformed_github_repository_variable_is_an_error_when_the_template_links() {
let root = temp_repo("template-bad-slug");
fs::remove_dir(root.join(".git")).expect("fake .git");
support::fixture::git(&root, &["init"]);
cargo_package(&root, "demo", "0.1.0");
write_config(&root, "template = \"{{ version }} {{ repo }}\\n\"\n");
write_patch_changeset(&root, "demo");
let output = oakum(&root)
.arg("version")
.env("GITHUB_REPOSITORY", "not-a-slug")
.output()
.expect("run");
assert!(
!output.status.success(),
"a set but malformed slug is not silently none"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("GITHUB_REPOSITORY"), "{stderr}");
assert!(root.join(".changeset/one.md").is_file(), "nothing consumed");
}
#[test]
fn a_notes_file_replaces_changes_as_well_as_notes() {
let root = temp_repo("notes-file-changes");
cargo_package(&root, "demo", "0.1.0");
write_config(
&root,
"template = \"## {{ version }}\\n\\nN={{ notes | length }} C={{ changes | length }}\\n\"\n",
);
write_patch_changeset(&root, "demo");
fs::write(root.join("NOTES.md"), "supplied body\n").expect("notes");
let output = oakum(&root)
.args(["version", "--notes-file", "NOTES.md"])
.output()
.expect("run");
assert!(
output.status.success(),
"stderr: {}",
String::from_utf8_lossy(&output.stderr)
);
let log = fs::read_to_string(root.join("CHANGELOG.md")).unwrap();
assert!(log.contains("N=1 C=0\n"), "{log}");
}