Skip to content

Commit 7a98035

Browse files
committed
test: t.TempDir instead of CreateTemp, MkdirTemp
1 parent a66951f commit 7a98035

File tree

2 files changed

+25
-88
lines changed

2 files changed

+25
-88
lines changed

sumdb/dirhash/hash_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ func TestHash1(t *testing.T) {
4646
}
4747

4848
func TestHashDir(t *testing.T) {
49-
dir, err := os.MkdirTemp("", "dirhash-test-")
50-
if err != nil {
51-
t.Fatal(err)
52-
}
53-
defer os.RemoveAll(dir)
49+
dir := t.TempDir()
5450
if err := os.WriteFile(filepath.Join(dir, "xyz"), []byte("data for xyz"), 0666); err != nil {
5551
t.Fatal(err)
5652
}
@@ -68,11 +64,10 @@ func TestHashDir(t *testing.T) {
6864
}
6965

7066
func TestHashZip(t *testing.T) {
71-
f, err := os.CreateTemp("", "dirhash-test-")
67+
f, err := os.CreateTemp(t.TempDir(), "dirhash-test-")
7268
if err != nil {
7369
t.Fatal(err)
7470
}
75-
defer os.Remove(f.Name())
7671
defer f.Close()
7772

7873
z := zip.NewWriter(f)
@@ -105,11 +100,7 @@ func TestHashZip(t *testing.T) {
105100

106101
func TestDirFiles(t *testing.T) {
107102
t.Run("valid directory with files", func(t *testing.T) {
108-
dir, err := os.MkdirTemp("", "dirfiles-test-")
109-
if err != nil {
110-
t.Fatal(err)
111-
}
112-
defer os.RemoveAll(dir)
103+
dir := t.TempDir()
113104
if err := os.WriteFile(filepath.Join(dir, "xyz"), []byte("data for xyz"), 0666); err != nil {
114105
t.Fatal(err)
115106
}

zip/zip_test.go

Lines changed: 22 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -419,15 +419,12 @@ func TestCreate(t *testing.T) {
419419
}
420420

421421
// Write zip to temporary file.
422-
tmpZip, err := os.CreateTemp("", "TestCreate-*.zip")
422+
tmpZip, err := os.CreateTemp(t.TempDir(), "TestCreate-*.zip")
423423
if err != nil {
424424
t.Fatal(err)
425425
}
426426
tmpZipPath := tmpZip.Name()
427-
defer func() {
428-
tmpZip.Close()
429-
os.Remove(tmpZipPath)
430-
}()
427+
defer tmpZip.Close()
431428
m := module.Version{Path: test.path, Version: test.version}
432429
files := make([]modzip.File, len(test.archive.Files))
433430
for i, tf := range test.archive.Files {
@@ -491,15 +488,12 @@ func TestCreateFromDir(t *testing.T) {
491488
}
492489

493490
// Create zip from the directory.
494-
tmpZip, err := os.CreateTemp("", "TestCreateFromDir-*.zip")
491+
tmpZip, err := os.CreateTemp(t.TempDir(), "TestCreateFromDir-*.zip")
495492
if err != nil {
496493
t.Fatal(err)
497494
}
498495
tmpZipPath := tmpZip.Name()
499-
defer func() {
500-
tmpZip.Close()
501-
os.Remove(tmpZipPath)
502-
}()
496+
defer tmpZip.Close()
503497
m := module.Version{Path: test.path, Version: test.version}
504498
if err := modzip.CreateFromDir(tmpZip, m, tmpDir); err != nil {
505499
if test.wantErr == "" {
@@ -569,22 +563,14 @@ func TestCreateFromDirSpecial(t *testing.T) {
569563
},
570564
} {
571565
t.Run(test.desc, func(t *testing.T) {
572-
tmpDir, err := os.MkdirTemp("", "TestCreateFromDirSpecial-"+test.desc)
573-
if err != nil {
574-
t.Fatal(err)
575-
}
576-
defer os.RemoveAll(tmpDir)
577-
dir := test.setup(t, tmpDir)
566+
dir := test.setup(t, t.TempDir())
578567

579-
tmpZipFile, err := os.CreateTemp("", "TestCreateFromDir-*.zip")
568+
tmpZipFile, err := os.CreateTemp(t.TempDir(), "TestCreateFromDir-*.zip")
580569
if err != nil {
581570
t.Fatal(err)
582571
}
583572
tmpZipPath := tmpZipFile.Name()
584-
defer func() {
585-
tmpZipFile.Close()
586-
os.Remove(tmpZipPath)
587-
}()
573+
defer tmpZipFile.Close()
588574

589575
m := module.Version{Path: "example.com/m", Version: "v1.0.0"}
590576
if err := modzip.CreateFromDir(tmpZipFile, m, dir); err != nil {
@@ -629,11 +615,7 @@ func TestUnzip(t *testing.T) {
629615
}
630616

631617
// Extract to a temporary directory.
632-
tmpDir, err := os.MkdirTemp("", "TestUnzip")
633-
if err != nil {
634-
t.Fatal(err)
635-
}
636-
defer os.RemoveAll(tmpDir)
618+
tmpDir := t.TempDir()
637619
m := module.Version{Path: test.path, Version: test.version}
638620
if err := modzip.Unzip(tmpDir, m, tmpZipPath); err != nil {
639621
if test.wantErr == "" {
@@ -808,17 +790,12 @@ func TestUnzipSizeLimits(t *testing.T) {
808790
test := test
809791
t.Run(test.desc, func(t *testing.T) {
810792
t.Parallel()
811-
tmpZipFile, err := os.CreateTemp("", "TestUnzipSizeLimits-*.zip")
793+
tmpZipFile, err := os.CreateTemp(t.TempDir(), "TestUnzipSizeLimits-*.zip")
812794
if err != nil {
813795
t.Fatal(err)
814796
}
815797
tmpZipPath := tmpZipFile.Name()
816-
defer func() {
817-
tmpZipFile.Close()
818-
if err := os.Remove(tmpZipPath); err != nil {
819-
t.Errorf("removing temp zip file: %v", err)
820-
}
821-
}()
798+
defer tmpZipFile.Close()
822799

823800
zw := zip.NewWriter(tmpZipFile)
824801
prefix := fmt.Sprintf("%s@%s/", sizeLimitVersion.Path, sizeLimitVersion.Version)
@@ -844,16 +821,6 @@ func TestUnzipSizeLimits(t *testing.T) {
844821
t.Fatal(err)
845822
}
846823

847-
tmpDir, err := os.MkdirTemp("", "TestUnzipSizeLimits")
848-
if err != nil {
849-
t.Fatal(err)
850-
}
851-
defer func() {
852-
if err := os.RemoveAll(tmpDir); err != nil {
853-
t.Errorf("removing temp dir: %v", err)
854-
}
855-
}()
856-
857824
wantCheckZipErr := test.wantCheckZipErr
858825
if wantCheckZipErr == "" {
859826
wantCheckZipErr = test.wantErr
@@ -874,7 +841,7 @@ func TestUnzipSizeLimits(t *testing.T) {
874841
if wantUnzipErr == "" {
875842
wantUnzipErr = test.wantErr
876843
}
877-
if err := modzip.Unzip(tmpDir, sizeLimitVersion, tmpZipPath); err == nil && wantUnzipErr != "" {
844+
if err := modzip.Unzip(t.TempDir(), sizeLimitVersion, tmpZipPath); err == nil && wantUnzipErr != "" {
878845
t.Fatalf("Unzip: unexpected success; want error containing %q", wantUnzipErr)
879846
} else if err != nil && wantUnzipErr == "" {
880847
t.Fatalf("Unzip: got error %q; want success", err)
@@ -974,27 +941,18 @@ func TestUnzipSizeLimitsSpecial(t *testing.T) {
974941
test := test
975942
t.Run(test.desc, func(t *testing.T) {
976943
t.Parallel()
977-
tmpZipFile, err := os.CreateTemp("", "TestUnzipSizeLimitsSpecial-*.zip")
944+
tmpZipFile, err := os.CreateTemp(t.TempDir(), "TestUnzipSizeLimitsSpecial-*.zip")
978945
if err != nil {
979946
t.Fatal(err)
980947
}
981948
tmpZipPath := tmpZipFile.Name()
982-
defer func() {
983-
tmpZipFile.Close()
984-
os.Remove(tmpZipPath)
985-
}()
949+
defer tmpZipFile.Close()
986950

987951
test.writeZip(t, tmpZipFile)
988952
if err := tmpZipFile.Close(); err != nil {
989953
t.Fatal(err)
990954
}
991955

992-
tmpDir, err := os.MkdirTemp("", "TestUnzipSizeLimitsSpecial")
993-
if err != nil {
994-
t.Fatal(err)
995-
}
996-
defer os.RemoveAll(tmpDir)
997-
998956
want := func() string {
999957
s := fmt.Sprintf("%q", test.wantErr1)
1000958
if test.wantErr2 != "" {
@@ -1003,7 +961,7 @@ func TestUnzipSizeLimitsSpecial(t *testing.T) {
1003961
return s
1004962
}
1005963

1006-
if err := modzip.Unzip(tmpDir, test.m, tmpZipPath); err == nil && test.wantErr1 != "" {
964+
if err := modzip.Unzip(t.TempDir(), test.m, tmpZipPath); err == nil && test.wantErr1 != "" {
1007965
t.Fatalf("unexpected success; want error containing %s", want())
1008966
} else if err != nil && test.wantErr1 == "" {
1009967
t.Fatalf("got error %q; want success", err)
@@ -1327,15 +1285,12 @@ func TestVCS(t *testing.T) {
13271285
})
13281286
}
13291287

1330-
tmpModZipFile, err := os.CreateTemp("", "TestVCS-*.zip")
1288+
tmpModZipFile, err := os.CreateTemp(t.TempDir(), "TestVCS-*.zip")
13311289
if err != nil {
13321290
t.Fatal(err)
13331291
}
13341292
tmpModZipPath := tmpModZipFile.Name()
1335-
defer func() {
1336-
tmpModZipFile.Close()
1337-
os.Remove(tmpModZipPath)
1338-
}()
1293+
defer tmpModZipFile.Close()
13391294
h := sha256.New()
13401295
w := io.MultiWriter(tmpModZipFile, h)
13411296
if err := modzip.Create(w, test.m, files); err != nil {
@@ -1400,7 +1355,7 @@ func downloadVCSZip(t testing.TB, vcs, url, rev, subdir string) (repoDir string,
14001355
}
14011356

14021357
// Create an archive.
1403-
tmpZipFile, err := os.CreateTemp("", "downloadVCSZip-*.zip")
1358+
tmpZipFile, err := os.CreateTemp(t.TempDir(), "downloadVCSZip-*.zip")
14041359
if err != nil {
14051360
return "", nil, err
14061361
}
@@ -1440,13 +1395,12 @@ func downloadVCSZip(t testing.TB, vcs, url, rev, subdir string) (repoDir string,
14401395
}
14411396

14421397
// Create an archive.
1443-
tmpZipFile, err := os.CreateTemp("", "downloadVCSZip-*.zip")
1398+
tmpZipFile, err := os.CreateTemp(t.TempDir(), "downloadVCSZip-*.zip")
14441399
if err != nil {
14451400
return "", nil, err
14461401
}
14471402
tmpZipPath := tmpZipFile.Name()
14481403
tmpZipFile.Close()
1449-
t.Cleanup(func() { os.Remove(tmpZipPath) })
14501404
args := []string{"archive", "-t", "zip", "--no-decode", "-r", rev, "--prefix=prefix/"}
14511405
if subdir != "" {
14521406
args = append(args, "-I", subdir+"/**")
@@ -1700,15 +1654,11 @@ var A = 5
17001654
}
17011655

17021656
// Create zip from the directory.
1703-
tmpZip, err := os.CreateTemp("", "TestCreateFromDir-*.zip")
1657+
tmpZip, err := os.CreateTemp(t.TempDir(), "TestCreateFromDir-*.zip")
17041658
if err != nil {
17051659
t.Fatal(err)
17061660
}
1707-
tmpZipPath := tmpZip.Name()
1708-
defer func() {
1709-
tmpZip.Close()
1710-
os.Remove(tmpZipPath)
1711-
}()
1661+
defer tmpZip.Close()
17121662

17131663
m := module.Version{Path: "example.com/foo/bar", Version: "v0.0.1"}
17141664

@@ -1744,15 +1694,11 @@ var A = 5
17441694
gitInit(t, tmpDir)
17451695

17461696
// Create zip from the directory.
1747-
tmpZip, err := os.CreateTemp("", "TestCreateFromDir-*.zip")
1697+
tmpZip, err := os.CreateTemp(t.TempDir(), "TestCreateFromDir-*.zip")
17481698
if err != nil {
17491699
t.Fatal(err)
17501700
}
1751-
tmpZipPath := tmpZip.Name()
1752-
defer func() {
1753-
tmpZip.Close()
1754-
os.Remove(tmpZipPath)
1755-
}()
1701+
defer tmpZip.Close()
17561702

17571703
m := module.Version{Path: "example.com/foo/bar", Version: "v0.0.1"}
17581704

0 commit comments

Comments
 (0)