go: testing: test failes if leaving files without write permission in T.TempDir
What version of Go are you using (go version
)?
$ go version go version go1.15 darwin/amd64
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (go env
)?
go env
Output
$ go env GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/Users/tikuta/Library/Caches/go-build" GOENV="/Users/tikuta/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOINSECURE="" GOMODCACHE="/Users/tikuta/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/tikuta/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/Users/tikuta/homebrew/Cellar/go/1.15/libexec" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/Users/tikuta/homebrew/Cellar/go/1.15/libexec/pkg/tool/darwin_amd64" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/jz/z42xl9mx2d576zq1q21hlggc00bk9x/T/go-build114835528=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you do?
I wrote a test leaving a file without write permission.
package test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestNoDir(t *testing.T) {
dir := t.TempDir()
empty := filepath.Join(dir, "empty")
if err := os.MkdirAll(filepath.Dir(empty), 0300); err != nil {
t.Errorf("%v", err)
}
if err := ioutil.WriteFile(empty, nil, 0444); err != nil {
t.Errorf("%v", err)
}
}
func TestDir(t *testing.T) {
dir := t.TempDir()
empty := filepath.Join(dir, "a", "empty")
if err := os.MkdirAll(filepath.Dir(empty), 0300); err != nil {
t.Errorf("%v", err)
}
if err := ioutil.WriteFile(empty, nil, 0444); err != nil {
t.Errorf("%v", err)
}
}
What did you expect to see?
Both test passed.
What did you see instead?
Only TestNoDir
passed.
$ go test -v go_test.go === RUN TestNoDir --- PASS: TestNoDir (0.00s) === RUN TestDir testing.go:894: TempDir RemoveAll cleanup: openfdat /var/folders/jz/z42xl9mx2d576zq1q21hlggc00bk9x/T/TestDir492614072/001/a: permission denied --- FAIL: TestDir (0.00s) FAIL FAIL command-line-arguments 0.115s FAIL
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 15 (10 by maintainers)
Commits related to this issue
- snapshots: fix flaky tests The use testing.T.TempDir() seems to cause test failures in CI environvements in those cases where temporary directories' subdirs are created with permissions that are diff... — committed to cosmos/cosmos-sdk by deleted user 3 years ago
- snapshots: fix flaky tests (#8475) The use testing.T.TempDir() seems to cause test failures in CI environvements in those cases where temporary directories' subdirs are created with permissions that ... — committed to cosmos/cosmos-sdk by deleted user 3 years ago
Simplicity. Why add complexity for a case that essentially never arises?
Let’s just document the restriction: don’t create unwritable directions in the directory returned by
t.TempDir
.I think this is a reasonable error. If the test does something that the test harness cannot undo there should be some indication to the user.