From e9fb8055227c77054b0d9f055fb53da89d60863e Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Tue, 7 Jan 2025 15:44:29 +0100 Subject: [PATCH] update golangci/golangci-lint to v1.63.4 Fix new issues found by usetesting, mainly we should use t.TempDir() in test which makes the code better as this will be removed on test end automatically so no need for defer or any error checking. Also fix issues reported by exptostd, these mainly show where we can switch the imports to the std maps/slices packages instead of the golang.org/x/exp/... packages. Signed-off-by: Paul Holzinger --- Makefile | 2 +- cmd/quadlet/main_test.go | 20 ++++---------------- libpod/events/logfile_test.go | 12 ++++++------ libpod/runtime.go | 2 +- pkg/api/handlers/compat/networks.go | 2 +- pkg/api/handlers/libpod/manifests.go | 2 +- pkg/auth/auth_test.go | 20 ++++++++------------ pkg/ctime/ctime_test.go | 7 +++---- pkg/env/env.go | 3 +-- pkg/machine/compression/copy_test.go | 24 ++++++------------------ 10 files changed, 32 insertions(+), 62 deletions(-) diff --git a/Makefile b/Makefile index 97d7015733..bf81a02e02 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ BUILDTAGS += ${EXTRA_BUILDTAGS} # N/B: This value is managed by Renovate, manual changes are # possible, as long as they don't disturb the formatting # (i.e. DO NOT ADD A 'v' prefix!) -GOLANGCI_LINT_VERSION := 1.62.2 +GOLANGCI_LINT_VERSION := 1.63.4 PYTHON ?= $(shell command -v python3 python|head -n1) PKG_MANAGER ?= $(shell command -v dnf yum|head -n1) # ~/.local/bin is not in PATH on all systems diff --git a/cmd/quadlet/main_test.go b/cmd/quadlet/main_test.go index d7efc80cc8..e2340ac021 100644 --- a/cmd/quadlet/main_test.go +++ b/cmd/quadlet/main_test.go @@ -95,11 +95,7 @@ func TestUnitDirs(t *testing.T) { unitDirs = getUnitDirs(false) assert.Equal(t, []string{}, unitDirs) - name, err := os.MkdirTemp("", "dir") - assert.Nil(t, err) - // remove the temporary directory at the end of the program - defer os.RemoveAll(name) - + name := t.TempDir() t.Setenv("QUADLET_UNIT_DIRS", name) unitDirs = getUnitDirs(false) assert.Equal(t, []string{name}, unitDirs, "rootful should use environment variable") @@ -107,10 +103,7 @@ func TestUnitDirs(t *testing.T) { unitDirs = getUnitDirs(true) assert.Equal(t, []string{name}, unitDirs, "rootless should use environment variable") - symLinkTestBaseDir, err := os.MkdirTemp("", "podman-symlinktest") - assert.Nil(t, err) - // remove the temporary directory at the end of the program - defer os.RemoveAll(symLinkTestBaseDir) + symLinkTestBaseDir := t.TempDir() actualDir := filepath.Join(symLinkTestBaseDir, "actual") err = os.Mkdir(actualDir, 0755) @@ -152,10 +145,7 @@ func TestUnitDirs(t *testing.T) { assert.Nil(t, err) } - symLinkRecursiveTestBaseDir, err := os.MkdirTemp("", "podman-symlink-recursive-test") - assert.Nil(t, err) - // remove the temporary directory at the end of the program - defer os.RemoveAll(symLinkRecursiveTestBaseDir) + symLinkRecursiveTestBaseDir := t.TempDir() expectedDirs := make([]string, 0) // Create /unitDir @@ -214,9 +204,7 @@ func TestUnitDirs(t *testing.T) { } else { fmt.Println(os.Args) - symLinkTestBaseDir, err := os.MkdirTemp("", "podman-symlinktest2") - assert.Nil(t, err) - defer os.RemoveAll(symLinkTestBaseDir) + symLinkTestBaseDir := t.TempDir() rootF, err := os.Open("/") assert.Nil(t, err) defer rootF.Close() diff --git a/libpod/events/logfile_test.go b/libpod/events/logfile_test.go index f391ef2277..bd1b46ea3f 100644 --- a/libpod/events/logfile_test.go +++ b/libpod/events/logfile_test.go @@ -30,10 +30,10 @@ func TestRotateLog(t *testing.T) { {1000, 600, 1500, true}, } + tmpDir := t.TempDir() for _, test := range tests { - tmp, err := os.CreateTemp("", "log-rotation-") + tmp, err := os.CreateTemp(tmpDir, "log-rotation-") require.NoError(t, err) - defer os.Remove(tmp.Name()) defer tmp.Close() // Create dummy file and content. @@ -80,9 +80,8 @@ func TestTruncationOutput(t *testing.T) { 10 ` // Create dummy file - tmp, err := os.CreateTemp("", "log-rotation") + tmp, err := os.CreateTemp(t.TempDir(), "log-rotation") require.NoError(t, err) - defer os.Remove(tmp.Name()) defer tmp.Close() // Write content before truncation to dummy file @@ -114,10 +113,11 @@ func TestRenameLog(t *testing.T) { 4 5 ` + tmpDir := t.TempDir() // Create two dummy files - source, err := os.CreateTemp("", "removing") + source, err := os.CreateTemp(tmpDir, "removing") require.NoError(t, err) - target, err := os.CreateTemp("", "renaming") + target, err := os.CreateTemp(tmpDir, "renaming") require.NoError(t, err) // Write to source dummy file diff --git a/libpod/runtime.go b/libpod/runtime.go index 1daf2855b2..ddef694a36 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -10,6 +10,7 @@ import ( "io/fs" "os" "path/filepath" + "slices" "strings" "sync" "syscall" @@ -44,7 +45,6 @@ import ( jsoniter "github.com/json-iterator/go" spec "github.com/opencontainers/runtime-spec/specs-go" "github.com/sirupsen/logrus" - "golang.org/x/exp/slices" ) // Set up the JSON library for all of Libpod diff --git a/pkg/api/handlers/compat/networks.go b/pkg/api/handlers/compat/networks.go index 4d31cfd2e9..a2e88ca3ba 100644 --- a/pkg/api/handlers/compat/networks.go +++ b/pkg/api/handlers/compat/networks.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "net" "net/http" @@ -18,7 +19,6 @@ import ( "github.com/containers/podman/v5/pkg/domain/entities" "github.com/containers/podman/v5/pkg/domain/infra/abi" "github.com/containers/podman/v5/pkg/util" - "golang.org/x/exp/maps" dockerNetwork "github.com/docker/docker/api/types/network" "github.com/sirupsen/logrus" diff --git a/pkg/api/handlers/libpod/manifests.go b/pkg/api/handlers/libpod/manifests.go index d7ab50f918..8f19554de9 100644 --- a/pkg/api/handlers/libpod/manifests.go +++ b/pkg/api/handlers/libpod/manifests.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "io" + "maps" "net/http" "net/url" "os" @@ -33,7 +34,6 @@ import ( "github.com/gorilla/schema" "github.com/opencontainers/go-digest" "github.com/sirupsen/logrus" - "golang.org/x/exp/maps" ) func ManifestCreate(w http.ResponseWriter, r *http.Request) { diff --git a/pkg/auth/auth_test.go b/pkg/auth/auth_test.go index 90a81ac9ab..37cf817917 100644 --- a/pkg/auth/auth_test.go +++ b/pkg/auth/auth_test.go @@ -31,17 +31,17 @@ var largeAuthFileValues = map[string]types.DockerAuthConfig{ // systemContextForAuthFile returns a types.SystemContext with AuthFilePath pointing // to a temporary file with fileContents, or nil if fileContents is empty; and a cleanup // function the caller must arrange to call. -func systemContextForAuthFile(t *testing.T, fileContents string) (*types.SystemContext, func()) { +func systemContextForAuthFile(t *testing.T, fileContents string) *types.SystemContext { if fileContents == "" { - return nil, func() {} + return nil } - f, err := os.CreateTemp("", "auth.json") + f, err := os.CreateTemp(t.TempDir(), "auth.json") require.NoError(t, err) path := f.Name() err = os.WriteFile(path, []byte(fileContents), 0700) require.NoError(t, err) - return &types.SystemContext{AuthFilePath: path}, func() { os.Remove(path) } + return &types.SystemContext{AuthFilePath: path} } // Test that GetCredentials() correctly parses what MakeXRegistryConfigHeader() produces @@ -78,8 +78,7 @@ func TestMakeXRegistryConfigHeaderGetCredentialsRoundtrip(t *testing.T) { expectedFileValues: largeAuthFileValues, }, } { - sys, cleanup := systemContextForAuthFile(t, tc.fileContents) - defer cleanup() + sys := systemContextForAuthFile(t, tc.fileContents) headers, err := MakeXRegistryConfigHeader(sys, tc.username, tc.password) require.NoError(t, err) req, err := http.NewRequest(http.MethodPost, "/", nil) @@ -130,8 +129,7 @@ func TestMakeXRegistryAuthHeaderGetCredentialsRoundtrip(t *testing.T) { expectedFileValues: largeAuthFileValues, }, } { - sys, cleanup := systemContextForAuthFile(t, tc.fileContents) - defer cleanup() + sys := systemContextForAuthFile(t, tc.fileContents) headers, err := MakeXRegistryAuthHeader(sys, tc.username, tc.password) require.NoError(t, err) req, err := http.NewRequest(http.MethodPost, "/", nil) @@ -205,8 +203,7 @@ func TestMakeXRegistryConfigHeader(t *testing.T) { }`, }, } { - sys, cleanup := systemContextForAuthFile(t, tc.fileContents) - defer cleanup() + sys := systemContextForAuthFile(t, tc.fileContents) res, err := MakeXRegistryConfigHeader(sys, tc.username, tc.password) if tc.shouldErr { assert.Error(t, err, tc.name) @@ -268,8 +265,7 @@ func TestMakeXRegistryAuthHeader(t *testing.T) { }`, }, } { - sys, cleanup := systemContextForAuthFile(t, tc.fileContents) - defer cleanup() + sys := systemContextForAuthFile(t, tc.fileContents) res, err := MakeXRegistryAuthHeader(sys, tc.username, tc.password) if tc.shouldErr { assert.Error(t, err, tc.name) diff --git a/pkg/ctime/ctime_test.go b/pkg/ctime/ctime_test.go index 014f15aa91..7d26e7bfc2 100644 --- a/pkg/ctime/ctime_test.go +++ b/pkg/ctime/ctime_test.go @@ -9,17 +9,16 @@ import ( func TestCreated(t *testing.T) { before := time.Now() - fileA, err := os.CreateTemp("", "ctime-test-") + tmpDir := t.TempDir() + fileA, err := os.CreateTemp(tmpDir, "ctime-test-") if err != nil { t.Error(err) } - defer os.Remove(fileA.Name()) - fileB, err := os.CreateTemp("", "ctime-test-") + fileB, err := os.CreateTemp(tmpDir, "ctime-test-") if err != nil { t.Error(err) } - defer os.Remove(fileB.Name()) after := time.Now() diff --git a/pkg/env/env.go b/pkg/env/env.go index a2165931ca..31ec789593 100644 --- a/pkg/env/env.go +++ b/pkg/env/env.go @@ -6,10 +6,9 @@ package env import ( "bufio" "fmt" + "maps" "os" "strings" - - "golang.org/x/exp/maps" ) const whiteSpaces = " \t" diff --git a/pkg/machine/compression/copy_test.go b/pkg/machine/compression/copy_test.go index 1344211c5c..b27a86d959 100644 --- a/pkg/machine/compression/copy_test.go +++ b/pkg/machine/compression/copy_test.go @@ -2,7 +2,6 @@ package compression import ( "os" - "path/filepath" "testing" crcOs "github.com/crc-org/crc/v2/pkg/os" @@ -11,11 +10,9 @@ import ( func TestCopyFile(t *testing.T) { testStr := "test-machine" - srcFile, err := os.CreateTemp("", "machine-test-") - if err != nil { - t.Fatal(err) - } - srcFi, err := srcFile.Stat() + tmpDir := t.TempDir() + + srcFile, err := os.CreateTemp(tmpDir, "machine-test-") if err != nil { t.Fatal(err) } @@ -23,27 +20,18 @@ func TestCopyFile(t *testing.T) { _, _ = srcFile.Write([]byte(testStr)) //nolint:mirror srcFile.Close() - srcFilePath := filepath.Join(os.TempDir(), srcFi.Name()) - - destFile, err := os.CreateTemp("", "machine-copy-test-") - if err != nil { - t.Fatal(err) - } - - destFi, err := destFile.Stat() + destFile, err := os.CreateTemp(tmpDir, "machine-copy-test-") if err != nil { t.Fatal(err) } destFile.Close() - destFilePath := filepath.Join(os.TempDir(), destFi.Name()) - - if err := crcOs.CopyFile(srcFilePath, destFilePath); err != nil { + if err := crcOs.CopyFile(srcFile.Name(), destFile.Name()); err != nil { t.Fatal(err) } - data, err := os.ReadFile(destFilePath) + data, err := os.ReadFile(destFile.Name()) if err != nil { t.Fatal(err) }