Skip to content

Set RAILS_ENV to test for test tasks - #441

Closed
etiennebarrie wants to merge 1 commit into
bkeepers:masterfrom
etiennebarrie:set-rails-env-for-test
Closed

etiennebarrie wants to merge 1 commit into
bkeepers:masterfrom
etiennebarrie:set-rails-env-for-test

Conversation

@etiennebarrie

Copy link
Copy Markdown

Rails had a hack that was removed in 7.0, this adds it back here.

Rails had a hack that was removed in 7.0, this adds it back here.
@bkeepers

Copy link
Copy Markdown
Owner

@etiennebarrie Do you have any more context on why this hack was removed from Rails 7? How does Rails currently handle switching environments for rake tasks?

@etiennebarrie

Copy link
Copy Markdown
Author

I removed it because I felt like it was bad form to change the environment when requiring a file and it wasn't necessary anymore since we were now using TestUnit::Runner (i.e. rails test) to run the tests which sets the environment to whatever is passed on the command line or test by default.

Also it was causing issues using Rails engines, which load all the dummy app's tasks under app:, which resulted in app:test not running in the test environment.

How does Rails currently handle switching environments for rake tasks?

There's no longer any specific code related to the environment for rake tasks as far as I know. If you happen to run one of the test tasks, they fork to rails test, and it changes the environment as previously mentioned (only for the fork).

@bkeepers

Copy link
Copy Markdown
Owner

Fixed this in #470.

I removed it because I felt like it was bad form to change the environment when requiring a file…

Yeah, good call. I refactored this to not mutate the environment, but just return a different value internally for dotenv.

@bkeepers bkeepers closed this Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants