Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Rails environment when running tests with Ruby #31355

Merged
merged 4 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'

Expand Down
Original file line number Diff line number Diff line change
@@ -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__)]
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/test_unit/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Runner
class << self
def attach_before_load_options(opts)
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") {}
end

def parse_options(argv)
Expand Down
73 changes: 73 additions & 0 deletions railties/test/application/test_runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,40 @@ def test_rake_passes_TESTOPTS_to_minitest
assert_match "AccountTest#test_truth", output, "passing TEST= should run selected test"
end

def test_running_with_ruby_gets_test_env_by_default
# Subshells inherit `ENV`, so we need to ensure `RAILS_ENV` is set to
# nil before we run the tests in the test app.
re, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], nil

file = create_test_for_env("test")
results = Dir.chdir(app_path) {
`ruby -Ilib:test #{file}`.each_line.map { |line| JSON.parse line }
}
assert_equal 1, results.length
failures = results.first["failures"]
flunk(failures.first) unless failures.empty?

ensure
ENV["RAILS_ENV"] = re
end

def test_running_with_ruby_can_set_env_via_cmdline
# Subshells inherit `ENV`, so we need to ensure `RAILS_ENV` is set to
# nil before we run the tests in the test app.
re, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], nil

file = create_test_for_env("development")
results = Dir.chdir(app_path) {
`RAILS_ENV=development ruby -Ilib:test #{file}`.each_line.map { |line| JSON.parse line }
}
assert_equal 1, results.length
failures = results.first["failures"]
flunk(failures.first) unless failures.empty?

ensure
ENV["RAILS_ENV"] = re
end

def test_rake_passes_multiple_TESTOPTS_to_minitest
create_test_file :models, "account"
output = Dir.chdir(app_path) { `bin/rake test TESTOPTS='-v --seed=1234'` }
Expand Down Expand Up @@ -727,6 +761,45 @@ def create_schema
app_file "db/schema.rb", ""
end

def create_test_for_env(env)
app_file "test/models/environment_test.rb", <<-RUBY
require 'test_helper'
class JSONReporter < Minitest::AbstractReporter
def record(result)
puts JSON.dump(klass: result.class.name,
name: result.name,
failures: result.failures,
assertions: result.assertions,
time: result.time)
end
end

def Minitest.plugin_json_reporter_init(opts)
Minitest.reporter.reporters.clear
Minitest.reporter.reporters << JSONReporter.new
end

Minitest.extensions << "rails"
Minitest.extensions << "json_reporter"

# Minitest uses RubyGems to find plugins, and since RubyGems
# doesn't know about the Rails installation we're pointing at,
# Minitest won't require the Rails minitest plugin when we run
# these integration tests. So we have to manually require the
# Minitest plugin here.
require 'minitest/rails_plugin'

class EnvironmentTest < ActiveSupport::TestCase
def test_environment
test_db = ActiveRecord::Base.configurations[#{env.dump}]["database"]
db_file = ActiveRecord::Base.connection_config[:database]
assert_match(test_db, db_file)
assert_equal #{env.dump}, ENV["RAILS_ENV"]
end
end
RUBY
end

def create_test_file(path = :unit, name = "test", pass: true)
app_file "test/#{path}/#{name}_test.rb", <<-RUBY
require 'test_helper'
Expand Down
6 changes: 4 additions & 2 deletions railties/test/isolation/abstract_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,12 @@ def remove_from_file(file, str)
end

def app_file(path, contents, mode = "w")
FileUtils.mkdir_p File.dirname("#{app_path}/#{path}")
File.open("#{app_path}/#{path}", mode) do |f|
file_name = "#{app_path}/#{path}"
FileUtils.mkdir_p File.dirname(file_name)
File.open(file_name, mode) do |f|
f.puts contents
end
file_name
end

def remove_file(path)
Expand Down