From b0aa27b208fe77313349542c1f22d1fe65428fdc Mon Sep 17 00:00:00 2001 From: Lucas Weng Date: Thu, 20 Oct 2022 10:03:49 +0800 Subject: [PATCH] fix(rome_cli): print the correct subcommand in error messages --- crates/rome_cli/src/commands/format.rs | 5 ++++- crates/rome_cli/src/execute.rs | 9 +++++++++ crates/rome_cli/src/termination.rs | 14 ++++++++++---- crates/rome_cli/src/traversal.rs | 6 +++++- crates/rome_cli/tests/commands/format.rs | 8 +++++++- crates/rome_cli/tests/main.rs | 14 ++++++++++++-- 6 files changed, 47 insertions(+), 9 deletions(-) diff --git a/crates/rome_cli/src/commands/format.rs b/crates/rome_cli/src/commands/format.rs index 1cf680f6caa..67bed3d1ff3 100644 --- a/crates/rome_cli/src/commands/format.rs +++ b/crates/rome_cli/src/commands/format.rs @@ -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 diff --git a/crates/rome_cli/src/execute.rs b/crates/rome_cli/src/execute.rs index cb78db332cf..61b1ca16abe 100644 --- a/crates/rome_cli/src/execute.rs +++ b/crates/rome_cli/src/execute.rs @@ -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 diff --git a/crates/rome_cli/src/termination.rs b/crates/rome_cli/src/termination.rs index a7982369bd9..906afae1554 100644 --- a/crates/rome_cli/src/termination.rs +++ b/crates/rome_cli/src/termination.rs @@ -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")] diff --git a/crates/rome_cli/src/traversal.rs b/crates/rome_cli/src/traversal.rs index 6e80f90d4dd..9313f49aee5 100644 --- a/crates/rome_cli/src/traversal.rs +++ b/crates/rome_cli/src/traversal.rs @@ -47,7 +47,10 @@ pub(crate) fn traverse(execution: Execution, mut session: CliSession) -> Result< } // `--` or `-` if without_dashes != input { - return Err(Termination::UnexpectedArgument { argument: input }); + return Err(Termination::UnexpectedArgument { + subcommand: execution.traversal_mode_subcommand(), + argument: input, + }); } } inputs.push(input); @@ -55,6 +58,7 @@ pub(crate) fn traverse(execution: Execution, mut session: CliSession) -> Result< if inputs.is_empty() && execution.as_stdin_file().is_none() { return Err(Termination::MissingArgument { + subcommand: execution.traversal_mode_subcommand(), argument: "", }); } diff --git a/crates/rome_cli/tests/commands/format.rs b/crates/rome_cli/tests/commands/format.rs index fc4be9e4adb..cc47663f703 100644 --- a/crates/rome_cli/tests/commands/format.rs +++ b/crates/rome_cli/tests/commands/format.rs @@ -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") } diff --git a/crates/rome_cli/tests/main.rs b/crates/rome_cli/tests/main.rs index 2c98851f9e1..9c5b596e79e 100644 --- a/crates/rome_cli/tests/main.rs +++ b/crates/rome_cli/tests/main.rs @@ -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"), @@ -122,7 +126,13 @@ mod main { ); match result { - Err(Termination::MissingArgument { argument }) => assert_eq!(argument, ""), + Err(Termination::MissingArgument { + subcommand, + argument, + }) => { + assert_eq!(subcommand, "format"); + assert_eq!(argument, "") + } _ => panic!("run_cli returned {result:?} for a missing argument, expected an error"), } }