Skip to content

Commit

Permalink
Introduce suspenders:test:environment generator
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stevepolitodesign committed Apr 5, 2024
1 parent f6c6f45 commit 98b3777
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Unreleased
* Introduce `suspenders:prerequisites` generator
* Introduce `suspenders:ci` generator
* Introduce `suspenders:cleanup:organize_gemfile` task
* Introduce `suspenders:test:environment` generator

20230113.0 (January, 13, 2023)

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ Creates CI files for GitHub Actions.
bin/rails g suspenders:ci
```

### Environments

#### Test

Configures test environment.

```
bin/rails g suspenders:test:environment
```

- Enables [raise_on_missing_translations][]
- Disables [action_dispatch.show_exceptions][]

[raise_on_missing_translations]: https://guides.rubyonrails.org/configuring.html#config-i18n-raise-on-missing-translations
[action_dispatch.show_exceptions]: https://edgeguides.rubyonrails.org/configuring.html#config-action-dispatch-show-exceptions

## Contributing

See the [CONTRIBUTING] document.
Expand Down
32 changes: 32 additions & 0 deletions lib/generators/suspenders/test/environment_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Suspenders
module Generators
module Test
class EnvironmentGenerator < Rails::Generators::Base
desc <<~MARKDOWN
Configures test environment.
```
bin/rails g suspenders:test:environment
```
- Enables [raise_on_missing_translations][]
- Disables [action_dispatch.show_exceptions][]
[raise_on_missing_translations]: https://guides.rubyonrails.org/configuring.html#config-i18n-raise-on-missing-translations
[action_dispatch.show_exceptions]: https://edgeguides.rubyonrails.org/configuring.html#config-action-dispatch-show-exceptions
MARKDOWN

def raise_on_missing_translations
uncomment_lines "config/environments/test.rb", /config\.i18n\.raise_on_missing_translations\s*=\s*true/
end

def disable_action_dispatch_show_exceptions
gsub_file "config/environments/test.rb", /^\s*config\.action_dispatch\.show_exceptions\s*=\s*:rescuable/,
"config.action_dispatch.show_exceptions = :none"

gsub_file "config/environments/test.rb", /^\s*#\s*Raise exceptions instead of rendering exception templates/i, ""
end
end
end
end
end
45 changes: 45 additions & 0 deletions test/generators/suspenders/test/environment_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "test_helper"
require "generators/suspenders/test/environment_generator"

module Suspenders
module Generators
module Test
class EnvironmentGeneratorTest < Rails::Generators::TestCase
include Suspenders::TestHelpers

tests Suspenders::Generators::Test::EnvironmentGenerator
destination Rails.root
setup :prepare_destination
teardown :restore_destination

test "raise on missing translations" do
run_generator

assert_file app_root("config/environments/test.rb") do |file|
assert_match(/^\s*config\.i18n\.raise_on_missing_translations\s*=\s*true/, file)
end
end

test "disable action_dispatch.show_exceptions" do
run_generator

assert_file app_root("config/environments/test.rb") do |file|
assert_match(/^\s*config\.action_dispatch\.show_exceptions\s*=\s*:none/, file)
assert_no_match(/^\s*config\.action_dispatch\.show_exceptions\s*=\s*:rescuable/, file)
assert_no_match(/^\s*#\s*Raise exceptions instead of rendering exception templates/i, file)
end
end

private

def prepare_destination
backup_file "config/environments/test.rb"
end

def restore_destination
restore_file "config/environments/test.rb"
end
end
end
end
end

0 comments on commit 98b3777

Please sign in to comment.