]> Cypherpunks repositories - gostls13.git/commitdiff
test/run: use correct $PWD to make os.Getwd less expensive
authorRuss Cox <rsc@golang.org>
Thu, 14 Feb 2013 19:21:26 +0000 (14:21 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 14 Feb 2013 19:21:26 +0000 (14:21 -0500)
The commands being run are 'go tool this' and 'go tool that',
and the go command will call Getwd during its init.

R=golang-dev, iant
CC=golang-dev
https://golang.org/cl/7336045

test/run.go

index 36c8b7ad7bbab37598431cf9a0ee43c937cf54f8..5e167d6b0ccc324c7072da8efb11427687089704 100644 (file)
@@ -433,6 +433,7 @@ func (t *test) run() {
                cmd.Stderr = &buf
                if useTmp {
                        cmd.Dir = t.tempDir
+                       cmd.Env = envForDir(cmd.Dir)
                }
                err := cmd.Run()
                if err != nil {
@@ -828,3 +829,20 @@ func checkShouldTest() {
        assertNot(shouldTest("// +build arm 386", "linux", "amd64"))
        assert(shouldTest("// This is a test.", "os", "arch"))
 }
+
+// envForDir returns a copy of the environment
+// suitable for running in the given directory.
+// The environment is the current process's environment
+// but with an updated $PWD, so that an os.Getwd in the
+// child will be faster.
+func envForDir(dir string) []string {
+       env := os.Environ()
+       for i, kv := range env {
+               if strings.HasPrefix(kv, "PWD=") {
+                       env[i] = "PWD=" + dir
+                       return env
+               }
+       }
+       env = append(env, "PWD="+dir)
+       return env
+}