Skip to content

Commit

Permalink
Merge pull request #3534 from dtrudg/additional-linter-swaps
Browse files Browse the repository at this point in the history
chore: swap deprecated tenv linter for usetesting
  • Loading branch information
dtrudg authored Feb 18, 2025
2 parents de0ed19 + 9314d86 commit 2fcb7ac
Show file tree
Hide file tree
Showing 32 changed files with 117 additions and 257 deletions.
10 changes: 9 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ linters:
- reassign
- revive
- staticcheck
- tenv
- typecheck
- unparam
- unused
- usetesting
- whitespace

linters-settings:
Expand All @@ -70,5 +70,13 @@ issues:
# G204 disallows exec.Command() with a command/args stored in variables
# G306 disallows creation of files with permissions greater than 0600
text: "^(G107|G204|G306):"
- path: 'e2e/'
linters:
# The usetesting linter shows a lot of lint for env var handling and
# tempdir creation in e2e tests. It's not really valid lint - the e2e
# tests were explicitly written to keep tempdirs in a per-run outer dir,
# and env var handling is complicated by the e2e.SingularityCmd
# execution flow etc.
- usetesting
exclude-files:
- "internal/pkg/util/user/cgo_lookup_unix.go"
5 changes: 2 additions & 3 deletions cmd/internal/cli/key_newpair_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2025, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -61,14 +61,13 @@ func Test_collectInput_flags(t *testing.T) {
}

