use std::collections::BTreeSet;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
const TAG: &str = "v2026.8.99";
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}
fn repo_file(relative: &str) -> String {
let path = repo_root().join(relative);
fs::read_to_string(&path).unwrap_or_else(|error| panic!("read {relative}: {error}"))
}
fn release_py(dir: &Path, args: &[&str]) -> Output {
Command::new("python3")
.arg(repo_root().join("scripts/release.py"))
.args(args)
.current_dir(dir)
.env_remove("GITHUB_OUTPUT")
.env("GITHUB_REPOSITORY", "grahambrooks/brake")
.output()
.expect("python3 should run scripts/release.py")
}
fn formula_targets() -> BTreeSet<String> {
repo_file("scripts/release.py")
.lines()
.skip_while(|line| !line.starts_with("FORMULA_TARGETS = {"))
.skip(1)
.take_while(|line| !line.trim_start().starts_with('}'))
.filter_map(|line| {
let value = line.split_once(':')?.1.trim().trim_end_matches(',');
Some(value.trim_matches('"').to_owned())
})
.collect()
}
fn built_targets() -> BTreeSet<String> {
let output = release_py(&repo_root(), &["plan", TAG]);
assert!(
output.status.success(),
"release.py plan failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8_lossy(&output.stdout);
let matrix = stdout
.lines()
.find_map(|line| line.strip_prefix("matrix="))
.expect("plan should print a matrix");
matrix
.split("\"target\":\"")
.skip(1)
.filter_map(|rest| rest.split('"').next())
.map(str::to_owned)
.collect()
}
fn scratch_with_config() -> tempfile::TempDir {
let dir = tempfile::tempdir().expect("tempdir");
fs::copy(
repo_root().join(".release.env"),
dir.path().join(".release.env"),
)
.expect("copy .release.env");
dir
}
#[test]
fn every_platform_the_formula_serves_is_actually_built() {
let built = built_targets();
let served = formula_targets();
assert!(!built.is_empty(), "no targets found in the build matrix");
assert_eq!(
served.len(),
4,
"expected four formula platforms: {served:?}"
);
for target in &served {
assert!(
built.contains(target),
"the formula serves `{target}`, which the build matrix does not produce — \
`brew install` would 404 on that platform"
);
}
assert!(
built.contains("x86_64-pc-windows-msvc"),
"Windows binaries are advertised in the README but not built"
);
}
#[test]
fn the_formula_renders_and_names_the_archives_the_matrix_builds() {
let served = formula_targets();
let dir = scratch_with_config();
let sums: String = served
.iter()
.map(|target| format!("{} brake-{TAG}-{target}.tar.gz\n", "0".repeat(64)))
.collect();
fs::write(dir.path().join("SHA256SUMS"), sums).expect("write SHA256SUMS");
let output = release_py(dir.path(), &["formula", TAG, "SHA256SUMS"]);
assert!(
output.status.success(),
"the formula did not render: {}",
String::from_utf8_lossy(&output.stderr)
);
let formula = fs::read_to_string(dir.path().join("Formula/brake.rb")).expect("formula");
for target in &served {
let expected = format!("brake-{TAG}-{target}.tar.gz");
assert!(
formula.contains(&expected),
"the formula does not reference `{expected}`:\n{formula}"
);
}
assert!(formula.contains(r#"bin.install "brake""#), "{formula}");
}
#[test]
fn the_formula_refuses_to_reference_an_archive_that_was_not_built() {
let dir = scratch_with_config();
fs::write(
dir.path().join("SHA256SUMS"),
format!(
"{} brake-{TAG}-aarch64-apple-darwin.tar.gz\n",
"0".repeat(64)
),
)
.expect("write SHA256SUMS");
let output = release_py(dir.path(), &["formula", TAG, "SHA256SUMS"]);
assert!(!output.status.success());
assert!(
String::from_utf8_lossy(&output.stderr).contains("is missing from SHA256SUMS"),
"{}",
String::from_utf8_lossy(&output.stderr)
);
assert!(!dir.path().join("Formula/brake.rb").exists());
}
#[test]
fn the_released_binaries_include_the_mcp_server() {
let features: BTreeSet<String> = repo_file(".release.env")
.lines()
.filter_map(|line| line.trim().strip_prefix("FEATURES="))
.flat_map(|value| {
value
.split([',', ' '])
.map(str::to_owned)
.collect::<Vec<_>>()
})
.collect();
assert!(
features.contains("mcp"),
"the release build does not enable the mcp feature: {features:?}"
);
}