use std::path::{Path, PathBuf};
fn crates_dir() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("crates/eclipse-cli has a parent")
.to_path_buf()
}
fn workspace_version() -> String {
let manifest = crates_dir().parent().unwrap().join("Cargo.toml");
let text = std::fs::read_to_string(&manifest).expect("workspace Cargo.toml");
let mut in_section = false;
for line in text.lines() {
let line = line.trim();
if line.starts_with('[') {
in_section = line == "[workspace.package]";
continue;
}
if !in_section {
continue;
}
if let Some(rest) = line.strip_prefix("version") {
if let Some(value) = rest.trim_start().strip_prefix('=') {
return value.trim().trim_matches('"').to_string();
}
}
}
panic!("no version in [workspace.package]");
}
fn internal_deps() -> Vec<(String, String, String)> {
let mut out = Vec::new();
for crate_name in [
"eclipse-core",
"eclipse-web",
"eclipse-cli",
"eclipse-gateway",
] {
let manifest = crates_dir().join(crate_name).join("Cargo.toml");
let text = std::fs::read_to_string(&manifest).expect("crate manifest");
for line in text.lines() {
let line = line.trim();
if !line.contains("path = \"..") {
continue;
}
let name = line.split_whitespace().next().unwrap_or("").to_string();
out.push((crate_name.to_string(), name, line.to_string()));
}
}
out
}
#[test]
fn every_internal_dependency_carries_the_workspace_version() {
let version = workspace_version();
let deps = internal_deps();
assert!(
deps.len() >= 4,
"the four internal dependencies are the point of this test: {deps:?}"
);
for (crate_name, dep, line) in &deps {
assert!(
line.contains(&format!("version = \"{version}\"")),
"`{crate_name}` depends on `{dep}` without `version = \"{version}\"` beside its path, so \
`cargo publish -p {crate_name}` refuses to package it: {line}"
);
}
}
#[test]
fn every_crate_carries_what_a_published_package_needs() {
for crate_name in [
"eclipse-core",
"eclipse-web",
"eclipse-cli",
"eclipse-gateway",
] {
let dir = crates_dir().join(crate_name);
let manifest = std::fs::read_to_string(dir.join("Cargo.toml")).expect("crate manifest");
assert!(
dir.join("LICENSE").exists(),
"{crate_name} has no LICENSE inside the crate directory"
);
assert!(
dir.join("README.md").exists(),
"{crate_name} has no README.md, so its crates.io page would be blank"
);
for key in [
"license.workspace = true",
"readme = \"README.md\"",
"repository.workspace = true",
] {
assert!(
manifest.contains(key),
"{crate_name}'s manifest is missing `{key}`"
);
}
}
}