mit-commit-message-lints 6.0.12

Check the correctness of a specific commit message. Designed to be used in tools providing commit-msg style hooks
Documentation
use std::collections::BTreeMap;

use crate::{
    external::InMemory,
    mit::{cmd::set_config_authors::set_config_authors, Author},
};

#[test]
fn can_set_an_author() {
    let mut store: BTreeMap<String, String> = BTreeMap::new();
    let mut vcs = InMemory::new(&mut store);

    set_config_authors(
        &mut vcs,
        "zy",
        &Author::new("Z Y".into(), "zy@example.com".into(), None),
    )
    .expect("command to have succeeded");

    let mut expected: BTreeMap<String, String> = BTreeMap::new();
    expected.insert("mit.author.config.zy.email".into(), "zy@example.com".into());
    expected.insert("mit.author.config.zy.name".into(), "Z Y".into());

    assert_eq!(store, expected);
}

#[test]
fn can_set_an_author_with_signing_key() {
    let mut store: BTreeMap<String, String> = BTreeMap::new();
    let mut vcs = InMemory::new(&mut store);

    set_config_authors(
        &mut vcs,
        "bt",
        &Author::new(
            "Billie Thompson".into(),
            "billie@example.com".into(),
            Some("ABC".into()),
        ),
    )
    .expect("Should succeed");

    let mut expected: BTreeMap<String, String> = BTreeMap::new();
    expected.insert("mit.author.config.bt.name".into(), "Billie Thompson".into());
    expected.insert(
        "mit.author.config.bt.email".into(),
        "billie@example.com".into(),
    );
    expected.insert("mit.author.config.bt.signingkey".into(), "ABC".into());

    assert_eq!(store, expected);
}

#[test]
fn updating_author_without_signing_key_removes_old_signing_key() {
    // First, set an author WITH a signing key
    let mut store: BTreeMap<String, String> = BTreeMap::new();
    {
        let mut vcs = InMemory::new(&mut store);

        set_config_authors(
            &mut vcs,
            "bt",
            &Author::new(
                "Billie Thompson".into(),
                "billie@example.com".into(),
                Some("ABC".into()),
            ),
        )
        .expect("Should succeed");
    }

    assert!(
        store.contains_key("mit.author.config.bt.signingkey"),
        "Signing key should be present after first set"
    );

    // Now update the same author WITHOUT a signing key
    {
        let mut vcs = InMemory::new(&mut store);

        set_config_authors(
            &mut vcs,
            "bt",
            &Author::new(
                "Billie Thompson".into(),
                "billie@newdomain.com".into(),
                None,
            ),
        )
        .expect("Should succeed");
    }

    let mut expected: BTreeMap<String, String> = BTreeMap::new();
    expected.insert("mit.author.config.bt.name".into(), "Billie Thompson".into());
    expected.insert(
        "mit.author.config.bt.email".into(),
        "billie@newdomain.com".into(),
    );
    // signingkey should NOT be present

    assert_eq!(
        store, expected,
        "Updating an author without a signing key should remove the old signing key entry"
    );
}