test: check the resulting compressed files through MIME types
[ouch.git] / tests / utils.rs
blob3ab98319bd504138690adc7b4f15748131289401
1 //! Files in common between one or more integration tests
3 #![allow(dead_code)]
5 use std::{
6     fs,
7     path::{Path, PathBuf},
8 };
10 use ouch::{cli::Command, commands::run, oof};
12 pub fn create_empty_dir(at: &Path, filename: &str) -> PathBuf {
13     let dirname = Path::new(filename);
14     let full_path = at.join(dirname);
16     fs::create_dir(&full_path).expect("Failed to create an empty directory");
18     full_path
21 pub fn compress_files(at: &Path, paths_to_compress: &[PathBuf], format: &str) -> PathBuf {
22     let archive_path = String::from("archive.") + format;
23     let archive_path = at.join(archive_path);
25     let command = Command::Compress { files: paths_to_compress.to_vec(), output_path: archive_path.to_path_buf() };
26     run(command, &oof::Flags::default()).expect("Failed to compress test dummy files");
28     archive_path
31 pub fn extract_files(archive_path: &Path) -> Vec<PathBuf> {
32     // We will extract in the same folder as the archive
33     // If the archive is at:
34     //   /tmp/ouch-testing-tar.Rbq4DusBrtF8/archive.tar
35     // Then the extraction_output_folder will be:
36     //   /tmp/ouch-testing-tar.Rbq4DusBrtF8/extraction_results/
37     let mut extraction_output_folder = archive_path.to_path_buf();
38     // Remove the name of the extracted archive
39     assert!(extraction_output_folder.pop());
40     // Add the suffix "results"
41     extraction_output_folder.push("extraction_results");
43     let command = Command::Decompress {
44         files: vec![archive_path.to_owned()],
45         output_folder: Some(extraction_output_folder.clone()),
46     };
47     run(command, &oof::Flags::default()).expect("Failed to extract");
49     fs::read_dir(extraction_output_folder).unwrap().map(Result::unwrap).map(|entry| entry.path()).collect()
52 pub fn assert_correct_paths(original: &[PathBuf], extracted: &[PathBuf], format: &str) {
53     assert_eq!(
54         original.len(),
55         extracted.len(),
56         "Number of compressed files does not match number of decompressed when testing archive format '{:?}'.",
57         format
58     );
59     for (original, extracted) in original.iter().zip(extracted) {
60         assert_eq!(original.file_name(), extracted.file_name());
61     }