Skip to content

Commit fe9abe4

Browse files
Leverage new :env option for Thor::Actions#run
Follow-up to rails#34980. Also, refactor tests to be less brittle.
1 parent 858e429 commit fe9abe4

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

Gemfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ PATH
9595
activesupport (= 6.1.0.alpha)
9696
method_source
9797
rake (>= 0.8.7)
98-
thor (>= 0.20.3, < 2.0)
98+
thor (~> 1.0)
9999

100100
GEM
101101
remote: https://rubygems.org/
@@ -487,7 +487,7 @@ GEM
487487
daemons (~> 1.0, >= 1.0.9)
488488
eventmachine (~> 1.0, >= 1.0.4)
489489
rack (>= 1, < 3)
490-
thor (0.20.3)
490+
thor (1.0.0)
491491
thread_safe (0.3.6)
492492
thread_safe (0.3.6-java)
493493
tilt (2.0.10)

railties/lib/rails/generators/actions.rb

+8-9
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ def generate(what, *args)
225225
log :generate, what
226226

227227
options = args.extract_options!
228-
options[:without_rails_env] = true
229228
argument = args.flat_map(&:to_s).join(" ")
230229

231230
execute_command :rails, "generate #{what} #{argument}", options
@@ -292,15 +291,15 @@ def log(*args) # :doc:
292291
# based on the executor parameter provided.
293292
def execute_command(executor, command, options = {}) # :doc:
294293
log executor, command
295-
env = options[:env] || ENV["RAILS_ENV"] || "development"
296-
rails_env = " RAILS_ENV=#{env}" unless options[:without_rails_env]
297294
sudo = options[:sudo] && !Gem.win_platform? ? "sudo " : ""
298-
config = { verbose: false }
299-
300-
config[:capture] = options[:capture] if options[:capture]
301-
config[:abort_on_failure] = options[:abort_on_failure] if options[:abort_on_failure]
302-
303-
in_root { run("#{sudo}#{extify(executor)} #{command}#{rails_env}", config) }
295+
config = {
296+
env: { "RAILS_ENV" => (options[:env] || ENV["RAILS_ENV"] || "development") },
297+
verbose: false,
298+
capture: options[:capture],
299+
abort_on_failure: options[:abort_on_failure],
300+
}
301+
302+
in_root { run("#{sudo}#{extify(executor)} #{command}", config) }
304303
end
305304

306305
# Add an extension to the given name based on the platform.

railties/railties.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Gem::Specification.new do |s|
4040
s.add_dependency "actionpack", version
4141

4242
s.add_dependency "rake", ">= 0.8.7"
43-
s.add_dependency "thor", ">= 0.20.3", "< 2.0"
43+
s.add_dependency "thor", "~> 1.0"
4444
s.add_dependency "method_source"
4545

4646
s.add_development_dependency "actionview", version

railties/test/generators/actions_test.rb

+39-38
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,13 @@ def test_environment_should_include_block_contents_with_multiline_data_in_enviro
307307
end
308308

309309
def test_git_with_symbol_should_run_command_using_git_scm
310-
assert_called_with(generator, :run, ["git init"]) do
310+
assert_runs "git init", nil do
311311
action :git, :init
312312
end
313313
end
314314

315315
def test_git_with_hash_should_run_each_command_using_git_scm
316-
assert_called_with(generator, :run, [ ["git rm README"], ["git add ."] ]) do
316+
assert_runs ["git rm README", "git add ."], nil do
317317
action :git, rm: "README", add: "."
318318
end
319319
end
@@ -396,98 +396,88 @@ def test_generate_aborts_when_subprocess_fails_if_requested
396396
assert_no_file "app/models/my_model.rb"
397397
end
398398

399-
def test_generate_should_run_command_without_env
400-
assert_called_with(generator, :run, ["rails generate model MyModel name:string", verbose: false]) do
401-
action :generate, "model", "MyModel", "name:string"
402-
end
403-
end
404-
405-
def test_rake_should_run_rake_command_with_default_env
406-
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=development", verbose: false]) do
399+
test "rake should run rake with the default environment" do
400+
assert_runs "rake log:clear", env: { "RAILS_ENV" => "development" } do
407401
with_rails_env nil do
408402
action :rake, "log:clear"
409403
end
410404
end
411405
end
412406

413-
def test_rake_with_env_option_should_run_rake_command_in_env
414-
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=production", verbose: false]) do
407+
test "rake with env option should run rake with the env environment" do
408+
assert_runs "rake log:clear", env: { "RAILS_ENV" => "production" } do
415409
action :rake, "log:clear", env: "production"
416410
end
417411
end
418412

