Skip to content

Commit

Permalink
os: ensure tests pass even if GOROOT is read-only
Browse files Browse the repository at this point in the history
We achieve this by always running all tests that create files in a
fresh temporary directory, rather than just on darwin/{arm,arm64}.
As a bonus, this lets us simplify the cleanup code for these tests
and assume their working directory starts out empty.

Updates #28387

Change-Id: I952007ae390a2451c9a368da26c7f9f5af64b2ba
Reviewed-on: https://go-review.googlesource.com/c/145283
Reviewed-by: Brad Fitzpatrick <[email protected]>
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
dpinela authored and bradfitz committed Oct 28, 2018
1 parent 3fe9d4c commit 81475ca
Showing 1 changed file with 1 addition and 40 deletions.
41 changes: 1 addition & 40 deletions src/os/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ func TestStatError(t *testing.T) {
defer chtmpdir(t)()

path := "no-such-file"
Remove(path) // Just in case

fi, err := Stat(path)
if err == nil {
Expand All @@ -194,12 +193,10 @@ func TestStatError(t *testing.T) {
testenv.MustHaveSymlink(t)

link := "symlink"
Remove(link) // Just in case
err = Symlink(path, link)
if err != nil {
t.Fatal(err)
}
defer Remove(link)

fi, err = Stat(link)
if err == nil {
Expand Down Expand Up @@ -688,12 +685,10 @@ func TestHardLink(t *testing.T) {

defer chtmpdir(t)()
from, to := "hardlinktestfrom", "hardlinktestto"
Remove(from) // Just in case.
file, err := Create(to)
if err != nil {
t.Fatalf("open %q failed: %v", to, err)
}
defer Remove(to)
if err = file.Close(); err != nil {
t.Errorf("close %q failed: %v", to, err)
}
Expand All @@ -709,7 +704,6 @@ func TestHardLink(t *testing.T) {
t.Errorf("link %q, %q failed to return a valid error", none, none)
}

defer Remove(from)
tostat, err := Stat(to)
if err != nil {
t.Fatalf("stat %q failed: %v", to, err)
Expand Down Expand Up @@ -745,11 +739,8 @@ func TestHardLink(t *testing.T) {
}

// chtmpdir changes the working directory to a new temporary directory and
// provides a cleanup function. Used when PWD is read-only.
// provides a cleanup function.
func chtmpdir(t *testing.T) func() {
if runtime.GOOS != "darwin" || (runtime.GOARCH != "arm" && runtime.GOARCH != "arm64") {
return func() {} // only needed on darwin/arm{,64}
}
oldwd, err := Getwd()
if err != nil {
t.Fatalf("chtmpdir: %v", err)
Expand All @@ -774,20 +765,17 @@ func TestSymlink(t *testing.T) {

defer chtmpdir(t)()
from, to := "symlinktestfrom", "symlinktestto"
Remove(from) // Just in case.
file, err := Create(to)
if err != nil {
t.Fatalf("Create(%q) failed: %v", to, err)
}
defer Remove(to)
if err = file.Close(); err != nil {
t.Errorf("Close(%q) failed: %v", to, err)
}
err = Symlink(to, from)
if err != nil {
t.Fatalf("Symlink(%q, %q) failed: %v", to, from, err)
}
defer Remove(from)
tostat, err := Lstat(to)
if err != nil {
t.Fatalf("Lstat(%q) failed: %v", to, err)
Expand Down Expand Up @@ -841,12 +829,10 @@ func TestLongSymlink(t *testing.T) {
// Long, but not too long: a common limit is 255.
s = s + s + s + s + s + s + s + s + s + s + s + s + s + s + s
from := "longsymlinktestfrom"
Remove(from) // Just in case.
err := Symlink(s, from)
if err != nil {
t.Fatalf("symlink %q, %q failed: %v", s, from, err)
}
defer Remove(from)
r, err := Readlink(from)
if err != nil {
t.Fatalf("readlink %q failed: %v", from, err)
Expand All @@ -859,9 +845,6 @@ func TestLongSymlink(t *testing.T) {
func TestRename(t *testing.T) {
defer chtmpdir(t)()
from, to := "renamefrom", "renameto"
// Ensure we are not testing the overwrite case here.
Remove(from)
Remove(to)

file, err := Create(from)
if err != nil {
Expand All @@ -874,7 +857,6 @@ func TestRename(t *testing.T) {
if err != nil {
t.Fatalf("rename %q, %q failed: %v", to, from, err)
}
defer Remove(to)
_, err = Stat(to)
if err != nil {
t.Errorf("stat %q failed: %v", to, err)
Expand All @@ -884,9 +866,6 @@ func TestRename(t *testing.T) {
func TestRenameOverwriteDest(t *testing.T) {
defer chtmpdir(t)()
from, to := "renamefrom", "renameto"
// Just in case.
Remove(from)
Remove(to)

toData := []byte("to")
fromData := []byte("from")
Expand All @@ -904,7 +883,6 @@ func TestRenameOverwriteDest(t *testing.T) {
if err != nil {
t.Fatalf("rename %q, %q failed: %v", to, from, err)
}
defer Remove(to)

_, err = Stat(from)
if err == nil {
Expand All @@ -925,9 +903,6 @@ func TestRenameOverwriteDest(t *testing.T) {
func TestRenameFailed(t *testing.T) {
defer chtmpdir(t)()
from, to := "renamefrom", "renameto"
// Ensure we are not testing the overwrite case here.
Remove(from)
Remove(to)

err := Rename(from, to)
switch err := err.(type) {
Expand All @@ -943,9 +918,6 @@ func TestRenameFailed(t *testing.T) {
}
case nil:
t.Errorf("rename %q, %q: expected error, got nil", from, to)

// cleanup whatever was placed in "renameto"
Remove(to)
default:
t.Errorf("rename %q, %q: expected %T, got %T %v", from, to, new(LinkError), err, err)
}
Expand All @@ -956,7 +928,6 @@ func TestRenameNotExisting(t *testing.T) {
from, to := "doesnt-exist", "dest"

Mkdir(to, 0777)
defer Remove(to)

if err := Rename(from, to); !IsNotExist(err) {
t.Errorf("Rename(%q, %q) = %v; want an IsNotExist error", from, to, err)
Expand All @@ -967,12 +938,8 @@ func TestRenameToDirFailed(t *testing.T) {
defer chtmpdir(t)()
from, to := "renamefrom", "renameto"

Remove(from)
Remove(to)
Mkdir(from, 0777)
Mkdir(to, 0777)
defer Remove(from)
defer Remove(to)

err := Rename(from, to)
switch err := err.(type) {
Expand All @@ -988,9 +955,6 @@ func TestRenameToDirFailed(t *testing.T) {
}
case nil:
t.Errorf("rename %q, %q: expected error, got nil", from, to)

// cleanup whatever was placed in "renameto"
Remove(to)
default:
t.Errorf("rename %q, %q: expected %T, got %T %v", from, to, new(LinkError), err, err)
}
Expand Down Expand Up @@ -1702,7 +1666,6 @@ func writeFile(t *testing.T, fname string, flag int, text string) string {
func TestAppend(t *testing.T) {
defer chtmpdir(t)()
const f = "append.txt"
defer Remove(f)
s := writeFile(t, f, O_CREATE|O_TRUNC|O_RDWR, "new")
if s != "new" {
t.Fatalf("writeFile: have %q want %q", s, "new")
Expand Down Expand Up @@ -1769,13 +1732,11 @@ func TestSameFile(t *testing.T) {
if err != nil {
t.Fatalf("Create(a): %v", err)
}
defer Remove(fa.Name())
fa.Close()
fb, err := Create("b")
if err != nil {
t.Fatalf("Create(b): %v", err)
}
defer Remove(fb.Name())
fb.Close()

ia1, err := Stat("a")
Expand Down

0 comments on commit 81475ca

Please sign in to comment.