Skip to content

Commit 810f689

Browse files
committed
lint: usetesting - prefer test methods to handle temporary files
1 parent 6e7fc85 commit 810f689

File tree

4 files changed

+5
-24
lines changed

4 files changed

+5
-24
lines changed

.golangci.yml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ linters:
504504
constant-kind: true
505505

506506
usetesting:
507-
os-temp-dir: false
507+
os-temp-dir: true
508508
context-background: true
509509
context-todo: true
510510

@@ -627,16 +627,6 @@ linters:
627627
path: pkg/(appsec|acquisition|dumps|alertcontext|leakybucket|exprhelpers)
628628
text: 'rangeValCopy: .*'
629629

630-
- linters:
631-
- usetesting
632-
path: pkg/apiserver/(.+)_test.go
633-
text: os.MkdirTemp.* could be replaced by t.TempDir.*
634-
635-
- linters:
636-
- usetesting
637-
path: pkg/apiserver/(.+)_test.go
638-
text: os.CreateTemp.* could be replaced by os.CreateTemp.*
639-
640630
- linters:
641631
- containedctx
642632
path: cmd/notification-file/main.go

pkg/acquisition/modules/appsec/appsec_rules_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package appsecacquisition
33
import (
44
"net/http"
55
"net/url"
6-
"os"
76
"path/filepath"
87
"testing"
98

@@ -384,7 +383,7 @@ toto
384383
require.True(t, responses[0].InBandInterrupt)
385384

386385
// Might fail if you have artifacts from previous tests, but good enough 99% of the time
387-
tmpFiles, err := filepath.Glob(filepath.Join(os.TempDir(), "crzmp*"))
386+
tmpFiles, err := filepath.Glob(filepath.Join(t.TempDir(), "crzmp*"))
388387
require.NoError(t, err)
389388
require.Empty(t, tmpFiles)
390389
},

pkg/apiserver/apic_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
func getDBClient(t *testing.T, ctx context.Context) *database.Client {
3939
t.Helper()
4040

41-
dbPath, err := os.CreateTemp("", "*sqlite")
41+
dbPath, err := os.CreateTemp(t.TempDir(), "*sqlite")
4242
require.NoError(t, err)
4343
dbClient, err := database.NewClient(ctx, &csconfig.DatabaseCfg{
4444
Type: "sqlite",

pkg/apiserver/apiserver_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,9 @@ func LoadTestConfig(t *testing.T) csconfig.Config {
5454
MaxAge: maxAge,
5555
}
5656

57-
tempDir, _ := os.MkdirTemp("", "crowdsec_tests")
58-
59-
t.Cleanup(func() { os.RemoveAll(tempDir) })
60-
6157
dbconfig := csconfig.DatabaseCfg{
6258
Type: "sqlite",
63-
DbPath: filepath.Join(tempDir, "ent"),
59+
DbPath: filepath.Join(t.TempDir(), "ent"),
6460
Flush: &flushConfig,
6561
}
6662
apiServerConfig := csconfig.LocalApiServerCfg{
@@ -104,13 +100,9 @@ func LoadTestConfigForwardedFor(t *testing.T) csconfig.Config {
104100
MaxAge: maxAge,
105101
}
106102

107-
tempDir, _ := os.MkdirTemp("", "crowdsec_tests")
108-
109-
t.Cleanup(func() { os.RemoveAll(tempDir) })
110-
111103
dbconfig := csconfig.DatabaseCfg{
112104
Type: "sqlite",
113-
DbPath: filepath.Join(tempDir, "ent"),
105+
DbPath: filepath.Join(t.TempDir(), "ent"),
114106
Flush: &flushConfig,
115107
}
116108
apiServerConfig := csconfig.LocalApiServerCfg{

0 commit comments

Comments
 (0)