eclipse-cli 0.13.4

Command-line interface for eclipse
//! What `cargo publish` needs, asserted rather than remembered ('#'241).
//!
//! The packaging half of the issue landed in #301: a `LICENSE` inside each crate, a README per
//! crate, `repository`/`license`/`readme` in every manifest, an `msrv` job. The part that could not
//! land was the publish itself, and it needs one thing the local tree does not: a **version** beside
//! each internal `path` dependency. crates.io has no way to resolve `path`, and cargo refuses to
//! package a manifest that has one without a version.
//!
//! That pin cuts against a rule the release runbook keeps for good reasons — a pinned dependency
//! version goes stale the moment the workspace version moves, and a stale pin is how a release
//! ships a crate that requires a version which does not exist. This test is the answer: the pins
//! exist for the publish, and a bump that forgets them fails here instead of failing on crates.io
//! after the tag is already public.

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()
}

/// The version in `[workspace.package]`. Read by hand rather than with a TOML parser: this test's
/// job is to notice a disagreement, and a parser that silently defaulted would be the same bug.
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]");
}

/// Every `name = { ... path = ".." ... }` line in the four crate manifests.
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");
        // A `LICENSE` inside the crate, not only at the root: cargo copies one from the package
        // directory, so a crate carrying only `license = "Apache-2.0"` would carry no text (#241).
        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}`"
            );
        }
    }
}