Skip to content

Commit 98b3777

Browse files
Introduce suspenders:test:environment generator
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 f6c6f45 commit 98b3777

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Unreleased
1818
* Introduce `suspenders:prerequisites` generator
1919
* Introduce `suspenders:ci` generator
2020
* Introduce `suspenders:cleanup:organize_gemfile` task
21+
* Introduce `suspenders:test:environment` generator
2122

2223
20230113.0 (January, 13, 2023)
2324

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,22 @@ Creates CI files for GitHub Actions.
197197
bin/rails g suspenders:ci
198198
```
199199

200+
### Environments
201+
202+
#### Test
203+
204+
Configures test environment.
205+
206+
```
207+
bin/rails g suspenders:test:environment
208+
```
209+
210+
- Enables [raise_on_missing_translations][]
211+
- Disables [action_dispatch.show_exceptions][]
212+
213+
[raise_on_missing_translations]: https://guides.rubyonrails.org/configuring.html#config-i18n-raise-on-missing-translations
214+
[action_dispatch.show_exceptions]: https://edgeguides.rubyonrails.org/configuring.html#config-action-dispatch-show-exceptions
215+
200216
## Contributing
201217

202218
See the [CONTRIBUTING] document.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
uncomment_lines "config/environments/test.rb", /config\.i18n\.raise_on_missing_translations\s*=\s*true/
21+
end
22+
23+
def disable_action_dispatch_show_exceptions
24+
gsub_file "config/environments/test.rb", /^\s*config\.action_dispatch\.show_exceptions\s*=\s*:rescuable/,
25+
"config.action_dispatch.show_exceptions = :none"
26+
27+
gsub_file "config/environments/test.rb", /^\s*#\s*Raise exceptions instead of rendering exception templates/i, ""
28+
end
29+
end
30+
end
31+
end
32+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 "disable action_dispatch.show_exceptions" do
24+
run_generator
25+
26+
assert_file app_root("config/environments/test.rb") do |file|
27+
assert_match(/^\s*config\.action_dispatch\.show_exceptions\s*=\s*:none/, file)
28+
assert_no_match(/^\s*config\.action_dispatch\.show_exceptions\s*=\s*:rescuable/, file)
29+
assert_no_match(/^\s*#\s*Raise exceptions instead of rendering exception templates/i, file)
30+
end
31+
end
32+
33+
private
34+
35+
def prepare_destination
36+
backup_file "config/environments/test.rb"
37+
end
38+
39+
def restore_destination
40+
restore_file "config/environments/test.rb"
41+
end
42+
end
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)