installation_testing/
git.rs

1pub mod vcs {
2    use std::path::Path;
3    use std::{fs::remove_dir_all, process::Command};
4
5    pub struct Git {
6        repository: String,
7        url: String,
8    }
9
10    impl Git {
11        ///
12        /// Git constructor
13        ///
14        /// - `repo`    The repository install path
15        /// - `uri`     The repository remote url
16        ///
17        /// ```
18        /// use installation_testing::git::vcs::Git;
19        /// use std::process::Command;
20        ///
21        /// let mut cargo = Command::new("cargo");
22        ///
23        /// let test = cargo.arg("build");
24        /// let mut checker =  Git::new("https://github.com/taishingi/zuu","/tmp/zuu");
25        ///
26        /// checker.run(test).clean();
27        /// ```
28        ///
29        pub fn new(uri: &str, repo: &str) -> Git {
30            Self {
31                repository: repo.to_string(),
32                url: uri.to_string(),
33            }
34            .get()
35        }
36
37        ///
38        /// # Get the remote repository
39        ///
40        fn get(self) -> Git {
41            if Path::new(self.repository.as_str()).is_dir() {
42                remove_dir_all(self.repository.as_str())
43                    .expect("failed to remove the last repository");
44            }
45            assert!(Command::new("git")
46                .arg("clone")
47                .arg(self.url.as_str())
48                .arg(self.repository.as_str())
49                .spawn()
50                .expect("Failed to clone repository")
51                .wait()
52                .expect("msg")
53                .success());
54
55            self
56        }
57
58        pub fn run(&mut self, command: &mut Command) -> &mut Git {
59            command
60                .current_dir(self.repository.as_str())
61                .spawn()
62                .expect("failed")
63                .wait()
64                .expect("");
65            self
66        }
67
68        ///
69        /// # remove the local repository
70        ///
71        pub fn clean(&mut self) -> bool {
72            remove_dir_all(self.repository.as_str()).expect("Failed to remove the directory");
73            true
74        }
75    }
76}