diff options
| author | Jonathan Amsterdam <jba@google.com> | 2026-04-09 08:10:31 -0400 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2026-04-09 12:22:10 -0700 |
| commit | 654ef90febc3cd4bbc261b97fbea7a6c45e1d26d (patch) | |
| tree | 2212d51f016accb4bc88199011969604828b0dce | |
| parent | 845e9f3a89f1a84a47606ae3b50cb3fb91b2d95a (diff) | |
| download | go-x-pkgsite-654ef90febc3cd4bbc261b97fbea7a6c45e1d26d.tar.xz | |
internal/api: test licenses and imports params
Test the "licenses" and "imports" query params for the packages route.
Change-Id: I46c7dd18f0eb2e104d124d2d724039f920247e5b
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/764540
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
TryBot-Bypass: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Ethan Lee <ethanalee@google.com>
Auto-Submit: Jonathan Amsterdam <jba@google.com>
| -rw-r--r-- | internal/api/api_test.go | 39 | ||||
| -rw-r--r-- | internal/testing/fakedatasource/fakedatasource.go | 15 |
2 files changed, 52 insertions, 2 deletions
diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 5de0ab6e..0a66895c 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -39,6 +39,7 @@ func TestServePackage(t *testing.T) { Version: version, LatestVersion: "v1.2.4", }, + Licenses: sample.Licenses(), Units: []*internal.Unit{{ UnitMeta: internal.UnitMeta{ Path: "example.com/pkg", @@ -50,6 +51,8 @@ func TestServePackage(t *testing.T) { Name: "pkg", }, Documentation: []*internal.Documentation{sample.Documentation("linux", "amd64", sample.DocContents)}, + Licenses: sample.LicenseMetadata(), + Imports: []string{pkgPath}, }}, }) @@ -283,6 +286,42 @@ func TestServePackage(t *testing.T) { Docs: "", }, }, + { + name: "licenses", + url: "/v1/package/example.com/pkg?version=v1.2.3&licenses=true", + wantStatus: http.StatusOK, + want: &Package{ + Path: "example.com/pkg", + ModulePath: "example.com", + ModuleVersion: version, + Synopsis: "This is a package synopsis for GOOS=linux, GOARCH=amd64", + IsLatest: false, + GOOS: "linux", + GOARCH: "amd64", + Licenses: []License{ + { + Types: []string{sample.LicenseType}, + FilePath: sample.LicenseFilePath, + Contents: "Lorem Ipsum", + }, + }, + }, + }, + { + name: "imports", + url: "/v1/package/example.com/pkg?version=v1.2.3&imports=true", + wantStatus: http.StatusOK, + want: &Package{ + Path: "example.com/pkg", + ModulePath: "example.com", + ModuleVersion: version, + Synopsis: "This is a package synopsis for GOOS=linux, GOARCH=amd64", + IsLatest: false, + GOOS: "linux", + GOARCH: "amd64", + Imports: []string{pkgPath}, + }, + }, } { t.Run(test.name, func(t *testing.T) { r := httptest.NewRequest("GET", test.url, nil) diff --git a/internal/testing/fakedatasource/fakedatasource.go b/internal/testing/fakedatasource/fakedatasource.go index 3da3b3bf..e621e2e8 100644 --- a/internal/testing/fakedatasource/fakedatasource.go +++ b/internal/testing/fakedatasource/fakedatasource.go @@ -135,10 +135,22 @@ func (ds *FakeDataSource) GetUnit(ctx context.Context, um *internal.UnitMeta, fi if u == nil { return nil, fmt.Errorf("import path %s not found in module %s: %w", um.Path, um.ModulePath, derrors.NotFound) } + + // Clone the unit, because we might modify it. + // A shallow copy suffices. + u2 := *u + // Return licenses only if requested. + if fields&internal.WithLicenses == 0 { + u2.Licenses = nil + u2.LicenseContents = nil + } + // Return imports only if requested. + if fields&internal.WithImports == 0 { + u2.Imports = nil + } // Return only the Documentation matching the given BuildContext, if any. // Since we cache the module and its units, we have to copy this unit before we modify it. // It can be a shallow copy, since we're only modifying the Unit.Documentation field. - u2 := *u if fields&internal.WithDocsSource != 0 { if d := internal.DocumentationForBuildContext(u.Documentation, bc); d != nil { u2.Documentation = []*internal.Documentation{d} @@ -186,7 +198,6 @@ func (ds *FakeDataSource) findModule(pkgPath, modulePath, version string) *inter if m := ds.getModule(modulePath, version); m != nil { return m } - } return nil } |
