Skip to content

Commit fd8d5f9

Browse files
committed
internal/mod/zip: use testing.T.TempDir
Note that we were leaving files behind on Windows, since assertNoExcludedFiles left the temporary zip file open and on Windows one can't remove a file without closing it first. I started noticing since testing.T.TempDir catches os.RemoveAll errors. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Ie303ff50997b31129fc73b5fdf006e5ac30e1974 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1170019 Reviewed-by: Roger Peppe <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent b004dd6 commit fd8d5f9

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

internal/mod/zip/zip_test.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func TestCreate(t *testing.T) {
401401
t.Logf("test file: %s", testPath)
402402

403403
// Write zip to temporary file.
404-
tmpZipFile := tempFile(t, "TestCreate-*.zip")
404+
tmpZipFile := tempFile(t, "tmp.zip")
405405
m := module.MustNewVersion(test.path, test.version)
406406
files := make([]fakeFile, len(test.archive.Files))
407407
for i, tf := range test.archive.Files {
@@ -442,6 +442,7 @@ func assertNoExcludedFiles(t *testing.T, zf string) {
442442
if err != nil {
443443
t.Fatal(err)
444444
}
445+
defer z.Close()
445446
for _, f := range z.File {
446447
if shouldExclude(f) {
447448
t.Errorf("file %s should have been excluded but was not", f.Name)
@@ -493,7 +494,7 @@ func TestCreateFromDir(t *testing.T) {
493494
}
494495

495496
// Create zip from the directory.
496-
tmpZipFile := tempFile(t, "TestCreateFromDir-*.zip")
497+
tmpZipFile := tempFile(t, "tmp.zip")
497498
m := module.MustNewVersion(test.path, test.version)
498499
if err := modzip.CreateFromDir(tmpZipFile, m, tmpDir); err != nil {
499500
if test.wantErr == "" {
@@ -576,7 +577,7 @@ func TestCreateFromDirSpecial(t *testing.T) {
576577
tmpDir := t.TempDir()
577578
dir := test.setup(t, tmpDir)
578579

579-
tmpZipFile := tempFile(t, "TestCreateFromDir-*.zip")
580+
tmpZipFile := tempFile(t, "tmp.zip")
580581
m := module.MustNewVersion("example.com/m@v1", "v1.0.0")
581582

582583
if err := modzip.CreateFromDir(tmpZipFile, m, dir); err != nil {
@@ -789,7 +790,7 @@ func TestUnzipSizeLimits(t *testing.T) {
789790
test := test
790791
t.Run(test.desc, func(t *testing.T) {
791792
t.Parallel()
792-
tmpZipFile := tempFile(t, "TestUnzipSizeLimits-*.zip")
793+
tmpZipFile := tempFile(t, "tmp.zip")
793794

794795
zw := zip.NewWriter(tmpZipFile)
795796
for _, tf := range test.files {
@@ -935,7 +936,7 @@ func TestUnzipSizeLimitsSpecial(t *testing.T) {
935936
t.Run(test.desc, func(t *testing.T) {
936937
t.Parallel()
937938

938-
tmpZipFile := tempFile(t, "TestUnzipSizeLimits-*.zip")
939+
tmpZipFile := tempFile(t, "tmp.zip")
939940
test.writeZip(t, tmpZipFile)
940941
if err := tmpZipFile.Close(); err != nil {
941942
t.Fatal(err)
@@ -963,14 +964,11 @@ func mustWriteFile(name string, content string) {
963964
}
964965
}
965966

966-
func tempFile(t *testing.T, tmpl string) *os.File {
967-
f, err := os.CreateTemp("", tmpl)
967+
func tempFile(t *testing.T, name string) *os.File {
968+
f, err := os.Create(filepath.Join(t.TempDir(), name))
968969
if err != nil {
969970
t.Fatal(err)
970971
}
971-
t.Cleanup(func() {
972-
f.Close()
973-
os.Remove(f.Name())
974-
})
972+
t.Cleanup(func() { f.Close() })
975973
return f
976974
}

0 commit comments

Comments
 (0)