diff options
| author | Julie Qiu <julie@golang.org> | 2020-09-30 21:58:12 -0400 |
|---|---|---|
| committer | Julie Qiu <julie@golang.org> | 2020-10-01 19:51:39 +0000 |
| commit | a7dc5d4d1de69cee58d38866ad5c42aa0025bb64 (patch) | |
| tree | 2ab84c2831d48c9474e98f8b6356d0316ff2c951 /internal/testing/sample | |
| parent | a8718fac7c742a3a1c61b8fddd6ef3ce1dd4d7c8 (diff) | |
| download | go-x-pkgsite-a7dc5d4d1de69cee58d38866ad5c42aa0025bb64.tar.xz | |
internal/testing/sample: move legacy functions to legacy.go
Any function that references a legacy struct is moved to legacy.go.
Pure code in motion.
These functions will either be deprecated or rewritten to not use a
legacy struct in a future CL.
For golang/go#39629
Change-Id: I8574fe3f90c1f17eec6b5f181feb42ee137bca5d
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/258597
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Diffstat (limited to 'internal/testing/sample')
| -rw-r--r-- | internal/testing/sample/legacy.go | 133 | ||||
| -rw-r--r-- | internal/testing/sample/sample.go | 114 |
2 files changed, 133 insertions, 114 deletions
diff --git a/internal/testing/sample/legacy.go b/internal/testing/sample/legacy.go new file mode 100644 index 00000000..59fa3fb0 --- /dev/null +++ b/internal/testing/sample/legacy.go @@ -0,0 +1,133 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package sample provides functionality for generating sample values of +// the types contained in the internal package. +package sample + +import ( + "fmt" + "path" + "strings" + + "golang.org/x/pkgsite/internal" + "golang.org/x/pkgsite/internal/licenses" + "golang.org/x/pkgsite/internal/stdlib" +) + +// LegacyPackage constructs a package with the given module path and suffix. +// +// If modulePath is the standard library, the package path is the +// suffix, which must not be empty. Otherwise, the package path +// is the concatenation of modulePath and suffix. +// +// The package name is last component of the package path. +func LegacyPackage(modulePath, suffix string) *internal.LegacyPackage { + p := constructFullPath(modulePath, suffix) + return &internal.LegacyPackage{ + Name: path.Base(p), + Path: p, + V1Path: internal.V1Path(p, modulePath), + Synopsis: Synopsis, + IsRedistributable: true, + Licenses: LicenseMetadata, + DocumentationHTML: DocumentationHTML, + Imports: Imports, + GOOS: GOOS, + GOARCH: GOARCH, + } +} + +func LegacyModuleInfo(modulePath, versionString string) *internal.LegacyModuleInfo { + mi := ModuleInfoReleaseType(modulePath, versionString) + return &internal.LegacyModuleInfo{ + ModuleInfo: *mi, + LegacyReadmeFilePath: ReadmeFilePath, + LegacyReadmeContents: ReadmeContents, + } +} + +func DefaultModule() *internal.Module { + return AddPackage( + Module(ModulePath, VersionString), + LegacyPackage(ModulePath, Suffix)) +} + +// Module creates a Module with the given path and version. +// The list of suffixes is used to create LegacyPackages within the module. +func Module(modulePath, version string, suffixes ...string) *internal.Module { + mi := LegacyModuleInfo(modulePath, version) + m := &internal.Module{ + LegacyModuleInfo: *mi, + LegacyPackages: nil, + Licenses: Licenses, + } + m.Units = []*internal.Unit{UnitForModuleRoot(mi, LicenseMetadata)} + for _, s := range suffixes { + lp := LegacyPackage(modulePath, s) + if s != "" { + AddPackage(m, lp) + } else { + m.LegacyPackages = append(m.LegacyPackages, lp) + u := UnitForPackage(lp, modulePath, version) + m.Units[0].Documentation = u.Documentation + m.Units[0].Name = u.Name + } + } + return m +} + +func AddPackage(m *internal.Module, p *internal.LegacyPackage) *internal.Module { + if m.ModulePath != stdlib.ModulePath && !strings.HasPrefix(p.Path, m.ModulePath) { + panic(fmt.Sprintf("package path %q not a prefix of module path %q", + p.Path, m.ModulePath)) + } + m.LegacyPackages = append(m.LegacyPackages, p) + AddUnit(m, UnitForPackage(p, m.ModulePath, m.Version)) + minLen := len(m.ModulePath) + if m.ModulePath == stdlib.ModulePath { + minLen = 1 + } + for pth := p.Path; len(pth) > minLen; pth = path.Dir(pth) { + found := false + for _, u := range m.Units { + if u.Path == pth { + found = true + break + } + } + if !found { + AddUnit(m, UnitEmpty(pth, m.ModulePath, m.Version)) + } + } + return m +} + +func UnitForModuleRoot(m *internal.LegacyModuleInfo, licenses []*licenses.Metadata) *internal.Unit { + u := &internal.Unit{ + UnitMeta: *UnitMeta(m.ModulePath, m.ModulePath, m.Version, "", m.IsRedistributable), + LicenseContents: Licenses, + } + if m.LegacyReadmeFilePath != "" { + u.Readme = &internal.Readme{ + Filepath: m.LegacyReadmeFilePath, + Contents: m.LegacyReadmeContents, + } + } + return u +} + +func UnitForPackage(pkg *internal.LegacyPackage, modulePath, version string) *internal.Unit { + return &internal.Unit{ + UnitMeta: *UnitMeta(pkg.Path, modulePath, version, pkg.Name, pkg.IsRedistributable), + Imports: pkg.Imports, + LicenseContents: Licenses, + Documentation: &internal.Documentation{ + Synopsis: pkg.Synopsis, + HTML: pkg.DocumentationHTML, + GOOS: pkg.GOOS, + GOARCH: pkg.GOARCH, + }, + } +} diff --git a/internal/testing/sample/sample.go b/internal/testing/sample/sample.go index 4f6cad03..de80a691 100644 --- a/internal/testing/sample/sample.go +++ b/internal/testing/sample/sample.go @@ -93,29 +93,6 @@ func NowTruncated() time.Time { return time.Now().Truncate(time.Microsecond) } -// LegacyPackage constructs a package with the given module path and suffix. -// -// If modulePath is the standard library, the package path is the -// suffix, which must not be empty. Otherwise, the package path -// is the concatenation of modulePath and suffix. -// -// The package name is last component of the package path. -func LegacyPackage(modulePath, suffix string) *internal.LegacyPackage { - p := constructFullPath(modulePath, suffix) - return &internal.LegacyPackage{ - Name: path.Base(p), - Path: p, - V1Path: internal.V1Path(p, modulePath), - Synopsis: Synopsis, - IsRedistributable: true, - Licenses: LicenseMetadata, - DocumentationHTML: DocumentationHTML, - Imports: Imports, - GOOS: GOOS, - GOARCH: GOARCH, - } -} - func PackageMeta(fullPath string) *internal.PackageMeta { return &internal.PackageMeta{ Path: fullPath, @@ -126,15 +103,6 @@ func PackageMeta(fullPath string) *internal.PackageMeta { } } -func LegacyModuleInfo(modulePath, versionString string) *internal.LegacyModuleInfo { - mi := ModuleInfoReleaseType(modulePath, versionString) - return &internal.LegacyModuleInfo{ - ModuleInfo: *mi, - LegacyReadmeFilePath: ReadmeFilePath, - LegacyReadmeContents: ReadmeContents, - } -} - func ModuleInfo(modulePath, versionString string) *internal.ModuleInfo { mi := ModuleInfoReleaseType(modulePath, versionString) return mi @@ -154,12 +122,6 @@ func ModuleInfoReleaseType(modulePath, versionString string) *internal.ModuleInf } } -func DefaultModule() *internal.Module { - return AddPackage( - Module(ModulePath, VersionString), - LegacyPackage(ModulePath, Suffix)) -} - func DefaultVersionMap() *internal.VersionMap { return &internal.VersionMap{ ModulePath: ModulePath, @@ -171,56 +133,6 @@ func DefaultVersionMap() *internal.VersionMap { } } -// Module creates a Module with the given path and version. -// The list of suffixes is used to create LegacyPackages within the module. -func Module(modulePath, version string, suffixes ...string) *internal.Module { - mi := LegacyModuleInfo(modulePath, version) - m := &internal.Module{ - LegacyModuleInfo: *mi, - LegacyPackages: nil, - Licenses: Licenses, - } - m.Units = []*internal.Unit{UnitForModuleRoot(mi, LicenseMetadata)} - for _, s := range suffixes { - lp := LegacyPackage(modulePath, s) - if s != "" { - AddPackage(m, lp) - } else { - m.LegacyPackages = append(m.LegacyPackages, lp) - u := UnitForPackage(lp, modulePath, version) - m.Units[0].Documentation = u.Documentation - m.Units[0].Name = u.Name - } - } - return m -} - -func AddPackage(m *internal.Module, p *internal.LegacyPackage) *internal.Module { - if m.ModulePath != stdlib.ModulePath && !strings.HasPrefix(p.Path, m.ModulePath) { - panic(fmt.Sprintf("package path %q not a prefix of module path %q", - p.Path, m.ModulePath)) - } - m.LegacyPackages = append(m.LegacyPackages, p) - AddUnit(m, UnitForPackage(p, m.ModulePath, m.Version)) - minLen := len(m.ModulePath) - if m.ModulePath == stdlib.ModulePath { - minLen = 1 - } - for pth := p.Path; len(pth) > minLen; pth = path.Dir(pth) { - found := false - for _, u := range m.Units { - if u.Path == pth { - found = true - break - } - } - if !found { - AddUnit(m, UnitEmpty(pth, m.ModulePath, m.Version)) - } - } - return m -} - func AddUnit(m *internal.Module, u *internal.Unit) { for _, e := range m.Units { if e.Path == u.Path { @@ -249,32 +161,6 @@ func UnitEmpty(path, modulePath, version string) *internal.Unit { } } -func UnitForModuleRoot(m *internal.LegacyModuleInfo, licenses []*licenses.Metadata) *internal.Unit { - u := &internal.Unit{ - UnitMeta: *UnitMeta(m.ModulePath, m.ModulePath, m.Version, "", m.IsRedistributable), - LicenseContents: Licenses, - } - u.Readme = &internal.Readme{ - Filepath: ReadmeFilePath, - Contents: ReadmeContents, - } - return u -} - -func UnitForPackage(pkg *internal.LegacyPackage, modulePath, version string) *internal.Unit { - return &internal.Unit{ - UnitMeta: *UnitMeta(pkg.Path, modulePath, version, pkg.Name, pkg.IsRedistributable), - Imports: pkg.Imports, - LicenseContents: Licenses, - Documentation: &internal.Documentation{ - Synopsis: pkg.Synopsis, - HTML: pkg.DocumentationHTML, - GOOS: pkg.GOOS, - GOARCH: pkg.GOARCH, - }, - } -} - func UnitMeta(path, modulePath, version, name string, isRedistributable bool) *internal.UnitMeta { return &internal.UnitMeta{ ModulePath: modulePath, |
