Skip to content

Commit e49cb02

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
os: clean up tests
- Use testenv.Command instead of exec.Command to try to get more useful timeout behavior. - Parallelize tests that appear not to require global state. (And add explanatory comments for a few that are not parallelizable for subtle reasons.) - Consolidate some “Helper” tests with their parent tests. - Use t.TempDir instead of os.MkdirTemp when appropriate. - Factor out subtests for repeated test helpers. For #36107. Updates #22315. Change-Id: Ic24b6957094dcd40908a59f48e44c8993729222b Reviewed-on: https://go-review.googlesource.com/c/go/+/458015 Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Auto-Submit: Bryan Mills <[email protected]>
1 parent f2884bf commit e49cb02

16 files changed

+450
-250
lines changed

src/os/env_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ func TestLookupEnv(t *testing.T) {
171171
// Check that they are properly reported by LookupEnv and can be set by SetEnv.
172172
// See https://golang.org/issue/49886.
173173
func TestEnvironConsistency(t *testing.T) {
174+
t.Parallel()
175+
174176
for _, kv := range Environ() {
175177
i := strings.Index(kv, "=")
176178
if i == 0 {

src/os/error_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
)
1515

1616
func TestErrIsExist(t *testing.T) {
17+
t.Parallel()
18+
1719
f, err := os.CreateTemp("", "_Go_ErrIsExist")
1820
if err != nil {
1921
t.Fatalf("open ErrIsExist tempfile: %s", err)
@@ -148,6 +150,8 @@ func TestIsPermission(t *testing.T) {
148150
}
149151

150152
func TestErrPathNUL(t *testing.T) {
153+
t.Parallel()
154+
151155
f, err := os.CreateTemp("", "_Go_ErrPathNUL\x00")
152156
if err == nil {
153157
f.Close()

src/os/exec_unix_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ import (
1414

1515
func TestErrProcessDone(t *testing.T) {
1616
testenv.MustHaveGoBuild(t)
17-
path, err := testenv.GoTool()
18-
if err != nil {
19-
t.Errorf("finding go tool: %v", err)
20-
}
21-
p, err := StartProcess(path, []string{"go"}, &ProcAttr{})
17+
t.Parallel()
18+
19+
p, err := StartProcess(testenv.GoToolPath(t), []string{"go"}, &ProcAttr{})
2220
if err != nil {
2321
t.Errorf("starting test process: %v", err)
2422
}

src/os/executable_test.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"internal/testenv"
1010
"os"
11-
osexec "os/exec"
1211
"path/filepath"
1312
"runtime"
1413
"testing"
@@ -18,6 +17,8 @@ const executable_EnvVar = "OSTEST_OUTPUT_EXECPATH"
1817

1918
func TestExecutable(t *testing.T) {
2019
testenv.MustHaveExec(t)
20+
t.Parallel()
21+
2122
ep, err := os.Executable()
2223
if err != nil {
2324
t.Fatalf("Executable failed: %v", err)
@@ -29,18 +30,18 @@ func TestExecutable(t *testing.T) {
2930
t.Fatalf("filepath.Rel: %v", err)
3031
}
3132

32-
cmd := &osexec.Cmd{}
33+
cmd := testenv.Command(t, fn, "-test.run=XXXX")
3334
// make child start with a relative program path
3435
cmd.Dir = dir
3536
cmd.Path = fn
36-
// forge argv[0] for child, so that we can verify we could correctly
37-
// get real path of the executable without influenced by argv[0].
38-
cmd.Args = []string{"-", "-test.run=XXXX"}
3937
if runtime.GOOS == "openbsd" || runtime.GOOS == "aix" {
4038
// OpenBSD and AIX rely on argv[0]
41-
cmd.Args[0] = fn
39+
} else {
40+
// forge argv[0] for child, so that we can verify we could correctly
41+
// get real path of the executable without influenced by argv[0].
42+
cmd.Args[0] = "-"
4243
}
43-
cmd.Env = append(os.Environ(), fmt.Sprintf("%s=1", executable_EnvVar))
44+
cmd.Env = append(cmd.Environ(), fmt.Sprintf("%s=1", executable_EnvVar))
4445
out, err := cmd.CombinedOutput()
4546
if err != nil {
4647
t.Fatalf("exec(self) failed: %v", err)
@@ -95,6 +96,7 @@ func TestExecutableDeleted(t *testing.T) {
9596
case "openbsd", "freebsd", "aix":
9697
t.Skipf("%v does not support reading deleted binary name", runtime.GOOS)
9798
}
99+
t.Parallel()
98100

99101
dir := t.TempDir()
100102

0 commit comments

Comments
 (0)