Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

fix(rome_cli): print the correct subcommand in error messages #3460

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/rome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ pub(crate) fn format(mut session: CliSession) -> Result<(), Termination> {
Some((path, input_code))
} else {
// we provided the argument without a piped stdin, we bail
return Err(Termination::MissingArgument { argument: "stdin" });
return Err(Termination::MissingArgument {
subcommand: "format",
argument: "stdin",
});
}
} else {
None
Expand Down
9 changes: 9 additions & 0 deletions crates/rome_cli/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ impl Execution {
_ => None,
}
}

/// Returns the subcommand of the [traversal mode](TraversalMode) execution
pub(crate) fn traversal_mode_subcommand(&self) -> &'static str {
match self.traversal_mode {
TraversalMode::Check { .. } => "check",
TraversalMode::CI { .. } => "ci",
TraversalMode::Format { .. } => "format",
}
}
}

/// Based on the [mode](ExecutionMode), the function might launch a traversal of the file system
Expand Down
14 changes: 10 additions & 4 deletions crates/rome_cli/src/termination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@ pub enum Termination {

/// Returned when the CLI doesn't recognize a command line argument
#[error(
"unrecognized option {argument:?}. Type '{} format --help' for more information.",
"unrecognized option {argument:?}. Type '{} {subcommand} --help' for more information.",
command_name()
)]
UnexpectedArgument { argument: OsString },
UnexpectedArgument {
subcommand: &'static str,
argument: OsString,
},

/// Returned when a required argument is not present in the command line
#[error(
"missing argument '{argument}'. Type '{} format --help' for more information.",
"missing argument '{argument}'. Type '{} {subcommand} --help' for more information.",
command_name()
)]
MissingArgument { argument: &'static str },
MissingArgument {
subcommand: &'static str,
argument: &'static str,
},

/// Returned when a subcommand is called without any arguments
#[error("empty arguments")]
Expand Down
6 changes: 5 additions & 1 deletion crates/rome_cli/src/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ pub(crate) fn traverse(execution: Execution, mut session: CliSession) -> Result<
}
// `--<some character>` or `-<some character>`
if without_dashes != input {
return Err(Termination::UnexpectedArgument { argument: input });
return Err(Termination::UnexpectedArgument {
subcommand: execution.traversal_mode_subcommand(),
argument: input,
});
}
}
inputs.push(input);
}

if inputs.is_empty() && execution.as_stdin_file().is_none() {
return Err(Termination::MissingArgument {
subcommand: execution.traversal_mode_subcommand(),
argument: "<INPUT>",
});
}
Expand Down
8 changes: 7 additions & 1 deletion crates/rome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,13 @@ fn format_stdin_with_errors() {
assert!(result.is_err(), "run_cli returned {result:?}");

match result {
Err(Termination::MissingArgument { argument }) => assert_eq!(argument, "stdin"),
Err(Termination::MissingArgument {
subcommand,
argument,
}) => {
assert_eq!(subcommand, "format");
assert_eq!(argument, "stdin")
}
_ => {
panic!("run_cli returned {result:?} for an unknown command help, expected an error")
}
Expand Down
14 changes: 12 additions & 2 deletions crates/rome_cli/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ mod main {
);

match result {
Err(Termination::UnexpectedArgument { argument, .. }) => {
Err(Termination::UnexpectedArgument {
subcommand,
argument,
}) => {
assert_eq!(subcommand, "format");
assert_eq!(argument, OsString::from("--unknown"))
}
_ => panic!("run_cli returned {result:?} for an unknown argument, expected an error"),
Expand Down Expand Up @@ -122,7 +126,13 @@ mod main {
);

match result {
Err(Termination::MissingArgument { argument }) => assert_eq!(argument, "<INPUT>"),
Err(Termination::MissingArgument {
subcommand,
argument,
}) => {
assert_eq!(subcommand, "format");
assert_eq!(argument, "<INPUT>")
}
_ => panic!("run_cli returned {result:?} for a missing argument, expected an error"),
}
}
Expand Down