use std::fs;
use std::path::PathBuf;
fn repo_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("rs/ has a parent")
.to_path_buf()
}
fn package_version() -> String {
let raw = fs::read_to_string(repo_root().join("ts/package.json"))
.expect("ts/package.json is readable, or VERSION cannot be checked");
let parsed: serde_json::Value =
serde_json::from_str(&raw).expect("ts/package.json is readable JSON");
parsed["version"]
.as_str()
.expect("ts/package.json has a version field")
.to_string()
}
#[test]
fn version_matches_package_json() {
assert_eq!(
tabnas_c::VERSION,
package_version(),
"VERSION drift: the crate says {} and ts/package.json says {}. \
Both are rewritten at release; if you bumped one by hand, bump \
the other.",
tabnas_c::VERSION,
package_version()
);
}
#[test]
fn cargo_manifest_matches_version() {
let manifest = fs::read_to_string(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("Cargo.toml"))
.expect("rs/Cargo.toml is readable");
let declared = manifest
.lines()
.find_map(|line| line.strip_prefix("version = "))
.map(|rest| rest.trim().trim_matches('"').to_string())
.expect("rs/Cargo.toml declares a version");
assert_eq!(
declared,
tabnas_c::VERSION,
"rs/Cargo.toml says {declared} and the crate's VERSION says {}",
tabnas_c::VERSION
);
}
#[test]
fn go_constant_matches_version() {
let source = fs::read_to_string(repo_root().join("go/c.go")).expect("go/c.go is readable");
let declared = source
.lines()
.find_map(|line| line.trim().strip_prefix("const VERSION = "))
.map(|rest| rest.trim().trim_matches('"').to_string())
.expect("go/c.go declares const VERSION");
assert_eq!(declared, tabnas_c::VERSION, "go/c.go VERSION drift");
}