build: replace tenv linter with usetesting (#31172)

This commit is contained in:
levisyin 2025-02-21 20:36:18 +08:00 committed by GitHub
parent cb9653d253
commit d103f179b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 20 additions and 43 deletions

View file

@ -25,7 +25,7 @@ linters:
- gocheckcompilerdirectives - gocheckcompilerdirectives
- reassign - reassign
- mirror - mirror
- tenv - usetesting
### linters we tried and will not be using: ### linters we tried and will not be using:
### ###
# - structcheck # lots of false positives # - structcheck # lots of false positives

View file

@ -16,7 +16,7 @@ archives are published at https://geth.ethereum.org/downloads/.
For prerequisites and detailed build instructions please read the [Installation Instructions](https://geth.ethereum.org/docs/getting-started/installing-geth). For prerequisites and detailed build instructions please read the [Installation Instructions](https://geth.ethereum.org/docs/getting-started/installing-geth).
Building `geth` requires both a Go (version 1.22 or later) and a C compiler. You can install Building `geth` requires both a Go (version 1.23 or later) and a C compiler. You can install
them using your favourite package manager. Once the dependencies are installed, run them using your favourite package manager. Once the dependencies are installed, run
```shell ```shell

View file

@ -57,13 +57,7 @@ func TestMain(m *testing.M) {
// This method creates a temporary keystore folder which will be removed after // This method creates a temporary keystore folder which will be removed after
// the test exits. // the test exits.
func runClef(t *testing.T, args ...string) *testproc { func runClef(t *testing.T, args ...string) *testproc {
ddir, err := os.MkdirTemp("", "cleftest-*") ddir := t.TempDir()
if err != nil {
return nil
}
t.Cleanup(func() {
os.RemoveAll(ddir)
})
return runWithKeystore(t, ddir, args...) return runWithKeystore(t, ddir, args...)
} }

View file

@ -87,11 +87,7 @@ func TestHistoryImportAndExport(t *testing.T) {
} }
// Make temp directory for era files. // Make temp directory for era files.
dir, err := os.MkdirTemp("", "history-export-test") dir := t.TempDir()
if err != nil {
t.Fatalf("error creating temp test directory: %v", err)
}
defer os.RemoveAll(dir)
// Export history to temp directory. // Export history to temp directory.
if err := ExportHistory(chain, dir, 0, count, step); err != nil { if err := ExportHistory(chain, dir, 0, count, step); err != nil {

View file

@ -21,6 +21,7 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/sha256" "crypto/sha256"
"errors" "errors"
"fmt"
"math" "math"
"math/big" "math/big"
"os" "os"
@ -452,8 +453,7 @@ func TestOpenDrops(t *testing.T) {
//log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true))) //log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true)))
// Create a temporary folder for the persistent backend // Create a temporary folder for the persistent backend
storage, _ := os.MkdirTemp("", "blobpool-") storage := t.TempDir()
defer os.RemoveAll(storage)
os.MkdirAll(filepath.Join(storage, pendingTransactionStore), 0700) os.MkdirAll(filepath.Join(storage, pendingTransactionStore), 0700)
store, _ := billy.Open(billy.Options{Path: filepath.Join(storage, pendingTransactionStore)}, newSlotter(testMaxBlobsPerBlock), nil) store, _ := billy.Open(billy.Options{Path: filepath.Join(storage, pendingTransactionStore)}, newSlotter(testMaxBlobsPerBlock), nil)
@ -775,8 +775,7 @@ func TestOpenIndex(t *testing.T) {
//log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true))) //log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true)))
// Create a temporary folder for the persistent backend // Create a temporary folder for the persistent backend
storage, _ := os.MkdirTemp("", "blobpool-") storage := t.TempDir()
defer os.RemoveAll(storage)
os.MkdirAll(filepath.Join(storage, pendingTransactionStore), 0700) os.MkdirAll(filepath.Join(storage, pendingTransactionStore), 0700)
store, _ := billy.Open(billy.Options{Path: filepath.Join(storage, pendingTransactionStore)}, newSlotter(testMaxBlobsPerBlock), nil) store, _ := billy.Open(billy.Options{Path: filepath.Join(storage, pendingTransactionStore)}, newSlotter(testMaxBlobsPerBlock), nil)
@ -864,8 +863,7 @@ func TestOpenHeap(t *testing.T) {
//log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true))) //log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true)))
// Create a temporary folder for the persistent backend // Create a temporary folder for the persistent backend
storage, _ := os.MkdirTemp("", "blobpool-") storage := t.TempDir()
defer os.RemoveAll(storage)
os.MkdirAll(filepath.Join(storage, pendingTransactionStore), 0700) os.MkdirAll(filepath.Join(storage, pendingTransactionStore), 0700)
store, _ := billy.Open(billy.Options{Path: filepath.Join(storage, pendingTransactionStore)}, newSlotter(testMaxBlobsPerBlock), nil) store, _ := billy.Open(billy.Options{Path: filepath.Join(storage, pendingTransactionStore)}, newSlotter(testMaxBlobsPerBlock), nil)
@ -951,8 +949,7 @@ func TestOpenCap(t *testing.T) {
//log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true))) //log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true)))
// Create a temporary folder for the persistent backend // Create a temporary folder for the persistent backend
storage, _ := os.MkdirTemp("", "blobpool-") storage := t.TempDir()
defer os.RemoveAll(storage)
os.MkdirAll(filepath.Join(storage, pendingTransactionStore), 0700) os.MkdirAll(filepath.Join(storage, pendingTransactionStore), 0700)
store, _ := billy.Open(billy.Options{Path: filepath.Join(storage, pendingTransactionStore)}, newSlotter(testMaxBlobsPerBlock), nil) store, _ := billy.Open(billy.Options{Path: filepath.Join(storage, pendingTransactionStore)}, newSlotter(testMaxBlobsPerBlock), nil)
@ -1041,8 +1038,7 @@ func TestChangingSlotterSize(t *testing.T) {
//log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true))) //log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true)))
// Create a temporary folder for the persistent backend // Create a temporary folder for the persistent backend
storage, _ := os.MkdirTemp("", "blobpool-") storage := t.TempDir()
defer os.RemoveAll(storage)
os.MkdirAll(filepath.Join(storage, pendingTransactionStore), 0700) os.MkdirAll(filepath.Join(storage, pendingTransactionStore), 0700)
store, _ := billy.Open(billy.Options{Path: filepath.Join(storage, pendingTransactionStore)}, newSlotter(6), nil) store, _ := billy.Open(billy.Options{Path: filepath.Join(storage, pendingTransactionStore)}, newSlotter(6), nil)
@ -1508,8 +1504,7 @@ func TestAdd(t *testing.T) {
} }
for i, tt := range tests { for i, tt := range tests {
// Create a temporary folder for the persistent backend // Create a temporary folder for the persistent backend
storage, _ := os.MkdirTemp("", "blobpool-") storage := filepath.Join(t.TempDir(), fmt.Sprintf("test-%d", i))
defer os.RemoveAll(storage) // late defer, still ok
os.MkdirAll(filepath.Join(storage, pendingTransactionStore), 0700) os.MkdirAll(filepath.Join(storage, pendingTransactionStore), 0700)
store, _ := billy.Open(billy.Options{Path: filepath.Join(storage, pendingTransactionStore)}, newSlotter(testMaxBlobsPerBlock), nil) store, _ := billy.Open(billy.Options{Path: filepath.Join(storage, pendingTransactionStore)}, newSlotter(testMaxBlobsPerBlock), nil)

View file

@ -181,7 +181,7 @@ func TestLoadECDSA(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
f, err := os.CreateTemp("", "loadecdsa_test.*.txt") f, err := os.CreateTemp(t.TempDir(), "loadecdsa_test.*.txt")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -202,7 +202,7 @@ func TestLoadECDSA(t *testing.T) {
} }
func TestSaveECDSA(t *testing.T) { func TestSaveECDSA(t *testing.T) {
f, err := os.CreateTemp("", "saveecdsa_test.*.txt") f, err := os.CreateTemp(t.TempDir(), "saveecdsa_test.*.txt")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View file

@ -33,11 +33,10 @@ var (
) )
func TestSignify(t *testing.T) { func TestSignify(t *testing.T) {
tmpFile, err := os.CreateTemp("", "") tmpFile, err := os.CreateTemp(t.TempDir(), "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.Remove(tmpFile.Name())
defer tmpFile.Close() defer tmpFile.Close()
data := make([]byte, 1024) data := make([]byte, 1024)
@ -52,7 +51,6 @@ func TestSignify(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.Remove(tmpFile.Name() + ".sig")
// Verify the signature using a golang library // Verify the signature using a golang library
sig, err := minisign.NewSignatureFromFile(tmpFile.Name() + ".sig") sig, err := minisign.NewSignatureFromFile(tmpFile.Name() + ".sig")
@ -75,11 +73,10 @@ func TestSignify(t *testing.T) {
} }
func TestSignifyTrustedCommentTooManyLines(t *testing.T) { func TestSignifyTrustedCommentTooManyLines(t *testing.T) {
tmpFile, err := os.CreateTemp("", "") tmpFile, err := os.CreateTemp(t.TempDir(), "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.Remove(tmpFile.Name())
defer tmpFile.Close() defer tmpFile.Close()
data := make([]byte, 1024) data := make([]byte, 1024)
@ -94,15 +91,13 @@ func TestSignifyTrustedCommentTooManyLines(t *testing.T) {
if err == nil || err.Error() == "" { if err == nil || err.Error() == "" {
t.Fatalf("should have errored on a multi-line trusted comment, got %v", err) t.Fatalf("should have errored on a multi-line trusted comment, got %v", err)
} }
defer os.Remove(tmpFile.Name() + ".sig")
} }
func TestSignifyTrustedCommentTooManyLinesLF(t *testing.T) { func TestSignifyTrustedCommentTooManyLinesLF(t *testing.T) {
tmpFile, err := os.CreateTemp("", "") tmpFile, err := os.CreateTemp(t.TempDir(), "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.Remove(tmpFile.Name())
defer tmpFile.Close() defer tmpFile.Close()
data := make([]byte, 1024) data := make([]byte, 1024)
@ -117,15 +112,13 @@ func TestSignifyTrustedCommentTooManyLinesLF(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.Remove(tmpFile.Name() + ".sig")
} }
func TestSignifyTrustedCommentEmpty(t *testing.T) { func TestSignifyTrustedCommentEmpty(t *testing.T) {
tmpFile, err := os.CreateTemp("", "") tmpFile, err := os.CreateTemp(t.TempDir(), "")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.Remove(tmpFile.Name())
defer tmpFile.Close() defer tmpFile.Close()
data := make([]byte, 1024) data := make([]byte, 1024)
@ -140,5 +133,4 @@ func TestSignifyTrustedCommentEmpty(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.Remove(tmpFile.Name() + ".sig")
} }

View file

@ -37,7 +37,7 @@ func TestEra1Builder(t *testing.T) {
t.Parallel() t.Parallel()
// Get temp directory. // Get temp directory.
f, err := os.CreateTemp("", "era1-test") f, err := os.CreateTemp(t.TempDir(), "era1-test")
if err != nil { if err != nil {
t.Fatalf("error creating temp file: %v", err) t.Fatalf("error creating temp file: %v", err)
} }
@ -78,6 +78,7 @@ func TestEra1Builder(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("failed to open era: %v", err) t.Fatalf("failed to open era: %v", err)
} }
defer e.Close()
it, err := NewRawIterator(e) it, err := NewRawIterator(e)
if err != nil { if err != nil {
t.Fatalf("failed to make iterator: %s", err) t.Fatalf("failed to make iterator: %s", err)

View file

@ -53,13 +53,12 @@ func TestDatadirCreation(t *testing.T) {
t.Fatalf("freshly created datadir not accessible: %v", err) t.Fatalf("freshly created datadir not accessible: %v", err)
} }
// Verify that an impossible datadir fails creation // Verify that an impossible datadir fails creation
file, err := os.CreateTemp("", "") file, err := os.CreateTemp(t.TempDir(), "")
if err != nil { if err != nil {
t.Fatalf("failed to create temporary file: %v", err) t.Fatalf("failed to create temporary file: %v", err)
} }
defer func() { defer func() {
file.Close() file.Close()
os.Remove(file.Name())
}() }()
dir = filepath.Join(file.Name(), "invalid/path") dir = filepath.Join(file.Name(), "invalid/path")