From 23eac3d12bdb690ba7b60cfb3bd6d8eaf168aa1f Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Thu, 30 Apr 2026 15:35:32 -0400 Subject: [PATCH] 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 Auto-Submit: Michael Matloob TryBot-Bypass: Michael Matloob Reviewed-by: Dmitri Shuralyov Reviewed-by: Michael Matloob --- src/cmd/go/internal/vcs/vcs_test.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/internal/vcs/vcs_test.go b/src/cmd/go/internal/vcs/vcs_test.go index 73ad6de48f..aeef497dec 100644 --- a/src/cmd/go/internal/vcs/vcs_test.go +++ b/src/cmd/go/internal/vcs/vcs_test.go @@ -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