-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89f0700
commit e9fc27f
Showing
2 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2014 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package testing_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestTempDirInCleanup(t *testing.T) { | ||
var dir string | ||
|
||
t.Run("test", func(t *testing.T) { | ||
t.Cleanup(func() { | ||
dir = t.TempDir() | ||
}) | ||
_ = t.TempDir() | ||
}) | ||
|
||
fi, err := os.Stat(dir) | ||
if fi != nil { | ||
t.Fatalf("Directory %q from user Cleanup still exists", dir) | ||
} | ||
if !os.IsNotExist(err) { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
} | ||
|
||
func TestTempDirInBenchmark(t *testing.T) { | ||
testing.Benchmark(func(b *testing.B) { | ||
if !b.Run("test", func(b *testing.B) { | ||
// Add a loop so that the test won't fail. See issue 38677. | ||
for i := 0; i < b.N; i++ { | ||
_ = b.TempDir() | ||
} | ||
}) { | ||
t.Fatal("Sub test failure in a benchmark") | ||
} | ||
}) | ||
} | ||
|
||
func TestTempDir(t *testing.T) { | ||
testTempDir(t) | ||
t.Run("InSubtest", testTempDir) | ||
t.Run("test/subtest", testTempDir) | ||
t.Run("test\\subtest", testTempDir) | ||
t.Run("test:subtest", testTempDir) | ||
t.Run("test/..", testTempDir) | ||
t.Run("../test", testTempDir) | ||
t.Run("test[]", testTempDir) | ||
t.Run("test*", testTempDir) | ||
t.Run("äöüéè", testTempDir) | ||
} | ||
|
||
func testTempDir(t *testing.T) { | ||
dirCh := make(chan string, 1) | ||
t.Cleanup(func() { | ||
// Verify directory has been removed. | ||
select { | ||
case dir := <-dirCh: | ||
fi, err := os.Stat(dir) | ||
if os.IsNotExist(err) { | ||
// All good | ||
return | ||
} | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Errorf("directory %q still exists: %v, isDir=%v", dir, fi, fi.IsDir()) | ||
default: | ||
if !t.Failed() { | ||
t.Fatal("never received dir channel") | ||
} | ||
} | ||
}) | ||
|
||
dir := t.TempDir() | ||
if dir == "" { | ||
t.Fatal("expected dir") | ||
} | ||
dir2 := t.TempDir() | ||
if dir == dir2 { | ||
t.Fatal("subsequent calls to TempDir returned the same directory") | ||
} | ||
if filepath.Dir(dir) != filepath.Dir(dir2) { | ||
t.Fatalf("calls to TempDir do not share a parent; got %q, %q", dir, dir2) | ||
} | ||
dirCh <- dir | ||
fi, err := os.Stat(dir) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !fi.IsDir() { | ||
t.Errorf("dir %q is not a dir", dir) | ||
} | ||
|
||
glob := filepath.Join(dir, "*.txt") | ||
if _, err := filepath.Glob(glob); err != nil { | ||
t.Error(err) | ||
} | ||
|
||
err = os.Remove(dir) | ||
if err != nil { | ||
t.Errorf("unexpected files in TempDir") | ||
} | ||
} |