aboutsummaryrefslogtreecommitdiff
path: root/internal/api/api_test.go
diff options
context:
space:
mode:
authorEthan Lee <ethanalee@google.com>2026-03-11 21:17:12 +0000
committerGopher Robot <gobot@golang.org>2026-03-26 10:33:29 -0700
commit06c847e1184d477cfb622ff6710b48848bf0ffcf (patch)
treef21802263c73f5a2882bb6393746283519e5fbd0 /internal/api/api_test.go
parentdb1fff145dfefcb1643606d3c661e01b1e91bbbd (diff)
downloadgo-x-pkgsite-06c847e1184d477cfb622ff6710b48848bf0ffcf.tar.xz
internal/api: implement package symbols endpoint
- Introduce a new ServePackageSymbols handler that utilizes the new db.GetSymbols method. - Consolidated module resolution logic shared by ServePackages into resolveModulePath. - Updated Datasource with GetSymbols, which provides a more efficient way to retrieve all symbols for a package at a given version and build context. This differs from the existing getPackageSymbols, since that will return symbols for the latest release/compatible version. - Refactored packageSymbolQueryJoin into a generic helper by removing hardcoded version filters. This allows GetSymbols to query for any specific version, while getPackageSymbols was updated to explicitly include release-only filters to preserve its existing behavior. - GetSymbols still utilizes the same underlying packageSymbolQueryJoin, but it will also attempt to match the most relevant BuildContext for the query. It will also provide a mapping of parent and child symbols. Change-Id: Ib18d2511d24ac6bc5b75c7b3809c4ce126245036 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/754864 Auto-Submit: Ethan Lee <ethanalee@google.com> kokoro-CI: kokoro <noreply+kokoro@google.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'internal/api/api_test.go')
-rw-r--r--internal/api/api_test.go127
1 files changed, 124 insertions, 3 deletions
diff --git a/internal/api/api_test.go b/internal/api/api_test.go
index 595a3d25..50c11327 100644
--- a/internal/api/api_test.go
+++ b/internal/api/api_test.go
@@ -289,7 +289,7 @@ func TestServeModule(t *testing.T) {
w := httptest.NewRecorder()
err := ServeModule(w, r, ds)
- if err != nil {
+ if err != nil && w.Code != test.wantStatus {
t.Fatalf("ServeModule returned error: %v", err)
}
@@ -357,7 +357,7 @@ func TestServeModuleVersions(t *testing.T) {
w := httptest.NewRecorder()
err := ServeModuleVersions(w, r, ds)
- if err != nil {
+ if err != nil && w.Code != test.wantStatus {
t.Fatalf("ServeModuleVersions returned error: %v", err)
}
@@ -413,7 +413,7 @@ func TestServeModulePackages(t *testing.T) {
w := httptest.NewRecorder()
err := ServeModulePackages(w, r, ds)
- if err != nil {
+ if err != nil && w.Code != test.wantStatus {
t.Fatalf("ServeModulePackages returned error: %v", err)
}
@@ -589,3 +589,124 @@ func TestServeSearchPagination(t *testing.T) {
})
}
}
+
+func TestServePackageSymbols(t *testing.T) {
+ ctx := context.Background()
+ ds := fakedatasource.New()
+
+ const (
+ pkgPath = "example.com/pkg"
+ modulePath = "example.com"
+ version = "v1.0.0"
+ )
+
+ ds.MustInsertModule(ctx, &internal.Module{
+ ModuleInfo: internal.ModuleInfo{ModulePath: modulePath, Version: version},
+ Units: []*internal.Unit{{
+ UnitMeta: internal.UnitMeta{
+ Path: pkgPath,
+ ModuleInfo: internal.ModuleInfo{ModulePath: modulePath, Version: version},
+ Name: "pkg",
+ },
+ Symbols: map[internal.BuildContext][]*internal.Symbol{
+ {GOOS: "linux", GOARCH: "amd64"}: {
+ {
+ SymbolMeta: internal.SymbolMeta{Name: "LinuxSym", Kind: internal.SymbolKindFunction},
+ GOOS: "linux",
+ GOARCH: "amd64",
+ },
+ {
+ SymbolMeta: internal.SymbolMeta{Name: "T", Kind: internal.SymbolKindType},
+ GOOS: "linux",
+ GOARCH: "amd64",
+ Children: []*internal.SymbolMeta{
+ {Name: "T.M", Kind: internal.SymbolKindMethod, ParentName: "T"},
+ },
+ },
+ },
+ {GOOS: "windows", GOARCH: "amd64"}: {
+ {SymbolMeta: internal.SymbolMeta{Name: "WindowsSym", Kind: internal.SymbolKindFunction}, GOOS: "windows", GOARCH: "amd64"},
+ },
+ {GOOS: "js", GOARCH: "wasm"}: {
+ {SymbolMeta: internal.SymbolMeta{Name: "WasmSym", Kind: internal.SymbolKindFunction}, GOOS: "js", GOARCH: "wasm"},
+ },
+ },
+ }},
+ })
+
+ for _, test := range []struct {
+ name string
+ url string
+ wantStatus int
+ wantCount int
+ wantName string // Check name of the first symbol to verify build context
+ }{
+ {
+ name: "default best match (linux)",
+ url: "/v1/symbols/example.com/pkg?version=v1.0.0",
+ wantStatus: http.StatusOK,
+ wantCount: 2,
+ wantName: "LinuxSym",
+ },
+ {
+ name: "explicit linux",
+ url: "/v1/symbols/example.com/pkg?version=v1.0.0&goos=linux&goarch=amd64",
+ wantStatus: http.StatusOK,
+ wantCount: 2,
+ wantName: "LinuxSym",
+ },
+ {
+ name: "version latest",
+ url: "/v1/symbols/example.com/pkg?version=latest",
+ wantStatus: http.StatusOK,
+ wantCount: 2,
+ wantName: "LinuxSym",
+ },
+ {
+ name: "explicit windows",
+ url: "/v1/symbols/example.com/pkg?version=v1.0.0&goos=windows&goarch=amd64",
+ wantStatus: http.StatusOK,
+ wantCount: 1,
+ wantName: "WindowsSym",
+ },
+ {
+ name: "explicit wasm",
+ url: "/v1/symbols/example.com/pkg?version=v1.0.0&goos=js&goarch=wasm",
+ wantStatus: http.StatusOK,
+ wantCount: 1,
+ wantName: "WasmSym",
+ },
+ {
+ name: "not found build context",
+ url: "/v1/symbols/example.com/pkg?version=v1.0.0&goos=darwin&goarch=amd64",
+ wantStatus: http.StatusNotFound,
+ },
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ r := httptest.NewRequest("GET", test.url, nil)
+ w := httptest.NewRecorder()
+
+ err := ServePackageSymbols(w, r, ds)
+ if err != nil && w.Code != test.wantStatus {
+ t.Fatalf("ServePackageSymbols returned error: %v", err)
+ }
+
+ if w.Code != test.wantStatus {
+ t.Errorf("status = %d, want %d. Body: %s", w.Code, test.wantStatus, w.Body.String())
+ }
+
+ if test.wantStatus == http.StatusOK {
+ var got PaginatedResponse[Symbol]
+ if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
+ t.Fatalf("json.Unmarshal: %v", err)
+ }
+ if len(got.Items) != test.wantCount {
+ t.Errorf("count = %d, want %d", len(got.Items), test.wantCount)
+ }
+ if test.wantName != "" && got.Items[0].Name != test.wantName {
+ t.Errorf("first symbol = %q, want %q", got.Items[0].Name, test.wantName)
+ }
+ }
+ })
+ }
+}