pkgs/runner/
rw.rs

1use std::path::PathBuf;
2
3use super::{Runner, RunnerError};
4use crate::config::Config;
5use crate::logger::LoggerOutput;
6use crate::meta::{PKGS_DIR, TOML_CONFIG_FILE};
7
8impl<O: LoggerOutput> Runner<O> {
9    pub fn read_config(&self) -> Result<Config, RunnerError> {
10        let toml_path = self.cwd.join(TOML_CONFIG_FILE);
11        if !toml_path.exists() {
12            return Err(RunnerError::ConfigNotFound);
13        }
14
15        let config = Config::read(&self.cwd.join(TOML_CONFIG_FILE))?;
16        Ok(config)
17    }
18
19    pub fn create_pkgs_dir(&mut self) -> Result<PathBuf, RunnerError> {
20        let pkgs_dir = self.cwd.join(PKGS_DIR).to_path_buf();
21        if !pkgs_dir.exists() {
22            self.create_dir(&pkgs_dir)?;
23            return Ok(pkgs_dir);
24        }
25        if !pkgs_dir.is_dir() {
26            return Err(RunnerError::PkgsDirNotADir);
27        }
28        Ok(pkgs_dir)
29    }
30
31    pub fn get_pkgs_dir(&self) -> Result<PathBuf, RunnerError> {
32        let pkgs_dir = self.cwd.join(PKGS_DIR).to_path_buf();
33        if !pkgs_dir.exists() {
34            return Err(RunnerError::PkgsDirNotFound);
35        }
36        if !pkgs_dir.is_dir() {
37            return Err(RunnerError::PkgsDirNotADir);
38        }
39        Ok(pkgs_dir)
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use indoc::indoc;
46
47    use super::*;
48    use crate::test_utils::prelude::*;
49
50    mod read_config {
51        use super::*;
52        use crate::config::PackageType;
53
54        #[gtest]
55        fn read_toml() -> Result<()> {
56            let td = TempDir::new()?.file(
57                TOML_CONFIG_FILE,
58                indoc! {r#"
59                    [packages.test.maps]
60                    src_file = "dst_file"
61                "#},
62            )?;
63            let runner = common_runner(td.path());
64            let config = runner.read_config()?;
65
66            expect_eq!(config.packages.len(), 1);
67
68            let test_pkg = &config.packages["test"];
69            expect_eq!(test_pkg.kind, PackageType::Local);
70            expect_eq!(test_pkg.maps["src_file"], "dst_file");
71
72            Ok(())
73        }
74
75        #[gtest]
76        fn config_not_found() -> Result<()> {
77            let td = TempDir::new()?;
78            let runner = common_runner(td.path());
79            let error = runner.read_config().unwrap_err();
80
81            expect_that!(error, pat!(RunnerError::ConfigNotFound));
82
83            Ok(())
84        }
85
86        #[gtest]
87        fn wrong_config_file_format() -> Result<()> {
88            let td = TempDir::new()?.file(TOML_CONFIG_FILE, "invalid file content")?;
89            let runner = common_runner(td.path());
90            let error = runner.read_config().unwrap_err();
91
92            expect_that!(error, pat!(RunnerError::ConfigReadError(_)));
93
94            Ok(())
95        }
96    }
97
98    mod create_pkgs_dir {
99
100        use super::*;
101
102        #[gtest]
103        fn create_if_not_exist() -> Result<()> {
104            let td = TempDir::new()?;
105            let mut runner = common_runner(td.path());
106            let pkgs_dir = runner.create_pkgs_dir()?;
107
108            expect_eq!(pkgs_dir, td.join(PKGS_DIR));
109            expect_pred!(pkgs_dir.is_dir());
110            expect_eq!(
111                runner.messages()[0],
112                LogMessage::CreateDir(td.join(PKGS_DIR))
113            );
114
115            Ok(())
116        }
117
118        #[gtest]
119        fn do_nothing_if_exist() -> Result<()> {
120            let td = TempDir::new()?.dir(PKGS_DIR)?;
121            let mut runner = common_runner(td.path());
122            let pkgs_dir = runner.create_pkgs_dir()?;
123
124            expect_eq!(pkgs_dir, td.join(PKGS_DIR));
125            expect_pred!(pkgs_dir.is_dir());
126
127            Ok(())
128        }
129
130        #[gtest]
131        fn error_if_pkgs_not_a_dir() -> Result<()> {
132            let td = TempDir::new()?.file(PKGS_DIR, "")?;
133            let mut runner = common_runner(td.path());
134            let err = runner.create_pkgs_dir().unwrap_err();
135
136            expect_that!(err, pat!(RunnerError::PkgsDirNotADir));
137
138            Ok(())
139        }
140    }
141
142    mod get_pkgs_dir {
143        use super::*;
144
145        #[gtest]
146        fn it_works() -> Result<()> {
147            let td = TempDir::new()?.dir(PKGS_DIR)?;
148            let runner = common_runner(td.path());
149            let pkgs_dir = runner.get_pkgs_dir()?;
150
151            expect_eq!(pkgs_dir, td.join(PKGS_DIR));
152
153            Ok(())
154        }
155
156        #[gtest]
157        fn not_found() -> Result<()> {
158            let td = TempDir::new()?;
159            let runner = common_runner(td.path());
160            let err = runner.get_pkgs_dir().unwrap_err();
161
162            expect_that!(err, pat!(RunnerError::PkgsDirNotFound));
163
164            Ok(())
165        }
166
167        #[gtest]
168        fn not_a_dir() -> Result<()> {
169            let td = TempDir::new()?.file(PKGS_DIR, "")?;
170            let runner = common_runner(td.path());
171            let err = runner.get_pkgs_dir().unwrap_err();
172
173            expect_that!(err, pat!(RunnerError::PkgsDirNotADir));
174
175            Ok(())
176        }
177    }
178}