Force RAILS_ENV=test in spec/rails_helper.rb so specs never use the dev database - #148
Open
eastagiletracker wants to merge 1 commit into
Open
eastagiletracker wants to merge 1 commit into
eastagiletracker wants to merge 1 commit into
Conversation
spec/rails_helper.rb set the Rails environment with ENV["RAILS_ENV"] ||= "test", which honors any RAILS_ENV already present in the shell. When RAILS_ENV=development is set, the suite boots in development and maintain_test_schema! plus the transactional fixtures operate on the development database instead of app_test, which can wipe real data. Force ENV["RAILS_ENV"] = "test" so specs always run against the test database regardless of the inherited environment, and add a regression spec covering it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR proposes forcing
RAILS_ENV=testinspec/rails_helper.rbso the spec suite always runs against the test database and never falls back to development. We include this PR work along with a full history of your repo at https://eastagiletracker.com/projects/270. You can sign in with your GitHub ID to claim ownership of the project.Fixes #83.
What's wrong
spec/rails_helper.rbselects the environment withENV["RAILS_ENV"] ||= "test". That only defaults totestwhenRAILS_ENVis unset — if a value is already present in the shell (a commonRAILS_ENV=development, a.env, or a CI variable) it is honored instead. The only guard below it aborts onproduction, not ondevelopment:Because
config/database.ymlmapsdevelopmentto the primaryappdatabase and onlytesttoapp_test, booting the suite in development pointsActiveRecord::Migration.maintain_test_schema!and the transactional fixtures at the developer's development database — which can wipe real data. This is the behavior issue #83 asks to close ("RAILS_ENV=testshould be the default and we should not fall back to development").Reproduce on
mainRunning the suite with a
RAILS_ENV=developmentinherited from the shell boots in development and connects to theapp(development) database rather thanapp_test:The fix
Force the environment so the value inherited from the shell can never redirect the suite:
Normal invocations (
rspec,bin/run_tests) are unchanged — they don't setRAILS_ENV, sotestwas and remains the effective environment. The only behavior change is that a leaked non-testRAILS_ENVnow resolves totestinstead of silently running against the wrong database, which is the intended safety guarantee. This complements the earlier compose.yml change (d5ea3ff) that stopped the container from defaultingRAILS_ENVto development, closing the same gap at the spec-helper layer regardless of howRAILS_ENVis set.Verification
I added
spec/rails_helper_env_spec.rb, a regression test asserting the suite runs in the test environment and connects to a_testdatabase. It fails on the current tree and passes with the fix, run with a leakedRAILS_ENV=development:The full suite stays green and
rubocop(standard + rubocop-rspec) reports no offenses on the changed files:One heads-up for merge order: the open PR #138 also edits
spec/rails_helper.rb, but on different lines (support-file loading and Devise/Shoulda config), so this one-line change to the environment guard should apply cleanly alongside it.How this was managed
We imported your issues and pull requests into a live agile board and used it to manage this work. This change is tracked on its own story at https://eastagiletracker.com/projects/270/stories/159506, on the board at https://eastagiletracker.com/projects/270.
If you'd rather not receive contributions like this, reply
no-more-prson this pull request and we won't open any further ones on your repositories.Lawrence W. Sinclair
CEO / East Agile
linkedin.com/in/lwsinclair/
eastagile.com