litl bit more testing

This commit is contained in:
Lynne Megido 2021-10-05 04:45:05 +10:00
parent 38ca71cc14
commit 955f0e1510
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
4 changed files with 46 additions and 15 deletions

View File

@ -298,7 +298,7 @@ pub fn mime_type<T: MimeDb>(db: &T, path: &Path) -> io::Result<Option<Mime>> {
/// Returns a list of known extensions for this mime type, if any.
/// This function uses the [`Mime`]'s "essence" rather than the [`Mime`] itself - [`mime_guess::get_mime_extensions`]
/// ignores the type suffix, treating "image/svg+xml" as "image/svg", and thus fails to find any extensions. Passing the
/// essence_str (which includes the suffix) fixes this.
/// `essence_str` (which includes the suffix) fixes this.
pub fn mime_extension_lookup(essence: String) -> Option<Vec<String>> {
if let Ok(cache) = MIMEXT.read() {
if let Some(exts) = cache.get(&essence) {

View File

@ -13,7 +13,7 @@ use crate::files::mime_extension_lookup;
use crate::String;
/// Information about a scanned file.
#[derive(Eq, PartialEq)]
#[derive(Eq, PartialEq, Debug)]
pub struct Findings {
/// The location of the scanned file.
pub file: PathBuf,
@ -45,12 +45,6 @@ impl Ord for Findings {
fn cmp(&self, other: &Self) -> Ordering {
// files with no recommended extension should appear first, so that fif outputs the "no known extension for x"
// comments before the "mv x y" instructions
// since fif doesn't output anything for valid files, the comparison will consider any comparison involving a
// valid Findings to be equal, avoiding the (somewhat) expensive call to recommended_extension. after all, since
// fif never displays valid files, it really doesn't matter what position they end up in.
if self.valid || other.valid {
return Ordering::Equal;
}
match (self.recommended_extension(), other.recommended_extension()) {
(None, Some(_)) => Ordering::Greater,
(Some(_), None) => Ordering::Less,

View File

@ -23,10 +23,9 @@ use std::process::exit;
use cfg_if::cfg_if;
use clap::Clap;
use fif::files::{scan_directory, scan_from_walkdir};
use fif::formats::Format;
use fif::parameters::{OutputFormat, Prompt};
use fif::formats::{self, Format};
use fif::parameters::{self, OutputFormat, Prompt};
use fif::utils::{os_name, CLAP_LONG_VERSION};
use fif::{formats, parameters};
use itertools::Itertools;
use log::{debug, error, info, trace, warn, Level};
@ -205,7 +204,7 @@ fn main() {
let mut buffered_stdout = BufWriter::new(stdout());
let result = match args.output_format {
// i want to simplify this to something like formats::write_all(args.output_format, ...)
// TODO: simplify this to something like formats::write_all(args.output_format, ...)
OutputFormat::Sh => formats::Shell.write_all(&mut buffered_stdout, &findings, &errors),
OutputFormat::PowerShell => formats::PowerShell.write_all(&mut buffered_stdout, &findings, &errors),
#[cfg(feature = "json")]

View File

@ -8,6 +8,7 @@ use fif::findings::Findings;
use fif::formats::{Format, PowerShell, Shell};
use fif::mime_db::MimeDb;
use fif::{String, MIMEDB};
use itertools::Itertools;
use maplit::{btreeset, hashmap};
use mime::{Mime, APPLICATION_OCTET_STREAM, APPLICATION_PDF, IMAGE_JPEG, IMAGE_PNG};
@ -324,7 +325,7 @@ fn identify_random_bytes() {
let mut bytes: [u8; BUF_SIZE * 2] = [0; BUF_SIZE * 2];
let mut results: BTreeMap<Mime, i32> = BTreeMap::new();
for _ in 1..10000 {
for _ in 1..1000 {
rng.fill_bytes(&mut bytes);
if let Some(detected_type) = MIMEDB.get_type(&bytes) {
*results.entry(detected_type).or_insert(0) += 1;
@ -467,7 +468,44 @@ fn verbosity() {
}
#[test]
/// Ensures that smart strings don't deviate from std's Strings
/// Ensures `os_name()`'s output is the same as [`std::env::consts::OS`], capitalisation notwithstanding
fn validate_os_name() {
assert_eq!(
fif::utils::os_name().to_lowercase(),
std::env::consts::OS.to_lowercase()
);
}
#[test]
/// Ensures that [`Findings`] are sorted properly.
fn sort_findings() {
let findings = vec![
Findings {
file: Path::new("ccc").to_path_buf(),
valid: false,
mime: IMAGE_JPEG,
},
Findings {
file: Path::new("bbb.xyz").to_path_buf(),
valid: true,
mime: IMAGE_PNG,
},
Findings {
file: Path::new("aaa").to_path_buf(),
valid: true,
mime: APPLICATION_PDF,
},
];
let mut findings = findings.iter().sorted_unstable();
assert_eq!(findings.next().unwrap().file, Path::new("aaa"));
assert_eq!(findings.next().unwrap().file, Path::new("bbb.xyz"));
assert_eq!(findings.next().unwrap().file, Path::new("ccc"));
assert_eq!(findings.next(), None);
}
#[test]
/// Ensures that [`SmartString`]s don't deviate from std's Strings
fn validate_string_type() {
use std::string::String as StdString;