]> Cypherpunks repositories - gostls13.git/commitdiff
os: fix ForkExec() handling of envv == nil
authorAlex Brainman <alex.brainman@gmail.com>
Mon, 9 Aug 2010 01:21:20 +0000 (11:21 +1000)
committerAlex Brainman <alex.brainman@gmail.com>
Mon, 9 Aug 2010 01:21:20 +0000 (11:21 +1000)
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/1913047

src/pkg/exec/exec_test.go
src/pkg/os/exec.go

index 3e4ab7d780070660c5ee88a8f9f3139e1d217136..898f42582b976f93af8cca9f628a30f8261c4a2a 100644 (file)
@@ -8,6 +8,7 @@ import (
        "io"
        "io/ioutil"
        "testing"
+       "os"
 )
 
 func TestRunCat(t *testing.T) {
@@ -84,3 +85,25 @@ func TestMergeWithStdout(t *testing.T) {
                t.Fatal("close:", err)
        }
 }
+
+func TestAddEnvVar(t *testing.T) {
+       err := os.Setenv("NEWVAR", "hello world")
+       if err != nil {
+               t.Fatal("setenv:", err)
+       }
+       cmd, err := Run("/bin/sh", []string{"sh", "-c", "echo $NEWVAR"}, nil, "",
+               DevNull, Pipe, DevNull)
+       if err != nil {
+               t.Fatal("run:", err)
+       }
+       buf, err := ioutil.ReadAll(cmd.Stdout)
+       if err != nil {
+               t.Fatal("read:", err)
+       }
+       if string(buf) != "hello world\n" {
+               t.Fatalf("read: got %q", buf)
+       }
+       if err = cmd.Close(); err != nil {
+               t.Fatal("close:", err)
+       }
+}
index d55acbaa708e8fbd94a472856e6f5cbb4bb0f58a..c0831cab6907bcca06487d940a3ee46892b36852 100644 (file)
@@ -16,6 +16,9 @@ import (
 // will cause the child to have no open file descriptor with that index.
 // If dir is not empty, the child chdirs into the directory before execing the program.
 func ForkExec(argv0 string, argv []string, envv []string, dir string, fd []*File) (pid int, err Error) {
+       if envv == nil {
+               envv = Environ()
+       }
        // Create array of integer (system) fds.
        intfd := make([]int, len(fd))
        for i, f := range fd {