gotestsum icon indicating copy to clipboard operation
gotestsum copied to clipboard

Add env file support

Open sagikazarmark opened this issue 6 years ago • 5 comments

It would be nice to add an option for importing environment variables from an env file.

In integration tests it's a common pattern to use tokens and access keys from the environment and skip tests when they are missing. In local development environments this is commonly solved by using env files.

Although it's relatively easy to use godotenv for that, it adds an extra dependency, hence more complexity to the environment, so I think it would be a good fit for gotestsum.

sagikazarmark avatar Dec 27 '18 19:12 sagikazarmark

Support for setting environment variables for the test run is an interesting idea. There are already a few options for solving this problem that seem to work pretty well:

  • As you mention godotenv
  • I generally run tests in a docker container which has support for including env vars from a file
  • There is always the option to call gotestsum from a bash script, dobi, a Makefile, Magefile, or any one of many other task runners, all of which should be able to set env vars

Do you know if there is a standard or a spec that documents the format of these files? Is there a library that implements parsing an env file that is well supported? I wouldn't want to implement an env file parser in gotestsum.

I also wonder if env file is the right thing to support. If the goal is to run tests directly with gotestsum with minimal arguments then maybe a config file that allows setting env vars, args to go test, and any gotestsum flags from the config would be more appropriate.

dnephin avatar Dec 28 '18 21:12 dnephin

Is there a library that implements parsing an env file that is well supported?

Well, godotenv is actually a library as well, it only happens to provide a binary as well. The original dotenv implementation is from Ruby IIRC.

I also wonder if env file is the right thing to support.

A config file might work as well, but that locks you in on gotestsum. Env file is still somewhat universal. That being said, I would be happy with a gotestsum specific config file as well. (gotestsum could also accept -e flags, like docker commands for quick environment variable support)

sagikazarmark avatar Dec 28 '18 23:12 sagikazarmark

I wouldn't want to implement an env file parser in gotestsum.

The env files I'm familiar with (jenkins) are just java properties files. If that's the format you're talking about, it's quite simple - each line is VAR=value, so you can split on newlines and assign to exec.Command.Env - that's just a []string, same as os.Environ() returns.

mpictor avatar Sep 06 '19 16:09 mpictor

We inherited some tests that use env vars too, the Go test cache is a real trap for this scenario. We're already using godotenv, but the footgun for us is the test cache. It's not practical for us to rearchitect our test cases at this stage, so we always need to remember to pass -count=1, which people often forget, or didn't know about in the first place.

bjwrk avatar Oct 12 '22 05:10 bjwrk

What has seemed to work for me is godotenv -f .env gotestsum ./... . So you can programmatically swap out the env files for different environments, and also set certain tests to skip if the api keys aren't provided.

tanzyy96 avatar Apr 04 '24 07:04 tanzyy96