diff --git a/tests/compress_and_decompress.rs b/tests/compress_and_decompress.rs index fa07d13..cb207ad 100644 --- a/tests/compress_and_decompress.rs +++ b/tests/compress_and_decompress.rs @@ -1,3 +1,5 @@ +mod utils; + use std::{ env, fs, io::prelude::*, @@ -6,6 +8,7 @@ use std::{ use ouch::{cli::Command, commands::run, oof}; use rand::{rngs::SmallRng, RngCore, SeedableRng}; +use utils::*; #[test] /// Tests each format that supports multiple files with random input. @@ -94,16 +97,6 @@ fn create_files(at: &Path, contents: &[FileContent]) -> Vec { .collect() } -fn compress_files(at: &Path, paths_to_compress: &[PathBuf], format: &str) -> PathBuf { - let archive_path = String::from("archive.") + format; - let archive_path = at.join(archive_path); - - let command = Command::Compress { files: paths_to_compress.to_vec(), output_path: archive_path.to_path_buf() }; - run(command, &oof::Flags::default()).expect("Failed to compress test dummy files"); - - archive_path -} - fn extract_files(archive_path: &Path) -> Vec { // We will extract in the same folder as the archive // If the archive is at: diff --git a/tests/compress_empty_dir.rs b/tests/compress_empty_dir.rs index 438565a..4a7c108 100644 --- a/tests/compress_empty_dir.rs +++ b/tests/compress_empty_dir.rs @@ -1,9 +1,8 @@ -use std::{ - env, fs, - path::{Path, PathBuf}, -}; +mod utils; -use ouch::{cli::Command, commands::run, oof}; +use std::{env, path::PathBuf}; + +use utils::*; #[test] fn test_compress_decompress_with_empty_dir() { @@ -38,55 +37,3 @@ fn test_compress_decompress_with_empty_dir() { assert_correct_paths(&file_paths, &extracted_paths, format); } - -fn create_empty_dir(at: &Path, filename: &str) -> PathBuf { - let dirname = Path::new(filename); - let full_path = at.join(dirname); - - fs::create_dir(&full_path).expect("Failed to create an empty directory"); - - full_path -} - -fn compress_files(at: &Path, paths_to_compress: &[PathBuf], format: &str) -> PathBuf { - let archive_path = String::from("archive.") + format; - let archive_path = at.join(archive_path); - - let command = Command::Compress { files: paths_to_compress.to_vec(), output_path: archive_path.to_path_buf() }; - run(command, &oof::Flags::default()).expect("Failed to compress test dummy files"); - - archive_path -} - -fn extract_files(archive_path: &Path) -> Vec { - // We will extract in the same folder as the archive - // If the archive is at: - // /tmp/ouch-testing-tar.Rbq4DusBrtF8/archive.tar - // Then the extraction_output_folder will be: - // /tmp/ouch-testing-tar.Rbq4DusBrtF8/extraction_results/ - let mut extraction_output_folder = archive_path.to_path_buf(); - // Remove the name of the extracted archive - assert!(extraction_output_folder.pop()); - // Add the suffix "results" - extraction_output_folder.push("extraction_results"); - - let command = Command::Decompress { - files: vec![archive_path.to_owned()], - output_folder: Some(extraction_output_folder.clone()), - }; - run(command, &oof::Flags::default()).expect("Failed to extract"); - - fs::read_dir(extraction_output_folder).unwrap().map(Result::unwrap).map(|entry| entry.path()).collect() -} - -fn assert_correct_paths(original: &[PathBuf], extracted: &[PathBuf], format: &str) { - assert_eq!( - original.len(), - extracted.len(), - "Number of compressed files does not match number of decompressed when testing archive format '{:?}'.", - format - ); - for (original, extracted) in original.iter().zip(extracted) { - assert_eq!(original.file_name(), extracted.file_name(), ""); - } -} diff --git a/tests/utils.rs b/tests/utils.rs new file mode 100644 index 0000000..9152e05 --- /dev/null +++ b/tests/utils.rs @@ -0,0 +1,59 @@ +#[allow(dead_code)] +use std::{ + fs, + path::{Path, PathBuf}, +}; + +use ouch::{cli::Command, commands::run, oof}; + +pub fn create_empty_dir(at: &Path, filename: &str) -> PathBuf { + let dirname = Path::new(filename); + let full_path = at.join(dirname); + + fs::create_dir(&full_path).expect("Failed to create an empty directory"); + + full_path +} + +pub fn compress_files(at: &Path, paths_to_compress: &[PathBuf], format: &str) -> PathBuf { + let archive_path = String::from("archive.") + format; + let archive_path = at.join(archive_path); + + let command = Command::Compress { files: paths_to_compress.to_vec(), output_path: archive_path.to_path_buf() }; + run(command, &oof::Flags::default()).expect("Failed to compress test dummy files"); + + archive_path +} + +pub fn extract_files(archive_path: &Path) -> Vec { + // We will extract in the same folder as the archive + // If the archive is at: + // /tmp/ouch-testing-tar.Rbq4DusBrtF8/archive.tar + // Then the extraction_output_folder will be: + // /tmp/ouch-testing-tar.Rbq4DusBrtF8/extraction_results/ + let mut extraction_output_folder = archive_path.to_path_buf(); + // Remove the name of the extracted archive + assert!(extraction_output_folder.pop()); + // Add the suffix "results" + extraction_output_folder.push("extraction_results"); + + let command = Command::Decompress { + files: vec![archive_path.to_owned()], + output_folder: Some(extraction_output_folder.clone()), + }; + run(command, &oof::Flags::default()).expect("Failed to extract"); + + fs::read_dir(extraction_output_folder).unwrap().map(Result::unwrap).map(|entry| entry.path()).collect() +} + +pub fn assert_correct_paths(original: &[PathBuf], extracted: &[PathBuf], format: &str) { + assert_eq!( + original.len(), + extracted.len(), + "Number of compressed files does not match number of decompressed when testing archive format '{:?}'.", + format + ); + for (original, extracted) in original.iter().zip(extracted) { + assert_eq!(original.file_name(), extracted.file_name()); + } +}