func TestCollectInput(t *testing.T) {
tf, err := os.CreateTemp("", "collect-test-")
tf, err := os.CreateTemp(t.TempDir(), "collect-test-")
assert.NilError(t, err)
defer tf.Close()

oldStdin := os.Stdin
defer func(ostdin *os.File) {
os.Stdin = ostdin
os.Remove(tf.Name())
}(oldStdin)
os.Stdin = tf

Expand Down
31 changes: 10 additions & 21 deletions internal/app/singularity/remote_add_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2019-2025, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand All @@ -7,6 +7,7 @@ package singularity

import (
"os"
"path/filepath"
"testing"

"github.com/sylabs/singularity/v4/internal/pkg/remote"
Expand All @@ -23,12 +24,7 @@ const (
)

func createInvalidCfgFile(t *testing.T) string {
f, err := os.CreateTemp("", "")
if err != nil {
t.Fatalf("cannot create temporary configuration file for testing: %s\n", err)
}

path := f.Name()
path := filepath.Join(t.TempDir(), "invalid.yml")

// Set an invalid configuration
type aDummyStruct struct {
Expand All @@ -40,24 +36,18 @@ func createInvalidCfgFile(t *testing.T) string {

yaml, err := yaml.Marshal(cfg)
if err != nil {
f.Close()
os.Remove(path)
t.Fatalf("cannot marshal YAML: %s\n", err)
}

f.Write(yaml)
f.Close()
if err := os.WriteFile(path, yaml, 0o644); err != nil {
t.Fatal(err)
}

return path
}

func createValidCfgFile(t *testing.T) string {
f, err := os.CreateTemp("", "")
if err != nil {
t.Fatalf("cannot create temporary configuration file for testing: %s\n", err)
}

path := f.Name()
path := filepath.Join(t.TempDir(), "valid.yml")

// Set a valid configuration
cfg := remote.Config{
Expand All @@ -76,13 +66,12 @@ func createValidCfgFile(t *testing.T) string {

yaml, err := yaml.Marshal(cfg)
if err != nil {
f.Close()
os.Remove(path)
t.Fatalf("cannot marshal YAML: %s\n", err)
}

f.Write(yaml)
f.Close()
if err := os.WriteFile(path, yaml, 0o644); err != nil {
t.Fatal(err)
}

return path
}
Expand Down
13 changes: 3 additions & 10 deletions internal/pkg/build/sources/conveyorPacker_local_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2024, Sylabs Inc. All rights reserved.
// Copyright (c) 2018-2025, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand All @@ -20,12 +20,7 @@ import (

func TestLocalPackerSquashfs(t *testing.T) {
require.Command(t, "mksquashfs")

tempDirPath, err := os.MkdirTemp("", "test-localpacker-squashfs")
if err != nil {
t.Fatalf("while creating temp dir: %v", err)
}
defer os.RemoveAll(tempDirPath)
tempDirPath := t.TempDir()

// Image root directory
rootfs := filepath.Join(tempDirPath, "issue_3084_rootfs")
Expand Down Expand Up @@ -65,9 +60,7 @@ func TestLocalPackerSquashfs(t *testing.T) {
defer os.Remove(image)

// Creates bundle
bundleTmp, _ := os.MkdirTemp("", "bundle-temp-*")
defer os.RemoveAll(bundleTmp)

bundleTmp := t.TempDir()
b, err := types.NewBundle(tempDirPath, bundleTmp)
if err != nil {
t.Fatalf("while creating bundle: %v", err)
Expand Down
15 changes: 4 additions & 11 deletions internal/pkg/cgroups/manager_linux_v1_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2018-2025, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -118,20 +118,13 @@ func testNewUpdateV1(t *testing.T, systemd bool) {

// Write a new config with [pids] limit = 512
content := []byte("[pids]\nlimit = 512")
tmpfile, err := os.CreateTemp("", "cgroups")
if err != nil {
t.Fatalf("While creating update file: %v", err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(content); err != nil {
tmpfile := filepath.Join(t.TempDir(), "cgroups")
if err := os.WriteFile(tmpfile, content, 0o644); err != nil {
t.Fatalf("While writing update file: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("While closing update file: %v", err)
}

// Update existing cgroup from new config
if err := manager.UpdateFromFile(tmpfile.Name()); err != nil {
if err := manager.UpdateFromFile(tmpfile); err != nil {
t.Fatalf("While updating cgroup: %v", err)
}

Expand Down
15 changes: 4 additions & 11 deletions internal/pkg/cgroups/manager_linux_v2_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2025, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -109,20 +109,13 @@ func testNewUpdateV2(t *testing.T, systemd bool) {

// Write a new config with [pids] limit = 512
content := []byte("[pids]\nlimit = 512")
tmpfile, err := os.CreateTemp("", "cgroups")
if err != nil {
t.Fatalf("While creating update file: %v", err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write(content); err != nil {
tmpfile := filepath.Join(t.TempDir(), "cgroups")
if err := os.WriteFile(tmpfile, content, 0o644); err != nil {
t.Fatalf("While writing update file: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("While closing update file: %v", err)
}

// Update existing cgroup from new config
if err := manager.UpdateFromFile(tmpfile.Name()); err != nil {
if err := manager.UpdateFromFile(tmpfile); err != nil {
t.Fatalf("While updating cgroup: %v", err)
}

Expand Down
7 changes: 3 additions & 4 deletions internal/pkg/image/unpacker/squashfs_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2020, Control Command Inc. All rights reserved.
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2019-2025, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand All @@ -19,7 +19,7 @@ func createArchive(t *testing.T) *os.File {
if err != nil {
t.SkipNow()
}
f, err := os.CreateTemp("", "archive-")
f, err := os.CreateTemp(t.TempDir(), "archive-")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -56,15 +56,14 @@ func testSquashfs(t *testing.T, tmpParent string) {
t.Skip("unsquashfs not found")
}

dir, err := os.MkdirTemp(tmpParent, "test-squashfs-")
dir, err := os.MkdirTemp(tmpParent, "test-squashfs-") //nolint:usetesting
if err != nil {
t.Fatalf("while creating tmpdir: %v", err)
}
defer os.RemoveAll(dir)

// create archive with files present in this directory
archive := createArchive(t)
defer os.Remove(archive.Name())

savedPath := s.UnsquashfsPath

Expand Down
10 changes: 3 additions & 7 deletions internal/pkg/instance/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2019-2025, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand All @@ -8,6 +8,7 @@ package instance
import (
"bytes"
"os"
"path/filepath"
"testing"

"github.com/sylabs/singularity/v4/internal/pkg/test"
Expand Down Expand Up @@ -72,12 +73,7 @@ func TestLogger(t *testing.T) {
},
}

logfile, err := os.CreateTemp("", "log-")
if err != nil {
t.Errorf("failed to create temporary log file: %s", err)
}
filename := logfile.Name()
logfile.Close()
filename := filepath.Join(t.TempDir(), "log")

for _, f := range formatTest {
logger, err := NewLogger(filename, f.formatter)
Expand Down
13 changes: 2 additions & 11 deletions internal/pkg/runtime/launcher/oci/mounts_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022-2023, Sylabs Inc. All rights reserved.
// Copyright (c) 2022-2025, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -501,16 +501,7 @@ func ptsFlags(t *testing.T) []string {
}

func TestLauncher_addLibrariesMounts(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "add-libraries-mounts")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if !t.Failed() {
os.RemoveAll(tmpDir)
}
})

tmpDir := t.TempDir()
lib1 := filepath.Join(tmpDir, "lib1.so")
lib2 := filepath.Join(tmpDir, "lib2.so")
libInvalid := filepath.Join(tmpDir, "invalid")
Expand Down
7 changes: 2 additions & 5 deletions internal/pkg/security/seccomp/seccomp_supported_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2018-2025, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -41,13 +41,10 @@ func defaultProfile() *specs.LinuxSeccomp {
}

func testFchmod(t *testing.T) {
tmpfile, err := os.CreateTemp("", "chmod_file")
tmpfile, err := os.CreateTemp(t.TempDir(), "chmod_file-")
if err != nil {
t.Fatal(err)
}
file := tmpfile.Name()

defer os.Remove(file)
defer tmpfile.Close()

if hasConditionSupport() {
Expand Down
11 changes: 3 additions & 8 deletions internal/pkg/syecl/syecl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,13 @@ func TestAPutConfig(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tf, err := os.CreateTemp("", "eclconfig-test")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tf.Name())
tf.Close()
tf := filepath.Join(t.TempDir(), "ecl.toml")

if err := PutConfig(tt.c, tf.Name()); err != nil {
if err := PutConfig(tt.c, tf); err != nil {
t.Fatal(err)
}

b, err := os.ReadFile(tf.Name())
b, err := os.ReadFile(tf)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/test/privilege_linux.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2018-2025, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -50,7 +50,7 @@ func DropPrivilege(t *testing.T) {
t.Fatalf("failed to set user identity: %v", err)
}

if err := os.Setenv("HOME", unprivHome); err != nil {
if err := os.Setenv("HOME", unprivHome); err != nil { //nolint:usetesting
t.Fatalf("failed to set HOME environment variable: %v", err)
}
}
Expand All @@ -68,7 +68,7 @@ func ResetPrivilege(t *testing.T) {
// We might want restoration of HOME env var to persist past this individual
// test, so use os.Setenv() rather than t.Setenv()
//nolint:tenv
os.Setenv("HOME", origHome)
os.Setenv("HOME", origHome) //nolint:usetesting

runtime.UnlockOSThread()
}
Expand Down
Loading

0 comments on commit 2fcb7ac

Please sign in to comment.