Skip to content

Commit 0d75d36

Browse files
authored
Rails.app_env: Unfollow Rails.env changes (#13)
Modifying `Rails.app_env` after loading config and credentials causes confusions.
1 parent 15a7183 commit 0d75d36

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

lib/rails/app_env/helpers.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ module Rails
22
module AppEnv
33
module Helpers
44
def app_env
5-
return Rails.env if ENV["APP_ENV"].blank?
6-
7-
@_app_env ||= EnvironmentInquirer.new(ENV["APP_ENV"])
5+
@_app_env ||= EnvironmentInquirer.new(ENV["APP_ENV"] || Rails.env)
86
end
97
end
108
end

test/features/app_env_test.rb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ module Rails::AppEnv::FeaturesTest
55
class AppEnvTest < ActiveSupport::TestCase
66
include EnvHelpers
77

8+
def setup
9+
Rails.instance_variable_set :@_app_env, nil
10+
end
11+
812
test "Rails.app_env is a kind of ActiveSupport::EnvironmentInquirer when APP_ENV is present" do
913
switch_env "APP_ENV", "foo" do
1014
assert_kind_of ActiveSupport::EnvironmentInquirer, Rails.app_env
@@ -46,7 +50,6 @@ class AppEnvTest < ActiveSupport::TestCase
4650
with_rails_env("foo") do
4751
assert_equal "foo", Rails.app_env
4852
assert_equal "foo", Rails.env
49-
assert_same Rails.env, Rails.app_env
5053
end
5154
end
5255
end
@@ -56,14 +59,16 @@ class AppEnvTest < ActiveSupport::TestCase
5659
with_rails_env(nil) do
5760
assert_equal DEFAULT_RAILS_ENV, Rails.app_env
5861
assert_equal DEFAULT_RAILS_ENV, Rails.env
59-
assert_same Rails.env, Rails.app_env
6062
end
6163
end
6264
end
6365

6466
test "Rails.app_env does not follow Rails.env changes when both APP_ENV and RAILS_ENV are present" do
6567
switch_env "APP_ENV", "foo" do
6668
with_rails_env("bar") do
69+
assert_equal "foo", Rails.app_env
70+
assert_equal "bar", Rails.env
71+
6772
Rails.env = "baz"
6873

6974
assert_equal "foo", Rails.app_env
@@ -75,6 +80,9 @@ class AppEnvTest < ActiveSupport::TestCase
7580
test "Rails.app_env does not follow Rails.env changes when APP_ENV is present and RAILS_ENV is blank" do
7681
switch_env "APP_ENV", "foo" do
7782
with_rails_env(nil) do
83+
assert_equal "foo", Rails.app_env
84+
assert_equal DEFAULT_RAILS_ENV, Rails.env
85+
7886
Rails.env = "bar"
7987

8088
assert_equal "foo", Rails.app_env
@@ -83,26 +91,30 @@ class AppEnvTest < ActiveSupport::TestCase
8391
end
8492
end
8593

86-
test "Rails.app_env follows Rails.env changes when APP_ENV is blank but RAILS_ENV is present" do
94+
test "Rails.app_env does not follow Rails.env changes when APP_ENV is blank but RAILS_ENV is present" do
8795
switch_env "APP_ENV", nil do
8896
with_rails_env("foo") do
97+
assert_equal "foo", Rails.app_env
98+
assert_equal "foo", Rails.env
99+
89100
Rails.env = "bar"
90101

91-
assert_equal "bar", Rails.app_env
102+
assert_equal "foo", Rails.app_env
92103
assert_equal "bar", Rails.env
93-
assert_same Rails.env, Rails.app_env
94104
end
95105
end
96106
end
97107

98-
test "Rails.app_env follows Rails.env changes when both APP_ENV and RAILS_ENV are blank" do
108+
test "Rails.app_env does not follow Rails.env changes when both APP_ENV and RAILS_ENV are blank" do
99109
switch_env "APP_ENV", nil do
100110
with_rails_env(nil) do
111+
assert_equal DEFAULT_RAILS_ENV, Rails.app_env
112+
assert_equal DEFAULT_RAILS_ENV, Rails.env
113+
101114
Rails.env = "foo"
102115

103-
assert_equal "foo", Rails.app_env
116+
assert_equal DEFAULT_RAILS_ENV, Rails.app_env
104117
assert_equal "foo", Rails.env
105-
assert_same Rails.env, Rails.app_env
106118
end
107119
end
108120
end

0 commit comments

Comments
 (0)