aboutsummaryrefslogtreecommitdiff
path: root/src/command/add_remote.rs
blob: a40b18494f5d105742131261f1e9e12c845291ec (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use crate::configuration::Configuration;
use crate::remote::add;
use std::io::{Error, ErrorKind::Other, Result};

pub struct AddRemote;

impl AddRemote {
    pub fn new() -> Self {
        AddRemote
    }
}

impl super::Command for AddRemote {
    fn before_dependencies(&self) -> Vec<Box<dyn super::Command>> {
        vec![]
    }

    fn execute(
        &self,
        input: Option<&String>,
        configuration: &Configuration,
        _: &str,
    ) -> Result<()> {
        let input = input
            .ok_or_else(|| Error::new(Other, "You must provide a location for the remote."))?;
        add(
            &configuration.config_directory,
            &configuration.remote_config,
            input,
        )
    }

    fn after_dependencies(&self) -> Vec<Box<dyn super::Command>> {
        vec![]
    }

    fn command(&self) -> &'static str {
        "add-remote"
    }

    fn help(&self) -> &'static str {
        "<git_url>\t\tAdds or updates a git remote to sync with"
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use crate::command::Command;
    use crate::configuration::Configuration;

    use test_utilities::*;

    #[test]
    fn test_add_remote_command() {
        let add_remote = AddRemote::new();

        // Create all directories
        let test_dir = setup_test_dir();
        let remote_config = test_dir.join("blogremote");
        assert!(!remote_config.exists());

        let mut configuration = Configuration::new();
        configuration.config_directory = test_dir.clone();
        configuration.remote_config = remote_config.clone();
        add_remote
            .execute(Some(&"beep".to_string()), &configuration, "add_remote")
            .expect("Could not call add_remote");
        assert_file_contents(&remote_config, "beep");
        cleanup_test_dir(&test_dir);
    }

    #[test]
    fn test_fails_if_no_remote_sent() {
        let add_remote = AddRemote::new();
        let configuration = Configuration::new();
        let result = add_remote.execute(None, &configuration, "add_remote");
        assert!(result.is_err());
    }

    #[test]
    fn add_remote_before_dependencies() {
        let add_remote = AddRemote::new();
        let dependencies = add_remote.before_dependencies();

        assert_eq!(dependencies.len(), 0);
    }

    #[test]
    fn add_remote_after_dependencies() {
        let add_remote = AddRemote::new();
        let dependencies = add_remote.after_dependencies();

        assert_eq!(dependencies.len(), 0);
    }

    // These two tests feel pointless but I'm doing it for the coverage :p

    #[test]
    fn add_remote_command_output() {
        let add_remote = AddRemote::new();
        add_remote.command();
    }

    #[test]
    fn add_remote_help_output() {
        let add_remote = AddRemote::new();
        add_remote.help();
    }
}