From 7b0afb1f92ba41f734545b71c388f0744523f464 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 17 Jan 2025 00:37:25 +0100 Subject: [PATCH] tests: rewrite tests --- pkg/analyzer/analyzer_test.go | 60 ++++++++++++------- .../testdata/{ => src}/common/example.go | 0 .../{ => src}/no_structpointer/example.go | 0 .../{ => src}/structpointer/example.go | 0 4 files changed, 40 insertions(+), 20 deletions(-) rename pkg/analyzer/testdata/{ => src}/common/example.go (100%) rename pkg/analyzer/testdata/{ => src}/no_structpointer/example.go (100%) rename pkg/analyzer/testdata/{ => src}/structpointer/example.go (100%) diff --git a/pkg/analyzer/analyzer_test.go b/pkg/analyzer/analyzer_test.go index b577a1f..7ea8e42 100644 --- a/pkg/analyzer/analyzer_test.go +++ b/pkg/analyzer/analyzer_test.go @@ -1,8 +1,6 @@ package analyzer_test import ( - "os" - "path/filepath" "testing" "golang.org/x/tools/go/analysis/analysistest" @@ -11,27 +9,49 @@ import ( ) func TestAnalyzer(t *testing.T) { - wd, err := os.Getwd() - if err != nil { - t.Fatalf("Failed to get wd: %s", err) + testCases := []struct { + desc string + dir string + options map[string]string + }{ + { + desc: "no func decl", + dir: "common", + }, + { + desc: "no func decl", + dir: "no_structpointer", + }, + { + desc: "func decl", + dir: "common", + options: map[string]string{ + analyzer.FlagCheckStructPointers: "true", + }, + }, + { + desc: "func decl", + dir: "structpointer", + options: map[string]string{ + analyzer.FlagCheckStructPointers: "true", + }, + }, } - testdata := filepath.Join(wd, "testdata") - t.Run("no func decl", func(t *testing.T) { - an := analyzer.NewAnalyzer() - analysistest.Run(t, testdata, an, "./common") - analysistest.Run(t, testdata, an, "./no_structpointer") - }) + for _, test := range testCases { + t.Run(test.desc+"_"+test.dir, func(t *testing.T) { + t.Parallel() - t.Run("func decl", func(t *testing.T) { - an := analyzer.NewAnalyzer() + a := analyzer.NewAnalyzer() - err := an.Flags.Set(analyzer.FlagCheckStructPointers, "true") - if err != nil { - t.Fatal(err) - } + for k, v := range test.options { + err := a.Flags.Set(k, v) + if err != nil { + t.Fatal(err) + } + } - analysistest.Run(t, testdata, an, "./common") - analysistest.Run(t, testdata, an, "./structpointer") - }) + analysistest.Run(t, analysistest.TestData(), a, test.dir) + }) + } } diff --git a/pkg/analyzer/testdata/common/example.go b/pkg/analyzer/testdata/src/common/example.go similarity index 100% rename from pkg/analyzer/testdata/common/example.go rename to pkg/analyzer/testdata/src/common/example.go diff --git a/pkg/analyzer/testdata/no_structpointer/example.go b/pkg/analyzer/testdata/src/no_structpointer/example.go similarity index 100% rename from pkg/analyzer/testdata/no_structpointer/example.go rename to pkg/analyzer/testdata/src/no_structpointer/example.go diff --git a/pkg/analyzer/testdata/structpointer/example.go b/pkg/analyzer/testdata/src/structpointer/example.go similarity index 100% rename from pkg/analyzer/testdata/structpointer/example.go rename to pkg/analyzer/testdata/src/structpointer/example.go