Skip to content

Commit

Permalink
UnrecognizedCommandError can be corrected and retried.
Browse files Browse the repository at this point in the history
When encountering a UnrecognizedCommandError, the developer will be given the option to run of the suggested corrections instead.
  • Loading branch information
andrewn617 committed Feb 15, 2024
1 parent 7694426 commit 235139e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
14 changes: 14 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
* When encountering an Unrecognized Command Error, the developer will be given the option to
run of the suggested corrections instead.

```txt
bin/rails action_txt:install
Unrecognized command "action_txt:install"
Did you mean? action_text:install [Yn] Y
Installing JavaScript dependencies
...
```

*Andrew Novoselac*

* Allow Actionable Errors encountered when running tests to be retried. This can be configured by
`config.actionable_command_line_errors` and is `true` in the test environment unless the `"CI"` env variable
is set, and false otherwise.
Expand Down
18 changes: 17 additions & 1 deletion railties/lib/rails/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ def initialize(name)
end
end

class UnrecognizedCommandCorrector < Thor::Shell::Basic
def correct(error)
if defined?(DidYouMean::Correctable) && defined?(DidYouMean::SpellChecker)
say error.original_message

error.corrections.detect do |correction|
correction if yes? DidYouMean.formatter.message_for([correction]) + " [Yn]"
end
else
say error.detailed_message
nil
end
end
end

include Behavior

HELP_MAPPINGS = %w(-h -? --help).to_set
Expand Down Expand Up @@ -77,7 +92,8 @@ def invoke(full_namespace, args = [], **config)
if error.name == full_namespace && command && command_name == full_namespace
command.perform("help", [], config)
else
puts error.detailed_message
correction = UnrecognizedCommandCorrector.new.correct(error)
return invoke(correction, args, **config) if correction
end
exit(1)
end
Expand Down
2 changes: 1 addition & 1 deletion railties/test/command/help_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Rails::Command::HelpIntegrationTest < ActiveSupport::TestCase
output = rails "vershen", allow_failure: true

assert_match %(Unrecognized command "vershen"), output
assert_match "Did you mean? version", output
assert_match "Did you mean? version [Yn]", output
end

test "loads Rake tasks only once on unrecognized command" do
Expand Down

0 comments on commit 235139e

Please sign in to comment.