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 <ville@vesilehto.fi>
This commit is contained in:
Ville Vesilehto
2025-05-27 20:02:55 +03:00
committed by GitHub
parent 43ec317d0e
commit eeb1663847
4 changed files with 5 additions and 12 deletions

View File

@@ -8,6 +8,7 @@ linters:
- unconvert - unconvert
- unused - unused
- whitespace - whitespace
- usetesting
exclusions: exclusions:
generated: lax generated: lax
presets: presets:

View File

@@ -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 { if err != nil {
t.Error(err) t.Error(err)
} }

View File

@@ -1,7 +1,6 @@
package tls package tls
import ( import (
"os"
"path/filepath" "path/filepath"
"testing" "testing"
@@ -79,15 +78,8 @@ func TestNewTLSConfigFromArgs(t *testing.T) {
func TestNewTLSConfigFromArgsWithRoot(t *testing.T) { func TestNewTLSConfigFromArgsWithRoot(t *testing.T) {
cert, key, ca := getPEMFiles(t) cert, key, ca := getPEMFiles(t)
tempDir, err := os.MkdirTemp("", "go-test-pemfiles") tempDir := t.TempDir()
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)
}
root := tempDir root := tempDir
args := []string{cert, key, ca} args := []string{cert, key, ca}
for i := range args { for i := range args {

View File

@@ -23,7 +23,7 @@ func TestRoot(t *testing.T) {
nonExistingDir := filepath.Join(existingDirPath, "highly_unlikely_to_exist_dir") 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 { if err != nil {
t.Fatalf("BeforeTest: Failed to create temp file for testing! Error was: %v", err) t.Fatalf("BeforeTest: Failed to create temp file for testing! Error was: %v", err)
} }