Fix `cargo test` tests
[ouch.git] / src / test.rs
blobebc32f8d78cdff18cf17e1d647cbb8669114463c
1 use std::{fs, path::Path};
3 #[allow(dead_code)]
4 // ouch's command-line logic uses fs::canonicalize on its inputs so we cannot
5 // use made-up files for testing.
6 // make_dummy_file therefores creates a small temporary file to bypass fs::canonicalize errors
7 fn make_dummy_file<'a, P>(path: P) -> crate::Result<()>
8 where
9     P: AsRef<Path> + 'a,
11     fs::write(path.as_ref(), &[2, 3, 4, 5, 6, 7, 8, 9, 10])?;
12     Ok(())
15 #[allow(dead_code)]
16 fn make_dummy_files<'a, P>(paths: &[P]) -> crate::Result<()>
17 where
18     P: AsRef<Path> + 'a,
20     let _ = paths
21         .iter()
22         .map(make_dummy_file)
23         .map(Result::unwrap)
24         .collect::<Vec<_>>();
25     Ok(())
28 #[cfg(test)]
29 mod tests {
30     use super::{make_dummy_files};
31     use crate::cli;
32     use crate::cli::Command;
33     use std::{ffi::OsString, fs, path::PathBuf};
35     fn gen_args(text: &str) -> Vec<OsString> {
36         let args = text.split_whitespace();
37         args.map(OsString::from).collect()
38     }
40     macro_rules! parse {
41         ($input_text:expr) => {{
42             let args = gen_args($input_text);
43             cli::parse_args_from(args).unwrap()
44         }};
45     }
47     #[test]
48     // The absolute flags that ignore all the other argparsing rules are --help and --version
49     fn test_absolute_flags() {
50         let expected = Command::ShowHelp;
51         assert_eq!(expected, parse!("").command);
52         assert_eq!(expected, parse!("-h").command);
53         assert_eq!(expected, parse!("--help").command);
54         assert_eq!(expected, parse!("aaaaaaaa --help -o -e aaa").command);
55         assert_eq!(expected, parse!("aaaaaaaa -h").command);
56         assert_eq!(expected, parse!("--help compress aaaaaaaa").command);
57         assert_eq!(expected, parse!("compress --help").command);
58         assert_eq!(expected, parse!("--version --help").command);
59         assert_eq!(expected, parse!("aaaaaaaa -v aaaa -h").command);
61         let expected = Command::ShowVersion;
62         assert_eq!(expected, parse!("ouch --version").command);
63         assert_eq!(expected, parse!("ouch a --version b").command);
64     }
66     #[test]
67     fn test_arg_parsing_compress_subcommand() -> crate::Result<()> {
68         
69         let files = vec!["a", "b", "c"];
70         make_dummy_files(&*files)?;
71         let files= files.iter().map(fs::canonicalize).map(Result::unwrap).collect();
73         let expected = Command::Compress {
74             files,
75             compressed_output_path: "d".into(),
76         };
77         assert_eq!(expected, parse!("compress a b c d").command);
79         fs::remove_file("a")?;
80         fs::remove_file("b")?;
81         fs::remove_file("c")?;
82         Ok(())
83     }
85     #[test]
86     fn test_arg_parsing_decompress_subcommand() {
87         let files: Vec<_> = ["a", "b", "c"].iter().map(PathBuf::from).collect();
89         let expected = Command::Decompress {
90             files: files.clone(),
91             output_folder: None,
92         };
93         assert_eq!(expected, parse!("a b c").command);
95         let expected = Command::Decompress {
96             files,
97             output_folder: Some("folder".into()),
98         };
99         assert_eq!(expected, parse!("a b c --output folder").command);
100         assert_eq!(expected, parse!("a b --output folder c").command);
101         assert_eq!(expected, parse!("a --output folder b c").command);
102         assert_eq!(expected, parse!("--output folder a b c").command);
103     }
106 // #[cfg(test)]
107 // mod cli {
108 //     use super::*;
110 //     #[test]
111 //     fn decompress_files_into_folder() -> crate::Result<()> {
112 //         make_dummy_file("file.zip")?;
113 //         let args = gen_args("ouch -i file.zip -o folder/");
114 //         let (command, flags) = cli::parse_args_and_flags_from(args)?;
116 //         assert_eq!(
117 //             command,
118 //             Command::Decompress {
119 //                 files: args,
120 //                 compressed_output_path: PathBuf,
121 //             } //     kind: Decompress(vec![File {
122 //               //         path: fs::canonicalize("file.zip")?,
123 //               //         contents_in_memory: None,
124 //               //         extension: Some(Extension::from(Zip))
125 //               //     }]),
126 //               //     output: Some(File {
127 //               //         path: "folder".into(),
128 //               //         contents_in_memory: None,
129 //               //         extension: None
130 //               //     }),
131 //               // }
132 //         );
134 //         fs::remove_file("file.zip")?;
136 //         Ok(())
137 //     }
139 //     #[test]
140 //     fn decompress_files() -> crate::Result<()> {
141 //         make_dummy_file("my-cool-file.zip")?;
142 //         make_dummy_file("file.tar")?;
143 //         let matches =
144 //             clap_app().get_matches_from(vec!["ouch", "-i", "my-cool-file.zip", "file.tar"]);
145 //         let command_from_matches = Command::try_from(matches)?;
147 //         assert_eq!(
148 //             command_from_matches,
149 //             Command {
150 //                 kind: Decompress(vec![
151 //                     File {
152 //                         path: fs::canonicalize("my-cool-file.zip")?,
153 //                         contents_in_memory: None,
154 //                         extension: Some(Extension::from(Zip))
155 //                     },
156 //                     File {
157 //                         path: fs::canonicalize("file.tar")?,
158 //                         contents_in_memory: None,
159 //                         extension: Some(Extension::from(Tar))
160 //                     }
161 //                 ],),
162 //                 output: None,
163 //             }
164 //         );
166 //         fs::remove_file("my-cool-file.zip")?;
167 //         fs::remove_file("file.tar")?;
169 //         Ok(())
170 //     }
172 //     #[test]
173 //     fn compress_files() -> crate::Result<()> {
174 //         make_dummy_file("file")?;
175 //         make_dummy_file("file2.jpeg")?;
176 //         make_dummy_file("file3.ok")?;
178 //         let matches = clap_app().get_matches_from(vec![
179 //             "ouch",
180 //             "-i",
181 //             "file",
182 //             "file2.jpeg",
183 //             "file3.ok",
184 //             "-o",
185 //             "file.tar",
186 //         ]);
187 //         let command_from_matches = Command::try_from(matches)?;
189 //         assert_eq!(
190 //             command_from_matches,
191 //             Command {
192 //                 kind: Compress(vec![
193 //                     fs::canonicalize("file")?,
194 //                     fs::canonicalize("file2.jpeg")?,
195 //                     fs::canonicalize("file3.ok")?
196 //                 ]),
197 //                 output: Some(File {
198 //                     path: "file.tar".into(),
199 //                     contents_in_memory: None,
200 //                     extension: Some(Extension::from(Tar))
201 //                 }),
202 //             }
203 //         );
205 //         fs::remove_file("file")?;
206 //         fs::remove_file("file2.jpeg")?;
207 //         fs::remove_file("file3.ok")?;
209 //         Ok(())
210 //     }
211 // }
213 // #[cfg(test)]
214 // mod cli_errors {
216 //     #[test]
217 //     fn compress_files() -> crate::Result<()> {
218 //         let matches =
219 //             clap_app().get_matches_from(vec!["ouch", "-i", "a_file", "file2.jpeg", "file3.ok"]);
220 //         let res = Command::try_from(matches);
222 //         assert_eq!(
223 //             res,
224 //             Err(crate::Error::InputsMustHaveBeenDecompressible(
225 //                 "a_file".into()
226 //             ))
227 //         );
229 //         Ok(())
230 //     }
231 // }
233 // #[cfg(test)]
234 // mod extension_extraction {
236 //     #[test]
237 //     fn test_extension_zip() {
238 //         let path = "filename.tar.zip";
239 //         assert_eq!(
240 //             CompressionFormat::try_from(path),
241 //             Ok(CompressionFormat::Zip)
242 //         );
243 //     }
245 //     #[test]
246 //     fn test_extension_tar_gz() {
247 //         let extension = Extension::from(OsStr::new("folder.tar.gz")).unwrap();
248 //         assert_eq!(
249 //             extension,
250 //             Extension {
251 //                 first_ext: Some(CompressionFormat::Tar),
252 //                 second_ext: CompressionFormat::Gzip
253 //             }
254 //         );
255 //     }
257 //     #[test]
258 //     fn test_extension_tar() {
259 //         let path = "pictures.tar";
260 //         assert_eq!(
261 //             CompressionFormat::try_from(path),
262 //             Ok(CompressionFormat::Tar)
263 //         );
264 //     }
266 //     #[test]
267 //     fn test_extension_gz() {
268 //         let path = "passwords.tar.gz";
269 //         assert_eq!(
270 //             CompressionFormat::try_from(path),
271 //             Ok(CompressionFormat::Gzip)
272 //         );
273 //     }
275 //     #[test]
276 //     fn test_extension_lzma() {
277 //         let path = "mygame.tar.lzma";
278 //         assert_eq!(
279 //             CompressionFormat::try_from(path),
280 //             Ok(CompressionFormat::Lzma)
281 //         );
282 //     }
284 //     #[test]
285 //     fn test_extension_bz() {
286 //         let path = "songs.tar.bz";
287 //         assert_eq!(
288 //             CompressionFormat::try_from(path),
289 //             Ok(CompressionFormat::Bzip)
290 //         );
291 //     }
292 // }