Skip to content

Commit

Permalink
tests: improve test coverage for flags
Browse files Browse the repository at this point in the history
  • Loading branch information
cfabianski committed Dec 18, 2023
1 parent 31a76bb commit 7f04bcf
Show file tree
Hide file tree
Showing 5 changed files with 371 additions and 55 deletions.
36 changes: 23 additions & 13 deletions internal/flag/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (

type Flags []flagtypes.FlagGroup

const envPrefix = "bearer"

var ErrInvalidScannerReportCombination = errors.New("invalid scanner argument; privacy report requires sast scanner")

type flagGroupBase struct {
Expand Down Expand Up @@ -46,6 +48,26 @@ func addFlag(cmd *cobra.Command, flag *flagtypes.Flag) {
}
}

func BindViper(flag *flagtypes.Flag) error {
arguments := append(
[]string{
flag.ConfigName,
strings.ToUpper(
strings.Join(
[]string{
envPrefix,
strings.ReplaceAll(flag.Name, "-", "_"),
},
"_",
),
),
},
flag.EnvironmentVariables...,
)

return viper.BindEnv(arguments...)
}

func bind(cmd *cobra.Command, flag *flagtypes.Flag) error {
if flag == nil {
return nil
Expand All @@ -59,19 +81,7 @@ func bind(cmd *cobra.Command, flag *flagtypes.Flag) error {
return err
}

viper.AutomaticEnv()
viper.SetEnvPrefix("bearer")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
arguments := append(
[]string{flag.ConfigName},
flag.EnvironmentVariables...,
)

if err := viper.BindEnv(arguments...); err != nil {
return err
}

return nil
return BindViper(flag)
}

func argsToMap(flag *flagtypes.Flag) map[string]bool {
Expand Down
46 changes: 8 additions & 38 deletions internal/flag/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,18 @@ package flag

import (
"testing"

flagtypes "github.com/bearer/bearer/internal/flag/types"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func Test_getStringSlice(t *testing.T) {
type env struct {
key string
value string
}
tests := []struct {
name string
flag *flagtypes.Flag
flagValue interface{}
env env
want []string
}{
testCases := []TestCase{
{
name: "happy path. Empty value",
name: "Empty value",
flag: ScannerFlag,
flagValue: "",
want: nil,
},
{
name: "happy path. String value",
name: "String value",
flag: ScannerFlag,
flagValue: "sast,secrets",
want: []string{
Expand All @@ -36,7 +22,7 @@ func Test_getStringSlice(t *testing.T) {
},
},
{
name: "happy path. Slice value",
name: "Slice value",
flag: ScannerFlag,
flagValue: []string{
"sast",
Expand All @@ -48,11 +34,11 @@ func Test_getStringSlice(t *testing.T) {
},
},
{
name: "happy path. Env value",
name: "Env value",
flag: ScannerFlag,
env: env{
env: Env{
key: "BEARER_SCANNER",
value: "secrets,sast",
value: "sast,secrets",
},
want: []string{
string(ScannerSAST),
Expand All @@ -61,21 +47,5 @@ func Test_getStringSlice(t *testing.T) {
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.env.key == "" {
viper.Set(tt.flag.ConfigName, tt.flagValue)
} else {
// err := viper.BindEnv(tt.flag.ConfigName, tt.env.key)
// assert.NoError(t, err)

t.Setenv(tt.env.key, tt.env.value)
}

sl := getStringSlice(tt.flag)
assert.Equal(t, tt.want, sl)

viper.Reset()
})
}
RunFlagTests(testCases, t)
}
Loading

0 comments on commit 7f04bcf

Please sign in to comment.