forked from ouch-org/ouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mime.rs
55 lines (44 loc) · 1.72 KB
/
mime.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#[macro_use]
mod utils;
use rand::{rngs::SmallRng, SeedableRng};
use tempfile::NamedTempFile;
use crate::utils::write_random_content;
#[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() {
let temp_dir = tempfile::tempdir().expect("to build a temporary directory");
let temp_dir_path = temp_dir.path();
let test_file = &mut NamedTempFile::new_in(temp_dir_path).expect("to be able to build a temporary file");
write_random_content(test_file, &mut SmallRng::from_entropy());
let formats = [
"tar", "zip", "tar.gz", "tgz", "tbz", "tbz2", "txz", "tlzma", "tzst", "tar.bz", "tar.bz2", "tar.lzma",
"tar.xz", "tar.zst",
];
let expected_mimes = [
"application/x-tar",
"application/zip",
"application/gzip",
"application/gzip",
"application/x-bzip2",
"application/x-bzip2",
"application/x-xz",
"application/x-xz",
"application/zstd",
"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 path_to_compress = test_file.path();
let compressed_file_path = &format!("{}.{}", path_to_compress.display(), format);
ouch!("c", path_to_compress, compressed_file_path);
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);
}
}