Skip to content

Commit 5e955ca

Browse files
authored
Test: Explicit specific environment variable when asserting with runner; Refactor tests (#1)
1 parent 0eee2b8 commit 5e955ca

File tree

4 files changed

+129
-119
lines changed

4 files changed

+129
-119
lines changed

test/features/credentials_test.rb

Lines changed: 83 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,144 @@
11
require_relative "../test_helper"
22
require_relative "runner_helper"
3+
require_relative "file_helper"
34

45
module Rails::AppEnv::FeaturesTest
56
class CredentialsTest < ActiveSupport::TestCase
67
include RunnerHelper
8+
include FileHelper
79

8-
CREDENTIALS_PATH_TEST_CASES = [
9-
{
10-
name: "is {APP_ENV}.yml.enc when both APP_ENV and RAILS_ENV are present",
10+
CREDENTIALS_PATH_TEST_CASES = {
11+
"is {APP_ENV}.yml.enc when both APP_ENV and RAILS_ENV are present": {
1112
expected_path: "config/credentials/foo.yml.enc",
12-
app_env: "foo",
13-
rails_env: "bar",
14-
secret_key_base_dummy: "1",
15-
touch_file: "config/credentials/foo.yml.enc"
13+
touch_file: "config/credentials/foo.yml.enc",
14+
env: {
15+
"APP_ENV" => "foo",
16+
"RAILS_ENV" => "bar"
17+
}
1618
},
17-
{
18-
name: "falls back to credentials.yml.enc when {APP_ENV}.yml.enc is absent and both APP_ENV and RAILS_ENV are present",
19+
"falls back to credentials.yml.enc when {APP_ENV}.yml.enc is absent and both APP_ENV and RAILS_ENV are present": {
1920
expected_path: "config/credentials.yml.enc",
20-
app_env: "foo",
21-
rails_env: "bar",
22-
secret_key_base_dummy: "1"
21+
env: {
22+
"APP_ENV" => "foo",
23+
"RAILS_ENV" => "bar"
24+
}
2325
},
24-
{
25-
name: "is {APP_ENV}.yml.enc when APP_ENV is present but RAILS_ENV is blank",
26+
"is {APP_ENV}.yml.enc when APP_ENV is present but RAILS_ENV is blank": {
2627
expected_path: "config/credentials/foo.yml.enc",
27-
app_env: "foo",
28-
touch_file: "config/credentials/foo.yml.enc"
28+
touch_file: "config/credentials/foo.yml.enc",
29+
env: {
30+
"APP_ENV" => "foo"
31+
}
2932
},
30-
{
31-
name: "falls back to credentials.yml.enc when {APP_ENV}.yml.enc is absent and APP_ENV is present but RAILS_ENV is blank",
33+
"falls back to credentials.yml.enc when {APP_ENV}.yml.enc is absent and APP_ENV is present but RAILS_ENV is blank": {
3234
expected_path: "config/credentials.yml.enc",
33-
app_env: "foo",
34-
secret_key_base_dummy: "1"
35+
env: {
36+
"APP_ENV" => "foo"
37+
}
3538
},
36-
{
37-
name: "is {RAILS_ENV}.yml.enc when APP_ENV is blank but RAILS_ENV is present",
39+
"is {RAILS_ENV}.yml.enc when APP_ENV is blank but RAILS_ENV is present": {
3840
expected_path: "config/credentials/foo.yml.enc",
39-
rails_env: "foo",
40-
secret_key_base_dummy: "1",
41-
touch_file: "config/credentials/foo.yml.enc"
41+
touch_file: "config/credentials/foo.yml.enc",
42+
env: {
43+
"RAILS_ENV" => "foo"
44+
}
4245
},
43-
{
44-
name: "falls back to credentials.yml.enc when {RAILS_ENV}.yml.enc is absent and when APP_ENV is blank but RAILS_ENV is present",
46+
"falls back to credentials.yml.enc when {RAILS_ENV}.yml.enc is absent and when APP_ENV is blank but RAILS_ENV is present": {
4547
expected_path: "config/credentials.yml.enc",
46-
rails_env: "foo",
47-
secret_key_base_dummy: "1"
48+
env: {
49+
"RAILS_ENV" => "foo"
50+
}
4851
},
49-
{
50-
name: "is {DEFAULT_RAILS_ENV}.yml.enc when both APP_ENV and RAILS_ENV are blank",
52+
"is {DEFAULT_RAILS_ENV}.yml.enc when both APP_ENV and RAILS_ENV are blank": {
5153
expected_path: "config/credentials/#{DEFAULT_RAILS_ENV}.yml.enc",
5254
touch_file: "config/credentials/#{DEFAULT_RAILS_ENV}.yml.enc"
5355
},
54-
{
55-
name: "falls back to credentials.yml.enc when {DEFAULT_RAILS_ENV}.yml.enc is absent and both APP_ENV and RAILS_ENV are blank",
56+
"falls back to credentials.yml.enc when {DEFAULT_RAILS_ENV}.yml.enc is absent and both APP_ENV and RAILS_ENV are blank": {
5657
expected_path: "config/credentials.yml.enc"
5758
}
58-
]
59+
}
5960

60-
KEY_PATH_TEST_CASES = [
61-
{
62-
name: "is {APP_ENV}.key when both APP_ENV and RAILS_ENV are present",
61+
KEY_PATH_TEST_CASES = {
62+
"is {APP_ENV}.key when both APP_ENV and RAILS_ENV are present": {
6363
expected_path: "config/credentials/foo.key",
64-
app_env: "foo",
65-
rails_env: "bar",
66-
secret_key_base_dummy: "1",
67-
touch_file: "config/credentials/foo.key"
64+
touch_file: "config/credentials/foo.key",
65+
env: {
66+
"APP_ENV" => "foo",
67+
"RAILS_ENV" => "bar"
68+
}
6869
},
69-
{
70-
name: "falls back to master.key when {APP_ENV}.key is absent and both APP_ENV and RAILS_ENV are present",
70+
"falls back to master.key when {APP_ENV}.key is absent and both APP_ENV and RAILS_ENV are present": {
7171
expected_path: "config/master.key",
72-
app_env: "foo",
73-
rails_env: "bar",
74-
secret_key_base_dummy: "1"
72+
env: {
73+
"APP_ENV" => "foo",
74+
"RAILS_ENV" => "bar"
75+
}
7576
},
76-
{
77-
name: "is {APP_ENV}.key when APP_ENV is present but RAILS_ENV is blank",
77+
"is {APP_ENV}.key when APP_ENV is present but RAILS_ENV is blank": {
7878
expected_path: "config/credentials/foo.key",
79-
app_env: "foo",
80-
touch_file: "config/credentials/foo.key"
79+
touch_file: "config/credentials/foo.key",
80+
env: {
81+
"APP_ENV" => "foo"
82+
}
8183
},
82-
{
83-
name: "falls back to master.key when {APP_ENV}.key is absent and APP_ENV is present but RAILS_ENV is blank",
84+
"falls back to master.key when {APP_ENV}.key is absent and APP_ENV is present but RAILS_ENV is blank": {
8485
expected_path: "config/master.key",
85-
app_env: "foo"
86+
env: {
87+
"APP_ENV" => "foo"
88+
}
8689
},
87-
{
88-
name: "is {RAILS_ENV}.key when APP_ENV is blank but RAILS_ENV is present",
90+
"is {RAILS_ENV}.key when APP_ENV is blank but RAILS_ENV is present": {
8991
expected_path: "config/credentials/foo.key",
90-
rails_env: "foo",
9192
touch_file: "config/credentials/foo.key",
92-
secret_key_base_dummy: "1"
93+
env: {
94+
"RAILS_ENV" => "foo"
95+
}
9396
},
94-
{
95-
name: "falls back to master.key when {RAILS_ENV}.key is absent and when APP_ENV is blank but RAILS_ENV is present",
97+
"falls back to master.key when {RAILS_ENV}.key is absent and when APP_ENV is blank but RAILS_ENV is present": {
9698
expected_path: "config/master.key",
97-
rails_env: "foo",
98-
secret_key_base_dummy: "1"
99+
env: {
100+
"RAILS_ENV" => "foo"
101+
}
99102
},
100-
{
101-
name: "is {DEFAULT_RAILS_ENV}.key when both APP_ENV and RAILS_ENV are blank",
103+
"is {DEFAULT_RAILS_ENV}.key when both APP_ENV and RAILS_ENV are blank": {
102104
expected_path: "config/credentials/#{DEFAULT_RAILS_ENV}.key",
103105
touch_file: "config/credentials/#{DEFAULT_RAILS_ENV}.key"
104106
},
105-
{
106-
name: "falls back to master.key when {DEFAULT_RAILS_ENV}.key is absent and both APP_ENV and RAILS_ENV are blank",
107+
"falls back to master.key when {DEFAULT_RAILS_ENV}.key is absent and both APP_ENV and RAILS_ENV are blank": {
107108
expected_path: "config/master.key"
108109
}
109-
]
110+
}
110111

111-
CREDENTIALS_PATH_TEST_CASES.each_with_index do |args, i|
112+
CREDENTIALS_PATH_TEST_CASES.each_key do |name|
112113
class_eval <<~RUBY, __FILE__, __LINE__ + 1
113-
test "content path #{args[:name]}" do
114-
assert_credentials_path(**CREDENTIALS_PATH_TEST_CASES[#{i}])
114+
test "content path #{name}" do
115+
assert_credentials_path(**CREDENTIALS_PATH_TEST_CASES[:"#{name}"])
115116
end
116117
RUBY
117118
end
118119

119-
KEY_PATH_TEST_CASES.each_with_index do |args, i|
120+
KEY_PATH_TEST_CASES.each_key do |name|
120121
class_eval <<~RUBY, __FILE__, __LINE__ + 1
121-
test "key path #{args[:name]}" do
122-
assert_key_path(**KEY_PATH_TEST_CASES[#{i}])
122+
test "key path #{name}" do
123+
assert_key_path(**KEY_PATH_TEST_CASES[:"#{name}"])
123124
end
124125
RUBY
125126
end
126127

127128
private
128129

129-
def assert_credentials_path(expected_path:, **options)
130-
assert_runner Rails.root.join(expected_path), "Rails.configuration.credentials.content_path", **options
130+
def assert_credentials_path(expected_path:, touch_file: nil, **options)
131+
assert_runner_puts_with_file Rails.root.join(expected_path), "Rails.configuration.credentials.content_path", touch_file, **options
132+
end
133+
134+
def assert_key_path(expected_path:, touch_file: nil, **options)
135+
assert_runner_puts_with_file Rails.root.join(expected_path), "Rails.configuration.credentials.key_path", touch_file, **options
131136
end
132137

133-
def assert_key_path(expected_path:, **options)
134-
assert_runner Rails.root.join(expected_path), "Rails.configuration.credentials.key_path", **options
138+
def assert_runner_puts_with_file(expected, subject, file_path, **options)
139+
with_file(file_path) do
140+
assert_runner_puts expected, subject, **options
141+
end
135142
end
136143
end
137144
end

test/features/file_helper.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module FileHelper
2+
private
3+
4+
def with_file(path, &block)
5+
return block.call nil if path.nil?
6+
7+
full_path = Rails.root.join path
8+
9+
FileUtils.mkdir_p File.dirname(full_path)
10+
FileUtils.touch full_path
11+
12+
block.call full_path
13+
ensure
14+
FileUtils.rm_f full_path unless full_path.nil?
15+
end
16+
end

test/features/info_test.rb

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,43 @@ module Rails::AppEnv::FeaturesTest
55
class InfoTest < ActiveSupport::TestCase
66
include RunnerHelper
77

8-
TEST_CASES = [
9-
{
10-
name: "is APP_ENV when both APP_ENV and RAILS_ENV are present",
8+
TEST_CASES = {
9+
"is APP_ENV when both APP_ENV and RAILS_ENV are present": {
1110
expected: "foo",
12-
app_env: "foo",
13-
rails_env: "bar",
14-
secret_key_base_dummy: "1"
11+
env: {
12+
"APP_ENV" => "foo",
13+
"RAILS_ENV" => "bar"
14+
}
1515
},
16-
{
17-
name: "is APP_ENV when APP_ENV is present but RAILS_ENV is blank",
16+
"is APP_ENV when APP_ENV is present but RAILS_ENV is blank": {
1817
expected: "foo",
19-
app_env: "foo"
18+
env: {
19+
"APP_ENV" => "foo"
20+
}
2021
},
21-
{
22-
name: "falls back to RAILS_ENV when APP_ENV is blank but RAILS_ENV is present",
22+
"falls back to RAILS_ENV when APP_ENV is blank but RAILS_ENV is present": {
2323
expected: "foo",
24-
rails_env: "foo",
25-
secret_key_base_dummy: "1"
24+
env: {
25+
"RAILS_ENV" => "foo"
26+
}
2627
},
27-
{
28-
name: "falls back to DEFAULT_RAILS_ENV when both APP_ENV and RAILS_ENV are blank",
28+
"falls back to DEFAULT_RAILS_ENV when both APP_ENV and RAILS_ENV are blank": {
2929
expected: DEFAULT_RAILS_ENV # development
3030
}
31-
]
31+
}
3232

33-
TEST_CASES.each_with_index do |args, i|
33+
TEST_CASES.each_key do |name|
3434
class_eval <<~RUBY, __FILE__, __LINE__ + 1
35-
test "Rails::Info's 'Application environment' #{args[:name]}" do
36-
assert_runner args[:expected], 'Rails::Info.properties.value_for("Application environment")', **args
35+
test "Rails::Info's 'Application environment' #{name}" do
36+
assert_rails_info_property(**TEST_CASES[:"#{name}"])
3737
end
3838
RUBY
3939
end
40+
41+
private
42+
43+
def assert_rails_info_property(expected:, **options)
44+
assert_runner_puts expected, 'Rails::Info.properties.value_for("Application environment")', **options
45+
end
4046
end
4147
end

test/features/runner_helper.rb

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,12 @@ module RunnerHelper
77

88
DUMMY_RAILS = File.expand_path "../../dummy/bin/rails", __FILE__
99

10-
def assert_runner(expected, subject, app_env: nil, rails_env: nil, secret_key_base_dummy: nil, touch_file: nil, **options)
11-
switch_env "APP_ENV", app_env do
12-
with_rails_env(rails_env) do
13-
switch_env "SECRET_KEY_BASE_DUMMY", secret_key_base_dummy do
14-
with_file(touch_file) do
15-
stdout, status = Open3.capture2(DUMMY_RAILS, "runner", "puts #{subject}")
10+
def assert_runner_puts(expected, subject, env: {})
11+
env = env.with_defaults({"APP_ENV" => nil, "RAILS_ENV" => nil, "SECRET_KEY_BASE_DUMMY" => "1"})
1612

17-
assert_predicate status, :success?
18-
assert_equal expected.to_s, stdout.chomp
19-
end
20-
end
21-
end
22-
end
23-
end
24-
25-
def with_file(path, &block)
26-
return block.call nil if path.nil?
27-
28-
full_path = Rails.root.join path
29-
30-
FileUtils.mkdir_p File.dirname(full_path)
31-
FileUtils.touch full_path
13+
stdout, status = Open3.capture2(env, DUMMY_RAILS, "runner", "puts #{subject}")
3214

33-
block.call full_path
34-
ensure
35-
FileUtils.rm_f full_path unless full_path.nil?
15+
assert_predicate status, :success?
16+
assert_equal expected.to_s, stdout.chomp
3617
end
3718
end

0 commit comments

Comments
 (0)