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