From 3c8abd6a69a5031ca55e55d16e725476f1d69cba Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Tue, 7 Apr 2026 14:44:16 +0000 Subject: internal/api: add support for readme and licenses in ServeModule - Utilize conditional fieldset in GetUnit to efficiently retrieve readme and license fields. - Modify Module type to include HasGoMod. Change-Id: Id22ac3f2485392749742332701d2e913f047b3da Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/763401 kokoro-CI: kokoro Auto-Submit: Ethan Lee LUCI-TryBot-Result: Go LUCI Reviewed-by: Jonathan Amsterdam --- internal/api/api_test.go | 80 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 14 deletions(-) (limited to 'internal/api/api_test.go') diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 15659c37..36066f46 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -230,20 +230,34 @@ func TestServeModule(t *testing.T) { version = "v1.2.3" ) + mi1 := sample.ModuleInfo(modulePath, version) + mi1.LatestVersion = "v1.2.4" + mi1.HasGoMod = true + ds.MustInsertModule(ctx, &internal.Module{ - ModuleInfo: internal.ModuleInfo{ - ModulePath: modulePath, - Version: version, - }, + ModuleInfo: *mi1, + Licenses: sample.Licenses(), Units: []*internal.Unit{{ UnitMeta: internal.UnitMeta{ - Path: modulePath, - ModuleInfo: internal.ModuleInfo{ - ModulePath: modulePath, - Version: version, - }, + Path: modulePath, + ModuleInfo: *mi1, + }, + Readme: &internal.Readme{Filepath: "README.md", Contents: "Hello world"}, + Licenses: sample.LicenseMetadata(), + }}, + }) + + mi2 := sample.ModuleInfo(modulePath, "v1.2.4") + mi2.LatestVersion = "v1.2.4" + mi2.HasGoMod = true + + ds.MustInsertModule(ctx, &internal.Module{ + ModuleInfo: *mi2, + Units: []*internal.Unit{{ + UnitMeta: internal.UnitMeta{ + Path: modulePath, + ModuleInfo: *mi2, }, - Readme: &internal.Readme{Filepath: "README.md", Contents: "Hello world"}, }}, }) @@ -258,8 +272,24 @@ func TestServeModule(t *testing.T) { url: "/v1/module/example.com?version=v1.2.3", wantStatus: http.StatusOK, want: &Module{ - Path: modulePath, - Version: version, + Path: modulePath, + Version: version, + IsRedistributable: true, + HasGoMod: true, + RepoURL: "https://example.com", + }, + }, + { + name: "latest module metadata", + url: "/v1/module/example.com?version=v1.2.4", + wantStatus: http.StatusOK, + want: &Module{ + Path: modulePath, + Version: "v1.2.4", + IsLatest: true, + IsRedistributable: true, + HasGoMod: true, + RepoURL: "https://example.com", }, }, { @@ -273,14 +303,36 @@ func TestServeModule(t *testing.T) { url: "/v1/module/example.com?version=v1.2.3&readme=true", wantStatus: http.StatusOK, want: &Module{ - Path: modulePath, - Version: version, + Path: modulePath, + Version: version, + IsRedistributable: true, + HasGoMod: true, + RepoURL: "https://example.com", Readme: &Readme{ Filepath: "README.md", Contents: "Hello world", }, }, }, + { + name: "module with licenses", + url: "/v1/module/example.com?version=v1.2.3&licenses=true", + wantStatus: http.StatusOK, + want: &Module{ + Path: modulePath, + Version: version, + IsRedistributable: true, + HasGoMod: true, + RepoURL: "https://example.com", + Licenses: []License{ + { + Types: []string{"MIT"}, + FilePath: "LICENSE", + Contents: "Lorem Ipsum", + }, + }, + }, + }, } { t.Run(test.name, func(t *testing.T) { r := httptest.NewRequest("GET", test.url, nil) -- cgit v1.3