Skip to content

Commit

Permalink
cmd/go/internal: use t.TempDir in tests
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
kolyshkin authored and gopherbot committed Sep 5, 2024
1 parent f22d731 commit 89958ab
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 69 deletions.
31 changes: 5 additions & 26 deletions src/cmd/go/internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
30 changes: 4 additions & 26 deletions src/cmd/go/internal/lockedfile/lockedfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(_)")

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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"))
Expand Down
4 changes: 1 addition & 3 deletions src/cmd/go/internal/lockedfile/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 2 additions & 8 deletions src/cmd/go/internal/modfetch/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@ package modfetch

import (
"context"
"os"
"path/filepath"
"testing"
)

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)
}
Expand Down
8 changes: 2 additions & 6 deletions src/cmd/go/internal/work/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 89958ab

Please sign in to comment.