github.com/gobuffalo/buffalo-cli/v2@v2.0.0-alpha.15.0.20200919213536-a7350c8e6799/cli/cmds/test/go_env.go (about)

     1  package test
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  
     7  	"github.com/gobuffalo/plugins"
     8  )
     9  
    10  var _ plugins.Plugin = &GoEnv{}
    11  var _ BeforeTester = &GoEnv{}
    12  var _ AfterTester = &GoEnv{}
    13  
    14  //GoEnv Sets GO_ENV before tests run
    15  type GoEnv struct {
    16  	cachedValue string
    17  }
    18  
    19  //PluginName for BeforeTestEnv
    20  func (ebt GoEnv) PluginName() string {
    21  	return "env/tests"
    22  }
    23  
    24  //BeforeTest should be invoked before tests run to set the GO_ENV variable
    25  func (ebt *GoEnv) BeforeTest(ctx context.Context, root string, args []string) error {
    26  	ebt.cachedValue = os.Getenv("GO_ENV")
    27  	return os.Setenv("GO_ENV", "test")
    28  }
    29  
    30  //AfterTest should be invoked after tests run to reset GO_ENV variable
    31  func (ebt *GoEnv) AfterTest(ctx context.Context, root string, args []string, err error) error {
    32  	return os.Setenv("GO_ENV", ebt.cachedValue)
    33  }