Skip to main content

mit_commit_message_lints/mit/cmd/
set_config_rotation.rs

1use miette::Result;
2
3use crate::{external::Vcs, mit::lib::rotation_option::RotationOption};
4
5/// Set the rotation configuration setting
6///
7/// # Errors
8///
9/// Returns an error if writing to the git config fails.
10pub fn set_config_rotation(store: &mut dyn Vcs, rotation: RotationOption) -> Result<()> {
11    store.set_str(super::CONFIG_KEY_ROTATION, &rotation.to_string())
12}
13
14#[cfg(test)]
15mod tests {
16    use std::collections::BTreeMap;
17
18    use miette::Result;
19
20    use crate::external::InMemory;
21    use crate::mit::lib::rotation_option::RotationOption;
22
23    #[test]
24    fn set_config_rotation_writes_round_robin_and_reads_back() -> Result<()> {
25        let mut buffer = BTreeMap::new();
26        {
27            let mut vcs_config = InMemory::new(&mut buffer);
28            crate::mit::cmd::set_config_rotation::set_config_rotation(
29                &mut vcs_config,
30                RotationOption::RoundRobin,
31            )?;
32        }
33
34        assert_eq!(
35            buffer.get("mit.author.rotate"),
36            Some(&"round-robin".to_string()),
37            "Expected the rotation config to be set to 'round-robin'"
38        );
39
40        let vcs_config = InMemory::new(&mut buffer);
41        let result = crate::mit::cmd::get_config_rotation::get_config_rotation(&vcs_config);
42        assert_eq!(
43            result.unwrap(),
44            Some(RotationOption::RoundRobin),
45            "Expected to read back the round-robin rotation config after writing it"
46        );
47
48        Ok(())
49    }
50}