Added helper to switch environments.

This commit is contained in:
mikestefanello 2021-12-14 20:14:11 -05:00
parent 6b537c5626
commit 0a8d08d8bd
2 changed files with 9 additions and 3 deletions

View file

@ -1,6 +1,7 @@
package config package config
import ( import (
"os"
"time" "time"
"github.com/joeshaw/envdecode" "github.com/joeshaw/envdecode"
@ -85,3 +86,9 @@ func GetConfig() (Config, error) {
err := envdecode.StrictDecode(&cfg) err := envdecode.StrictDecode(&cfg)
return cfg, err return cfg, err
} }
func SwitchEnv(env Env) {
if err := os.Setenv("APP_ENV", string(env)); err != nil {
panic(err)
}
}

View file

@ -23,15 +23,14 @@ var (
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
// Set the environment to test // Set the environment to test
if err := os.Setenv("APP_ENV", string(config.EnvTest)); err != nil { config.SwitchEnv(config.EnvTest)
panic(err)
}
// Start a test HTTP server // Start a test HTTP server
c = container.NewContainer() c = container.NewContainer()
BuildRouter(c) BuildRouter(c)
srv = httptest.NewServer(c.Web) srv = httptest.NewServer(c.Web)
// Run tests
exitVal := m.Run() exitVal := m.Run()
srv.Close() srv.Close()
os.Exit(exitVal) os.Exit(exitVal)