updated tests
parent
caf8e6402e
commit
73b9efa695
|
@ -22,6 +22,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
"k8s.io/minikube/pkg/minikube/config"
|
||||
)
|
||||
|
@ -88,8 +89,11 @@ func TestAudit(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("shouldLog", func(t *testing.T) {
|
||||
oldArgs := os.Args
|
||||
defer func() { os.Args = oldArgs }()
|
||||
oldCommandLine := pflag.CommandLine
|
||||
defer func() {
|
||||
pflag.CommandLine = oldCommandLine
|
||||
pflag.Parse()
|
||||
}()
|
||||
|
||||
tests := []struct {
|
||||
args []string
|
||||
|
@ -122,19 +126,22 @@ func TestAudit(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, test := range tests {
|
||||
os.Args = test.args
|
||||
mockArgs(t, test.args)
|
||||
|
||||
got := shouldLog()
|
||||
|
||||
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) {
|
||||
oldArgs := os.Args
|
||||
defer func() { os.Args = oldArgs }()
|
||||
oldCommandLine := pflag.CommandLine
|
||||
defer func() {
|
||||
pflag.CommandLine = oldCommandLine
|
||||
pflag.Parse()
|
||||
}()
|
||||
|
||||
tests := []struct {
|
||||
args []string
|
||||
|
@ -159,12 +166,12 @@ func TestAudit(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, test := range tests {
|
||||
os.Args = test.args
|
||||
mockArgs(t, test.args)
|
||||
|
||||
got := isDeletePurge()
|
||||
|
||||
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 }()
|
||||
os.Args = []string{"minikube"}
|
||||
|
||||
oldCommandLine := pflag.CommandLine
|
||||
defer func() {
|
||||
pflag.CommandLine = oldCommandLine
|
||||
pflag.Parse()
|
||||
}()
|
||||
mockArgs(t, os.Args)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue