fix(cli): return error if --format receives an unknown value

This commit is contained in:
Boshen 2024-04-11 21:38:39 +08:00
parent f3a28c61b9
commit c250b288ef
No known key found for this signature in database
GPG key ID: 234DA6A7079C6801

View file

@ -149,10 +149,11 @@ pub enum OutputFormat {
impl FromStr for OutputFormat {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"json" => Self::Json,
_ => Self::Default,
})
match s {
"json" => Ok(Self::Json),
"default" => Ok(Self::Default),
_ => Err(format!("'{s}' is not a known format")),
}
}
}
@ -316,6 +317,15 @@ mod lint_options {
assert!(options.paths.is_empty());
}
#[test]
fn format_error() {
let args = "-f asdf".split(' ').map(std::string::ToString::to_string).collect::<Vec<_>>();
let result = lint_command().run_inner(args.as_slice());
assert!(result.is_err_and(
|err| err.unwrap_stderr() == "couldn't parse `asdf`: 'asdf' is not a known format"
));
}
#[test]
fn list_rules() {
let options = get_lint_options("--rules");