1 use std::{fs, path::Path};
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<()>
11 fs::write(path.as_ref(), &[2, 3, 4, 5, 6, 7, 8, 9, 10])?;
16 fn make_dummy_files<'a, P>(paths: &[P]) -> crate::Result<()>
30 use super::{make_dummy_files};
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()
41 ($input_text:expr) => {{
42 let args = gen_args($input_text);
43 cli::parse_args_from(args).unwrap()
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);
67 fn test_arg_parsing_compress_subcommand() -> crate::Result<()> {
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 {
75 compressed_output_path: "d".into(),
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")?;
86 fn test_arg_parsing_decompress_subcommand() {
87 let files: Vec<_> = ["a", "b", "c"].iter().map(PathBuf::from).collect();
89 let expected = Command::Decompress {
93 assert_eq!(expected, parse!("a b c").command);
95 let expected = Command::Decompress {
97 output_folder: Some("folder".into()),
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);
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)?;
118 // Command::Decompress {
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))
126 // // output: Some(File {
127 // // path: "folder".into(),
128 // // contents_in_memory: None,
129 // // extension: None
134 // fs::remove_file("file.zip")?;
140 // fn decompress_files() -> crate::Result<()> {
141 // make_dummy_file("my-cool-file.zip")?;
142 // make_dummy_file("file.tar")?;
144 // clap_app().get_matches_from(vec!["ouch", "-i", "my-cool-file.zip", "file.tar"]);
145 // let command_from_matches = Command::try_from(matches)?;
148 // command_from_matches,
150 // kind: Decompress(vec![
152 // path: fs::canonicalize("my-cool-file.zip")?,
153 // contents_in_memory: None,
154 // extension: Some(Extension::from(Zip))
157 // path: fs::canonicalize("file.tar")?,
158 // contents_in_memory: None,
159 // extension: Some(Extension::from(Tar))
166 // fs::remove_file("my-cool-file.zip")?;
167 // fs::remove_file("file.tar")?;
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![
187 // let command_from_matches = Command::try_from(matches)?;
190 // command_from_matches,
192 // kind: Compress(vec![
193 // fs::canonicalize("file")?,
194 // fs::canonicalize("file2.jpeg")?,
195 // fs::canonicalize("file3.ok")?
197 // output: Some(File {
198 // path: "file.tar".into(),
199 // contents_in_memory: None,
200 // extension: Some(Extension::from(Tar))
205 // fs::remove_file("file")?;
206 // fs::remove_file("file2.jpeg")?;
207 // fs::remove_file("file3.ok")?;
217 // fn compress_files() -> crate::Result<()> {
219 // clap_app().get_matches_from(vec!["ouch", "-i", "a_file", "file2.jpeg", "file3.ok"]);
220 // let res = Command::try_from(matches);
224 // Err(crate::Error::InputsMustHaveBeenDecompressible(
234 // mod extension_extraction {
237 // fn test_extension_zip() {
238 // let path = "filename.tar.zip";
240 // CompressionFormat::try_from(path),
241 // Ok(CompressionFormat::Zip)
246 // fn test_extension_tar_gz() {
247 // let extension = Extension::from(OsStr::new("folder.tar.gz")).unwrap();
251 // first_ext: Some(CompressionFormat::Tar),
252 // second_ext: CompressionFormat::Gzip
258 // fn test_extension_tar() {
259 // let path = "pictures.tar";
261 // CompressionFormat::try_from(path),
262 // Ok(CompressionFormat::Tar)
267 // fn test_extension_gz() {
268 // let path = "passwords.tar.gz";
270 // CompressionFormat::try_from(path),
271 // Ok(CompressionFormat::Gzip)
276 // fn test_extension_lzma() {
277 // let path = "mygame.tar.lzma";
279 // CompressionFormat::try_from(path),
280 // Ok(CompressionFormat::Lzma)
285 // fn test_extension_bz() {
286 // let path = "songs.tar.bz";
288 // CompressionFormat::try_from(path),
289 // Ok(CompressionFormat::Bzip)