john-slop/tests/workspace_contract.rs

152 lines
4.8 KiB
Rust

use std::{fs, path::Path, process::Command};
use toml::Value;
const PACKAGE_MANIFESTS: [&str; 3] = [
"Cargo.toml",
"crates/john-slop-core/Cargo.toml",
"crates/john-slop-lsp/Cargo.toml",
];
const INHERITED_PACKAGE_FIELDS: [&str; 5] = [
"version",
"edition",
"rust-version",
"license",
"repository",
];
fn manifest(path: impl AsRef<Path>) -> Value {
let path = path.as_ref();
let source = fs::read_to_string(path)
.unwrap_or_else(|error| panic!("failed to read {}: {error}", path.display()));
toml::from_str(&source)
.unwrap_or_else(|error| panic!("failed to parse {}: {error}", path.display()))
}
fn string_array<'a>(value: &'a Value, label: &str) -> Vec<&'a str> {
value
.as_array()
.unwrap_or_else(|| panic!("{label} must be an array"))
.iter()
.map(|entry| {
entry
.as_str()
.unwrap_or_else(|| panic!("{label} entries must be strings"))
})
.collect()
}
#[test]
fn workspace_members_and_resolved_versions_are_synchronized() {
let root = manifest("Cargo.toml");
let workspace = root["workspace"]
.as_table()
.expect("workspace must be a table");
assert_eq!(
string_array(&workspace["members"], "workspace.members"),
["crates/john-slop-core", "crates/john-slop-lsp"]
);
assert_eq!(
string_array(&workspace["default-members"], "workspace.default-members"),
[".", "crates/john-slop-core", "crates/john-slop-lsp"]
);
let output = Command::new(env!("CARGO"))
.args(["metadata", "--locked", "--no-deps", "--format-version", "1"])
.output()
.expect("cargo metadata must run");
assert!(
output.status.success(),
"cargo metadata failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let metadata: serde_json::Value =
serde_json::from_slice(&output.stdout).expect("cargo metadata must emit JSON");
let packages = metadata["packages"]
.as_array()
.expect("metadata packages must be an array");
assert_eq!(packages.len(), 3);
assert!(packages.iter().all(|package| package["version"] == "0.1.0"));
}
#[test]
fn packages_inherit_metadata_and_dependency_sources() {
let root = manifest("Cargo.toml");
let workspace_dependencies = root["workspace"]["dependencies"]
.as_table()
.expect("workspace.dependencies must be a table");
for path in PACKAGE_MANIFESTS {
let package_manifest = manifest(path);
let package = package_manifest["package"]
.as_table()
.unwrap_or_else(|| panic!("{path} package must be a table"));
for field in INHERITED_PACKAGE_FIELDS {
assert_eq!(
package[field]["workspace"].as_bool(),
Some(true),
"{path} must inherit package.{field}"
);
}
for section in ["dependencies", "dev-dependencies"] {
let Some(dependencies) = package_manifest.get(section).and_then(Value::as_table) else {
continue;
};
for (name, specification) in dependencies {
assert!(
workspace_dependencies.contains_key(name),
"{path} {section}.{name} is missing from workspace.dependencies"
);
assert_eq!(
specification.get("workspace").and_then(Value::as_bool),
Some(true),
"{path} {section}.{name} must inherit its workspace requirement"
);
}
}
}
}
#[test]
fn core_and_lsp_feature_boundaries_remain_explicit() {
let root = manifest("Cargo.toml");
let core = manifest("crates/john-slop-core/Cargo.toml");
let lsp = manifest("crates/john-slop-lsp/Cargo.toml");
let core_dependencies = core["dependencies"]
.as_table()
.expect("core dependencies must be a table");
for forbidden in ["clap", "john-slop-lsp-engine", "tokio", "tower-lsp-server"] {
assert!(
!core_dependencies.contains_key(forbidden),
"john-slop-core must not depend on {forbidden}"
);
}
assert!(
root["workspace"]["dependencies"]["tokio"]
.get("features")
.is_none()
);
assert_eq!(
string_array(
&lsp["dependencies"]["tokio"]["features"],
"john-slop-lsp tokio features"
),
["sync"]
);
assert_eq!(
root["dependencies"]["john-slop-lsp-engine"]["optional"].as_bool(),
Some(true)
);
assert_eq!(
root["dependencies"]["tokio"]["optional"].as_bool(),
Some(true)
);
assert_eq!(
root["dependencies"]["tower-lsp-server"]["optional"].as_bool(),
Some(true)
);
}