From da225c0db640aec1df0920e86c1eb4ca35d82073 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 6 Dec 2017 17:33:16 -0800 Subject: Fix Rails environment when running tests with Ruby I frequently run tests with `ruby`, not with a runner like `rake` or `rails`. When running the test with just `ruby` the `RAILS_ENV` environment variable did not get set to "test", and this would cause the tests to fail (and even mutate the development database!) This commit adds integration tests for running tests with just `ruby` and ensures the environment gets defaulted to "test". I also added a test to ensure that passing an environment to `-e` actually works (and fixed that case too). An interesting / annoying thing is that Minitest picks up it's plugins by asking RubyGems for a list of files: https://github.com/seattlerb/minitest/blob/ca6a71ca901016db09a5ad466b4adea4b52a504a/lib/minitest.rb#L92-L100 This means that RubyGems needs to somehow know about the file before it can return it to Minitest. Since we are not packaging Rails as a Gem before running the integration tests on it (duh, why would you do that?), RubyGems doesn't know about the file, so it can't tell Minitest, so Minitest doesn't automatically require it. This means I had to manually require and insert the plugin in our integration test. I've left comments about that in the test as well. Ugh. --- railties/lib/rails/test_unit/runner.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index 5c2f6451e2..45e8472f6a 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -12,8 +12,11 @@ module Rails class << self def attach_before_load_options(opts) + ENV["RAILS_ENV"] = "test" opts.on("--warnings", "-w", "Run with Ruby warnings enabled") {} - opts.on("--environment", "-e", "Run tests in the ENV environment") {} + opts.on("-e", "--environment ENV", "Run tests in the ENV environment") { |e| + ENV["RAILS_ENV"] = e + } end def parse_options(argv) -- cgit v1.2.3 From cd0283ef29eaa4a8eb9c90d6c2efaccb20012a20 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 8 Dec 2017 13:27:30 -0800 Subject: Revert "remove unnecessary `RAILS_ENV` setting" This reverts commit 9a80f52541ed2c93ebef02909ecab3aaf9127150. --- .../lib/rails/generators/rails/app/templates/test/test_helper.rb.tt | 1 + .../lib/rails/generators/rails/plugin/templates/test/test_helper.rb.tt | 3 +++ 2 files changed, 4 insertions(+) (limited to 'railties/lib') diff --git a/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb.tt b/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb.tt index 6ad1f11781..52d68cc77c 100644 --- a/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb.tt @@ -1,3 +1,4 @@ +ENV['RAILS_ENV'] ||= 'test' require_relative '../config/environment' require 'rails/test_help' diff --git a/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb.tt b/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb.tt index 7fa9973931..755d19ef5d 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb.tt +++ b/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb.tt @@ -1,3 +1,6 @@ +# Configure Rails Environment +ENV["RAILS_ENV"] = "test" + require_relative "<%= File.join('..', options[:dummy_path], 'config/environment') -%>" <% unless options[:skip_active_record] -%> ActiveRecord::Migrator.migrations_paths = [File.expand_path("../<%= options[:dummy_path] -%>/db/migrate", __dir__)] -- cgit v1.2.3 From a50b8ea3503f5642330c1e6b94320115796b4bab Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 8 Dec 2017 13:42:01 -0800 Subject: Set the Rails environment from an environment variable Option parsing happens too late to have any impact on the Rails environment. Rails accesses the environment name and memoizes it too early in the boot process for a commandline option to have any impact on the database connection, so we'll change this test to set the environment from an environment variable (and ensure it still works when running tests with `ruby`) --- railties/lib/rails/test_unit/runner.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'railties/lib') diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index 45e8472f6a..de5744c662 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -12,11 +12,8 @@ module Rails class << self def attach_before_load_options(opts) - ENV["RAILS_ENV"] = "test" opts.on("--warnings", "-w", "Run with Ruby warnings enabled") {} - opts.on("-e", "--environment ENV", "Run tests in the ENV environment") { |e| - ENV["RAILS_ENV"] = e - } + opts.on("-e", "--environment ENV", "Run tests in the ENV environment") {} end def parse_options(argv) -- cgit v1.2.3