Skip to content

Commit a70e7ba

Browse files
committed
Add Rails.app_env into Rails::Info
1 parent f188358 commit a70e7ba

File tree

7 files changed

+177
-110
lines changed

7 files changed

+177
-110
lines changed

lib/rails/app_env/railtie.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
require_relative "helpers"
2-
31
module Rails
42
module AppEnv
5-
class Railtie < ::Rails::Railtie
3+
class Railtie < Rails::Railtie
64
config.before_configuration do
7-
::Rails.extend(Helpers)
5+
Rails.extend(Helpers)
6+
end
7+
8+
config.after_initialize do
9+
Rails::Info.property "Application environment" do
10+
Rails.app_env
11+
end
812
end
913
end
1014
end

test/dummy/config/application.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ class Application < Rails::Application
2222
#
2323
# config.time_zone = "Central Time (US & Canada)"
2424
# config.eager_load_paths << Rails.root.join("extras")
25+
26+
config.eager_load = false if config.eager_load.nil?
2527
end
2628
end

test/env_helpers.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
module EnvHelpers
66
private
77

8+
DEFAULT_RAILS_ENV = "development"
9+
810
def with_rails_env(env, &block)
911
Rails.instance_variable_set :@_env, nil
1012
switch_env "RAILS_ENV", env do

test/rails/app_env/environment_inquirer_test.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require "minitest/autorun"
2-
31
class Rails::AppEnv::EnvironmentInquirerTest < ActiveSupport::TestCase
42
test "EnvironmentInquirer is a kind of ActiveSupport::EnvironmentInquirer" do
53
assert_kind_of ActiveSupport::EnvironmentInquirer, Rails::AppEnv::EnvironmentInquirer.new("foo")

test/rails/app_env/helpers_test.rb

