Skip to content

Commit 105f9d5

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
os/exec: simplify Windows-specific tests
- Use the test binary itself for printing paths instead of building a separate binary and running it through additional subprocesses. - Factor out a common chdir helper. - Use t.Setenv where appropriate. - Reduce indirection in test helpers. - Set NoDefaultCurrentDirectoryInExePath consistently in the environment. Also add a test case demonstrating an interesting behavior for relative paths that may interact with #62596. Fixes #62594. For #62596. Change-Id: I19b9325034edf78cd0ca747594476cd7432bb451 Reviewed-on: https://go-review.googlesource.com/c/go/+/528035 Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Bryan Mills <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent dd2279e commit 105f9d5

File tree

4 files changed

+400
-423
lines changed

4 files changed

+400
-423
lines changed

src/os/exec/dot_test.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var pathVar string = func() string {
2424

2525
func TestLookPath(t *testing.T) {
2626
testenv.MustHaveExec(t)
27-
// Not parallel: uses os.Chdir and t.Setenv.
27+
// Not parallel: uses Chdir and Setenv.
2828

2929
tmpDir := filepath.Join(t.TempDir(), "testdir")
3030
if err := os.Mkdir(tmpDir, 0777); err != nil {
@@ -38,18 +38,7 @@ func TestLookPath(t *testing.T) {
3838
if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0777); err != nil {
3939
t.Fatal(err)
4040
}
41-
cwd, err := os.Getwd()
42-
if err != nil {
43-
t.Fatal(err)
44-
}
45-
defer func() {
46-
if err := os.Chdir(cwd); err != nil {
47-
panic(err)
48-
}
49-
}()
50-
if err = os.Chdir(tmpDir); err != nil {
51-
t.Fatal(err)
52-
}
41+
chdir(t, tmpDir)
5342
t.Setenv("PWD", tmpDir)
5443
t.Logf(". is %#q", tmpDir)
5544

src/os/exec/exec_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ func TestMain(m *testing.M) {
7777
if os.Getenv("GO_EXEC_TEST_PID") == "" {
7878
os.Setenv("GO_EXEC_TEST_PID", strconv.Itoa(pid))
7979

80+
if runtime.GOOS == "windows" {
81+
// Normalize environment so that test behavior is consistent.
82+
// (The behavior of LookPath varies depending on this variable.)
83+
//
84+
// Ideally we would test both with the variable set and with it cleared,
85+
// but I (bcmills) am not sure that that's feasible: it may already be set
86+
// in the Windows registry, and I'm not sure if it is possible to remove
87+
// a registry variable in a program's environment.
88+
//
89+
// Per https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-needcurrentdirectoryforexepathw#remarks,
90+
// “the existence of the NoDefaultCurrentDirectoryInExePath environment
91+
// variable is checked, and not its value.”
92+
os.Setenv("NoDefaultCurrentDirectoryInExePath", "TRUE")
93+
}
94+
8095
code := m.Run()
8196
if code == 0 && flag.Lookup("test.run").Value.String() == "" && flag.Lookup("test.list").Value.String() == "" {
8297
for cmd := range helperCommands {
@@ -180,6 +195,28 @@ var exeOnce struct {
180195
sync.Once
181196
}
182197

198+
func chdir(t *testing.T, dir string) {
199+
t.Helper()
200+
201+
prev, err := os.Getwd()
202+
if err != nil {
203+
t.Fatal(err)
204+
}
205+
if err := os.Chdir(dir); err != nil {
206+
t.Fatal(err)
207+
}
208+
t.Logf("Chdir(%#q)", dir)
209+
210+
t.Cleanup(func() {
211+
if err := os.Chdir(prev); err != nil {
212+
// Couldn't chdir back to the original working directory.
213+
// panic instead of t.Fatal so that we don't run other tests
214+
// in an unexpected location.
215+
panic("couldn't restore working directory: " + err.Error())
216+
}
217+
})
218+
}
219+
183220
var helperCommandUsed sync.Map
184221

185222
var helperCommands = map[string]func(...string){

src/os/exec/lp_unix_test.go

+6-17
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,19 @@
44

55
//go:build unix
66

7-
package exec
7+
package exec_test
88

99
import (
1010
"os"
11+
"os/exec"
1112
"testing"
1213
)
1314

1415
func TestLookPathUnixEmptyPath(t *testing.T) {
15-
// Not parallel: uses os.Chdir.
16+
// Not parallel: uses Chdir and Setenv.
1617

17-
tmp, err := os.MkdirTemp("", "TestLookPathUnixEmptyPath")
18-
if err != nil {
19-
t.Fatal("TempDir failed: ", err)
20-
}
21-
defer os.RemoveAll(tmp)
22-
wd, err := os.Getwd()
23-
if err != nil {
24-
t.Fatal("Getwd failed: ", err)
25-
}
26-
err = os.Chdir(tmp)
27-
if err != nil {
28-
t.Fatal("Chdir failed: ", err)
29-
}
30-
defer os.Chdir(wd)
18+
tmp := t.TempDir()
19+
chdir(t, tmp)
3120

3221
f, err := os.OpenFile("exec_me", os.O_CREATE|os.O_EXCL, 0700)
3322
if err != nil {
@@ -40,7 +29,7 @@ func TestLookPathUnixEmptyPath(t *testing.T) {
4029

4130
t.Setenv("PATH", "")
4231

43-
path, err := LookPath("exec_me")
32+
path, err := exec.LookPath("exec_me")
4433
if err == nil {
4534
t.Fatal("LookPath found exec_me in empty $PATH")
4635
}

0 commit comments

Comments
 (0)