From 34ff6a0a4aa9211fcfec962aaddd8e822dbf9f0a Mon Sep 17 00:00:00 2001 From: Alex Ghiculescu Date: Thu, 13 May 2021 12:26:41 -0500 Subject: [PATCH] Display a help message if `rails new` is called without a path Fixes https://github.com/rails/rails/issues/42196 Now, `rails new` will display the same error message that `rails` and `rails help new` does. To test: - Pull this branch - Uninstall your local rails copy using `gem uninstall rails` - Move into the root directory where you pulled down the rails code, then install: `rake install` - In another directory run `rails new` --- railties/lib/rails/command.rb | 5 +++++ railties/test/command/application_test.rb | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 railties/test/command/application_test.rb diff --git a/railties/lib/rails/command.rb b/railties/lib/rails/command.rb index c319bccdf8..54c8f6e054 100644 --- a/railties/lib/rails/command.rb +++ b/railties/lib/rails/command.rb @@ -38,6 +38,7 @@ module Rails end command_name, namespace = "help", "help" if command_name.blank? || HELP_MAPPINGS.include?(command_name) + command_name, namespace, args = "application", "application", ["--help"] if rails_new_with_no_path?(args) command_name, namespace = "version", "version" if %w( -v --version ).include?(command_name) original_argv = ARGV.dup @@ -92,6 +93,10 @@ module Rails COMMANDS_IN_USAGE = %w(generate console server test test:system dbconsole new) private_constant :COMMANDS_IN_USAGE + def rails_new_with_no_path?(args) + args == ["new"] + end + def commands lookup! diff --git a/railties/test/command/application_test.rb b/railties/test/command/application_test.rb new file mode 100644 index 0000000000..c0a414f50d --- /dev/null +++ b/railties/test/command/application_test.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "abstract_unit" +require "rails/command" + +class Rails::Command::ApplicationTest < ActiveSupport::TestCase + test "rails new without path prints help" do + output = capture(:stdout) do + Rails::Command.invoke(:application, %w[new]) + end + + # Doesn't include the default thor error message: + assert_not output.start_with?("No value provided for required arguments") + + # Includes contents of ~/railties/lib/rails/generators/rails/app/USAGE: + assert output.include?("The 'rails new' command creates a new Rails application with a default + directory structure and configuration at the path you specify.") + end +end