cmd/go/internal/imports: remove test dependency on json internals

TestScan loads encoding/json and verifies that various imports
match expectations. The new v2 encoding/json violates these
expectations. Since this test is testing the ScanDir function,
not encoding/json, change it to use a test package with defined
imports instead.

Change-Id: I68a0813ccf37daadbd6ea52872a8ac132141e82a
Reviewed-on: https://go-review.googlesource.com/c/go/+/665795
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Bypass: Damien Neil <dneil@google.com>
Auto-Submit: Damien Neil <dneil@google.com>
This commit is contained in:
Damien Neil 2025-04-15 11:17:03 -07:00 committed by Gopher Robot
parent fcd73b0ac3
commit 64371adcf4
6 changed files with 37 additions and 11 deletions

View File

@ -17,26 +17,26 @@ import (
func TestScan(t *testing.T) {
testenv.MustHaveGoBuild(t)
imports, testImports, err := ScanDir(filepath.Join(testenv.GOROOT(t), "src/encoding/json"), Tags())
imports, testImports, err := ScanDir(filepath.Join(testenv.GOROOT(t), "src/cmd/go/internal/imports/testdata/test"), Tags())
if err != nil {
t.Fatal(err)
}
foundBase64 := false
foundFmt := false
for _, p := range imports {
if p == "encoding/base64" {
foundBase64 = true
if p == "fmt" {
foundFmt = true // test package imports fmt directly
}
if p == "encoding/binary" {
// A dependency but not an import
t.Errorf("json reported as importing encoding/binary but does not")
t.Errorf("testdata/test reported as importing encoding/binary but does not")
}
if p == "net/http" {
// A test import but not an import
t.Errorf("json reported as importing net/http but does not")
t.Errorf("testdata/test reported as importing net/http but does not")
}
}
if !foundBase64 {
t.Errorf("json missing import encoding/base64 (%q)", imports)
if !foundFmt {
t.Errorf("testdata/test missing import fmt (%q)", imports)
}
foundHTTP := false
@ -44,13 +44,13 @@ func TestScan(t *testing.T) {
if p == "net/http" {
foundHTTP = true
}
if p == "unicode/utf16" {
if p == "fmt" {
// A package import but not a test import
t.Errorf("json reported as test-importing unicode/utf16 but does not")
t.Errorf("testdata/test reported as test-importing fmt but does not")
}
}
if !foundHTTP {
t.Errorf("json missing test import net/http (%q)", testImports)
t.Errorf("testdata/test missing test import net/http (%q)", testImports)
}
}
func TestScanDir(t *testing.T) {

View File

@ -0,0 +1,5 @@
package child
import "encoding/binary"
var V = binary.MaxVarintLen16

View File

View File

@ -0,0 +1,10 @@
package test
import (
"cmd/go/internal/imports/testdata/test/child"
"fmt"
)
func F() {
fmt.Println(child.V)
}

View File

@ -0,0 +1,9 @@
package test_test
import (
_ "net/http"
"testing"
)
func Test(t *testing.T) {
}

View File

@ -0,0 +1,2 @@
cmd/go/internal/imports/testdata/test/child
fmt