Remove import comments
[ouch.git] / tests / utils.rs
blobec3ae7a10dd5ad3ae8a1c9423b991a109f1e29ad
1 //! Files in common between one or more integration tests
3 #![allow(dead_code)]
5 use std::path::{Path, PathBuf};
7 use fs_err as fs;
8 use ouch::{commands::run, Opts, QuestionPolicy, Subcommand};
10 pub fn create_empty_dir(at: &Path, filename: &str) -> PathBuf {
11     let dirname = Path::new(filename);
12     let full_path = at.join(dirname);
14     fs::create_dir(&full_path).expect("Failed to create an empty directory");
16     full_path
19 pub fn compress_files(at: &Path, paths_to_compress: &[PathBuf], format: &str) -> PathBuf {
20     let archive_path = String::from("archive.") + format;
21     let archive_path = at.join(archive_path);
23     let command = Opts {
24         yes: false,
25         no: false,
26         cmd: Subcommand::Compress { files: paths_to_compress.to_vec(), output: archive_path.clone() },
27     };
28     run(command, QuestionPolicy::Ask).expect("Failed to compress test dummy files");
30     archive_path
33 pub fn extract_files(archive_path: &Path) -> Vec<PathBuf> {
34     // We will extract in the same folder as the archive
35     // If the archive is at:
36     //   /tmp/ouch-testing-tar.Rbq4DusBrtF8/archive.tar
37     // Then the extraction_output_folder will be:
38     //   /tmp/ouch-testing-tar.Rbq4DusBrtF8/extraction_results/
39     let mut extraction_output_folder = archive_path.to_path_buf();
40     // Remove the name of the extracted archive
41     assert!(extraction_output_folder.pop());
42     // Add the suffix "results"
43     extraction_output_folder.push("extraction_results");
45     let command = Opts {
46         yes: false,
47         no: false,
48         cmd: Subcommand::Decompress {
49             files: vec![archive_path.to_owned()],
50             output_dir: Some(extraction_output_folder.clone()),
51         },
52     };
53     run(command, QuestionPolicy::Ask).expect("Failed to extract");
55     fs::read_dir(extraction_output_folder).unwrap().map(Result::unwrap).map(|entry| entry.path()).collect()
58 pub fn assert_correct_paths(original: &[PathBuf], extracted: &[PathBuf], format: &str) {
59     assert_eq!(
60         original.len(),
61         extracted.len(),
62         "Number of compressed files does not match number of decompressed when testing archive format '{:?}'.",
63         format
64     );
65     for (original, extracted) in original.iter().zip(extracted) {
66         assert_eq!(original.file_name(), extracted.file_name());
67     }