Skip to content

Commit 385ac5a

Browse files
authored
refactor: use t.TempDir() instead of os.MkdirTemp (#5620)
Signed-off-by: laterlaugh <manziwenzhai@sina.cn>
1 parent c453f8b commit 385ac5a

File tree

2 files changed

+6
-20
lines changed

2 files changed

+6
-20
lines changed

ingest/ledgerbackend/captive_core_backend_test.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import (
66
"fmt"
77
"net/http"
88
"net/http/httptest"
9-
"os"
109
"sync"
1110
"testing"
1211

1312
"github.com/prometheus/client_golang/prometheus"
1413
dto "github.com/prometheus/client_model/go"
1514
"github.com/stretchr/testify/assert"
1615
"github.com/stretchr/testify/mock"
17-
"github.com/stretchr/testify/require"
1816

1917
"github.com/stellar/go/historyarchive"
2018
"github.com/stellar/go/network"
@@ -138,9 +136,7 @@ type testLedgerHeader struct {
138136
}
139137

140138
func TestCaptiveNew(t *testing.T) {
141-
storagePath, err := os.MkdirTemp("", "captive-core-*")
142-
require.NoError(t, err)
143-
defer os.RemoveAll(storagePath)
139+
storagePath := t.TempDir()
144140

145141
var userAgent string
146142
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -173,9 +169,7 @@ func TestCaptiveNew(t *testing.T) {
173169
}
174170

175171
func TestCaptiveNewUnsupportedProtocolVersion(t *testing.T) {
176-
storagePath, err := os.MkdirTemp("", "captive-core-*")
177-
require.NoError(t, err)
178-
defer os.RemoveAll(storagePath)
172+
storagePath := t.TempDir()
179173

180174
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
181175
w.WriteHeader(http.StatusOK)
@@ -186,7 +180,7 @@ func TestCaptiveNewUnsupportedProtocolVersion(t *testing.T) {
186180
networkPassphrase := network.PublicNetworkPassphrase
187181
historyURLs := []string{server.URL}
188182

189-
_, err = NewCaptive(
183+
_, err := NewCaptive(
190184
CaptiveCoreConfig{
191185
BinaryPath: executablePath,
192186
NetworkPassphrase: networkPassphrase,
@@ -1022,9 +1016,7 @@ func TestCaptiveGetLedger_NextLedger0RangeFromIsSmallerThanLedgerFromBuffer(t *t
10221016
}
10231017

10241018
func TestCaptiveStellarCore_PrepareRangeAfterClose(t *testing.T) {
1025-
storagePath, err := os.MkdirTemp("", "captive-core-*")
1026-
require.NoError(t, err)
1027-
defer os.RemoveAll(storagePath)
1019+
storagePath := t.TempDir()
10281020

10291021
ctx := context.Background()
10301022
executablePath := "/etc/stellar-core"

ingest/ledgerbackend/file_watcher_test.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ package ledgerbackend
33
import (
44
"context"
55
"fmt"
6-
"os"
76
"sync"
87
"testing"
98
"time"
109

1110
"github.com/stellar/go/support/log"
1211

1312
"github.com/stretchr/testify/assert"
14-
"github.com/stretchr/testify/require"
1513
)
1614

1715
type mockHash struct {
@@ -45,9 +43,7 @@ func (m *mockHash) hashFile(fp string) (hash, error) {
4543
}
4644

4745
func createFWFixtures(t *testing.T) (*mockHash, *stellarCoreRunner, *fileWatcher) {
48-
storagePath, err := os.MkdirTemp("", "captive-core-*")
49-
require.NoError(t, err)
50-
defer os.RemoveAll(storagePath)
46+
storagePath := t.TempDir()
5147

5248
ms := &mockHash{
5349
hashResult: hash{},
@@ -75,9 +71,7 @@ func createFWFixtures(t *testing.T) (*mockHash, *stellarCoreRunner, *fileWatcher
7571
}
7672

7773
func TestNewFileWatcherError(t *testing.T) {
78-
storagePath, err := os.MkdirTemp("", "captive-core-*")
79-
require.NoError(t, err)
80-
defer os.RemoveAll(storagePath)
74+
storagePath := t.TempDir()
8175

8276
ms := &mockHash{
8377
hashResult: hash{},

0 commit comments

Comments
 (0)