|
| 1 | +package loader |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "golang.org/x/tools/go/packages" |
| 8 | +) |
| 9 | + |
| 10 | +func TestStdlibDetectionNoBazel(t *testing.T) { |
| 11 | + // Add a test-only package to the hardcoded list to verify different behavior |
| 12 | + // between dynamic loading and hardcoded list |
| 13 | + stdPackages["testonly"] = struct{}{} |
| 14 | + t.Cleanup(func() { |
| 15 | + delete(stdPackages, "testonly") |
| 16 | + }) |
| 17 | + |
| 18 | + testCases := []struct { |
| 19 | + pkgPath string |
| 20 | + isStdlib bool |
| 21 | + goPackagesDriver string |
| 22 | + }{ |
| 23 | + {"fmt", true, ""}, |
| 24 | + {"net/http", true, ""}, |
| 25 | + {"encoding/json", true, ""}, |
| 26 | + {"vendor/golang.org/x/crypto/chacha20", true, ""}, |
| 27 | + {"github.com/sourcegraph/scip-go", false, ""}, |
| 28 | + {"golang.org/x/tools", false, ""}, |
| 29 | + {"vendorapi.com/foo", false, ""}, |
| 30 | + {"testonly", false, ""}, |
| 31 | + {"testonly", true, "bazel"}, |
| 32 | + } |
| 33 | + |
| 34 | + t.Run("DynamicLoading", func(t *testing.T) { |
| 35 | + for _, tc := range testCases { |
| 36 | + stdlibOnce = sync.Once{} |
| 37 | + t.Setenv("GOPACKAGESDRIVER", tc.goPackagesDriver) |
| 38 | + |
| 39 | + pkg := &packages.Package{PkgPath: tc.pkgPath} |
| 40 | + if got := IsStandardLib(pkg); got != tc.isStdlib { |
| 41 | + t.Errorf("IsStandardLib(%q) = %v, want %v", |
| 42 | + tc.pkgPath, got, tc.isStdlib) |
| 43 | + } |
| 44 | + } |
| 45 | + }) |
| 46 | +} |
0 commit comments