updated tests

pull/13995/head
Steven Powell 2022-04-20 16:35:13 -07:00
parent caf8e6402e
commit 73b9efa695
1 changed files with 37 additions and 8 deletions

View File

@ -22,6 +22,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/spf13/pflag"
"github.com/spf13/viper" "github.com/spf13/viper"
"k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/config"
) )
@ -88,8 +89,11 @@ func TestAudit(t *testing.T) {
}) })
t.Run("shouldLog", func(t *testing.T) { t.Run("shouldLog", func(t *testing.T) {
oldArgs := os.Args oldCommandLine := pflag.CommandLine
defer func() { os.Args = oldArgs }() defer func() {
pflag.CommandLine = oldCommandLine
pflag.Parse()
}()
tests := []struct { tests := []struct {
args []string args []string
@ -122,19 +126,22 @@ func TestAudit(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
os.Args = test.args mockArgs(t, test.args)
got := shouldLog() got := shouldLog()
if got != test.want { if got != test.want {
t.Errorf("os.Args = %q; shouldLog() = %t; want %t", os.Args, got, test.want) t.Errorf("test.args = %q; shouldLog() = %t; want %t", test.args, got, test.want)
} }
} }
}) })
t.Run("isDeletePurge", func(t *testing.T) { t.Run("isDeletePurge", func(t *testing.T) {
oldArgs := os.Args oldCommandLine := pflag.CommandLine
defer func() { os.Args = oldArgs }() defer func() {
pflag.CommandLine = oldCommandLine
pflag.Parse()
}()
tests := []struct { tests := []struct {
args []string args []string
@ -159,12 +166,12 @@ func TestAudit(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
os.Args = test.args mockArgs(t, test.args)
got := isDeletePurge() got := isDeletePurge()
if got != test.want { if got != test.want {
t.Errorf("os.Args = %q; isDeletePurge() = %t; want %t", os.Args, got, test.want) t.Errorf("test.args = %q; isDeletePurge() = %t; want %t", test.args, got, test.want)
} }
} }
}) })
@ -175,6 +182,28 @@ func TestAudit(t *testing.T) {
defer func() { os.Args = oldArgs }() defer func() { os.Args = oldArgs }()
os.Args = []string{"minikube"} os.Args = []string{"minikube"}
oldCommandLine := pflag.CommandLine
defer func() {
pflag.CommandLine = oldCommandLine
pflag.Parse()
}()
mockArgs(t, os.Args)
Log(time.Now()) Log(time.Now())
}) })
} }
func mockArgs(t *testing.T, args []string) {
if len(args) == 0 {
t.Fatalf("cannot pass an empty slice to mockArgs")
}
fs := pflag.NewFlagSet(args[0], pflag.ExitOnError)
fs.Bool("purge", false, "")
if err := fs.Parse(args[1:]); err != nil {
t.Fatal(err)
}
pflag.CommandLine = fs
if err := viper.BindPFlags(pflag.CommandLine); err != nil {
t.Fatal(err)
}
}