Skip to content

Commit c017a4e

Browse files
committed
cmd/go: sometimes name tmp test binary test.test.exe on Windows
Right now it is always pkgname.test.exe, but if pkgname is patch or install or setup or update, Windows thinks that running it will install new software, so it pops up a dialog box asking for more permission. Renaming the binary avoids the Windows security check. This only applies to the binary that the Go command writes to its temporary work directory. If the user runs 'go test -c' or any of the other ways to generate a test binary, it will continue to use pkgname.test.exe. Fixes #8711. LGTM=bradfitz R=golang-codereviews, r CC=alex.brainman, bradfitz, golang-codereviews, iant https://golang.org/cl/146580043
1 parent 8b5221a commit c017a4e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/cmd/go/test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,13 @@ func contains(x []string, s string) bool {
535535
return false
536536
}
537537

538+
var windowsBadWords = []string{
539+
"install",
540+
"patch",
541+
"setup",
542+
"update",
543+
}
544+
538545
func (b *builder) test(p *Package) (buildAction, runAction, printAction *action, err error) {
539546
if len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
540547
build := b.action(modeBuild, modeBuild, p)
@@ -794,6 +801,36 @@ func (b *builder) test(p *Package) (buildAction, runAction, printAction *action,
794801
a.objdir = testDir + string(filepath.Separator)
795802
a.objpkg = filepath.Join(testDir, "main.a")
796803
a.target = filepath.Join(testDir, testBinary) + exeSuffix
804+
if goos == "windows" {
805+
// There are many reserved words on Windows that,
806+
// if used in the name of an executable, cause Windows
807+
// to try to ask for extra permissions.
808+
// The word list includes setup, install, update, and patch,
809+
// but it does not appear to be defined anywhere.
810+
// We have run into this trying to run the
811+
// go.codereview/patch tests.
812+
// For package names containing those words, use test.test.exe
813+
// instead of pkgname.test.exe.
814+
// Note that this file name is only used in the Go command's
815+
// temporary directory. If the -c or other flags are
816+
// given, the code below will still use pkgname.test.exe.
817+
// There are two user-visible effects of this change.
818+
// First, you can actually run 'go test' in directories that
819+
// have names that Windows thinks are installer-like,
820+
// without getting a dialog box asking for more permissions.
821+
// Second, in the Windows process listing during go test,
822+
// the test shows up as test.test.exe, not pkgname.test.exe.
823+
// That second one is a drawback, but it seems a small
824+
// price to pay for the test running at all.
825+
// If maintaining the list of bad words is too onerous,
826+
// we could just do this always on Windows.
827+
for _, bad := range windowsBadWords {
828+
if strings.Contains(testBinary, bad) {
829+
a.target = filepath.Join(testDir, "test.test") + exeSuffix
830+
break
831+
}
832+
}
833+
}
797834
buildAction = a
798835

799836
if testC || testNeedBinary {

0 commit comments

Comments
 (0)