Skip to content

Commit f173ec7

Browse files
deivid-rodriguezy-yagi
authored andcommitted
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
1 parent b86f65a commit f173ec7

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ PATH
9393
activesupport (= 6.0.0.alpha)
9494
method_source
9595
rake (>= 0.8.7)
96-
thor (>= 0.19.0, < 2.0)
96+
thor (>= 0.20.3, < 2.0)
9797

9898
GEM
9999
remote: https://rubygems.org/
@@ -304,7 +304,7 @@ GEM
304304
marcel (0.3.3)
305305
mimemagic (~> 0.3.2)
306306
memoist (0.16.0)
307-
method_source (0.9.1)
307+
method_source (0.9.2)
308308
mime-types (3.2.2)
309309
mime-types-data (~> 3.2015)
310310
mime-types-data (3.2018.0812)

guides/source/rails_application_templates.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ You can also run commands as a super-user:
195195
rails_command "log:clear", sudo: true
196196
```
197197

198+
You can also run commands that should abort application generation if they fail:
199+
200+
```ruby
201+
rails_command "db:migrate", abort_on_failure: true
202+
```
203+
198204
### route(routing_code)
199205

200206
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:

railties/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Add an `abort_on_failure` boolean option to the generator method that shell
2+
out (`generate`, `rake`, `rails_command`) to abort the generator if the
3+
command fails.
4+
5+
*David Rodríguez*
6+
17
* Remove `app/assets` and `app/javascript` from `eager_load_paths` and `autoload_paths`.
28

39
*Gannon McGibbon*

railties/lib/rails/generators/actions.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,11 @@ def initializer(filename, data = nil)
221221
# generate(:authenticated, "user session")
222222
def generate(what, *args)
223223
log :generate, what
224+
225+
options = args.extract_options!
224226
argument = args.flat_map(&:to_s).join(" ")
225227

226-
in_root { run_ruby_script("bin/rails generate #{what} #{argument}", verbose: false) }
228+
execute_command :rails, "generate #{what} #{argument}", options
227229
end
228230

229231
# Runs the supplied rake task (invoked with 'rake ...')
@@ -307,6 +309,7 @@ def execute_command(executor, command, options = {}) # :doc:
307309
config = { verbose: false }
308310

309311
config[:capture] = options[:capture] if options[:capture]
312+
config[:abort_on_failure] = options[:abort_on_failure] if options[:abort_on_failure]
310313

311314
in_root { run("#{sudo}#{extify(executor)} #{command} RAILS_ENV=#{env}", config) }
312315
end

railties/railties.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
3737
s.add_dependency "actionpack", version
3838

3939
s.add_dependency "rake", ">= 0.8.7"
40-
s.add_dependency "thor", ">= 0.19.0", "< 2.0"
40+
s.add_dependency "thor", ">= 0.20.3", "< 2.0"
4141
s.add_dependency "method_source"
4242

4343
s.add_development_dependency "actionview", version

railties/test/generators/actions_test.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,21 @@ def test_initializer_should_write_date_to_file_with_block_in_config_initializers
303303
end
304304

305305
def test_generate_should_run_script_generate_with_argument_and_options
306-
assert_called_with(generator, :run_ruby_script, ["bin/rails generate model MyModel", verbose: false]) do
307-
action :generate, "model", "MyModel"
306+
run_generator
307+
action :generate, "model", "MyModel"
308+
assert_file "app/models/my_model.rb", /MyModel/
309+
end
310+
311+
def test_generate_aborts_when_subprocess_fails_if_requested
312+
run_generator
313+
content = capture(:stderr) do
314+
assert_raises SystemExit do
315+
action :generate, "model", "MyModel:ADsad", abort_on_failure: true
316+
action :generate, "model", "MyModel"
317+
end
308318
end
319+
assert_match(/wrong constant name MyModel:aDsad/, content)
320+
assert_no_file "app/models/my_model.rb"
309321
end
310322

311323
def test_rake_should_run_rake_command_with_default_env

0 commit comments

Comments
 (0)