Skip to content

Commit bcc27db

Browse files
committed
add TestForExec
Signed-off-by: leongross <[email protected]>
1 parent 23aa25f commit bcc27db

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/os/exec_posix.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build aix || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || wasip1 || windows
5+
//go:build aix || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris || wasip1
66

77
package os
88

99
import (
10+
"errors"
1011
"runtime"
1112
"syscall"
1213
"unsafe"
@@ -49,6 +50,14 @@ func forkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err error)
4950
ret uintptr
5051
)
5152

53+
if len(argv) == 0 {
54+
return 0, errors.New("exec: no argv")
55+
}
56+
57+
if attr == nil {
58+
attr = new(ProcAttr)
59+
}
60+
5261
argv0p, err := syscall.BytePtrFromString(argv0)
5362
if err != nil {
5463
return 0, err

src/os/exec_posix_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package os_test
2+
3+
import (
4+
. "os"
5+
"runtime"
6+
"testing"
7+
)
8+
9+
// Test the functionality of the forkExec function, which is used to fork and exec a new process.
10+
// This test is not run on Windows, as forkExec is not supported on Windows.
11+
// This test is not run on Plan 9, as forkExec is not supported on Plan 9.
12+
func TestForkExec(t *testing.T) {
13+
if runtime.GOOS != "linux" {
14+
t.Logf("skipping test on %s", runtime.GOOS)
15+
return
16+
}
17+
18+
proc, err := StartProcess("echo", []string{"echo", "hello", "world"}, &ProcAttr{})
19+
if err != nil {
20+
t.Fatalf("forkExec failed: %v", err)
21+
return
22+
}
23+
24+
if proc.Pid == 0 {
25+
t.Fatalf("forkExec failed: new process has pid 0")
26+
} else {
27+
t.Logf("forkExec succeeded: new process has pid %d", proc)
28+
}
29+
}

0 commit comments

Comments
 (0)