cmd/go/internal/work: ensure correct group for TestRespectSetgidDir

mkdir(2) inherits the parent directory group on *BSD (including Darwin),
and it may inherit on other platforms if the parent directory is SetGID.

This can cause TestRespectSetgidDir SetGID to fail when the process does
not have have permission for the inherited group on the new temporary
directory.

Fixes #29160

Change-Id: Iac05511e501dfe307a753f801223b1049cc0947d
Reviewed-on: https://go-review.googlesource.com/c/153357
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/src/cmd/go/internal/work/build_test.go b/src/cmd/go/internal/work/build_test.go
index 010e17e..a875ec1 100644
--- a/src/cmd/go/internal/work/build_test.go
+++ b/src/cmd/go/internal/work/build_test.go
@@ -227,6 +227,8 @@
 		if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
 			t.Skip("can't set SetGID bit with chmod on iOS")
 		}
+	case "windows", "plan9":
+		t.Skip("chown/chmod setgid are not supported on Windows and Plan 9")
 	}
 
 	var b Builder
@@ -245,11 +247,13 @@
 	}
 	defer os.RemoveAll(setgiddir)
 
-	if runtime.GOOS == "freebsd" {
-		err = os.Chown(setgiddir, os.Getuid(), os.Getgid())
-		if err != nil {
-			t.Fatal(err)
-		}
+	// BSD mkdir(2) inherits the parent directory group, and other platforms
+	// can inherit the parent directory group via setgid. The test setup (chmod
+	// setgid) will fail if the process does not have the group permission to
+	// the new temporary directory.
+	err = os.Chown(setgiddir, os.Getuid(), os.Getgid())
+	if err != nil {
+		t.Fatal(err)
 	}
 
 	// Change setgiddir's permissions to include the SetGID bit.