From d33d293fd2ea5f8e399146f9237d2b179c90f1f1 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 11 Jun 2025 15:39:07 +0800 Subject: [PATCH] ci: enable the usetesting linter (#5222) This linter is useful to detect the creation of temporary directories and the setting of environment variables in tests. --- .golangci.yml | 5 +++++ fuzzing/internal/helper/helper_test.go | 14 +++----------- internal/utils/log_test.go | 8 +++----- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index e045e5e05..0c9ecffa5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -15,6 +15,7 @@ linters: - unconvert - unparam - unused + - usetesting settings: depguard: rules: @@ -48,6 +49,10 @@ linters: misspell: ignore-rules: - ect + # see https://github.com/ldez/usetesting/issues/10 + usetesting: + context-background: false + context-todo: false exclusions: generated: lax presets: diff --git a/fuzzing/internal/helper/helper_test.go b/fuzzing/internal/helper/helper_test.go index 2506dc277..a9cdb210e 100644 --- a/fuzzing/internal/helper/helper_test.go +++ b/fuzzing/internal/helper/helper_test.go @@ -8,19 +8,11 @@ import ( "github.com/stretchr/testify/require" ) -func createTempDir(t *testing.T) string { - t.Helper() - dir, err := os.MkdirTemp("", "fuzzing-helper") - require.NoError(t, err) - t.Cleanup(func() { os.RemoveAll(dir) }) - return dir -} - func TestWriteCorpusFile(t *testing.T) { const data = "lorem ipsum" const expectedShaSum = "bfb7759a67daeb65410490b4d98bb9da7d1ea2ce" - dir := createTempDir(t) + dir := t.TempDir() require.NoError(t, WriteCorpusFile(dir, []byte(data))) path := filepath.Join(dir, expectedShaSum) @@ -36,7 +28,7 @@ func TestWriteCorpusFileWithPrefix(t *testing.T) { const expectedShaSum = "523f5cab80fab0c7889dbf50dd310ab8c8879f9c" const prefixLen = 7 - dir := createTempDir(t) + dir := t.TempDir() require.NoError(t, WriteCorpusFileWithPrefix(dir, []byte(data), prefixLen)) path := filepath.Join(dir, expectedShaSum) @@ -49,7 +41,7 @@ func TestWriteCorpusFileWithPrefix(t *testing.T) { } func TestCreateDirectoryIfNotExists(t *testing.T) { - dir := createTempDir(t) + dir := t.TempDir() subdir := filepath.Join(dir, "corpus") require.NoDirExists(t, subdir) diff --git a/internal/utils/log_test.go b/internal/utils/log_test.go index e1b3d7ac1..7f42282ee 100644 --- a/internal/utils/log_test.go +++ b/internal/utils/log_test.go @@ -123,8 +123,6 @@ func TestLogAddPrefixes(t *testing.T) { } func TestLogLevelFromEnv(t *testing.T) { - defer os.Unsetenv(logEnv) - testCases := []struct { envValue string expected LogLevel @@ -136,13 +134,13 @@ func TestLogLevelFromEnv(t *testing.T) { } for _, tc := range testCases { - os.Setenv(logEnv, tc.envValue) + t.Setenv(logEnv, tc.envValue) require.Equal(t, tc.expected, readLoggingEnv()) } // invalid values - os.Setenv(logEnv, "") + t.Setenv(logEnv, "") require.Equal(t, LogLevelNothing, readLoggingEnv()) - os.Setenv(logEnv, "asdf") + t.Setenv(logEnv, "asdf") require.Equal(t, LogLevelNothing, readLoggingEnv()) }