integration test for arg parsing, minor shuffling around of things
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
d04fc22d46
commit
67fb03821d
4 changed files with 52 additions and 11 deletions
|
@ -3,6 +3,7 @@ fif
|
|||
[![Crates.io](https://img.shields.io/crates/v/fif.svg?style=flat-square)](https://crates.io/crates/fif)
|
||||
[![Crates.io](https://img.shields.io/crates/l/fif.svg?style=flat-square)](https://git.bune.city/lynnesbian/fif/src/branch/master/LICENSE)
|
||||
![Minimum Supported Rust Version](https://img.shields.io/badge/msrv-1.43.0-orange?style=flat-square)
|
||||
[![CI Status](https://drone.bune.city/api/badges/lynnesbian/fif/status.svg?style=flat-square)](https://drone.bune.city/lynnesbian/fif)
|
||||
|
||||
A command-line tool for detecting and optionally correcting files with incorrect extensions.
|
||||
|
||||
|
|
17
src/main.rs
17
src/main.rs
|
@ -214,10 +214,10 @@ fn init_db() {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
let args = parameters::Parameters::parse();
|
||||
let args: parameters::Parameters = parameters::Parameters::parse();
|
||||
|
||||
let mut builder = env_logger::Builder::from_env(
|
||||
Env::new().filter_or("RUST_LOG", "WARN")
|
||||
Env::new().filter_or("RUST_LOG", "INFO")
|
||||
);
|
||||
|
||||
builder
|
||||
|
@ -231,13 +231,7 @@ fn main() {
|
|||
|
||||
debug!("Iterating directory: {:?}", args.dirs);
|
||||
|
||||
let extensions: Vec<&str> = if let Some(exts) = &args.exts {
|
||||
exts.iter().map(|s| s.as_str()).collect()
|
||||
} else if let Some(exts) = &args.ext_set {
|
||||
exts.extensions().to_vec()
|
||||
} else {
|
||||
unreachable!()
|
||||
};
|
||||
let extensions = args.extensions();
|
||||
|
||||
debug!("Checking files with extensions: {:?}", extensions);
|
||||
|
||||
|
@ -254,7 +248,7 @@ fn main() {
|
|||
|
||||
if entries.is_empty() {
|
||||
warn!("No files matching requested options found.");
|
||||
exit(exitcode::DATAERR);
|
||||
exit(exitcode::OK);
|
||||
}
|
||||
|
||||
trace!("Found {} items to check", entries.len());
|
||||
|
@ -271,7 +265,7 @@ fn main() {
|
|||
for result in &results {
|
||||
match result {
|
||||
Ok(r) => {
|
||||
info!(
|
||||
debug!(
|
||||
"{:?} should have file extension {}",
|
||||
r.file,
|
||||
r.recommended_extension().unwrap_or_else(|| "???".into())
|
||||
|
@ -290,6 +284,7 @@ fn main() {
|
|||
OutputFormat::Script => {
|
||||
let s = Script::new();
|
||||
if s.write_all(&results, &mut BufWriter::new(stdout().lock())).is_err() {
|
||||
error!("Failed to write to stdout.");
|
||||
exit(exitcode::IOERR);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,3 +40,18 @@ pub struct Parameters {
|
|||
#[clap(name = "DIR", default_value = ".", parse(from_os_str))]
|
||||
pub dirs: PathBuf,
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
pub fn extensions(&self) -> Vec<&str> {
|
||||
if let Some(exts) = &self.exts {
|
||||
// extensions supplied like "-e png,jpg,jpeg"
|
||||
exts.iter().map(|s| s.as_str()).collect()
|
||||
} else if let Some(exts) = &self.ext_set {
|
||||
// extensions supplied like "-E images"
|
||||
exts.extensions()
|
||||
} else {
|
||||
// neither -E nor -e was passed - this should be impossible
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ use mime_guess::Mime;
|
|||
use smartstring::alias::String;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use crate::parameters::Parameters;
|
||||
|
||||
const JPEG_BYTES: &[u8] = b"\xFF\xD8\xFF";
|
||||
const PNG_BYTES: &[u8] = b"\x89\x50\x4E\x47\x0D\x0A\x1A\x0A";
|
||||
|
@ -109,12 +110,26 @@ fn simple_directory() {
|
|||
let results = scan_from_walkdir(&entries);
|
||||
for result in results {
|
||||
let result = result.expect("Error while scanning file");
|
||||
|
||||
if !result.valid {
|
||||
// this should be "wrong.jpg", which is a misnamed png file
|
||||
// 1. ensure extension is "png"
|
||||
assert_eq!(extension_from_path(&*result.file).unwrap(), String::from("jpg"));
|
||||
// 2. ensure mime type detected is IMAGE_PNG
|
||||
assert_eq!(result.mime, IMAGE_PNG);
|
||||
// 3. ensure recommended extension is in the list of known extensions for PNG files
|
||||
assert!(mime_extension_lookup(IMAGE_PNG).unwrap().contains(&result.recommended_extension().unwrap()));
|
||||
continue;
|
||||
}
|
||||
|
||||
// check if the recommended extension for this file is in the list of known extensions for its mimetype
|
||||
assert!(
|
||||
mime_extension_lookup(result.mime.clone())
|
||||
.unwrap()
|
||||
.contains(&result.recommended_extension().unwrap())
|
||||
);
|
||||
|
||||
// make sure the guessed mimetype is correct based on the extension of the scanned file
|
||||
assert_eq!(
|
||||
result.mime,
|
||||
match extension_from_path(&*result.file).as_deref() {
|
||||
|
@ -127,3 +142,18 @@ fn simple_directory() {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn argument_parsing() {
|
||||
use clap::Clap;
|
||||
|
||||
// check if "jpg" is in the list of extensions to be considered when passing "-E images"
|
||||
let args: Parameters = Parameters::parse_from(vec!["fif", "-E", "images"]);
|
||||
assert!(args.extensions().contains(&"jpg"));
|
||||
|
||||
// make sure "scan_hidden" is false
|
||||
assert!(!args.scan_hidden);
|
||||
|
||||
// exts should be none
|
||||
assert!(args.exts.is_none());
|
||||
}
|
Loading…
Reference in a new issue