Lines changed: 0 additions & 104 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
require_relative "../../../env_helpers"
2+
3+
module Rails::AppEnv::RailtieTest
4+
class HelperTest < ActiveSupport::TestCase
5+
include EnvHelpers
6+
7+
test "Rails.app_env is an instance of ActiveSupport::EnvironmentInquirer when APP_ENV is present" do
8+
switch_env "APP_ENV", nil do
9+
assert_instance_of ActiveSupport::EnvironmentInquirer, Rails.app_env
10+
end
11+
end
12+
13+
test "Rails.app_env is a kind of ActiveSupport::EnvironmentInquirer when APP_ENV is blank" do
14+
switch_env "APP_ENV", "foo" do
15+
assert_kind_of ActiveSupport::EnvironmentInquirer, Rails.app_env
16+
end
17+
end
18+
19+
test "Rails.app_env is an instance of Rails::AppEnv::EnvironmentInquirer when APP_ENV is blank" do
20+
switch_env "APP_ENV", "foo" do
21+
assert_instance_of Rails::AppEnv::EnvironmentInquirer, Rails.app_env
22+
end
23+
end
24+
25+
test "Rails.app_env is set from APP_ENV" do
26+
switch_env "APP_ENV", "foo" do
27+
assert_equal "foo", Rails.app_env
28+
end
29+
end
30+
31+
test "Rails.app_env is set from APP_ENV when both APP_ENV are RAILS_ENV present" do
32+
switch_env "APP_ENV", "foo" do
33+
with_rails_env("bar") do
34+
assert_equal "foo", Rails.app_env
35+
assert_equal "bar", Rails.env
36+
end
37+
end
38+
end
39+
40+
test "Rails.app_env is set from APP_ENV when APP_ENV is present but not RAILS_ENV" do
41+
switch_env "APP_ENV", "foo" do
42+
with_rails_env(nil) do
43+
assert_equal "foo", Rails.app_env
44+
assert_equal DEFAULT_RAILS_ENV, Rails.env
45+
end
46+
end
47+
end
48+
49+
test "Rails.app_env falls back to Rails.env when APP_ENV is blank" do
50+
switch_env "APP_ENV", nil do
51+
with_rails_env("foo") do
52+
assert_equal "foo", Rails.app_env
53+
assert_equal "foo", Rails.env
54+
assert_same Rails.env, Rails.app_env
55+
end
56+
end
57+
end
58+
59+
test "Rails.app_env falls back to Rails.env when APP_ENV is blank and follow its changes" do
60+
switch_env "APP_ENV", nil do
61+
with_rails_env("foo") do
62+
Rails.env = "bar"
63+
64+
assert_equal "bar", Rails.app_env
65+
assert_equal "bar", Rails.env
66+
assert_same Rails.env, Rails.app_env
67+
end
68+
end
69+
end
70+
71+
test "Rails.app_env falls back to Rails.env when both APP_ENV and RAILS_ENV are blank" do
72+
switch_env "APP_ENV", nil do
73+
with_rails_env(nil) do
74+
assert_equal DEFAULT_RAILS_ENV, Rails.app_env
75+
assert_equal DEFAULT_RAILS_ENV, Rails.env
76+
assert_same Rails.env, Rails.app_env
77+
end
78+
end
79+
end
80+
81+
test "Rails.app_env falls back to Rails.env when both APP_ENV and RAILS_ENV are blank and follow its changes" do
82+
switch_env "APP_ENV", nil do
83+
with_rails_env(nil) do
84+
Rails.env = "foo"
85+
86+
assert_equal "foo", Rails.app_env
87+
assert_equal "foo", Rails.env
88+
assert_same Rails.env, Rails.app_env
89+
end
90+
end
91+
end
92+
93+
test "Rails.app_env is set from APP_ENV and does not follow Rails.env changes" do
94+
switch_env "APP_ENV", "foo" do
95+
with_rails_env("bar") do
96+
Rails.env = "baz"
97+
98+
assert_equal "foo", Rails.app_env
99+
assert_equal "baz", Rails.env
100+
end
101+
end
102+
end
103+
end
104+
end
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require_relative "../../../env_helpers"
2+
3+
module Rails::AppEnv::RailtieTest
4+
class InfoTest < ActiveSupport::TestCase
5+
include EnvHelpers
6+
7+
DUMMY_RAILS = File.expand_path "../../../dummy/bin/rails", __dir__
8+
9+
test "Rails::Info has 'Application environment' defined" do
10+
assert_includes Rails::Info.properties.names, "Application environment"
11+
end
12+
13+
test "rails about command includes 'Application environment' when both APP_ENV and RAILS_ENV are present" do
14+
switch_env "APP_ENV", "foo" do
15+
with_rails_env("bar") do
16+
switch_env "SECRET_KEY_BASE_DUMMY", "1" do
17+
stdout, status = Open3.capture2(DUMMY_RAILS, "about")
18+
19+
assert status.success?
20+
assert_match /Application environment\s+foo/, stdout
21+
end
22+
end
23+
end
24+
end
25+
26+
test "rails about command includes 'Application environment' when APP_ENV is present but not RAILS_ENV" do
27+
switch_env "APP_ENV", "foo" do
28+
with_rails_env(nil) do
29+
stdout, status = Open3.capture2(DUMMY_RAILS, "about")
30+
31+
assert status.success?
32+
assert_match /Application environment\s+foo/, stdout
33+
end
34+
end
35+
end
36+
37+
test "rails about command falls back to use Rails.env as 'Application environment' when APP_ENV is blank" do
38+
switch_env "APP_ENV", nil do
39+
with_rails_env("foo") do
40+
switch_env "SECRET_KEY_BASE_DUMMY", "1" do
41+
stdout, status = Open3.capture2(DUMMY_RAILS, "about")
42+
43+
assert status.success?
44+
assert_match /Application environment\s+foo/, stdout
45+
end
46+
end
47+
end
48+
end
49+
50+
test "rails about command falls back to use Rails.env as 'Application environment' when both APP_ENV and RAILS_ENV are blank" do
51+
switch_env "APP_ENV", nil do
52+
with_rails_env(nil) do
53+
stdout, status = Open3.capture2(DUMMY_RAILS, "about")
54+
55+
assert status.success?
56+
assert_match /Application environment\s+development/, stdout # Default Rails environment
57+
end
58+
end
59+
end
60+
end
61+
end

0 commit comments

Comments
 (0)