提交 5a8f25f0 编写于 作者: C Carlos Antonio da Silva

Refactor tests that switch RAILS_ENV and RACK_ENV

This cleanup aims to fix a build failure:
https://travis-ci.org/rails/rails/jobs/3515951/#L482

Since travis always have both ENV vars set to "test", a test is failing
where it's expected to output the default env "development", but "test"
is the result due to RACK_ENV being set when we expect it to not be.

By cleaning this duplication we ensure that changing any of these env
variables will pick the right expected value.
上级 8942035f
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
require 'fileutils' require 'fileutils'
require 'active_support' require 'active_support'
require 'action_controller' require 'action_controller'
require 'rails/all' require 'rails/all'
......
require 'isolation/abstract_unit' require 'isolation/abstract_unit'
require 'env_helpers'
module ApplicationTests module ApplicationTests
class RunnerTest < ActiveSupport::TestCase class RunnerTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation include ActiveSupport::Testing::Isolation
include EnvHelpers
def setup def setup
build_app build_app
...@@ -73,21 +75,15 @@ def test_default_environment ...@@ -73,21 +75,15 @@ def test_default_environment
end end
def test_environment_with_rails_env def test_environment_with_rails_env
orig = ENV['RAILS_ENV'] with_rails_env "production" do
ENV['RAILS_ENV'] = "production" assert_match "production", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.env"` }
assert_match "production", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.env"` } end
ensure
ENV['RAILS_ENV'] = orig
end end
def test_environment_with_rack_env def test_environment_with_rack_env
rack, rails = ENV['RACK_ENV'], ENV['RAILS_ENV'] with_rack_env "production" do
ENV['RACK_ENV'] = "production" assert_match "production", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.env"` }
ENV['RAILS_ENV'] = nil end
assert_match "production", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.env"` }
ensure
ENV['RAILS_ENV'] = rails
ENV['RACK_ENV'] = rack
end end
end end
end end
require 'abstract_unit' require 'abstract_unit'
require 'env_helpers'
require 'rails/commands/console' require 'rails/commands/console'
class Rails::ConsoleTest < ActiveSupport::TestCase class Rails::ConsoleTest < ActiveSupport::TestCase
include EnvHelpers
class FakeConsole class FakeConsole
def self.start; end def self.start; end
end end
def setup
end
def test_sandbox_option def test_sandbox_option
console = Rails::Console.new(app, parse_arguments(["--sandbox"])) console = Rails::Console.new(app, parse_arguments(["--sandbox"]))
assert console.sandbox? assert console.sandbox?
...@@ -85,7 +85,7 @@ def test_default_environment_with_rack_env ...@@ -85,7 +85,7 @@ def test_default_environment_with_rack_env
assert_match(/\sproduction\s/, output) assert_match(/\sproduction\s/, output)
end end
end end
def test_e_option def test_e_option
start ['-e', 'special-production'] start ['-e', 'special-production']
assert_match(/\sspecial-production\s/, output) assert_match(/\sspecial-production\s/, output)
...@@ -133,20 +133,4 @@ def app ...@@ -133,20 +133,4 @@ def app
def parse_arguments(args) def parse_arguments(args)
Rails::Console.parse_arguments(args) Rails::Console.parse_arguments(args)
end end
def with_rails_env(env)
rails = ENV['RAILS_ENV']
ENV['RAILS_ENV'] = env
yield
ensure
ENV['RAILS_ENV'] = rails
end
def with_rack_env(env)
rack = ENV['RACK_ENV']
ENV['RACK_ENV'] = env
with_rails_env(nil) { yield }
ensure
ENV['RACK_ENV'] = rack
end
end end
require 'abstract_unit' require 'abstract_unit'
require 'env_helpers'
require 'rails/commands/server' require 'rails/commands/server'
class Rails::ServerTest < ActiveSupport::TestCase class Rails::ServerTest < ActiveSupport::TestCase
include EnvHelpers
def test_environment_with_server_option def test_environment_with_server_option
args = ["thin", "-e", "production"] args = ["thin", "-e", "production"]
...@@ -25,22 +27,16 @@ def test_server_option_without_environment ...@@ -25,22 +27,16 @@ def test_server_option_without_environment
end end
def test_environment_with_rails_env def test_environment_with_rails_env
rails = ENV['RAILS_ENV'] with_rails_env 'production' do
ENV['RAILS_ENV'] = 'production' server = Rails::Server.new
server = Rails::Server.new assert_equal 'production', server.options[:environment]
assert_equal 'production', server.options[:environment] end
ensure
ENV['RAILS_ENV'] = rails
end end
def test_environment_with_rack_env def test_environment_with_rack_env
rack, rails = ENV['RACK_ENV'], ENV['RAILS_ENV'] with_rack_env 'production' do
ENV['RAILS_ENV'] = nil server = Rails::Server.new
ENV['RACK_ENV'] = 'production' assert_equal 'production', server.options[:environment]
server = Rails::Server.new end
assert_equal 'production', server.options[:environment]
ensure
ENV['RACK_ENV'] = rack
ENV['RAILS_ENV'] = rails
end end
end end
module EnvHelpers
private
def with_rails_env(env)
switch_env 'RAILS_ENV', env do
switch_env 'RACK_ENV', nil do
yield
end
end
end
def with_rack_env(env)
switch_env 'RACK_ENV', env do
switch_env 'RAILS_ENV', nil do
yield
end
end
end
def switch_env(key, value)
old, ENV[key] = ENV[key], value
yield
ensure
ENV[key] = old
end
end
require 'generators/generators_test_helper' require 'generators/generators_test_helper'
require 'rails/generators/rails/app/app_generator' require 'rails/generators/rails/app/app_generator'
require 'env_helpers'
class ActionsTest < Rails::Generators::TestCase class ActionsTest < Rails::Generators::TestCase
include GeneratorsTestHelper include GeneratorsTestHelper
include EnvHelpers
tests Rails::Generators::AppGenerator tests Rails::Generators::AppGenerator
arguments [destination_root] arguments [destination_root]
...@@ -154,10 +157,9 @@ def test_generate_should_run_script_generate_with_argument_and_options ...@@ -154,10 +157,9 @@ def test_generate_should_run_script_generate_with_argument_and_options
def test_rake_should_run_rake_command_with_default_env def test_rake_should_run_rake_command_with_default_env
generator.expects(:run).once.with("rake log:clear RAILS_ENV=development", verbose: false) generator.expects(:run).once.with("rake log:clear RAILS_ENV=development", verbose: false)
old_env, ENV['RAILS_ENV'] = ENV["RAILS_ENV"], nil with_rails_env nil do
action :rake, 'log:clear' action :rake, 'log:clear'
ensure end
ENV["RAILS_ENV"] = old_env
end end
def test_rake_with_env_option_should_run_rake_command_in_env def test_rake_with_env_option_should_run_rake_command_in_env
...@@ -167,26 +169,23 @@ def test_rake_with_env_option_should_run_rake_command_in_env ...@@ -167,26 +169,23 @@ def test_rake_with_env_option_should_run_rake_command_in_env
def test_rake_with_rails_env_variable_should_run_rake_command_in_env def test_rake_with_rails_env_variable_should_run_rake_command_in_env
generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', verbose: false) generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', verbose: false)
old_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "production" with_rails_env "production" do
action :rake, 'log:clear' action :rake, 'log:clear'
ensure end
ENV["RAILS_ENV"] = old_env
end end
def test_env_option_should_win_over_rails_env_variable_when_running_rake def test_env_option_should_win_over_rails_env_variable_when_running_rake
generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', verbose: false) generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', verbose: false)
old_env, ENV["RAILS_ENV"] = ENV["RAILS_ENV"], "staging" with_rails_env "staging" do
action :rake, 'log:clear', env: 'production' action :rake, 'log:clear', env: 'production'
ensure end
ENV["RAILS_ENV"] = old_env
end end
def test_rake_with_sudo_option_should_run_rake_command_with_sudo def test_rake_with_sudo_option_should_run_rake_command_with_sudo
generator.expects(:run).once.with("sudo rake log:clear RAILS_ENV=development", verbose: false) generator.expects(:run).once.with("sudo rake log:clear RAILS_ENV=development", verbose: false)
old_env, ENV['RAILS_ENV'] = ENV["RAILS_ENV"], nil with_rails_env nil do
action :rake, 'log:clear', sudo: true action :rake, 'log:clear', sudo: true
ensure end
ENV["RAILS_ENV"] = old_env
end end
def test_capify_should_run_the_capify_command def test_capify_should_run_the_capify_command
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册