verctl-core 0.9.7

Core configuration, manifest, fragment, changelog, and version domain logic for verctl
Documentation
use std::fs;
use std::os::unix::fs::symlink;
use std::path::Path;

use indoc::{formatdoc, indoc};
use tempfile::TempDir;

use super::{cargo_opts_out, opt_out};

#[test]
fn a_private_package_json_opts_out() {
    let dir = TempDir::new().expect("tempdir");
    let manifest = dir.path().join("package.json");
    fs::write(&manifest, r#"{"name": "web", "private": true}"#).expect("write");
    assert_eq!(
        opt_out(&manifest, dir.path()).expect("json"),
        Some(r#"package.json sets "private": true"#)
    );
    fs::write(&manifest, r#"{"name": "web"}"#).expect("write");
    assert_eq!(opt_out(&manifest, dir.path()).expect("json"), None);
    assert_eq!(
        opt_out(&dir.path().join("missing/Cargo.toml"), dir.path()).expect("missing"),
        None
    );
}

#[test]
fn publish_false_and_an_empty_list_opt_out() {
    let opts_out = |text: &str| {
        cargo_opts_out(Path::new("Cargo.toml"), text, Path::new("."))
            .expect("toml")
            .is_some()
    };
    assert!(opts_out(indoc! {r#"
        [package]
        name = "a"
        publish = false
    "#}));
    assert!(opts_out(indoc! {r#"
        [package]
        name = "a"
        publish = []
    "#}));
    assert!(!opts_out(indoc! {r#"
        [package]
        name = "a"
        publish = ["crates-io"]
    "#}));
    assert!(opts_out(indoc! {r#"
        [package]
        name = "a"
        publish.workspace = true

        [workspace.package]
        publish = false
    "#}));
}

/// A member that inherits `publish` reads it from the workspace root,
/// as Cargo does.
#[test]
fn an_inherited_publish_reads_the_workspace_root() {
    let dir = TempDir::new().expect("tempdir");
    let member = dir.path().join("crates/a/Cargo.toml");
    fs::create_dir_all(member.parent().expect("parent")).expect("dir");
    let root = |line: &str| {
        fs::write(
            dir.path().join("Cargo.toml"),
            formatdoc! {r#"
                [workspace]
                members = ["crates/a"]

                [workspace.package]
                {line}
            "#},
        )
        .expect("write");
    };
    let text = indoc! {r#"
        [package]
        name = "a"
        publish.workspace = true
    "#};
    root("publish = false");
    assert!(
        cargo_opts_out(&member, text, dir.path())
            .expect("toml")
            .is_some()
    );
    root(r#"edition = "2024""#);
    assert!(
        cargo_opts_out(&member, text, dir.path())
            .expect("toml")
            .is_none()
    );
}

/// A manifest above the tree is never read: a workspace there does not
/// count, and a malformed one does not fail the check.
#[test]
fn the_workspace_search_stops_at_the_tree() {
    let dir = TempDir::new().expect("tempdir");
    let tree = dir.path().join("repo");
    let member = tree.join("crates/a/Cargo.toml");
    fs::create_dir_all(member.parent().expect("parent")).expect("dir");
    let text = indoc! {r#"
        [package]
        name = "a"
        publish.workspace = true
    "#};
    let above = dir.path().join("Cargo.toml");
    fs::write(
        &above,
        indoc! {"
            [workspace]

            [workspace.package]
            publish = false
        "},
    )
    .expect("write");
    assert!(
        cargo_opts_out(&member, text, &tree)
            .expect("toml")
            .is_none()
    );
    fs::write(&above, "[workspace").expect("write");
    assert!(
        cargo_opts_out(&member, text, &tree)
            .expect("toml")
            .is_none()
    );
}

/// A symlinked `Cargo.toml` above the package is never followed, so a
/// workspace outside the tree does not count, as in the merge-base copy.
#[test]
fn a_symlinked_workspace_manifest_is_skipped() {
    let dir = TempDir::new().expect("tempdir");
    let tree = dir.path().join("repo");
    let member = tree.join("crates/a/Cargo.toml");
    fs::create_dir_all(member.parent().expect("parent")).expect("dir");
    let outside = dir.path().join("outside.toml");
    fs::write(
        &outside,
        indoc! {"
            [workspace]

            [workspace.package]
            publish = false
        "},
    )
    .expect("write");
    symlink(&outside, tree.join("Cargo.toml")).expect("symlink");
    let text = indoc! {r#"
        [package]
        name = "a"
        publish.workspace = true
    "#};
    assert!(
        cargo_opts_out(&member, text, &tree)
            .expect("toml")
            .is_none()
    );
    fs::write(&outside, "[workspace").expect("write");
    assert!(
        cargo_opts_out(&member, text, &tree)
            .expect("toml")
            .is_none()
    );
}

/// A workspace reached through a directory symlink does not count. That
/// applies whether `package.workspace` names the link or the package itself
/// sits below one. Skipping it matches the merge-base copy, which contains
/// no links.
#[test]
fn a_workspace_behind_a_directory_symlink_is_skipped() {
    let dir = TempDir::new().expect("tempdir");
    let tree = dir.path().join("repo");
    let real = tree.join("real");
    fs::create_dir_all(real.join("a")).expect("dir");
    fs::create_dir_all(tree.join("crates/a")).expect("dir");
    fs::write(
        real.join("Cargo.toml"),
        indoc! {"
            [workspace]

            [workspace.package]
            publish = false
        "},
    )
    .expect("write");
    symlink("real", tree.join("linked")).expect("symlink");
    symlink("../real/a", tree.join("crates/b")).expect("symlink");
    let named = |workspace: &str| {
        formatdoc! {r#"
            [package]
            name = "a"
            workspace = "{workspace}"
            publish.workspace = true
        "#}
    };
    let member = tree.join("crates/a/Cargo.toml");
    assert!(
        cargo_opts_out(&member, &named("../../real"), &tree)
            .expect("toml")
            .is_some()
    );
    assert!(
        cargo_opts_out(&member, &named("../../linked"), &tree)
            .expect("toml")
            .is_none()
    );
    let text = indoc! {r#"
        [package]
        name = "a"
        publish.workspace = true
    "#};
    assert!(
        cargo_opts_out(&real.join("a/Cargo.toml"), text, &tree)
            .expect("toml")
            .is_some()
    );
    assert!(
        cargo_opts_out(&tree.join("crates/b/Cargo.toml"), text, &tree)
            .expect("toml")
            .is_none()
    );
}