From eeb16638475de5637e73a63f3f22fad00febe5e2 Mon Sep 17 00:00:00 2001 From: Ville Vesilehto Date: Tue, 27 May 2025 20:02:55 +0300 Subject: [PATCH] lint: enable usetesting linter (#7322) Enable the usetesting linter in golangci.yml configuration to enforce proper testing practices. Replace manual temporary directory and file creation with t.TempDir() in test files. This improves test reliability by ensuring proper cleanup and follows Go testing best practices. Signed-off-by: Ville Vesilehto --- .golangci.yml | 1 + plugin/file/tree/print_test.go | 2 +- plugin/pkg/tls/tls_test.go | 12 ++---------- plugin/root/root_test.go | 2 +- 4 files changed, 5 insertions(+), 12 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 9da63276a..3eca2593b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -8,6 +8,7 @@ linters: - unconvert - unused - whitespace + - usetesting exclusions: generated: lax presets: diff --git a/plugin/file/tree/print_test.go b/plugin/file/tree/print_test.go index 20ad37d9a..8c6c2b331 100644 --- a/plugin/file/tree/print_test.go +++ b/plugin/file/tree/print_test.go @@ -70,7 +70,7 @@ func TestPrint(t *testing.T) { */ - f, err := os.CreateTemp("", "print_test_tmp") + f, err := os.CreateTemp(t.TempDir(), "print_test_tmp") if err != nil { t.Error(err) } diff --git a/plugin/pkg/tls/tls_test.go b/plugin/pkg/tls/tls_test.go index a5635c177..b7ed109f7 100644 --- a/plugin/pkg/tls/tls_test.go +++ b/plugin/pkg/tls/tls_test.go @@ -1,7 +1,6 @@ package tls import ( - "os" "path/filepath" "testing" @@ -79,15 +78,8 @@ func TestNewTLSConfigFromArgs(t *testing.T) { func TestNewTLSConfigFromArgsWithRoot(t *testing.T) { cert, key, ca := getPEMFiles(t) - tempDir, err := os.MkdirTemp("", "go-test-pemfiles") - defer func() { - if err := os.RemoveAll(tempDir); err != nil { - t.Error("failed to clean up temporary directory", err) - } - }() - if err != nil { - t.Error("failed to create temporary directory", err) - } + tempDir := t.TempDir() + root := tempDir args := []string{cert, key, ca} for i := range args { diff --git a/plugin/root/root_test.go b/plugin/root/root_test.go index 27bdf84ad..f92912451 100644 --- a/plugin/root/root_test.go +++ b/plugin/root/root_test.go @@ -23,7 +23,7 @@ func TestRoot(t *testing.T) { nonExistingDir := filepath.Join(existingDirPath, "highly_unlikely_to_exist_dir") - existingFile, err := os.CreateTemp("", "root_test") + existingFile, err := os.CreateTemp(t.TempDir(), "root_test") if err != nil { t.Fatalf("BeforeTest: Failed to create temp file for testing! Error was: %v", err) }