419-
test "rake with RAILS_ENV variable should run rake command in env" do
420-
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=production", verbose: false]) do
413+
test "rake with RAILS_ENV set should run rake with the RAILS_ENV environment" do
414+
assert_runs "rake log:clear", env: { "RAILS_ENV" => "production" } do
421415
with_rails_env "production" do
422416
action :rake, "log:clear"
423417
end
424418
end
425419
end
426420

427-
test "env option should win over RAILS_ENV variable when running rake" do
428-
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=production", verbose: false]) do
421+
test "rake with env option and RAILS_ENV set should run rake with the env environment" do
422+
assert_runs "rake log:clear", env: { "RAILS_ENV" => "production" } do
429423
with_rails_env "staging" do
430424
action :rake, "log:clear", env: "production"
431425
end
432426
end
433427
end
434428

435-
test "rake with sudo option should run rake command with sudo" do
436-
assert_called_with(generator, :run, ["sudo rake log:clear RAILS_ENV=development", verbose: false]) do
437-
with_rails_env nil do
438-
action :rake, "log:clear", sudo: true
439-
end
429+
test "rake with sudo option should run rake with sudo" do
430+
assert_runs "sudo rake log:clear" do
431+
action :rake, "log:clear", sudo: true
440432
end
441433
end
442434

443-
test "rake command with capture option should run rake command with capture" do
444-
assert_called_with(generator, :run, ["rake log:clear RAILS_ENV=development", verbose: false, capture: true]) do
445-
with_rails_env nil do
446-
action :rake, "log:clear", capture: true
447-
end
435+
test "rake with capture option should run rake with capture" do
436+
assert_runs "rake log:clear", capture: true do
437+
action :rake, "log:clear", capture: true
448438
end
449439
end
450440

451-
test "rails command should run rails_command with default env" do
452-
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=development", verbose: false]) do
441+
test "rails_command should run rails with the default environment" do
442+
assert_runs "rails log:clear", env: { "RAILS_ENV" => "development" } do
453443
with_rails_env nil do
454444
action :rails_command, "log:clear"
455445
end
456446
end
457447
end
458448

459-
test "rails command with env option should run rails_command with same env" do
460-
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=production", verbose: false]) do
449+
test "rails_command with env option should run rails with the env environment" do
450+
assert_runs "rails log:clear", env: { "RAILS_ENV" => "production" } do
461451
action :rails_command, "log:clear", env: "production"
462452
end
463453
end
464454

465-
test "rails command with RAILS_ENV variable should run rails_command in env" do
466-
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=production", verbose: false]) do
455+
test "rails_command with RAILS_ENV set should run rails with the RAILS_ENV environment" do
456+
assert_runs "rails log:clear", env: { "RAILS_ENV" => "production" } do
467457
with_rails_env "production" do
468458
action :rails_command, "log:clear"
469459
end
470460
end
471461
end
472462

473-
def test_env_option_should_win_over_rails_env_variable_when_running_rails
474-
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=production", verbose: false]) do
463+
test "rails_command with env option and RAILS_ENV set should run rails with the env environment" do
464+
assert_runs "rails log:clear", env: { "RAILS_ENV" => "production" } do
475465
with_rails_env "staging" do
476466
action :rails_command, "log:clear", env: "production"
477467
end
478468
end
479469
end
480470

481-
test "rails command with sudo option should run rails_command with sudo" do
482-
assert_called_with(generator, :run, ["sudo rails log:clear RAILS_ENV=development", verbose: false]) do
471+
test "rails_command with sudo option should run rails with sudo" do
472+
assert_runs "sudo rails log:clear" do
483473
with_rails_env nil do
484474
action :rails_command, "log:clear", sudo: true
485475
end
486476
end
487477
end
488478

489-
test "rails command with capture option should run rails_command with capture" do
490-
assert_called_with(generator, :run, ["rails log:clear RAILS_ENV=development", verbose: false, capture: true]) do
479+
test "rails_command with capture option should run rails with capture" do
480+
assert_runs "rails log:clear", capture: true do
491481
with_rails_env nil do
492482
action :rails_command, "log:clear", capture: true
493483
end
@@ -587,6 +577,17 @@ def action(*args, &block)
587577
capture(:stdout) { generator.send(*args, &block) }
588578
end
589579

580+
def assert_runs(commands, config = {}, &block)
581+
config_matcher = ->(actual_config) do
582+
assert_equal config, actual_config.slice(*config.keys)
583+
end if config
584+
args = Array(commands).map { |command| [command, *config_matcher] }
585+
586+
assert_called_with(generator, :run, args) do
587+
block.call
588+
end
589+
end
590+
590591
def assert_routes(*route_commands)
591592
route_regexps = route_commands.flatten.map do |route_command|
592593
%r{

0 commit comments

Comments
 (0)