1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Abort early if generator command fails (#34420)

* No need to go through ruby

* Abort early if a generator command fails

* Reuse `rails_command` method

* Bump thor minimum dependency to 0.20.3

* Add some minimal docs

* Add a changelog entry

* Restore original logging
This commit is contained in:
David Rodríguez 2018-12-07 07:01:32 +01:00 committed by Yuji Yaginuma
parent b86f65a816
commit f173ec77fc
6 changed files with 33 additions and 6 deletions

View file

@ -93,7 +93,7 @@ PATH
activesupport (= 6.0.0.alpha) activesupport (= 6.0.0.alpha)
method_source method_source
rake (>= 0.8.7) rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0) thor (>= 0.20.3, < 2.0)
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
@ -304,7 +304,7 @@ GEM
marcel (0.3.3) marcel (0.3.3)
mimemagic (~> 0.3.2) mimemagic (~> 0.3.2)
memoist (0.16.0) memoist (0.16.0)
method_source (0.9.1) method_source (0.9.2)
mime-types (3.2.2) mime-types (3.2.2)
mime-types-data (~> 3.2015) mime-types-data (~> 3.2015)
mime-types-data (3.2018.0812) mime-types-data (3.2018.0812)

View file

@ -195,6 +195,12 @@ You can also run commands as a super-user:
rails_command "log:clear", sudo: true rails_command "log:clear", sudo: true
``` ```
You can also run commands that should abort application generation if they fail:
```ruby
rails_command "db:migrate", abort_on_failure: true
```
### route(routing_code) ### route(routing_code)
Adds a routing entry to the `config/routes.rb` file. In the steps above, we generated a person scaffold and also removed `README.rdoc`. Now, to make `PeopleController#index` the default page for the application: Adds a routing entry to the `config/routes.rb` file. In the steps above, we generated a person scaffold and also removed `README.rdoc`. Now, to make `PeopleController#index` the default page for the application:

View file

@ -1,3 +1,9 @@
* Add an `abort_on_failure` boolean option to the generator method that shell
out (`generate`, `rake`, `rails_command`) to abort the generator if the
command fails.
*David Rodríguez*
* Remove `app/assets` and `app/javascript` from `eager_load_paths` and `autoload_paths`. * Remove `app/assets` and `app/javascript` from `eager_load_paths` and `autoload_paths`.
*Gannon McGibbon* *Gannon McGibbon*

View file

@ -221,9 +221,11 @@ module Rails
# generate(:authenticated, "user session") # generate(:authenticated, "user session")
def generate(what, *args) def generate(what, *args)
log :generate, what log :generate, what
options = args.extract_options!
argument = args.flat_map(&:to_s).join(" ") argument = args.flat_map(&:to_s).join(" ")
in_root { run_ruby_script("bin/rails generate #{what} #{argument}", verbose: false) } execute_command :rails, "generate #{what} #{argument}", options
end end
# Runs the supplied rake task (invoked with 'rake ...') # Runs the supplied rake task (invoked with 'rake ...')
@ -307,6 +309,7 @@ module Rails
config = { verbose: false } config = { verbose: false }
config[:capture] = options[:capture] if options[:capture] config[:capture] = options[:capture] if options[:capture]
config[:abort_on_failure] = options[:abort_on_failure] if options[:abort_on_failure]
in_root { run("#{sudo}#{extify(executor)} #{command} RAILS_ENV=#{env}", config) } in_root { run("#{sudo}#{extify(executor)} #{command} RAILS_ENV=#{env}", config) }
end end

View file

@ -37,7 +37,7 @@ Gem::Specification.new do |s|
s.add_dependency "actionpack", version s.add_dependency "actionpack", version
s.add_dependency "rake", ">= 0.8.7" s.add_dependency "rake", ">= 0.8.7"
s.add_dependency "thor", ">= 0.19.0", "< 2.0" s.add_dependency "thor", ">= 0.20.3", "< 2.0"
s.add_dependency "method_source" s.add_dependency "method_source"
s.add_development_dependency "actionview", version s.add_development_dependency "actionview", version

View file

@ -303,9 +303,21 @@ class ActionsTest < Rails::Generators::TestCase
end end
def test_generate_should_run_script_generate_with_argument_and_options def test_generate_should_run_script_generate_with_argument_and_options
assert_called_with(generator, :run_ruby_script, ["bin/rails generate model MyModel", verbose: false]) do run_generator
action :generate, "model", "MyModel" action :generate, "model", "MyModel"
assert_file "app/models/my_model.rb", /MyModel/
end
def test_generate_aborts_when_subprocess_fails_if_requested
run_generator
content = capture(:stderr) do
assert_raises SystemExit do
action :generate, "model", "MyModel:ADsad", abort_on_failure: true
action :generate, "model", "MyModel"
end
end end
assert_match(/wrong constant name MyModel:aDsad/, content)
assert_no_file "app/models/my_model.rb"
end end
def test_rake_should_run_rake_command_with_default_env def test_rake_should_run_rake_command_with_default_env