]> Cypherpunks repositories - gostls13.git/commitdiff
os: add TestProgWideChdir
authorDavid du Colombier <0intro@gmail.com>
Sun, 12 Apr 2015 17:36:23 +0000 (19:36 +0200)
committerDavid du Colombier <0intro@gmail.com>
Sun, 12 Apr 2015 19:03:13 +0000 (19:03 +0000)
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>

src/os/os_test.go

index a2c181da2228110ae96cc9921c605b812598b3e2..f65845af867bbb7ae14edeb1572356930981129b 100644 (file)
@@ -1040,6 +1040,57 @@ func TestChdirAndGetwd(t *testing.T) {
        fd.Close()
 }
 
+// Test that Chdir+Getwd is program-wide.
+func TestProgWideChdir(t *testing.T) {
+       const N = 10
+       c := make(chan bool)
+       cpwd := make(chan string)
+       for i := 0; i < N; i++ {
+               go func(i int) {
+                       // Lock half the goroutines in their own operating system
+                       // thread to exercise more scheduler possibilities.
+                       if i%2 == 1 {
+                               // On Plan 9, after calling LockOSThread, the goroutines
+                               // run on different processes which don't share the working
+                               // directory. This used to be an issue because Go expects
+                               // the working directory to be program-wide.
+                               // See issue 9428.
+                               runtime.LockOSThread()
+                       }
+                       <-c
+                       pwd, err := Getwd()
+                       if err != nil {
+                               t.Fatal("Getwd: %v", err)
+                       }
+                       cpwd <- pwd
+               }(i)
+       }
+       oldwd, err := Getwd()
+       if err != nil {
+               t.Fatal("Getwd: %v", err)
+       }
+       d, err := ioutil.TempDir("", "test")
+       if err != nil {
+               t.Fatal("TempDir: %v", err)
+       }
+       defer func() {
+               if err := Chdir(oldwd); err != nil {
+                       t.Fatal("Chdir: %v", err)
+               }
+               RemoveAll(d)
+       }()
+       if err := Chdir(d); err != nil {
+               t.Fatal("Chdir: %v", err)
+       }
+       close(c)
+       for i := 0; i < N; i++ {
+               pwd := <-cpwd
+               if pwd != d {
+                       t.Errorf("Getwd returned %q want %q", pwd, d)
+               }
+       }
+}
+
 func TestSeek(t *testing.T) {
        f := newFile("TestSeek", t)
        defer Remove(f.Name())