mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
Fixes #35998 Change-Id: I87c6bf4e34e832be68862ca16ecfa6ea12048d31 Reviewed-on: https://go-review.googlesource.com/c/go/+/226877 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
// 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 (
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// This is exactly what a test would do without a TestMain.
|
|
// It's here only so that there is at least one package in the
|
|
// standard library with a TestMain, so that code is executed.
|
|
|
|
func TestMain(m *testing.M) {
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
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 stil 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("directory changed between calls")
|
|
}
|
|
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)
|
|
}
|
|
fis, err := ioutil.ReadDir(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(fis) > 0 {
|
|
t.Errorf("unexpected %d files in TempDir: %v", len(fis), fis)
|
|
}
|
|
}
|