From 73b9efa6950af4f04f27239bbdf8da7203940de1 Mon Sep 17 00:00:00 2001 From: Steven Powell Date: Wed, 20 Apr 2022 16:35:13 -0700 Subject: [PATCH] updated tests --- pkg/minikube/audit/audit_test.go | 45 ++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/pkg/minikube/audit/audit_test.go b/pkg/minikube/audit/audit_test.go index 7c36ae6556..e91f7d972a 100644 --- a/pkg/minikube/audit/audit_test.go +++ b/pkg/minikube/audit/audit_test.go @@ -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) + } +}