cmd/go/internal/vcs: stop making network connections in test

Change TestRepoRootForImportPath to not make network connections and
instead intercept requests in the cases where we would fall back to
trying to read the meta tags. We don't expect meta tags in those cases
so always return a Not Found code.

For #55164

Change-Id: I0c42658694670efe5cdd559076e88cca6a6a6964
Reviewed-on: https://go-review.googlesource.com/c/go/+/772640
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
Auto-Submit: Michael Matloob <matloob@golang.org>
TryBot-Bypass: Michael Matloob <matloob@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Michael Matloob <matloob@google.com>
This commit is contained in:
Michael Matloob 2026-04-30 15:35:32 -04:00
parent 70e521bdff
commit 23eac3d12b

View file

@ -6,13 +6,15 @@ package vcs
import (
"errors"
"internal/testenv"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"cmd/go/internal/web"
"cmd/go/internal/web/intercept"
)
func init() {
@ -26,7 +28,18 @@ func init() {
// Test that RepoRootForImportPath determines the correct RepoRoot for a given importPath.
// TODO(cmang): Add tests for SVN.
func TestRepoRootForImportPath(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
// Intercept requests to external servers to avoid making external network connections from the test.
ts := httptest.NewServer(http.NotFoundHandler())
t.Cleanup(ts.Close)
var interceptors []intercept.Interceptor
for _, d := range []string{"hub.jazz.net", "hubajazz.net", "hub2.jazz.net", "different.example.com"} {
interceptors = append(interceptors,
intercept.Interceptor{Scheme: "https", FromHost: d, ToHost: ts.Listener.Addr().String()},
intercept.Interceptor{Scheme: "http", FromHost: d, ToHost: ts.Listener.Addr().String()},
)
}
intercept.EnableTestHooks(interceptors)
t.Cleanup(intercept.DisableTestHooks)
tests := []struct {
path string