aboutsummaryrefslogtreecommitdiff
path: root/src/os/exec/exec_test.go
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2023-09-12 14:50:50 -0400
committerGopher Robot <gobot@golang.org>2023-09-13 18:19:21 +0000
commit105f9d51691d996c1698811ca3906b505639f49b (patch)
treedfaff28df7063619dbabb9f83b4ff9f860ce03db /src/os/exec/exec_test.go
parentdd2279ee344a59ad50a73ebce5ab80c93fbbe732 (diff)
downloadgo-105f9d51691d996c1698811ca3906b505639f49b.tar.xz
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 <iant@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/os/exec/exec_test.go')
-rw-r--r--src/os/exec/exec_test.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go
index 473f92ba8e..9783a133ba 100644
--- a/src/os/exec/exec_test.go
+++ b/src/os/exec/exec_test.go
@@ -77,6 +77,21 @@ func TestMain(m *testing.M) {
if os.Getenv("GO_EXEC_TEST_PID") == "" {
os.Setenv("GO_EXEC_TEST_PID", strconv.Itoa(pid))
+ if runtime.GOOS == "windows" {
+ // Normalize environment so that test behavior is consistent.
+ // (The behavior of LookPath varies depending on this variable.)
+ //
+ // Ideally we would test both with the variable set and with it cleared,
+ // but I (bcmills) am not sure that that's feasible: it may already be set
+ // in the Windows registry, and I'm not sure if it is possible to remove
+ // a registry variable in a program's environment.
+ //
+ // Per https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-needcurrentdirectoryforexepathw#remarks,
+ // “the existence of the NoDefaultCurrentDirectoryInExePath environment
+ // variable is checked, and not its value.”
+ os.Setenv("NoDefaultCurrentDirectoryInExePath", "TRUE")
+ }
+
code := m.Run()
if code == 0 && flag.Lookup("test.run").Value.String() == "" && flag.Lookup("test.list").Value.String() == "" {
for cmd := range helperCommands {
@@ -180,6 +195,28 @@ var exeOnce struct {
sync.Once
}
+func chdir(t *testing.T, dir string) {
+ t.Helper()
+
+ prev, err := os.Getwd()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Chdir(dir); err != nil {
+ t.Fatal(err)
+ }
+ t.Logf("Chdir(%#q)", dir)
+
+ t.Cleanup(func() {
+ if err := os.Chdir(prev); err != nil {
+ // Couldn't chdir back to the original working directory.
+ // panic instead of t.Fatal so that we don't run other tests
+ // in an unexpected location.
+ panic("couldn't restore working directory: " + err.Error())
+ }
+ })
+}
+
var helperCommandUsed sync.Map
var helperCommands = map[string]func(...string){