internal/testenv: add and use MustHaveExternalNetwork function

This CL adds a copy of the MustHaveExternalNetwork function from the
testenv package in the Go repo for use for filtering out tests that
need the network access from running on builders that don't guarantee
it. (External network access is only enabled on longtest builders). It
also calls the function from two tests that are failing because they
make external network calls.

For #61209

Change-Id: I68ed1d3f8bb65697b1025bca0ba40aa0ab7423a3
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/508295
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Michael Matloob <matloob@google.com>
TryBot-Bypass: Michael Matloob <matloob@google.com>
Reviewed-by: Michael Matloob <matloob@google.com>
diff --git a/internal/fetchdatasource/fetchdatasource_test.go b/internal/fetchdatasource/fetchdatasource_test.go
index 8f93105..d913ad7 100644
--- a/internal/fetchdatasource/fetchdatasource_test.go
+++ b/internal/fetchdatasource/fetchdatasource_test.go
@@ -25,6 +25,7 @@
 	"golang.org/x/pkgsite/internal/proxy/proxytest"
 	"golang.org/x/pkgsite/internal/source"
 	"golang.org/x/pkgsite/internal/stdlib"
+	"golang.org/x/pkgsite/internal/testenv"
 	"golang.org/x/pkgsite/internal/testing/testhelper"
 	"golang.org/x/pkgsite/internal/version"
 )
@@ -428,6 +429,11 @@
 		},
 	} {
 		t.Run(test.path, func(t *testing.T) {
+			if test.modulePath == stdlib.ModulePath {
+				// FetchModule for the stdlib calls stdlib.Versions which makes a network
+				// call to go.googlesource.com for the Go git repo.
+				testenv.MustHaveExternalNetwork(t)
+			}
 			got, err := ds.GetUnitMeta(ctx, test.path, test.modulePath, fetch.LocalVersion)
 			if test.wantErr != nil {
 				if !errors.Is(err, test.wantErr) {
diff --git a/internal/stdlib/stdlib_test.go b/internal/stdlib/stdlib_test.go
index 7297582..8841b0a 100644
--- a/internal/stdlib/stdlib_test.go
+++ b/internal/stdlib/stdlib_test.go
@@ -12,6 +12,7 @@
 	"testing"
 
 	"golang.org/x/mod/semver"
+	"golang.org/x/pkgsite/internal/testenv"
 	"golang.org/x/pkgsite/internal/version"
 )
 
@@ -379,6 +380,8 @@
 }
 
 func TestResolveSupportedBranches(t *testing.T) {
+	testenv.MustHaveExternalNetwork(t) // ResolveSupportedBranches accesses the go repo at go.googlesource.com
+
 	got, err := ResolveSupportedBranches()
 	if err != nil {
 		t.Fatal(err)
diff --git a/internal/testenv/testenv.go b/internal/testenv/testenv.go
new file mode 100644
index 0000000..87dca56
--- /dev/null
+++ b/internal/testenv/testenv.go
@@ -0,0 +1,30 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package testenv provides information about what functionality
+// is available in different testing environments run by the Go team.
+//
+// It is an internal package because these details are specific
+// to the Go team's test setup (on build.golang.org) and not
+// fundamental to tests in general.
+package testenv
+
+import (
+	"runtime"
+	"testing"
+)
+
+// MustHaveExternalNetwork checks that the current system can use
+// external (non-localhost) networks.
+// If not, MustHaveExternalNetwork calls t.Skip with an explanation.
+func MustHaveExternalNetwork(t testing.TB) {
+	if runtime.GOOS == "js" || runtime.GOOS == "wasip1" {
+		t.Helper()
+		t.Skipf("skipping test: no external network on %s", runtime.GOOS)
+	}
+	if testing.Short() {
+		t.Helper()
+		t.Skipf("skipping test: no external network in -short mode")
+	}
+}