gobuffalo / envy

Envy makes working with ENV variables in Go trivial.
MIT License
156 stars 21 forks source link

GO_ENV not set to "test" because golang test environment not yet initialized #16

Closed armhold closed 6 years ago

armhold commented 6 years ago

I had some test failures in my buffalo app that I traced back to the following problem: I was running go test ./actions/ manually rather than using buffalo test. GO_ENV was being automatically set to development inside my app, rather than test, causing a lot of confusion.

It looks like envy tries to detect the presence of the golang test environment, and if present, sets GO_ENV to test if otherwise undefined.

However this only works if the testing package has already been loaded by go before envy's init() func gets run. I don't think we can count on the order of package initialization if there is no explicit dependency between them. I've created a gist that demonstrates this.

In my own non-buffalo projects I have run into this general problem of how to detect when running in a unit test and setting the environment appropriately. I haven't come up with a good solution.

armhold commented 6 years ago

Clarification: the problem seems to come from envy expecting:

  1. go test is run with "-v"
  2. the test binary has already invoked flag.Parse() by the time that envy.init() is run.