1
0
mirror of https://github.com/golang/go synced 2024-11-22 03:54:39 -07:00

cmd/go/internal: use t.TempDir in tests

Change-Id: I8b4c19ed1085d2ffb07e2c8db33a10b6d70988eb
Reviewed-on: https://go-review.googlesource.com/c/go/+/611015
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Kir Kolyshkin 2024-09-04 14:51:44 -07:00 committed by Gopher Robot
parent f22d731976
commit 89958ab4ae
5 changed files with 14 additions and 69 deletions

View File

@ -20,12 +20,8 @@ func init() {
}
func TestBasic(t *testing.T) {
dir, err := os.MkdirTemp("", "cachetest-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
_, err = Open(filepath.Join(dir, "notexist"))
dir := t.TempDir()
_, err := Open(filepath.Join(dir, "notexist"))
if err == nil {
t.Fatal(`Open("tmp/notexist") succeeded, want failure`)
}
@ -65,13 +61,7 @@ func TestBasic(t *testing.T) {
}
func TestGrowth(t *testing.T) {
dir, err := os.MkdirTemp("", "cachetest-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
c, err := Open(dir)
c, err := Open(t.TempDir())
if err != nil {
t.Fatalf("Open: %v", err)
}
@ -118,13 +108,7 @@ func TestVerifyPanic(t *testing.T) {
t.Fatal("initEnv did not set verify")
}
dir, err := os.MkdirTemp("", "cachetest-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
c, err := Open(dir)
c, err := Open(t.TempDir())
if err != nil {
t.Fatalf("Open: %v", err)
}
@ -151,12 +135,7 @@ func dummyID(x int) [HashSize]byte {
}
func TestCacheTrim(t *testing.T) {
dir, err := os.MkdirTemp("", "cachetest-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()
c, err := Open(dir)
if err != nil {
t.Fatalf("Open: %v", err)

View File

@ -19,16 +19,6 @@ import (
"cmd/go/internal/lockedfile"
)
func mustTempDir(t *testing.T) (dir string, remove func()) {
t.Helper()
dir, err := os.MkdirTemp("", filepath.Base(t.Name()))
if err != nil {
t.Fatal(err)
}
return dir, func() { os.RemoveAll(dir) }
}
const (
quiescent = 10 * time.Millisecond
probablyStillBlocked = 10 * time.Second
@ -76,11 +66,7 @@ func mustBlock(t *testing.T, desc string, f func()) (wait func(*testing.T)) {
func TestMutexExcludes(t *testing.T) {
t.Parallel()
dir, remove := mustTempDir(t)
defer remove()
path := filepath.Join(dir, "lock")
path := filepath.Join(t.TempDir(), "lock")
mu := lockedfile.MutexAt(path)
t.Logf("mu := MutexAt(_)")
@ -112,11 +98,7 @@ func TestMutexExcludes(t *testing.T) {
func TestReadWaitsForLock(t *testing.T) {
t.Parallel()
dir, remove := mustTempDir(t)
defer remove()
path := filepath.Join(dir, "timestamp.txt")
path := filepath.Join(t.TempDir(), "timestamp.txt")
f, err := lockedfile.Create(path)
if err != nil {
t.Fatalf("Create: %v", err)
@ -163,10 +145,7 @@ func TestReadWaitsForLock(t *testing.T) {
func TestCanLockExistingFile(t *testing.T) {
t.Parallel()
dir, remove := mustTempDir(t)
defer remove()
path := filepath.Join(dir, "existing.txt")
path := filepath.Join(t.TempDir(), "existing.txt")
if err := os.WriteFile(path, []byte("ok"), 0777); err != nil {
t.Fatalf("os.WriteFile: %v", err)
}
@ -229,8 +208,7 @@ func TestSpuriousEDEADLK(t *testing.T) {
return
}
dir, remove := mustTempDir(t)
defer remove()
dir := t.TempDir()
// P.1 locks file A.
a, err := lockedfile.Edit(filepath.Join(dir, "A"))

View File

@ -36,9 +36,7 @@ func roundDownToPowerOf2(x int) int {
}
func TestTransform(t *testing.T) {
dir, remove := mustTempDir(t)
defer remove()
path := filepath.Join(dir, "blob.bin")
path := filepath.Join(t.TempDir(), "blob.bin")
const maxChunkWords = 8 << 10
buf := make([]byte, 2*maxChunkWords*8)

View File

@ -6,7 +6,6 @@ package modfetch
import (
"context"
"os"
"path/filepath"
"testing"
)
@ -14,13 +13,8 @@ import (
func TestWriteDiskCache(t *testing.T) {
ctx := context.Background()
tmpdir, err := os.MkdirTemp("", "go-writeCache-test-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
err = writeDiskCache(ctx, filepath.Join(tmpdir, "file"), []byte("data"))
tmpdir := t.TempDir()
err := writeDiskCache(ctx, filepath.Join(tmpdir, "file"), []byte("data"))
if err != nil {
t.Fatal(err)
}

View File

@ -230,17 +230,13 @@ func TestRespectSetgidDir(t *testing.T) {
return cmdBuf.WriteString(fmt.Sprint(a...))
})
setgiddir, err := os.MkdirTemp("", "SetGroupID")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(setgiddir)
setgiddir := t.TempDir()
// BSD mkdir(2) inherits the parent directory group, and other platforms
// can inherit the parent directory group via setgid. The test setup (chmod
// setgid) will fail if the process does not have the group permission to
// the new temporary directory.
err = os.Chown(setgiddir, os.Getuid(), os.Getgid())
err := os.Chown(setgiddir, os.Getuid(), os.Getgid())
if err != nil {
if testenv.SyscallIsNotSupported(err) {
t.Skip("skipping: chown is not supported on " + runtime.GOOS)