Skip to content

Commit ae740a4

Browse files
committed
os: add TestProgWideChdir
This test checks the working directory is always consistent after Chdir in a Go program. Fixes #10035. Change-Id: I6abf0e4fcd40680ee572c6b40fc52ab17ef38d54 Reviewed-on: https://go-review.googlesource.com/6382 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: David du Colombier <0intro@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 7d3f81a commit ae740a4

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/os/os_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,57 @@ func TestChdirAndGetwd(t *testing.T) {
10401040
fd.Close()
10411041
}
10421042

1043+
// Test that Chdir+Getwd is program-wide.
1044+
func TestProgWideChdir(t *testing.T) {
1045+
const N = 10
1046+
c := make(chan bool)
1047+
cpwd := make(chan string)
1048+
for i := 0; i < N; i++ {
1049+
go func(i int) {
1050+
// Lock half the goroutines in their own operating system
1051+
// thread to exercise more scheduler possibilities.
1052+
if i%2 == 1 {
1053+
// On Plan 9, after calling LockOSThread, the goroutines
1054+
// run on different processes which don't share the working
1055+
// directory. This used to be an issue because Go expects
1056+
// the working directory to be program-wide.
1057+
// See issue 9428.
1058+
runtime.LockOSThread()
1059+
}
1060+
<-c
1061+
pwd, err := Getwd()
1062+
if err != nil {
1063+
t.Fatal("Getwd: %v", err)
1064+
}
1065+
cpwd <- pwd
1066+
}(i)
1067+
}
1068+
oldwd, err := Getwd()
1069+
if err != nil {
1070+
t.Fatal("Getwd: %v", err)
1071+
}
1072+
d, err := ioutil.TempDir("", "test")
1073+
if err != nil {
1074+
t.Fatal("TempDir: %v", err)
1075+
}
1076+
defer func() {
1077+
if err := Chdir(oldwd); err != nil {
1078+
t.Fatal("Chdir: %v", err)
1079+
}
1080+
RemoveAll(d)
1081+
}()
1082+
if err := Chdir(d); err != nil {
1083+
t.Fatal("Chdir: %v", err)
1084+
}
1085+
close(c)
1086+
for i := 0; i < N; i++ {
1087+
pwd := <-cpwd
1088+
if pwd != d {
1089+
t.Errorf("Getwd returned %q want %q", pwd, d)
1090+
}
1091+
}
1092+
}
1093+
10431094
func TestSeek(t *testing.T) {
10441095
f := newFile("TestSeek", t)
10451096
defer Remove(f.Name())

0 commit comments

Comments
 (0)