Skip to content

Commit

Permalink
internal: embed ModuleInfo into UnitMeta
Browse files Browse the repository at this point in the history
UnitMeta had most of the fields of ModuleInfo, but did not embed
it. Now that we have added four more fields to ModuleInfo for
deprecation and retraction, it makes sense to embed it rather than
duplicate those fields.

For golang/go#43265
For golang/go#41321

Change-Id: I20e2b922b49c7873a5535745d644631123de37cd
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/296209
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
  • Loading branch information
jba committed Feb 25, 2021
1 parent 9e8d18f commit 25c6b92
Show file tree
Hide file tree
Showing 22 changed files with 321 additions and 288 deletions.
1 change: 1 addition & 0 deletions internal/fetch/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type fetchFunc func(t *testing.T, withLicenseDetector bool, ctx context.Context,
func TestMain(m *testing.M) {
dochtml.LoadTemplates(templateSource)
testModules = proxy.LoadTestModules("../proxy/testdata")
licenses.OmitExceptions = true
os.Exit(m.Run())
}

Expand Down
94 changes: 56 additions & 38 deletions internal/fetch/fetchdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ var moduleOnePackage = &testModule{
fr: &FetchResult{
Module: &internal.Module{
ModuleInfo: internal.ModuleInfo{
ModulePath: "example.com/single",
HasGoMod: true,
SourceInfo: source.NewGitHubInfo("https://example.com/single", "", "v1.0.0"),
ModulePath: "example.com/single",
HasGoMod: true,
SourceInfo: source.NewGitHubInfo("https://example.com/single", "", "v1.0.0"),
IsRedistributable: true,
},
Units: singleUnits,
},
Expand All @@ -91,9 +92,10 @@ var moduleNoGoMod = &testModule{
fr: &FetchResult{
Module: &internal.Module{
ModuleInfo: internal.ModuleInfo{
ModulePath: "example.com/nogo",
HasGoMod: false,
SourceInfo: source.NewGitHubInfo("https://example.com/nogo", "", "v1.0.0"),
ModulePath: "example.com/nogo",
HasGoMod: false,
SourceInfo: source.NewGitHubInfo("https://example.com/nogo", "", "v1.0.0"),
IsRedistributable: true,
},
Units: []*internal.Unit{
{
Expand Down Expand Up @@ -126,9 +128,10 @@ var moduleMultiPackage = &testModule{
fr: &FetchResult{
Module: &internal.Module{
ModuleInfo: internal.ModuleInfo{
ModulePath: "example.com/multi",
HasGoMod: true,
SourceInfo: source.NewGitHubInfo("https://example.com/multi", "", "v1.0.0"),
ModulePath: "example.com/multi",
HasGoMod: true,
SourceInfo: source.NewGitHubInfo("https://example.com/multi", "", "v1.0.0"),
IsRedistributable: true,
},
Units: []*internal.Unit{
{
Expand Down Expand Up @@ -216,7 +219,8 @@ var moduleBadPackages = &testModule{
Status: derrors.ToStatus(derrors.HasIncompletePackages),
Module: &internal.Module{
ModuleInfo: internal.ModuleInfo{
ModulePath: "bad.mod/module",
ModulePath: "bad.mod/module",
IsRedistributable: true,
},
Units: []*internal.Unit{
{
Expand Down Expand Up @@ -275,9 +279,10 @@ var moduleBuildConstraints = &testModule{
Status: derrors.ToStatus(derrors.HasIncompletePackages),
Module: &internal.Module{
ModuleInfo: internal.ModuleInfo{
ModulePath: "example.com/build-constraints",
HasGoMod: true,
SourceInfo: source.NewGitHubInfo("https://example.com/build-constraints", "", "v1.0.0"),
ModulePath: "example.com/build-constraints",
HasGoMod: true,
SourceInfo: source.NewGitHubInfo("https://example.com/build-constraints", "", "v1.0.0"),
IsRedistributable: true,
},
Units: []*internal.Unit{
{
Expand Down Expand Up @@ -364,9 +369,10 @@ var moduleNonRedist = &testModule{
fr: &FetchResult{
Module: &internal.Module{
ModuleInfo: internal.ModuleInfo{
ModulePath: "example.com/nonredist",
HasGoMod: true,
SourceInfo: source.NewGitHubInfo("https://example.com/nonredist", "", "v1.0.0"),
ModulePath: "example.com/nonredist",
HasGoMod: true,
SourceInfo: source.NewGitHubInfo("https://example.com/nonredist", "", "v1.0.0"),
IsRedistributable: true,
},
Units: []*internal.Unit{
{
Expand Down Expand Up @@ -508,8 +514,9 @@ var moduleDocTest = &testModule{
GoModPath: "doc.test",
Module: &internal.Module{
ModuleInfo: internal.ModuleInfo{
ModulePath: "doc.test",
HasGoMod: false,
ModulePath: "doc.test",
HasGoMod: false,
IsRedistributable: true,
},
Units: []*internal.Unit{
{
Expand Down Expand Up @@ -554,8 +561,9 @@ var moduleDocTooLarge = &testModule{
GoModPath: "bigdoc.test",
Module: &internal.Module{
ModuleInfo: internal.ModuleInfo{
ModulePath: "bigdoc.test",
HasGoMod: false,
ModulePath: "bigdoc.test",
HasGoMod: false,
IsRedistributable: true,
},
Units: []*internal.Unit{
{
Expand Down Expand Up @@ -602,8 +610,9 @@ var moduleWasm = &testModule{
fr: &FetchResult{
Module: &internal.Module{
ModuleInfo: internal.ModuleInfo{
ModulePath: "github.com/my/module/js",
SourceInfo: source.NewGitHubInfo("https://github.com/my/module", "js", "js/v1.0.0"),
ModulePath: "github.com/my/module/js",
SourceInfo: source.NewGitHubInfo("https://github.com/my/module", "js", "js/v1.0.0"),
IsRedistributable: true,
},
Units: []*internal.Unit{
{
Expand Down Expand Up @@ -654,20 +663,24 @@ var moduleStdMaster = &testModule{
ResolvedVersion: stdlib.TestVersion,
Module: &internal.Module{
ModuleInfo: internal.ModuleInfo{
ModulePath: stdlib.ModulePath,
Version: stdlib.TestVersion,
CommitTime: stdlib.TestCommitTime,
HasGoMod: true,
SourceInfo: source.NewStdlibInfo("master"),
ModulePath: stdlib.ModulePath,
Version: stdlib.TestVersion,
CommitTime: stdlib.TestCommitTime,
HasGoMod: true,
SourceInfo: source.NewStdlibInfo("master"),
IsRedistributable: true,
},
Units: []*internal.Unit{
{
UnitMeta: internal.UnitMeta{
Path: "errors",
Name: "errors",
IsRedistributable: true,
Version: stdlib.TestVersion,
ModulePath: stdlib.ModulePath,
ModuleInfo: internal.ModuleInfo{
Version: stdlib.TestVersion,
ModulePath: stdlib.ModulePath,
IsRedistributable: true,
},
},
Documentation: []*internal.Documentation{
{
Expand All @@ -691,8 +704,11 @@ var moduleStdMaster = &testModule{
UnitMeta: internal.UnitMeta{
Path: "std",
IsRedistributable: true,
Version: stdlib.TestVersion,
ModulePath: "std",
ModuleInfo: internal.ModuleInfo{
Version: stdlib.TestVersion,
ModulePath: "std",
IsRedistributable: true,
},
},
Readme: &internal.Readme{Filepath: "README.md", Contents: "# The Go Programming Language\n"},
},
Expand All @@ -719,11 +735,12 @@ var moduleStd = &testModule{
fr: &FetchResult{
Module: &internal.Module{
ModuleInfo: internal.ModuleInfo{
ModulePath: "std",
Version: "v1.12.5",
CommitTime: stdlib.TestCommitTime,
HasGoMod: true,
SourceInfo: source.NewStdlibInfo("v1.12.5"),
ModulePath: "std",
Version: "v1.12.5",
CommitTime: stdlib.TestCommitTime,
HasGoMod: true,
SourceInfo: source.NewStdlibInfo("v1.12.5"),
IsRedistributable: true,
},
Units: []*internal.Unit{
{
Expand Down Expand Up @@ -2365,8 +2382,9 @@ package example_test
GoModPath: path,
Module: &internal.Module{
ModuleInfo: internal.ModuleInfo{
ModulePath: path,
HasGoMod: false,
ModulePath: path,
HasGoMod: false,
IsRedistributable: true,
},
Units: []*internal.Unit{
{
Expand Down
7 changes: 5 additions & 2 deletions internal/fetch/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ func cleanFetchResult(t *testing.T, fr *FetchResult) *FetchResult {
shouldSetPVS := (fr.PackageVersionStates == nil)
for _, u := range fr.Module.Units {
u.UnitMeta = internal.UnitMeta{
ModulePath: fr.Module.ModulePath,
Version: fr.Module.Version,
ModuleInfo: internal.ModuleInfo{
ModulePath: fr.Module.ModulePath,
Version: fr.Module.Version,
IsRedistributable: fr.Module.IsRedistributable,
},
Path: u.Path,
Name: u.Name,
IsRedistributable: u.IsRedistributable,
Expand Down
7 changes: 5 additions & 2 deletions internal/fetch/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ func moduleUnits(modulePath, version string,
}
dir := &internal.Unit{
UnitMeta: internal.UnitMeta{
ModulePath: modulePath,
Version: version,
ModuleInfo: internal.ModuleInfo{
ModulePath: modulePath,
Version: version,
IsRedistributable: d.ModuleIsRedistributable(),
},
Path: dirPath,
IsRedistributable: isRedist,
Licenses: meta,
Expand Down
8 changes: 5 additions & 3 deletions internal/frontend/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ type ImportsDetails struct {
// pkgPath, modulePath and version from the database and returns a ImportsDetails.
func fetchImportsDetails(ctx context.Context, ds internal.DataSource, pkgPath, modulePath, resolvedVersion string) (_ *ImportsDetails, err error) {
u, err := ds.GetUnit(ctx, &internal.UnitMeta{
Path: pkgPath,
ModulePath: modulePath,
Version: resolvedVersion,
Path: pkgPath,
ModuleInfo: internal.ModuleInfo{
ModulePath: modulePath,
Version: resolvedVersion,
},
}, internal.WithImports)
if err != nil {
return nil, err
Expand Down
8 changes: 5 additions & 3 deletions internal/frontend/license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ func TestFetchLicensesDetails(t *testing.T) {
wantDetails := &LicensesDetails{Licenses: transformLicenses(
test.modulePath, test.version, test.want)}
got, err := fetchLicensesDetails(ctx, testDB, &internal.UnitMeta{
Path: test.fullPath,
ModulePath: test.modulePath,
Version: test.version,
Path: test.fullPath,
ModuleInfo: internal.ModuleInfo{
ModulePath: test.modulePath,
Version: test.version,
},
})
if err != nil {
t.Fatal(err)
Expand Down
18 changes: 14 additions & 4 deletions internal/frontend/readme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ func TestReadme(t *testing.T) {
{
name: "relative image markdown is made absolute for GitHub",
unit: &internal.Unit{
UnitMeta: internal.UnitMeta{SourceInfo: source.NewGitHubInfo("http://github.com/golang/go", "", "master")},
UnitMeta: internal.UnitMeta{
ModuleInfo: internal.ModuleInfo{
SourceInfo: source.NewGitHubInfo("http://github.com/golang/go", "", "master"),
},
},
},
readme: &internal.Readme{
Filepath: "README.md",
Expand All @@ -202,7 +206,11 @@ func TestReadme(t *testing.T) {
{
name: "relative image markdown is made absolute for GitHub, .git removed from repo URL",
unit: &internal.Unit{
UnitMeta: internal.UnitMeta{SourceInfo: source.NewGitHubInfo("https://github.com/robpike/ivy.git", "", "v0.1.0")},
UnitMeta: internal.UnitMeta{
ModuleInfo: internal.ModuleInfo{
SourceInfo: source.NewGitHubInfo("https://github.com/robpike/ivy.git", "", "v0.1.0"),
},
},
},
readme: &internal.Readme{
Filepath: "README.md",
Expand Down Expand Up @@ -294,8 +302,10 @@ func TestReadme(t *testing.T) {
name: "image link with bad URL",
unit: &internal.Unit{
UnitMeta: internal.UnitMeta{
Version: "v1.2.3",
SourceInfo: source.NewGitHubInfo("https://github.com/some/<script>", "", "v1.2.3"),
ModuleInfo: internal.ModuleInfo{
Version: "v1.2.3",
SourceInfo: source.NewGitHubInfo("https://github.com/some/<script>", "", "v1.2.3"),
},
},
},
readme: &internal.Readme{
Expand Down
18 changes: 12 additions & 6 deletions internal/frontend/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ func TestFetchSearchPage(t *testing.T) {
Units: []*internal.Unit{
{
UnitMeta: internal.UnitMeta{
ModuleInfo: internal.ModuleInfo{
ModulePath: "github.com/mod/foo",
Version: "v1.0.0",
CommitTime: now,
IsRedistributable: true,
},
Name: "foo",
Path: "github.com/mod/foo",
Licenses: sample.LicenseMetadata(),
CommitTime: now,
ModulePath: "github.com/mod/foo",
Version: "v1.0.0",
IsRedistributable: true,
},
Documentation: []*internal.Documentation{{
Expand All @@ -65,12 +68,15 @@ func TestFetchSearchPage(t *testing.T) {
Units: []*internal.Unit{
{
UnitMeta: internal.UnitMeta{
ModuleInfo: internal.ModuleInfo{
CommitTime: now,
ModulePath: "github.com/mod/bar",
Version: "v1.0.0",
IsRedistributable: true,
},
Name: "bar",
Path: "github.com/mod/bar",
Licenses: sample.LicenseMetadata(),
CommitTime: now,
ModulePath: "github.com/mod/bar",
Version: "v1.0.0",
IsRedistributable: true,
},
Documentation: []*internal.Documentation{{
Expand Down
2 changes: 1 addition & 1 deletion internal/frontend/unit_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestGetNestedModules(t *testing.T) {
t.Run(test.modulePath, func(t *testing.T) {
got, err := getNestedModules(ctx, testDB, &internal.UnitMeta{
Path: test.modulePath,
ModulePath: test.modulePath,
ModuleInfo: internal.ModuleInfo{ModulePath: test.modulePath},
}, test.subdirectories)
if err != nil {
t.Fatal(err)
Expand Down
3 changes: 1 addition & 2 deletions internal/frontend/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ func TestCanonicalURLPath(t *testing.T) {
} {
um := &internal.UnitMeta{
Path: test.path,
ModulePath: test.modpath,
Version: test.version,
ModuleInfo: internal.ModuleInfo{ModulePath: test.modpath, Version: test.version},
}
got := canonicalURLPath(um)
if got != test.want {
Expand Down
11 changes: 7 additions & 4 deletions internal/localdatasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,13 @@ func (ds *DataSource) GetUnitMeta(ctx context.Context, path, requestedModulePath
ds.mu.Unlock()

um := &internal.UnitMeta{
Path: path,
ModulePath: requestedModulePath,
Version: fetch.LocalVersion,
CommitTime: fetch.LocalCommitTime,
Path: path,
ModuleInfo: internal.ModuleInfo{
ModulePath: requestedModulePath,
Version: fetch.LocalVersion,
CommitTime: fetch.LocalCommitTime,
IsRedistributable: module.IsRedistributable,
},
}

for _, u := range module.Units {
Expand Down
Loading

0 comments on commit 25c6b92

Please sign in to comment.