verctl-core 0.9.7

Core configuration, manifest, fragment, changelog, and version domain logic for verctl
Documentation
use super::{RegistryToken, SECRET, TRUSTED};

#[test]
fn both_tokens_fail_and_name_both_variables() {
    let error = RegistryToken::resolve(
        Some("from-secret".into()),
        Some("from-trusted".into()),
        "CARGO_REGISTRY_TOKEN",
    )
    .expect_err("both set");
    let message = format!("{error:#}");
    assert!(message.contains(SECRET), "{message}");
    assert!(message.contains(TRUSTED), "{message}");
    assert!(!message.contains("from-"), "{message}");
}

#[test]
fn either_token_goes_under_the_named_variable() {
    for (secret, trusted) in [(Some("t0ken".into()), None), (None, Some("t0ken".into()))] {
        let token = RegistryToken::resolve(secret, trusted, "CARGO_REGISTRIES_DEMO_TOKEN")
            .expect("one token")
            .expect("set");
        assert_eq!(token.env(), ("CARGO_REGISTRIES_DEMO_TOKEN", "t0ken"));
    }
}

#[test]
fn no_token_is_none() {
    assert_eq!(
        RegistryToken::resolve(None, None, "CARGO_REGISTRY_TOKEN").expect("none"),
        None
    );
}

/// Under `--new` both tokens resolve: a crate that is created gets the
/// registry token, and every other upload the trusted one.
#[test]
fn new_gives_each_upload_its_token() {
    let token = RegistryToken::resolve_new(
        Some("secr3t".into()),
        Some("trust3d".into()),
        "CARGO_REGISTRY_TOKEN",
    )
    .expect("both");
    assert!(token.has_trusted_publishing());
    assert_eq!(token.creation_env(), ("CARGO_REGISTRY_TOKEN", "secr3t"));
    assert_eq!(token.env(), ("CARGO_REGISTRY_TOKEN", "trust3d"));
    let alone = RegistryToken::resolve_new(Some("secr3t".into()), None, "NODE_AUTH_TOKEN")
        .expect("registry token alone");
    assert!(!alone.has_trusted_publishing());
    assert_eq!(alone.creation_env(), ("NODE_AUTH_TOKEN", "secr3t"));
    assert_eq!(alone.env(), ("NODE_AUTH_TOKEN", "secr3t"));
}

/// Without `--new`, both tokens still fail.
#[test]
fn both_tokens_still_fail_without_new() {
    RegistryToken::resolve(Some("secr3t".into()), Some("trust3d".into()), "X").unwrap_err();
}

/// `--new` without the registry token fails naming the variable and the
/// workflow secret, whether or not a trusted token is set, and never
/// names a token's value.
#[test]
fn new_needs_the_registry_token() {
    for trusted in [None, Some("trust3d".to_owned())] {
        let error = RegistryToken::resolve_new(None, trusted, "CARGO_REGISTRY_TOKEN")
            .expect_err("no registry token");
        let message = format!("{error:#}");
        assert!(message.contains(SECRET), "{message}");
        assert!(message.contains("`registry-token`"), "{message}");
        assert!(!message.contains("trust3d"), "{message}");
    }
}

#[test]
fn new_debug_hides_both_tokens() {
    let token = RegistryToken::resolve_new(
        Some("secr3t".into()),
        Some("trust3d".into()),
        "CARGO_REGISTRY_TOKEN",
    )
    .expect("both");
    let shown = format!("{token:?}");
    assert!(!shown.contains("secr3t"), "{shown}");
    assert!(!shown.contains("trust3d"), "{shown}");
}

#[test]
fn debug_hides_the_token() {
    let token = RegistryToken::resolve(Some("t0ken".into()), None, "CARGO_REGISTRY_TOKEN")
        .expect("one token")
        .expect("set");
    let shown = format!("{token:?}");
    assert!(shown.contains("CARGO_REGISTRY_TOKEN"), "{shown}");
    assert!(!shown.contains("t0ken"), "{shown}");
}