Skip to content

Rails.app_env: Unfollow Rails.env changes #13

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

Merged
merged 1 commit into from
Jun 2, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions lib/rails/app_env/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ module Rails
module AppEnv
module Helpers
def app_env
return Rails.env if ENV["APP_ENV"].blank?

@_app_env ||= EnvironmentInquirer.new(ENV["APP_ENV"])
@_app_env ||= EnvironmentInquirer.new(ENV["APP_ENV"] || Rails.env)
end
end
end
Expand Down
28 changes: 20 additions & 8 deletions test/features/app_env_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module Rails::AppEnv::FeaturesTest
class AppEnvTest < ActiveSupport::TestCase
include EnvHelpers

def setup
Rails.instance_variable_set :@_app_env, nil
end

test "Rails.app_env is a kind of ActiveSupport::EnvironmentInquirer when APP_ENV is present" do
switch_env "APP_ENV", "foo" do
assert_kind_of ActiveSupport::EnvironmentInquirer, Rails.app_env
Expand Down Expand Up @@ -46,7 +50,6 @@ class AppEnvTest < ActiveSupport::TestCase
with_rails_env("foo") do
assert_equal "foo", Rails.app_env
assert_equal "foo", Rails.env
assert_same Rails.env, Rails.app_env
end
end
end
Expand All @@ -56,14 +59,16 @@ class AppEnvTest < ActiveSupport::TestCase
with_rails_env(nil) do
assert_equal DEFAULT_RAILS_ENV, Rails.app_env
assert_equal DEFAULT_RAILS_ENV, Rails.env
assert_same Rails.env, Rails.app_env
end
end
end

test "Rails.app_env does not follow Rails.env changes when both APP_ENV and RAILS_ENV are present" do
switch_env "APP_ENV", "foo" do
with_rails_env("bar") do
assert_equal "foo", Rails.app_env
assert_equal "bar", Rails.env

Rails.env = "baz"

assert_equal "foo", Rails.app_env
Expand All @@ -75,6 +80,9 @@ class AppEnvTest < ActiveSupport::TestCase
test "Rails.app_env does not follow Rails.env changes when APP_ENV is present and RAILS_ENV is blank" do
switch_env "APP_ENV", "foo" do
with_rails_env(nil) do
assert_equal "foo", Rails.app_env
assert_equal DEFAULT_RAILS_ENV, Rails.env

Rails.env = "bar"

assert_equal "foo", Rails.app_env
Expand All @@ -83,26 +91,30 @@ class AppEnvTest < ActiveSupport::TestCase
end
end

test "Rails.app_env follows Rails.env changes when APP_ENV is blank but RAILS_ENV is present" do
test "Rails.app_env does not follow Rails.env changes when APP_ENV is blank but RAILS_ENV is present" do
switch_env "APP_ENV", nil do
with_rails_env("foo") do
assert_equal "foo", Rails.app_env
assert_equal "foo", Rails.env

Rails.env = "bar"

assert_equal "bar", Rails.app_env
assert_equal "foo", Rails.app_env
assert_equal "bar", Rails.env
assert_same Rails.env, Rails.app_env
end
end
end

test "Rails.app_env follows Rails.env changes when both APP_ENV and RAILS_ENV are blank" do
test "Rails.app_env does not follow Rails.env changes when both APP_ENV and RAILS_ENV are blank" do
switch_env "APP_ENV", nil do
with_rails_env(nil) do
assert_equal DEFAULT_RAILS_ENV, Rails.app_env
assert_equal DEFAULT_RAILS_ENV, Rails.env

Rails.env = "foo"

assert_equal "foo", Rails.app_env
assert_equal DEFAULT_RAILS_ENV, Rails.app_env
assert_equal "foo", Rails.env
assert_same Rails.env, Rails.app_env
end
end
end
Expand Down