Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: check the resulting compressed files through MIME types #74

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ zstd = { version = "0.9.0", default-features = false, features = ["thin"]

[dev-dependencies]
tempfile = "3.2.0"
infer = "0.5.0"
rand = { version = "0.8.3", default-features = false, features = ["small_rng", "std"] }

[profile.release]
Expand Down
2 changes: 2 additions & 0 deletions src/archive/tar.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Contains Tar-specific building and unpacking functions

use std::{
env, fs,
io::prelude::*,
Expand Down
3 changes: 3 additions & 0 deletions src/archive/zip.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Contains Zip-specific building and unpacking functions

use std::{
env, fs,
io::{self, prelude::*},
Expand All @@ -14,6 +16,7 @@ use crate::{

use self::utf8::get_invalid_utf8_paths;

/// Unpacks the archive given by `archive` into the folder given by `into`.
pub fn unpack_archive<R>(mut archive: ZipArchive<R>, into: &Path, flags: &oof::Flags) -> crate::Result<Vec<PathBuf>>
where
R: Read + Seek,
Expand Down
4 changes: 2 additions & 2 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Compr
let extension = match () {
_ if extension == "tar" => Tar,
_ if extension == "zip" => Zip,
_ if extension == "bz" => Bzip,
_ if extension == "gz" || extension == "bz2" => Gzip,
_ if extension == "bz" || extension == "bz2" => Bzip,
_ if extension == "gz" => Gzip,
_ if extension == "xz" || extension == "lzma" || extension == "lz" => Lzma,
_ if extension == "zst" => Zstd,
_ => break,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use error::{Error, Result};

use lazy_static::lazy_static;

/// The status code ouch has when an error is encountered
pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE;

const VERSION: &'static str = env!("CARGO_PKG_VERSION");
Expand Down
18 changes: 18 additions & 0 deletions src/oof/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,21 @@ pub fn trim_single_hyphen(flag_text: &str) -> &str {
chars.next(); // Skipping 1 char
chars.as_str()
}

#[cfg(test)]
mod tests {
use super::trim_double_hyphen;
use super::trim_single_hyphen;

#[test]
fn _trim_double_hyphen() {
assert_eq!(trim_double_hyphen("--flag"), "flag");
assert_eq!(trim_double_hyphen("--verbose"), "verbose");
assert_eq!(trim_double_hyphen("--help"), "help");
}

fn _trim_single_hyphen() {
assert_eq!(trim_single_hyphen("-vv"), "vv");
assert_eq!(trim_single_hyphen("-h"), "h");
}
}
45 changes: 45 additions & 0 deletions tests/compress_and_decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,57 @@ use std::{
env, fs,
io::prelude::*,
path::{Path, PathBuf},
time::Duration,
};

use ouch::{cli::Command, commands::run, oof};
use rand::{rngs::SmallRng, RngCore, SeedableRng};
use tempfile::NamedTempFile;
use utils::*;

#[test]
/// Makes sure that the files ouch produces are what they claim to be, checking their
/// types through MIME sniffing.
fn sanity_check_through_mime() {
// Somehow this test causes test failures when run in parallel with test_each_format
// This is a temporary hack that should allow the tests to pass while this bug isn't solved.
std::thread::sleep(Duration::from_millis(100));

let temp_dir = tempfile::tempdir().expect("to build a temporary directory");

let mut test_file = NamedTempFile::new_in(temp_dir.path()).expect("to be able to build a temporary file");

let bytes = generate_random_file_content(&mut SmallRng::from_entropy());
test_file.write_all(&bytes).expect("to successfully write bytes to the file");

let formats = ["tar", "zip", "tar.gz", "tar.bz", "tar.bz2", "tar.lzma", "tar.xz", "tar.zst"];

let expected_mimes = [
"application/x-tar",
"application/zip",
"application/gzip",
"application/x-bzip2",
"application/x-bzip2",
"application/x-xz",
"application/x-xz",
"application/zstd",
];

assert_eq!(formats.len(), expected_mimes.len());

for (format, expected_mime) in formats.iter().zip(expected_mimes.iter()) {
let temp_dir_path = temp_dir.path();
let paths_to_compress = &[test_file.path().into()];

let compressed_file_path = compress_files(temp_dir_path, paths_to_compress, format);

let sniffed =
infer::get_from_path(compressed_file_path).expect("the file to be read").expect("the MIME to be found");

assert_eq!(&sniffed.mime_type(), expected_mime);
}
}

#[test]
/// Tests each format that supports multiple files with random input.
/// TODO: test the remaining formats.
Expand Down