From e6637a7bc8b9998a70198628d8b853ab0fd8fc07 Mon Sep 17 00:00:00 2001 From: serosset Date: Mon, 7 Nov 2022 14:14:09 +0000 Subject: [PATCH 1/2] refactor UT to make it easier to test glob expansion --- cmd/root_test.go | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index 87071e7e..e7193972 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -4,7 +4,9 @@ import ( "bytes" "io" "os" + "path/filepath" "regexp" + "strings" "testing" "github.com/get-woke/woke/pkg/output" @@ -20,9 +22,12 @@ import ( // run profiling with // go test -v -cpuprofile cpu.prof -memprofile mem.prof -bench=. ./cmd // memory: -// go tool pprof mem.prof +// +// go tool pprof mem.prof +// // cpu: -// go tool pprof cpu.prof +// +// go tool pprof cpu.prof func BenchmarkRootRunE(b *testing.B) { zerolog.SetGlobalLevel(zerolog.NoLevel) output.Stdout = io.Discard @@ -70,12 +75,39 @@ func TestParseArgs(t *testing.T) { t.Cleanup(func() { stdin = false }) - assert.Equal(t, parser.DefaultPath, parseArgs([]string{})) - assert.Equal(t, []string{"../.."}, parseArgs([]string{"../.."})) - - stdin = true - assert.Equal(t, []string{os.Stdin.Name()}, parseArgs([]string{})) - assert.Equal(t, []string{os.Stdin.Name()}, parseArgs([]string{"../.."})) + tests := []struct { + stdin bool + args []string + expectedArgs []string + }{ + { + stdin: false, + args: []string{}, + expectedArgs: parser.DefaultPath, + }, + { + stdin: false, + args: []string{"../.."}, + expectedArgs: []string{filepath.Join("..", "..")}, + }, + { + stdin: true, + args: []string{}, + expectedArgs: []string{os.Stdin.Name()}, + }, + { + stdin: true, + args: []string{"../.."}, + expectedArgs: []string{os.Stdin.Name()}, + }, + } + for _, tt := range tests { + t.Run(strings.Join(tt.args, " "), func(t *testing.T) { + stdin = tt.stdin + files := parseArgs(tt.args) + assert.Equal(t, tt.expectedArgs, files) + }) + } } func TestRunE(t *testing.T) { From fe2682a06721d9de9919ccb4acf37950d233dabd Mon Sep 17 00:00:00 2001 From: serosset Date: Mon, 7 Nov 2022 14:31:01 +0000 Subject: [PATCH 2/2] refactor UT to make it easier to test glob expansion --- cmd/root_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/root_test.go b/cmd/root_test.go index e7193972..706028d3 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -4,7 +4,6 @@ import ( "bytes" "io" "os" - "path/filepath" "regexp" "strings" "testing" @@ -88,7 +87,7 @@ func TestParseArgs(t *testing.T) { { stdin: false, args: []string{"../.."}, - expectedArgs: []string{filepath.Join("..", "..")}, + expectedArgs: []string{"../.."}, }, { stdin: true,