github.com/Accefy/pop@v0.0.0-20230428174248-e9f677eab5b9/soda/cmd/root_integration_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func Test_RootCmd_Environment(t *testing.T) {
    11  	oldEnv := os.Getenv("GO_ENV")
    12  	defer os.Setenv("GO_ENV", oldEnv)
    13  
    14  	r := require.New(t)
    15  	c := RootCmd
    16  
    17  	// Fallback on default env
    18  	c.SetArgs([]string{"help"})
    19  	err := c.Execute()
    20  	r.NoError(err)
    21  	r.Equal("development", env)
    22  
    23  	// Override with GO_ENV
    24  	c.SetArgs([]string{"help"})
    25  	os.Setenv("GO_ENV", "test")
    26  	err = c.Execute()
    27  	r.NoError(err)
    28  	r.Equal("test", env)
    29  
    30  	// CLI flag priority: the preferred order of flags and commands
    31  	c.SetArgs([]string{
    32  		"--env",
    33  		"production",
    34  		"help",
    35  	})
    36  	os.Setenv("GO_ENV", "test")
    37  	err = c.Execute()
    38  	r.NoError(err)
    39  	r.Equal("production", env)
    40  
    41  	// the following order works fine now but need to be considered again
    42  	// CLI flag priority
    43  	c.SetArgs([]string{
    44  		"help",
    45  		"--env",
    46  		"production",
    47  	})
    48  	os.Setenv("GO_ENV", "test")
    49  	err = c.Execute()
    50  	r.NoError(err)
    51  	r.Equal("production", env)
    52  }