1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::external::Vcs;
use crate::mit::{Author, VcsError};

/// # Errors
///
/// On write failure
pub fn set_config_authors(
    store: &mut dyn Vcs,
    initial: &str,
    author: &Author,
) -> Result<(), VcsError> {
    store.set_str(
        &format!("mit.author.config.{}.email", initial),
        &author.email(),
    )?;
    store.set_str(
        &format!("mit.author.config.{}.name", initial),
        &author.name(),
    )?;

    if let Some(signingkey) = author.signingkey() {
        store.set_str(
            &format!("mit.author.config.{}.signingkey", initial),
            &signingkey,
        )?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use crate::external::InMemory;
    use crate::mit::cmd::set_config_authors::set_config_authors;
    use crate::mit::Author;
    use std::collections::BTreeMap;

    #[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", "zy@example.com", None))
            .expect("command to have succeded");

        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", "billie@example.com", Some("ABC")),
        )
        .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);
    }
}