Merge pull request #615 from ouch-org/dependabot/cargo/libc-0.2.153
[ouch.git] / tests / mime.rs
blobbf52179848f2a4e20b50423c274ae56c312953b3
1 #[macro_use]
2 mod utils;
4 use rand::{rngs::SmallRng, SeedableRng};
5 use tempfile::NamedTempFile;
7 use crate::utils::write_random_content;
9 #[test]
10 /// Makes sure that the files ouch produces are what they claim to be, checking their
11 /// types through MIME sniffing.
12 fn sanity_check_through_mime() {
13     let temp_dir = tempfile::tempdir().expect("to build a temporary directory");
14     let temp_dir_path = temp_dir.path();
16     let test_file = &mut NamedTempFile::new_in(temp_dir_path).expect("to be able to build a temporary file");
17     write_random_content(test_file, &mut SmallRng::from_entropy());
19     let formats = [
20         "7z", "tar", "zip", "tar.gz", "tgz", "tbz", "tbz2", "txz", "tlzma", "tzst", "tar.bz", "tar.bz2", "tar.lzma",
21         "tar.xz", "tar.zst",
22     ];
24     let expected_mimes = [
25         "application/x-7z-compressed",
26         "application/x-tar",
27         "application/zip",
28         "application/gzip",
29         "application/gzip",
30         "application/x-bzip2",
31         "application/x-bzip2",
32         "application/x-xz",
33         "application/x-xz",
34         "application/zstd",
35         "application/x-bzip2",
36         "application/x-bzip2",
37         "application/x-xz",
38         "application/x-xz",
39         "application/zstd",
40     ];
42     assert_eq!(formats.len(), expected_mimes.len());
44     for (format, expected_mime) in formats.iter().zip(expected_mimes.iter()) {
45         let path_to_compress = test_file.path();
47         let compressed_file_path = &format!("{}.{}", path_to_compress.display(), format);
48         ouch!("c", path_to_compress, compressed_file_path);
50         let sniffed = infer::get_from_path(compressed_file_path)
51             .expect("the file to be read")
52             .expect("the MIME to be found");
54         assert_eq!(&sniffed.mime_type(), expected_mime);
55     }