fif 0.8.0

A command-line tool for detecting and optionally correcting files with incorrect extensions.
Documentation
use clap::Parser;
use fif::parameters::Parameters;

#[test]
/// Ensure that badly formed command line arguments are rejected.
fn rejects_bad_args() {
	use assert_cmd::Command;
	let tests = [
		// Non-existent flags:
		vec!["fif", "-abcdefghijklmnopqrstuvwxyz"],
		// `-E` without specifying a set:
		vec!["fif", "-E"],
		// `-E` with an invalid set:
		vec!["fif", "-E", "pebis"],
		// `-X` with an invalid set:
		vec!["fif", "-X", "pebis"],
		// `-e` with nothing but commas:
		vec!["fif", "-e", ",,,,,"],
		// `-x` with nothing but commas:
		vec!["fif", "-x", ",,,,,"],
		// `-j` with a negative value:
		vec!["fif", "-j", "-1"],
		// `--prompt` without `--fix`:
		vec!["fif", "--prompt", "always"],
		// `--overwrite` without `--fix`:
		vec!["fif", "--overwrite"],
	];

	for test in &tests {
		// first, try testing the flags against the Parameters struct...
		assert!(Parameters::try_parse_from(test).is_err(), "Failed to reject {test:?}");
		// ...then, make sure it actually works against the binary
		let mut cmd = Command::cargo_bin("fif").unwrap();
		cmd.args(test).assert().failure();
	}
}

#[test]
/// Ensure that a few simple, well-formed command line argument cases pass
fn accepts_good_args() {
	use assert_cmd::Command;

	// all of these commands pass either the version or help flag, ensuring that they won't fail for reasons relating
	// to filesystem access
	let tests = [
		vec!["-V"],
		vec!["--version"],
		vec!["-E", "images", "--version"],
		vec!["-h"],
		vec!["--help"],
		vec!["dir_name", "--version"],
	];

	for test in &tests {
		let mut cmd = Command::cargo_bin("fif").unwrap();
		cmd.args(test).assert().success();
	}
}

#[test]
/// Ensures that output from the `-V` and `--version` flags is formatted properly.
fn check_version_output() {
	use std::string::String;

	use assert_cmd::Command;
	use regex::Regex;

	// test `-V` matches the format of "fif x.y.z"
	let mut cmd = Command::cargo_bin("fif").unwrap();
	let output = cmd.arg("-V").ok().unwrap().stdout;
	let output = String::from_utf8(output).unwrap();
	assert!(
		Regex::new(r"fif v([0-9]\.){2}[0-9]").unwrap().is_match(output.trim()),
		"\"{output}\" does not match the expected `-v` format!"
	);

	// test `--version` matches the format of "fif x.y.z (OS, example backend, commit #1234abc)"
	let mut cmd = Command::cargo_bin("fif").unwrap();
	let output = cmd.arg("--version").ok().unwrap().stdout;
	let output = String::from_utf8(output).unwrap();
	assert!(
		Regex::new(r"fif v([0-9]\.){2}[0-9] \(.+, .+ backend, (unknown commit|commit #[[:xdigit:]]{7})\)")
			.unwrap()
			.is_match(output.trim()),
		"\"{}\" does not match the expected `--version` format!",
		output.trim()
	);
}