use std::fs;
use std::path::Path;
use std::process::{Command, Output};
use tempfile::{TempDir, tempdir};
const BRAKE: &str = env!("CARGO_BIN_EXE_brake");
const SPEC: &str = r#"
openapi: 3.1.0
paths:
/payments/{id}:
get:
operationId: getPayment
responses:
"200":
description: ok
"#;
const BROKEN: &str = r#"
openapi: 3.1.0
paths: {}
"#;
fn repo(files: &[(&str, &str)]) -> TempDir {
let repo = tempdir().expect("tempdir");
for (path, body) in files {
let full = repo.path().join(path);
fs::create_dir_all(full.parent().expect("parent")).expect("mkdir");
fs::write(full, body).expect("write");
}
repo
}
fn create_file_command(path: &Path) -> String {
format!("mkdir \"{}\"", path.display())
}
fn brake(cwd: &Path, args: &[&str]) -> Output {
Command::new(BRAKE)
.args(args)
.current_dir(cwd)
.output()
.expect("brake should launch")
}
fn code(output: &Output) -> i32 {
output.status.code().expect("brake should exit normally")
}
fn stdout(output: &Output) -> String {
String::from_utf8_lossy(&output.stdout).into_owned()
}
const TWO_CONTRACTS: &str = r#"
[[contract]]
name = "a"
format = "openapi"
source = "api/a.yaml"
baseline = { file = "api/a.baseline.yaml" }
[[contract]]
name = "b"
format = "openapi"
source = "api/b.yaml"
baseline = { file = "api/b.baseline.yaml" }
"#;
fn two_contracts() -> TempDir {
repo(&[
("brake.toml", TWO_CONTRACTS),
("api/a.baseline.yaml", SPEC),
("api/a.yaml", BROKEN),
("api/b.baseline.yaml", SPEC),
("api/b.yaml", SPEC),
])
}
#[test]
fn check_with_a_path_checks_only_that_contract() {
let repo = two_contracts();
let scoped = brake(repo.path(), &["check", "api/b.yaml", "--format", "text"]);
assert_eq!(
code(&scoped),
0,
"a run scoped to an untouched contract leaked another's finding:\n{}",
stdout(&scoped)
);
let also_scoped = brake(repo.path(), &["check", "api/a.yaml", "--format", "text"]);
assert_eq!(code(&also_scoped), 1);
assert!(stdout(&also_scoped).contains("endpoint-removed"));
}
#[test]
fn check_with_no_paths_checks_everything() {
let repo = two_contracts();
let output = brake(repo.path(), &["check", "--format", "text"]);
assert_eq!(code(&output), 1);
}
#[test]
fn check_accepts_the_many_paths_a_hook_passes_at_once() {
let repo = two_contracts();
let output = brake(
repo.path(),
&[
"check",
"api/b.yaml",
"README.md",
"src/main.rs",
"--format",
"text",
],
);
assert_eq!(code(&output), 0, "{}", stdout(&output));
}
#[test]
fn exit_codes_separate_a_broken_api_from_a_broken_gate() {
let broken_api = two_contracts();
assert_eq!(code(&brake(broken_api.path(), &["check"])), 1);
let broken_gate = repo(&[
("brake.toml", TWO_CONTRACTS),
("api/a.yaml", SPEC),
("api/b.baseline.yaml", SPEC),
("api/b.yaml", SPEC),
]);
assert_eq!(
code(&brake(broken_gate.path(), &["check"])),
2,
"an unresolvable baseline must not be reported as an API break"
);
let clean = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"b\"\nformat=\"openapi\"\nsource=\"api/b.yaml\"\nbaseline={file=\"api/b.baseline.yaml\"}\n",
),
("api/b.baseline.yaml", SPEC),
("api/b.yaml", SPEC),
]);
assert_eq!(code(&brake(clean.path(), &["check"])), 0);
}
#[test]
fn brake_toml_is_found_from_a_subdirectory() {
let repo = two_contracts();
let nested = repo.path().join("api/nested/deeper");
fs::create_dir_all(&nested).expect("mkdir");
let output = brake(&nested, &["check", "--format", "text"]);
assert_eq!(
code(&output),
1,
"brake.toml was not discovered from a subdirectory:\n{}",
String::from_utf8_lossy(&output.stderr)
);
}
#[test]
fn a_missing_config_is_a_tool_failure_with_an_actionable_message() {
let empty = tempdir().expect("tempdir");
let output = brake(empty.path(), &["check"]);
assert_eq!(code(&output), 2);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("brake.toml"), "{stderr}");
}
#[test]
fn contract_flag_restricts_the_run() {
let repo = two_contracts();
let output = brake(
repo.path(),
&["check", "--contract", "b", "--format", "text"],
);
assert_eq!(code(&output), 0, "{}", stdout(&output));
}
#[test]
fn compatibility_flag_changes_the_verdict() {
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"c\"\nformat=\"openapi\"\nsource=\"api/c.yaml\"\nbaseline={file=\"api/c.baseline.yaml\"}\n",
),
("api/c.baseline.yaml", SPEC),
("api/c.yaml", &SPEC.replace("getPayment", "fetchPayment")),
]);
assert_eq!(code(&brake(repo.path(), &["check"])), 0);
assert_eq!(
code(&brake(
repo.path(),
&["check", "--compatibility", "surface"]
)),
1,
"an operationId rename is a surface break"
);
assert_eq!(
code(&brake(
repo.path(),
&["check", "--compatibility", "nonsense"]
)),
2,
"an unknown level is a tool failure, not a silent default"
);
}
#[test]
fn format_auto_is_json_when_piped() {
let repo = two_contracts();
let output = brake(repo.path(), &["check"]);
let parsed: serde_json::Value =
serde_json::from_str(&stdout(&output)).expect("piped output should be JSON");
assert!(parsed["findings"].is_array());
}
#[test]
fn every_format_renders_and_agrees_on_the_verdict() {
let repo = two_contracts();
for format in [
"text",
"json",
"sarif",
"github",
"gitlab",
"github-actions",
"codequality",
] {
let output = brake(repo.path(), &["check", "--format", format]);
assert_eq!(code(&output), 1, "format {format} disagreed on the verdict");
assert!(
!stdout(&output).is_empty(),
"format {format} rendered nothing"
);
}
let bad = brake(repo.path(), &["check", "--format", "yaml"]);
assert_eq!(code(&bad), 2);
}
#[test]
fn github_format_renders_workflow_commands() {
let repo = two_contracts();
let output = brake(repo.path(), &["check", "--format", "github"]);
assert_eq!(code(&output), 1);
let out = stdout(&output);
assert!(
out.contains("::error file="),
"github format should emit workflow commands: {out}"
);
assert!(
out.contains("title=endpoint-removed"),
"github format should carry rule title: {out}"
);
}
#[test]
fn sarif_output_parses_and_carries_fingerprints() {
let repo = two_contracts();
let output = brake(repo.path(), &["check", "--format", "sarif"]);
let sarif: serde_json::Value =
serde_json::from_str(&stdout(&output)).expect("valid SARIF JSON");
assert_eq!(sarif["version"], "2.1.0");
let results = sarif["runs"][0]["results"].as_array().expect("results");
assert!(!results.is_empty());
for result in results {
assert!(result["partialFingerprints"]["brakeFindingV1"].is_string());
}
}
#[test]
fn diff_reports_changes_and_never_gates() {
let repo = two_contracts();
let output = brake(repo.path(), &["diff", "--format", "text"]);
assert_eq!(code(&output), 0, "diff must never fail a build");
assert!(
stdout(&output).contains("endpoint-removed"),
"diff should still describe the change:\n{}",
stdout(&output)
);
}
#[test]
fn analyze_covers_the_repository_and_honours_fail_on() {
let repo = two_contracts();
assert_eq!(code(&brake(repo.path(), &["analyze", "."])), 1);
assert_eq!(
code(&brake(repo.path(), &["analyze", ".", "--fail-on", "error"])),
1
);
}
#[test]
fn explain_covers_every_rule_with_no_placeholder_text() {
let here = tempdir().expect("tempdir");
let listing = brake(here.path(), &["explain"]);
assert_eq!(code(&listing), 0);
let listed = stdout(&listing);
for line in listed.lines() {
let rule_id = line.split_whitespace().next().expect("a rule id");
let output = brake(here.path(), &["explain", rule_id]);
assert_eq!(code(&output), 0, "`brake explain {rule_id}` failed");
let text = stdout(&output);
assert!(text.contains(rule_id), "{rule_id}: id missing from output");
assert!(
!text.to_lowercase().contains("todo") && !text.to_lowercase().contains("tbd"),
"{rule_id}: placeholder explanation"
);
assert!(
text.len() > 200,
"{rule_id}: explanation is too thin to be useful"
);
}
assert!(
listed.lines().count() > 25,
"the catalogue should cover the specified ruleset, got {} rules",
listed.lines().count()
);
}
#[test]
fn explain_rejects_an_unknown_rule_rather_than_inventing_one() {
let here = tempdir().expect("tempdir");
let output = brake(here.path(), &["explain", "no-such-rule"]);
assert_eq!(code(&output), 2);
assert!(
String::from_utf8_lossy(&output.stderr).contains("no-such-rule"),
"the error should name what was asked for"
);
}
#[test]
fn every_rule_the_tool_can_emit_is_explainable() {
let here = tempdir().expect("tempdir");
let listing = stdout(&brake(here.path(), &["explain"]));
let known: Vec<&str> = listing
.lines()
.filter_map(|line| line.split_whitespace().next())
.collect();
let repo = two_contracts();
let output = brake(repo.path(), &["check", "--format", "json"]);
let parsed: serde_json::Value = serde_json::from_str(&stdout(&output)).expect("json");
for finding in parsed["findings"].as_array().expect("findings") {
let rule = finding["rule"].as_str().expect("rule");
assert!(known.contains(&rule), "`{rule}` is not in the catalogue");
}
}
#[test]
fn drift_is_unreachable_without_the_flag() {
let witness = tempdir().expect("tempdir");
let marker = witness.path().join("generator-ran");
let repo = repo(&[
(
"brake.toml",
&format!(
"[[contract]]\nname=\"c\"\nformat=\"openapi\"\nsource=\"api/c.yaml\"\n\
baseline={{file=\"api/c.baseline.yaml\"}}\n\
[contract.generated]\ncommand = '{}'\n",
create_file_command(&marker)
),
),
("api/c.baseline.yaml", SPEC),
("api/c.yaml", SPEC),
]);
brake(repo.path(), &["check"]);
assert!(
!marker.exists(),
"brake check ran a config-declared command without --drift"
);
brake(repo.path(), &["check", "--drift"]);
assert!(marker.exists(), "--drift should run the declared generator");
}
fn released_repository() -> TempDir {
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"c\"\nformat=\"openapi\"\nsource=\"api/c.yaml\"\n\
baseline={latest-tag=\"v*\"}\n",
),
("api/c.yaml", SPEC),
]);
let git = |args: &[&str]| {
let output = Command::new("git")
.args(args)
.current_dir(repo.path())
.output()
.expect("git should launch");
assert!(
output.status.success(),
"git {args:?}: {}",
String::from_utf8_lossy(&output.stderr)
);
};
git(&["init", "-b", "main"]);
git(&["config", "user.name", "Brake Test"]);
git(&["config", "user.email", "brake@example.com"]);
git(&["add", "."]);
git(&["commit", "-m", "v9"]);
git(&["tag", "-a", "v9.0.0", "-m", "release nine"]);
git(&["commit", "--allow-empty", "-m", "v10"]);
git(&["tag", "v10.0.0"]);
fs::write(repo.path().join("api/c.yaml"), BROKEN).expect("write");
git(&["add", "."]);
git(&["commit", "-m", "break the api"]);
repo
}
#[test]
fn a_release_baseline_gates_against_the_last_tag() {
let repo = released_repository();
let output = brake(repo.path(), &["check", "--format", "json"]);
assert_eq!(code(&output), 1);
let parsed: serde_json::Value = serde_json::from_str(&stdout(&output)).expect("json");
let file = parsed["findings"][0]["file"].as_str().expect("file");
assert_eq!(
file, "tag:v10.0.0",
"latest-tag must pick v10.0.0, not v9.0.0 — byte order gets this backwards"
);
}
#[test]
fn the_baseline_flag_accepts_a_tag_a_glob_and_a_revision() {
let repo = released_repository();
for (flag, expected_label) in [
("v9.0.0", "tag:v9.0.0"),
("v10.0.0", "tag:v10.0.0"),
("latest-tag:v*", "tag:v10.0.0"),
] {
let output = brake(
repo.path(),
&["check", "--baseline", flag, "--format", "json"],
);
assert_eq!(code(&output), 1, "--baseline {flag}");
let parsed: serde_json::Value = serde_json::from_str(&stdout(&output)).expect("json");
let file = parsed["findings"][0]["file"].as_str().expect("file");
assert!(
file.ends_with(expected_label.trim_start_matches("tag:")),
"--baseline {flag} resolved to {file}, wanted {expected_label}"
);
}
}
#[test]
fn a_release_baseline_with_no_matching_tag_is_a_tool_failure() {
let repo = released_repository();
fs::write(
repo.path().join("brake.toml"),
"[[contract]]\nname=\"c\"\nformat=\"openapi\"\nsource=\"api/c.yaml\"\n\
baseline={latest-tag=\"release-*\"}\n",
)
.expect("write");
let output = brake(repo.path(), &["check", "--format", "text"]);
assert_eq!(
code(&output),
2,
"an unresolvable release baseline must not report clean"
);
assert!(
stdout(&output).contains("fetch-depth"),
"the message should name the usual cause:\n{}",
stdout(&output)
);
}
#[test]
fn a_baseline_setting_two_shapes_at_once_is_rejected() {
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"c\"\nformat=\"openapi\"\nsource=\"api/c.yaml\"\n\
baseline={tag=\"v1.0.0\", rev=\"abc123\"}\n",
),
("api/c.yaml", SPEC),
]);
let output = brake(repo.path(), &["check"]);
assert_eq!(code(&output), 2);
assert!(
String::from_utf8_lossy(&output.stderr).contains("several"),
"an ambiguous baseline must say so rather than picking one"
);
}
#[test]
fn version_prints_the_calver() {
let here = tempdir().expect("tempdir");
let output = brake(here.path(), &["--version"]);
assert_eq!(code(&output), 0);
assert!(stdout(&output).contains("2026."), "{}", stdout(&output));
}
#[test]
fn as_of_defaults_to_today_so_expiry_actually_expires() {
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"c\"\nformat=\"openapi\"\nsource=\"api/c.yaml\"\n\
baseline={file=\"api/c.baseline.yaml\"}\n\
[[contract.allow]]\nrule=\"endpoint-removed\"\nreason=\"migrated\"\n\
expires=\"2020-01-01\"\n",
),
("api/c.baseline.yaml", SPEC),
("api/c.yaml", BROKEN),
]);
let output = brake(repo.path(), &["check", "--format", "json"]);
let parsed: serde_json::Value = serde_json::from_str(&stdout(&output)).expect("json");
let rules: Vec<&str> = parsed["findings"]
.as_array()
.expect("findings")
.iter()
.filter_map(|finding| finding["rule"].as_str())
.collect();
assert!(
rules.contains(&"expired-allow"),
"a suppression that expired in 2020 must not still be suppressing: {rules:?}"
);
let before = brake(
repo.path(),
&["check", "--as-of", "2019-06-01", "--format", "json"],
);
let parsed: serde_json::Value = serde_json::from_str(&stdout(&before)).expect("json");
let rules: Vec<&str> = parsed["findings"]
.as_array()
.expect("findings")
.iter()
.filter_map(|finding| finding["rule"].as_str())
.collect();
assert!(
!rules.contains(&"expired-allow") && !rules.contains(&"endpoint-removed"),
"before its expiry the suppression should still apply: {rules:?}"
);
}
#[test]
fn a_suppression_for_an_unknown_rule_is_rejected_at_parse_time() {
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"c\"\nformat=\"openapi\"\nsource=\"api/c.yaml\"\n\
baseline={file=\"api/c.baseline.yaml\"}\n\
[[contract.allow]]\nrule=\"endpoint-remvoed\"\nreason=\"typo\"\n",
),
("api/c.baseline.yaml", SPEC),
("api/c.yaml", SPEC),
]);
let output = brake(repo.path(), &["check"]);
assert_eq!(code(&output), 2);
assert!(
String::from_utf8_lossy(&output.stderr).contains("endpoint-remvoed"),
"a misspelled rule must be named, not silently ignored"
);
}
#[test]
fn a_suppression_without_a_reason_is_rejected() {
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"c\"\nformat=\"openapi\"\nsource=\"api/c.yaml\"\n\
baseline={file=\"api/c.baseline.yaml\"}\n\
[[contract.allow]]\nrule=\"endpoint-removed\"\n",
),
("api/c.baseline.yaml", SPEC),
("api/c.yaml", SPEC),
]);
assert_eq!(code(&brake(repo.path(), &["check"])), 2);
}
#[test]
fn output_is_byte_identical_across_repeated_invocations() {
let repo = two_contracts();
for format in ["text", "json", "sarif", "github", "gitlab"] {
let first = stdout(&brake(repo.path(), &["check", "--format", format]));
let second = stdout(&brake(repo.path(), &["check", "--format", format]));
assert_eq!(first, second, "format {format} is not byte-stable");
assert!(
!first.contains(&repo.path().to_string_lossy().to_string()),
"format {format} embeds the absolute checkout path"
);
}
}
#[test]
fn every_breaking_finding_carries_a_way_out() {
let here = tempdir().expect("tempdir");
let listing = stdout(&brake(here.path(), &["explain"]));
for line in listing.lines() {
let rule_id = line.split_whitespace().next().expect("a rule id");
let text = stdout(&brake(here.path(), &["explain", rule_id]));
assert!(
!text.contains("{subject}") && !text.contains("{endpoint}"),
"`{rule_id}` renders an unbound placeholder"
);
if text.contains("ways to make the change safely:") {
assert!(
text.contains("costs:"),
"`{rule_id}` lists strategies with no costs, which reads as though \
they are all free"
);
}
}
}
#[test]
fn a_finding_names_the_field_its_advice_is_about() {
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"c\"\nformat=\"openapi\"\nsource=\"api/c.yaml\"\n\
baseline={file=\"api/c.baseline.yaml\"}\n",
),
(
"api/c.baseline.yaml",
"openapi: 3.1.0\npaths:\n /p:\n post:\n operationId: createPayment\n\
\x20 responses:\n \"201\": {description: ok}\n",
),
(
"api/c.yaml",
"openapi: 3.1.0\npaths:\n /p:\n post:\n operationId: createPayment\n\
\x20 parameters:\n - {name: tenant, in: query, required: true, \
schema: {type: string}}\n responses:\n \"201\": {description: ok}\n",
),
]);
let output = brake(repo.path(), &["check", "--format", "json"]);
assert_eq!(code(&output), 1);
let parsed: serde_json::Value = serde_json::from_str(&stdout(&output)).expect("json");
let finding = &parsed["findings"][0];
assert_eq!(finding["subject"], "tenant");
let summary = finding["remediation"][0]["summary"]
.as_str()
.expect("a bound summary");
assert!(
summary.contains("`tenant`"),
"the advice must name the parameter, not its position: {summary}"
);
let text = stdout(&brake(repo.path(), &["check", "--format", "text"]));
assert!(text.contains("optional-with-default"), "{text}");
assert!(text.contains("`tenant`"), "{text}");
}
#[test]
fn the_mcp_subcommand_is_listed_whether_or_not_it_is_built() {
let here = tempdir().expect("tempdir");
let help = stdout(&brake(here.path(), &["--help"]));
assert!(
help.contains("mcp"),
"`brake --help` does not mention the MCP server:\n{help}"
);
}
#[test]
fn brake_mcp_either_serves_or_says_how_to_get_it() {
let here = tempdir().expect("tempdir");
if cfg!(feature = "mcp") {
let help = stdout(&brake(here.path(), &["mcp", "--help"]));
assert!(help.contains("--as-of"), "{help}");
return;
}
let output = brake(here.path(), &["mcp", "."]);
assert_eq!(
code(&output),
2,
"a subcommand that cannot run must exit 2, not pretend to succeed"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("--features mcp"), "{stderr}");
assert!(stderr.contains("cargo install brake"), "{stderr}");
}
fn unconfigured_repo() -> TempDir {
let repo = repo(&[
("api/payments-openapi.yaml", SPEC),
("api/payments-openapi.baseline.yaml", SPEC),
(
".github/workflows/api-tests.yaml",
"name: api-tests\non: push\n",
),
("package.json", r#"{"name":"app","version":"1.0.0"}"#),
(
"docker-compose.yml",
"services:\n api:\n image: nginx\n",
),
]);
let git = |args: &[&str]| {
let ok = Command::new("git")
.args(args)
.current_dir(repo.path())
.output()
.expect("git should launch")
.status
.success();
assert!(ok, "git {args:?}");
};
git(&["init", "-b", "main"]);
git(&["config", "user.name", "Brake Test"]);
git(&["config", "user.email", "brake@example.com"]);
git(&["add", "."]);
git(&["commit", "-m", "init"]);
repo
}
#[test]
fn the_first_command_a_new_user_runs_names_the_one_that_fixes_it() {
let repo = unconfigured_repo();
let output = brake(repo.path(), &["check"]);
assert_eq!(code(&output), 2);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("brake init"),
"\"create one\" is not actionable without saying how:\n{stderr}"
);
}
#[test]
fn init_declares_the_real_contracts_and_ignores_everything_else() {
let repo = unconfigured_repo();
let output = brake(repo.path(), &["init"]);
assert_eq!(
code(&output),
0,
"{}",
String::from_utf8_lossy(&output.stderr)
);
let written = fs::read_to_string(repo.path().join("brake.toml")).expect("brake.toml");
assert!(written.contains("api/payments-openapi.yaml"), "{written}");
assert!(!written.contains("api-tests.yaml"), "{written}");
assert!(!written.contains("package.json"), "{written}");
assert!(!written.contains("docker-compose"), "{written}");
assert!(!written.contains("baseline.yaml\""), "{written}");
}
#[test]
fn what_init_writes_works_on_the_very_next_command() {
let repo = unconfigured_repo();
assert_eq!(code(&brake(repo.path(), &["init"])), 0);
let output = brake(
repo.path(),
&["check", "api/payments-openapi.yaml", "--format", "text"],
);
assert_eq!(
code(&output),
0,
"the generated config failed immediately:\nstdout: {}\nstderr: {}",
stdout(&output),
String::from_utf8_lossy(&output.stderr)
);
fs::write(repo.path().join("api/payments-openapi.yaml"), BROKEN).expect("write");
let output = brake(
repo.path(),
&["check", "api/payments-openapi.yaml", "--format", "text"],
);
assert_eq!(code(&output), 1, "{}", stdout(&output));
assert!(stdout(&output).contains("endpoint-removed"));
}
#[test]
fn init_refuses_to_overwrite_without_being_told_to() {
let repo = unconfigured_repo();
assert_eq!(code(&brake(repo.path(), &["init"])), 0);
fs::write(
repo.path().join("brake.toml"),
"# hand-written, do not lose\n",
)
.expect("write");
let output = brake(repo.path(), &["init"]);
assert_eq!(code(&output), 2);
assert!(String::from_utf8_lossy(&output.stderr).contains("--force"));
assert_eq!(
fs::read_to_string(repo.path().join("brake.toml")).expect("read"),
"# hand-written, do not lose\n",
"suppressions and their reasons are the most expensive thing in that file"
);
assert_eq!(code(&brake(repo.path(), &["init", "--force"])), 0);
}
#[test]
fn init_dry_run_writes_nothing() {
let repo = unconfigured_repo();
let output = brake(repo.path(), &["init", "--dry-run"]);
assert_eq!(code(&output), 0);
assert!(
stdout(&output).contains("[[contract]]"),
"{}",
stdout(&output)
);
assert!(!repo.path().join("brake.toml").exists());
}
#[test]
fn an_ordinary_ci_file_does_not_trip_the_contract_notice() {
let repo = unconfigured_repo();
assert_eq!(code(&brake(repo.path(), &["init"])), 0);
let output = brake(
repo.path(),
&[
"check",
".github/workflows/api-tests.yaml",
"package.json",
"docker-compose.yml",
"--format",
"text",
],
);
assert_eq!(code(&output), 0);
assert!(
!stdout(&output).contains("contract-unconfigured"),
"brake called ordinary files APIs:\n{}",
stdout(&output)
);
}
#[test]
fn a_genuinely_undeclared_contract_is_still_pointed_out() {
let repo = unconfigured_repo();
assert_eq!(code(&brake(repo.path(), &["init"])), 0);
fs::write(repo.path().join("api/ledger-openapi.yaml"), SPEC).expect("write");
let output = brake(
repo.path(),
&["check", "api/ledger-openapi.yaml", "--format", "text"],
);
assert_eq!(code(&output), 0, "a note must not block a commit");
let text = stdout(&output);
assert!(text.contains("contract-unconfigured"), "{text}");
assert!(text.contains("file: `api/ledger-openapi.yaml`"), "{text}");
}
#[test]
fn a_field_level_finding_points_at_the_field_not_the_response() {
let spec = |fields: &str| {
format!(
"openapi: 3.1.0\npaths:\n /payments/{{id}}:\n get:\n\
\x20 operationId: getPayment\n responses:\n \"200\":\n\
\x20 description: ok\n content:\n\
\x20 application/json:\n schema:\n\
\x20 type: object\n required: [id]\n\
\x20 properties:\n{fields}"
)
};
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"c\"\nformat=\"openapi\"\nsource=\"api/c.yaml\"\n\
baseline={file=\"api/c.baseline.yaml\"}\n",
),
(
"api/c.baseline.yaml",
&spec(
" id: { type: string }\n customer_id: { type: string }\n",
),
),
(
"api/c.yaml",
&spec(" id: { type: string }\n"),
),
]);
let output = brake(repo.path(), &["check", "--format", "json"]);
assert_eq!(code(&output), 1);
let parsed: serde_json::Value = serde_json::from_str(&stdout(&output)).expect("json");
let finding = &parsed["findings"][0];
assert_eq!(finding["rule"], "response-field-removed");
let line = finding["line"].as_u64().expect("line");
assert_eq!(
line,
16,
"expected the line declaring `customer_id`, got {line}:\n{}",
stdout(&output)
);
let text = stdout(&brake(repo.path(), &["check", "--format", "text"]));
assert!(
text.contains("customer_id: { type: string }"),
"the diagnostic should quote the field's own line:\n{text}"
);
}
#[test]
fn a_finding_with_no_field_span_still_locates_the_payload() {
let proto = |payment: &str| {
format!(
"syntax = \"proto3\";\npackage pay;\nmessage Req {{ string id = 1; }}\n\
message Payment {{ {payment} }}\n\
service S {{ rpc Get(Req) returns (Payment); }}\n"
)
};
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"p\"\nformat=\"proto\"\nsource=\"api/p.proto\"\n\
baseline={file=\"api/p.baseline.proto\"}\n",
),
(
"api/p.baseline.proto",
&proto("string id = 1; string note = 2;"),
),
("api/p.proto", &proto("string id = 1;")),
]);
let output = brake(repo.path(), &["check", "--format", "json"]);
assert_eq!(code(&output), 1);
let parsed: serde_json::Value = serde_json::from_str(&stdout(&output)).expect("json");
let finding = &parsed["findings"][0];
assert!(
finding["file"]
.as_str()
.is_some_and(|f| f.ends_with(".proto"))
);
assert!(
finding["line"].as_u64().is_some_and(|line| line > 0),
"a fallback span must still be a real location: {finding}"
);
}
#[test]
fn github_actions_and_gitlab_formats_render_properly() {
let repo = two_contracts();
let gh = stdout(&brake(repo.path(), &["check", "--format", "github"]));
assert!(gh.contains("::error"));
assert!(gh.contains("title=endpoint-removed"));
let gl = stdout(&brake(repo.path(), &["check", "--format", "gitlab"]));
let parsed: serde_json::Value = serde_json::from_str(&gl).expect("valid gitlab json array");
assert!(parsed.is_array());
assert!(!parsed.as_array().unwrap().is_empty());
assert_eq!(parsed[0]["check_name"], "endpoint-removed");
}
#[test]
fn analyze_and_diff_render_with_github_and_gitlab_formats() {
let repo = two_contracts();
let analyze_gl = stdout(&brake(repo.path(), &["analyze", ".", "--format", "gitlab"]));
let parsed_analyze: serde_json::Value =
serde_json::from_str(&analyze_gl).expect("valid gitlab json from analyze");
assert!(parsed_analyze.is_array());
let analyze_gh = stdout(&brake(repo.path(), &["analyze", ".", "--format", "github"]));
assert!(analyze_gh.contains("::error"));
let diff_gh = stdout(&brake(repo.path(), &["diff", "--format", "github"]));
assert!(diff_gh.contains("endpoint-removed"));
let diff_gl = stdout(&brake(repo.path(), &["diff", "--format", "gitlab"]));
let parsed_diff: serde_json::Value =
serde_json::from_str(&diff_gl).expect("valid gitlab json from diff");
assert!(parsed_diff.is_array());
}
#[test]
fn protobuf_reserved_fields_prevent_silent_reuse_in_cli() {
let proto_base = r#"
syntax = "proto3";
package pay;
message Payment {
string id = 1;
}
service S { rpc Get(Payment) returns (Payment); }
"#;
let proto_with_collision = r#"
syntax = "proto3";
package pay;
message Payment {
reserved 1, 4 to 8;
reserved "bad_field";
string id = 1;
}
service S { rpc Get(Payment) returns (Payment); }
"#;
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"p\"\nformat=\"proto\"\nsource=\"api/p.proto\"\n\
baseline={file=\"api/p.baseline.proto\"}\n",
),
("api/p.baseline.proto", proto_base),
("api/p.proto", proto_with_collision),
]);
let output = brake(
repo.path(),
&["analyze", ".", "--fail-on", "warning", "--format", "json"],
);
assert_eq!(
code(&output),
1,
"contract with reserved collision must fail when gating on warnings"
);
let parsed: serde_json::Value = serde_json::from_str(&stdout(&output)).expect("valid json");
assert_eq!(parsed["findings"][0]["rule"], "contract-partial");
assert!(
parsed["findings"][0]["message"]
.as_str()
.unwrap()
.contains("reuses reserved number range")
);
}
#[test]
fn multi_document_openapi_references_resolve_in_cli() {
let base_root = r#"
openapi: 3.1.0
paths:
/users:
get:
responses:
"200":
description: ok
content:
application/json:
schema:
$ref: "./models/user_base.yaml#/User"
"#;
let head_root = r#"
openapi: 3.1.0
paths:
/users:
get:
responses:
"200":
description: ok
content:
application/json:
schema:
$ref: "./models/user.yaml#/User"
"#;
let base_user = r#"
User:
type: object
required: [id, email]
properties:
id:
type: string
email:
type: string
"#;
let head_user_broken = r#"
User:
type: object
required: [id]
properties:
id:
type: string
"#;
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"users\"\nformat=\"openapi\"\nsource=\"api/users.yaml\"\n\
baseline={file=\"api/users.baseline.yaml\"}\n",
),
("api/users.baseline.yaml", base_root),
("api/users.yaml", head_root),
("api/models/user_base.yaml", base_user),
("api/models/user.yaml", head_user_broken),
]);
let output = brake(repo.path(), &["check", "--format", "json"]);
assert_eq!(code(&output), 1);
let parsed: serde_json::Value = serde_json::from_str(&stdout(&output)).expect("valid json");
assert_eq!(parsed["findings"][0]["rule"], "response-field-removed");
assert_eq!(parsed["findings"][0]["subject"], "email");
}
#[test]
fn asyncapi_breaking_changes_detected_in_cli() {
let base_asyncapi = r#"
asyncapi: '2.6.0'
info:
title: Order Events
version: 1.0.0
channels:
orders/created:
publish:
message:
payload:
type: object
required: [orderId, customerId]
properties:
orderId:
type: string
customerId:
type: string
"#;
let head_asyncapi = r#"
asyncapi: '2.6.0'
info:
title: Order Events
version: 1.0.0
channels:
orders/created:
publish:
message:
payload:
type: object
required: [orderId]
properties:
orderId:
type: string
"#;
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"orders\"\nformat=\"asyncapi\"\nsource=\"api/orders.yaml\"\n\
baseline={file=\"api/orders.baseline.yaml\"}\n",
),
("api/orders.baseline.yaml", base_asyncapi),
("api/orders.yaml", head_asyncapi),
]);
let output = brake(repo.path(), &["check", "--format", "json"]);
assert_eq!(code(&output), 1);
let parsed: serde_json::Value = serde_json::from_str(&stdout(&output)).expect("valid json");
assert_eq!(parsed["findings"][0]["rule"], "response-field-removed");
assert_eq!(parsed["findings"][0]["subject"], "customerId");
}
#[test]
fn openapi_31_discriminator_and_tuple_changes_detected_in_cli() {
let base_spec = r#"
openapi: 3.1.0
paths:
/geo:
get:
responses:
"200":
description: ok
content:
application/json:
schema:
type: array
prefixItems:
- type: number
- type: number
"#;
let head_spec = r#"
openapi: 3.1.0
paths:
/geo:
get:
responses:
"200":
description: ok
content:
application/json:
schema:
type: array
prefixItems:
- type: number
"#;
let repo = repo(&[
(
"brake.toml",
"[[contract]]\nname=\"geo\"\nformat=\"openapi\"\nsource=\"api/geo.yaml\"\n\
baseline={file=\"api/geo.baseline.yaml\"}\n",
),
("api/geo.baseline.yaml", base_spec),
("api/geo.yaml", head_spec),
]);
let output = brake(repo.path(), &["check", "--format", "json"]);
assert_eq!(code(&output), 1);
let parsed: serde_json::Value = serde_json::from_str(&stdout(&output)).expect("valid json");
assert_eq!(parsed["findings"][0]["rule"], "response-type-changed");
assert!(
parsed["findings"][0]["message"]
.as_str()
.unwrap()
.contains("tuple prefixItems")
);
}
#[test]
fn compare_needs_no_configuration_and_names_the_files_it_was_given() {
let dir = repo(&[("old.yaml", SPEC), ("new.yaml", BROKEN)]);
let broken = brake(
dir.path(),
&["compare", "-f", "json", "old.yaml", "new.yaml"],
);
assert_eq!(code(&broken), 1, "{}", stdout(&broken));
let report: serde_json::Value = serde_json::from_str(&stdout(&broken)).expect("json");
let files: Vec<&str> = report["findings"]
.as_array()
.expect("findings")
.iter()
.filter_map(|finding| finding["file"].as_str())
.collect();
assert!(!files.is_empty(), "{report}");
assert!(
files
.iter()
.all(|file| *file == "old.yaml" || *file == "new.yaml"),
"a CI annotation must point at a file the caller named: {files:?}"
);
let clean = brake(
dir.path(),
&["compare", "-f", "json", "old.yaml", "old.yaml"],
);
assert_eq!(code(&clean), 0, "{}", stdout(&clean));
}
#[test]
fn compare_refuses_a_head_it_cannot_identify() {
let dir = repo(&[("old.yaml", SPEC), ("notes.txt", "not a contract\n")]);
let output = brake(dir.path(), &["compare", "old.yaml", "notes.txt"]);
assert_eq!(code(&output), 2);
assert!(
String::from_utf8_lossy(&output.stderr).contains("--contract-format"),
"the error must say how to proceed"
);
}