From a86522836bf97df843b53dd2d28f75ebe3cf3b09 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius Date: Sun, 9 Apr 2023 22:01:20 +0900 Subject: [PATCH 1/3] refactor: define inputs and goldens on the same line --- gazelle/python/python_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gazelle/python/python_test.go b/gazelle/python/python_test.go index 51e0101df1..77e6f4c2a3 100644 --- a/gazelle/python/python_test.go +++ b/gazelle/python/python_test.go @@ -74,8 +74,7 @@ func TestGazelleBinary(t *testing.T) { func testPath(t *testing.T, name string, files []bazel.RunfileEntry) { t.Run(name, func(t *testing.T) { - var inputs []testtools.FileSpec - var goldens []testtools.FileSpec + var inputs, goldens []testtools.FileSpec var config *testYAML for _, f := range files { From 0cfa05b1537cdfbdd3b28017454ae7d740f2881d Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius Date: Sun, 9 Apr 2023 22:02:00 +0900 Subject: [PATCH 2/3] refactor: reduce indentation by early returns --- gazelle/python/python_test.go | 42 ++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/gazelle/python/python_test.go b/gazelle/python/python_test.go index 77e6f4c2a3..3fe74f99fe 100644 --- a/gazelle/python/python_test.go +++ b/gazelle/python/python_test.go @@ -110,35 +110,41 @@ func testPath(t *testing.T, name string, files []bazel.RunfileEntry) { Path: filepath.Join(name, strings.TrimSuffix(shortPath, ".in")), Content: string(content), }) - } else if strings.HasSuffix(shortPath, ".out") { + continue + } + + if strings.HasSuffix(shortPath, ".out") { goldens = append(goldens, testtools.FileSpec{ Path: filepath.Join(name, strings.TrimSuffix(shortPath, ".out")), Content: string(content), }) - } else { - inputs = append(inputs, testtools.FileSpec{ - Path: filepath.Join(name, shortPath), - Content: string(content), - }) - goldens = append(goldens, testtools.FileSpec{ - Path: filepath.Join(name, shortPath), - Content: string(content), - }) + continue } + + inputs = append(inputs, testtools.FileSpec{ + Path: filepath.Join(name, shortPath), + Content: string(content), + }) + goldens = append(goldens, testtools.FileSpec{ + Path: filepath.Join(name, shortPath), + Content: string(content), + }) } testdataDir, cleanup := testtools.CreateFiles(t, inputs) defer cleanup() defer func() { - if t.Failed() { - filepath.Walk(testdataDir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - t.Logf("%q exists", strings.TrimPrefix(path, testdataDir)) - return nil - }) + if !t.Failed() { + return } + + filepath.Walk(testdataDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + t.Logf("%q exists", strings.TrimPrefix(path, testdataDir)) + return nil + }) }() workspaceRoot := filepath.Join(testdataDir, name) From 8cd39904311c4df990feb28fb5bbe7a7af14a01b Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius Date: Sun, 9 Apr 2023 22:03:47 +0900 Subject: [PATCH 3/3] feat: run tests in parallel and use t.Cleanup --- gazelle/python/python_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/gazelle/python/python_test.go b/gazelle/python/python_test.go index 3fe74f99fe..79450ad584 100644 --- a/gazelle/python/python_test.go +++ b/gazelle/python/python_test.go @@ -74,6 +74,7 @@ func TestGazelleBinary(t *testing.T) { func testPath(t *testing.T, name string, files []bazel.RunfileEntry) { t.Run(name, func(t *testing.T) { + t.Parallel() var inputs, goldens []testtools.FileSpec var config *testYAML @@ -132,8 +133,8 @@ func testPath(t *testing.T, name string, files []bazel.RunfileEntry) { } testdataDir, cleanup := testtools.CreateFiles(t, inputs) - defer cleanup() - defer func() { + t.Cleanup(cleanup) + t.Cleanup(func() { if !t.Failed() { return } @@ -145,14 +146,14 @@ func testPath(t *testing.T, name string, files []bazel.RunfileEntry) { t.Logf("%q exists", strings.TrimPrefix(path, testdataDir)) return nil }) - }() + }) workspaceRoot := filepath.Join(testdataDir, name) args := []string{"-build_file_name=BUILD,BUILD.bazel"} ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) - defer cancel() + t.Cleanup(cancel) cmd := exec.CommandContext(ctx, gazellePath, args...) var stdout, stderr bytes.Buffer cmd.Stdout = &stdout