Skip to content

Commit 5a733e0

Browse files
Introduce suspenders:test:environment generator (#1182)
Configures test environment. This differs from #1156 in that this commit is concerned with configuration, where that commit was concerned with generating a holistic test suite. It's also possible to run each generator independently, and the two should not rely on one another. Disables [action_dispatch.show_exceptions][] in an effort to [improve failure output][comment]. Enables [raise_on_missing_translations][] to keep parity with the same setting in development #1149 [action_dispatch.show_exceptions]: https://edgeguides.rubyonrails.org/configuring.html#config-action-dispatch-show-exceptions [comment]: #1149 (comment) [raise_on_missing_translations]: https://guides.rubyonrails.org/configuring.html#config-i18n-raise-on-missing-translations
1 parent 667b7ae commit 5a733e0

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Unreleased
1919
* Introduce `suspenders:ci` generator
2020
* Introduce `suspenders:cleanup:organize_gemfile` task
2121
* Introduce `suspenders:production:environment` generator
22+
* Introduce `suspenders:test:environment` generator
2223

2324
20230113.0 (January, 13, 2023)
2425

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ bin/rails g suspenders:ci
199199

200200
### Environments
201201

202+
<<<<<<< HEAD
202203
#### Production
203204

204205
Configures the production environment.
@@ -211,6 +212,22 @@ Configures the production environment.
211212
bin/rails g suspenders:production:environment
212213
```
213214

215+
=======
216+
#### Test
217+
218+
Configures test environment.
219+
220+
```
221+
bin/rails g suspenders:test:environment
222+
```
223+
224+
- Enables [raise_on_missing_translations][]
225+
- Disables [action_dispatch.show_exceptions][]
226+
227+
[raise_on_missing_translations]: https://guides.rubyonrails.org/configuring.html#config-i18n-raise-on-missing-translations
228+
[action_dispatch.show_exceptions]: https://edgeguides.rubyonrails.org/configuring.html#config-action-dispatch-show-exceptions
229+
230+
>>>>>>> 9102851 (Introduce `suspenders:test:environment` generator)
214231
## Contributing
215232

216233
See the [CONTRIBUTING] document.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module Suspenders
2+
module Generators
3+
module Test
4+
class EnvironmentGenerator < Rails::Generators::Base
5+
desc <<~MARKDOWN
6+
Configures test environment.
7+
8+
```
9+
bin/rails g suspenders:test:environment
10+
```
11+
12+
- Enables [raise_on_missing_translations][]
13+
- Disables [action_dispatch.show_exceptions][]
14+
15+
[raise_on_missing_translations]: https://guides.rubyonrails.org/configuring.html#config-i18n-raise-on-missing-translations
16+
[action_dispatch.show_exceptions]: https://edgeguides.rubyonrails.org/configuring.html#config-action-dispatch-show-exceptions
17+
MARKDOWN
18+
19+
def raise_on_missing_translations
20+
if test_config.match?(/^\s*#\s*config\.i18n\.raise_on_missing_translations\s*=\s*true/)
21+
uncomment_lines "config/environments/test.rb", /config\.i18n\.raise_on_missing_translations\s*=\s*true/
22+
else
23+
environment %(config.i18n.raise_on_missing_translations = true), env: "test"
24+
end
25+
end
26+
27+
def disable_action_dispatch_show_exceptions
28+
if test_config.match?(/^\s*config\.action_dispatch\.show_exceptions\s*=\s*:rescuable/)
29+
gsub_file "config/environments/test.rb", /^\s*config\.action_dispatch\.show_exceptions\s*=\s*:rescuable/,
30+
"config.action_dispatch.show_exceptions = :none"
31+
gsub_file "config/environments/test.rb", /^\s*#\s*Raise exceptions instead of rendering exception templates/i, ""
32+
else
33+
environment %(config.action_dispatch.show_exceptions = :none), env: "test"
34+
end
35+
end
36+
37+
private
38+
39+
def test_config
40+
File.read(Rails.root.join("config/environments/test.rb"))
41+
end
42+
end
43+
end
44+
end
45+
end
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Rails.application.configure do
2+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require "test_helper"
2+
require "generators/suspenders/test/environment_generator"
3+
4+
module Suspenders
5+
module Generators
6+
module Test
7+
class EnvironmentGeneratorTest < Rails::Generators::TestCase
8+
include Suspenders::TestHelpers
9+
10+
tests Suspenders::Generators::Test::EnvironmentGenerator
11+
destination Rails.root
12+
setup :prepare_destination
13+
teardown :restore_destination
14+
15+
test "raise on missing translations" do
16+
run_generator
17+
18+
assert_file app_root("config/environments/test.rb") do |file|
19+
assert_match(/^\s*config\.i18n\.raise_on_missing_translations\s*=\s*true/, file)
20+
end
21+
end
22+
23+
test "raise on missing translations (when config is not commented out)" do
24+
content = file_fixture("environments/test.rb").read
25+
remove_file_if_exists "config/environments/test.rb"
26+
touch "config/environments/test.rb", content: content
27+
28+
run_generator
29+
30+
assert_file app_root("config/environments/test.rb") do |file|
31+
assert_match(/^\s*config\.i18n\.raise_on_missing_translations\s*=\s*true/, file)
32+
end
33+
end
34+
35+
test "disable action_dispatch.show_exceptions" do
36+
run_generator
37+
38+
assert_file app_root("config/environments/test.rb") do |file|
39+
assert_match(/^\s*config\.action_dispatch\.show_exceptions\s*=\s*:none/, file)
40+
assert_no_match(/^\s*config\.action_dispatch\.show_exceptions\s*=\s*:rescuable/, file)
41+
assert_no_match(/^\s*#\s*Raise exceptions instead of rendering exception templates/i, file)
42+
end
43+
end
44+
45+
test "disable action_dispatch.show_exceptions (when config does not exist)" do
46+
content = file_fixture("environments/test.rb").read
47+
remove_file_if_exists "config/environments/test.rb"
48+
touch "config/environments/test.rb", content: content
49+
50+
run_generator
51+
52+
assert_file app_root("config/environments/test.rb") do |file|
53+
assert_match(/^\s*config\.action_dispatch\.show_exceptions\s*=\s*:none/, file)
54+
end
55+
end
56+
57+
private
58+
59+
def prepare_destination
60+
backup_file "config/environments/test.rb"
61+
end
62+
63+
def restore_destination
64+
restore_file "config/environments/test.rb"
65+
end
66+
end
67+
end
68+
end
69+
end

0 commit comments

Comments
 (0)