aboutsummaryrefslogtreecommitdiff
path: root/internal/postgres
diff options
context:
space:
mode:
authorJulie Qiu <julie@golang.org>2020-08-31 21:19:41 -0400
committerJulie Qiu <julie@golang.org>2020-09-01 14:40:41 +0000
commita327f4f5ecaffa6d82bfe9cf767b10104c2dcd28 (patch)
tree5d980802ad466f0fa2841f770cd95919855ba0d1 /internal/postgres
parent4d4b7bf93903cfc8e05824502b05a2721859f292 (diff)
downloadgo-x-pkgsite-a327f4f5ecaffa6d82bfe9cf767b10104c2dcd28.tar.xz
internal: replace Unit.DirectoryMeta with Unit.PathInfo
Unit.DirectoryMeta is replaced with Unit.PathInfo DirectoryMeta isn't necessary and will be deleted in a future CL. For golang/go#39629 Change-Id: I521a062207d3f48592d6e949306a015458d1398d Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/251957 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/postgres')
-rw-r--r--internal/postgres/insert_module_test.go15
-rw-r--r--internal/postgres/path_test.go4
-rw-r--r--internal/postgres/unit.go28
-rw-r--r--internal/postgres/unit_test.go53
4 files changed, 33 insertions, 67 deletions
diff --git a/internal/postgres/insert_module_test.go b/internal/postgres/insert_module_test.go
index 482c5975..2b857915 100644
--- a/internal/postgres/insert_module_test.go
+++ b/internal/postgres/insert_module_test.go
@@ -41,7 +41,7 @@ func TestInsertModule(t *testing.T) {
},
{
name: "valid test with internal package",
- module: sample.Module(sample.ModulePath, sample.VersionString, "internal/ffoo"),
+ module: sample.Module(sample.ModulePath, sample.VersionString, "internal/foo"),
},
{
name: "valid test with go.mod missing",
@@ -77,7 +77,6 @@ func TestInsertModule(t *testing.T) {
if err := testDB.InsertModule(ctx, test.module); err != nil {
t.Fatal(err)
}
-
checkModule(ctx, t, test.module)
})
}
@@ -111,12 +110,7 @@ func checkModule(ctx context.Context, t *testing.T, want *internal.Module) {
}
for _, dir := range want.Units {
- pathInfo := &internal.PathInfo{
- Path: dir.Path,
- ModulePath: want.ModulePath,
- Version: want.Version,
- }
- got, err := testDB.GetUnit(ctx, pathInfo, internal.AllFields)
+ got, err := testDB.GetUnit(ctx, &dir.PathInfo, internal.AllFields)
if err != nil {
t.Fatal(err)
}
@@ -126,7 +120,6 @@ func checkModule(ctx context.Context, t *testing.T, want *internal.Module) {
Filepath: sample.ReadmeFilePath,
Contents: sample.ReadmeContents,
}
- dir.ModuleInfo = want.ModuleInfo
if dir.Package != nil {
dir.Name = dir.Package.Name
}
@@ -134,12 +127,11 @@ func checkModule(ctx context.Context, t *testing.T, want *internal.Module) {
opts := cmp.Options{
cmpopts.IgnoreFields(internal.LegacyModuleInfo{}, "LegacyReadmeFilePath"),
cmpopts.IgnoreFields(internal.LegacyModuleInfo{}, "LegacyReadmeContents"),
- cmpopts.IgnoreFields(internal.DirectoryMeta{}, "PathID"),
cmpopts.IgnoreFields(licenses.Metadata{}, "Coverage"),
cmp.AllowUnexported(source.Info{}, safehtml.HTML{}),
}
if diff := cmp.Diff(wantd, got, opts); diff != "" {
- t.Errorf("testDB.getDirectory(%q, %q) mismatch (-want +got):\n%s", dir.Path, want.Version, diff)
+ t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
}
@@ -240,7 +232,6 @@ func TestUpsertModule(t *testing.T) {
if err := testDB.InsertModule(ctx, m); err != nil {
t.Fatal(err)
}
-
// The changes should have been saved.
checkModule(ctx, t, m)
}
diff --git a/internal/postgres/path_test.go b/internal/postgres/path_test.go
index c77b6140..35e3079a 100644
--- a/internal/postgres/path_test.go
+++ b/internal/postgres/path_test.go
@@ -56,7 +56,7 @@ func TestGetPathInfo(t *testing.T) {
}
for d := pkgPath; d != "." && len(d) >= len(testModule.module); d = path.Dir(d) {
dir := &internal.Unit{
- DirectoryMeta: internal.DirectoryMeta{
+ PathInfo: internal.PathInfo{
Path: d,
Licenses: sample.LicenseMetadata,
},
@@ -203,13 +203,13 @@ func TestGetPathInfo(t *testing.T) {
test.want.Name,
test.want.IsRedistributable,
)
+ test.want.CommitTime = sample.CommitTime
got, err := testDB.GetPathInfo(ctx, test.path, test.module, test.version)
if err != nil {
t.Fatal(err)
}
opts := []cmp.Option{
cmpopts.IgnoreFields(licenses.Metadata{}, "Coverage"),
- cmpopts.IgnoreFields(internal.PathInfo{}, "CommitTime"),
cmp.AllowUnexported(source.Info{}, safehtml.HTML{}),
}
if diff := cmp.Diff(test.want, got, opts...); diff != "" {
diff --git a/internal/postgres/unit.go b/internal/postgres/unit.go
index 0cfac491..df5b5460 100644
--- a/internal/postgres/unit.go
+++ b/internal/postgres/unit.go
@@ -91,17 +91,7 @@ func (db *DB) GetUnit(ctx context.Context, pi *internal.PathInfo, fields interna
return nil, err
}
- u := &internal.Unit{
- DirectoryMeta: internal.DirectoryMeta{
- Path: pi.Path,
- PathID: pathID,
- IsRedistributable: pi.IsRedistributable,
- ModuleInfo: internal.ModuleInfo{
- ModulePath: pi.ModulePath,
- Version: pi.Version,
- },
- },
- }
+ u := &internal.Unit{PathInfo: *pi}
if fields&internal.WithReadme != 0 {
readme, err := db.getReadme(ctx, u.ModulePath, u.Version)
if err != nil && !errors.Is(err, derrors.NotFound) {
@@ -110,19 +100,20 @@ func (db *DB) GetUnit(ctx context.Context, pi *internal.PathInfo, fields interna
u.Readme = readme
}
if fields&internal.WithDocumentation != 0 {
- doc, err := db.getDocumentation(ctx, u.PathID)
+ doc, err := db.getDocumentation(ctx, pathID)
if err != nil && !errors.Is(err, derrors.NotFound) {
return nil, err
}
if doc != nil {
u.Package = &internal.Package{
Path: u.Path,
+ Name: u.Name,
Documentation: doc,
}
}
}
if fields&internal.WithImports != 0 {
- imports, err := db.getImports(ctx, u.PathID)
+ imports, err := db.getImports(ctx, pathID)
if err != nil {
return nil, err
}
@@ -131,16 +122,13 @@ func (db *DB) GetUnit(ctx context.Context, pi *internal.PathInfo, fields interna
}
}
if fields == internal.AllFields {
- dmeta, err := db.GetDirectoryMeta(ctx, pi.Path, pi.ModulePath, pi.Version)
- if err != nil {
- return nil, err
- }
- u.DirectoryMeta = *dmeta
if u.Name != "" {
if u.Package == nil {
- u.Package = &internal.Package{Path: u.Path}
+ u.Package = &internal.Package{
+ Path: u.Path,
+ }
}
- u.Package.Name = dmeta.Name
+ u.Package.Name = u.Name
}
}
if !db.bypassLicenseCheck {
diff --git a/internal/postgres/unit_test.go b/internal/postgres/unit_test.go
index 66d001e6..df19493e 100644
--- a/internal/postgres/unit_test.go
+++ b/internal/postgres/unit_test.go
@@ -276,13 +276,6 @@ func TestGetUnit(t *testing.T) {
want: unit("archive/zip", stdlib.ModulePath, "v1.13.4", nil, newPackage("zip", "archive/zip")),
},
{
- name: "stdlib package - incomplete last element",
- path: "archive/zi",
- modulePath: stdlib.ModulePath,
- version: "v1.13.4",
- wantNotFoundErr: true,
- },
- {
name: "stdlib - internal directory",
path: "cmd/internal",
modulePath: stdlib.ModulePath,
@@ -313,14 +306,13 @@ func TestGetUnit(t *testing.T) {
},
} {
t.Run(test.name, func(t *testing.T) {
- pathInfo := &internal.PathInfo{
- Path: test.path,
- ModulePath: test.modulePath,
- Version: test.version,
- }
- if test.want != nil {
- pathInfo.Name = test.want.Name
- }
+ pathInfo := sample.PathInfo(
+ test.path,
+ test.modulePath,
+ test.version,
+ test.want.Name,
+ true,
+ )
got, err := testDB.GetUnit(ctx, pathInfo, internal.AllFields)
if test.wantNotFoundErr {
if !errors.Is(err, derrors.NotFound) {
@@ -346,6 +338,7 @@ func TestGetUnit(t *testing.T) {
if test.want.Package != nil {
test.want.Imports = sample.Imports
}
+ test.want.SourceInfo = pathInfo.SourceInfo
if diff := cmp.Diff(test.want, got, opts...); diff != "" {
t.Errorf("mismatch (-want, +got):\n%s", diff)
}
@@ -367,19 +360,11 @@ func TestGetUnitFieldSet(t *testing.T) {
cleanFields := func(u *internal.Unit, fields internal.FieldSet) {
// Remove fields based on the FieldSet specified.
- u.DirectoryMeta = internal.DirectoryMeta{
- Path: u.Path,
- IsRedistributable: true,
- ModuleInfo: internal.ModuleInfo{
- ModulePath: u.ModulePath,
- Version: u.Version,
- },
- }
if fields&internal.WithImports != 0 {
u.Imports = sample.Imports
}
if u.Package != nil {
- u.Package.Name = ""
+ u.Package.Name = u.Name
}
}
@@ -411,12 +396,13 @@ func TestGetUnitFieldSet(t *testing.T) {
},
} {
t.Run(test.name, func(t *testing.T) {
- pathInfo := &internal.PathInfo{
- Path: test.want.Path,
- ModulePath: test.want.ModulePath,
- Version: test.want.Version,
- IsRedistributable: test.want.IsRedistributable,
- }
+ pathInfo := sample.PathInfo(
+ test.want.Path,
+ test.want.ModulePath,
+ test.want.Version,
+ test.want.Name,
+ true,
+ )
got, err := testDB.GetUnit(ctx, pathInfo, test.fields)
if err != nil {
t.Fatal(err)
@@ -427,6 +413,7 @@ func TestGetUnitFieldSet(t *testing.T) {
cmpopts.IgnoreFields(licenses.Metadata{}, "Coverage"),
cmpopts.IgnoreFields(internal.DirectoryMeta{}, "PathID"),
}
+ test.want.SourceInfo = pathInfo.SourceInfo
cleanFields(test.want, test.fields)
if diff := cmp.Diff(test.want, got, opts...); diff != "" {
t.Errorf("mismatch (-want, +got):\n%s", diff)
@@ -437,8 +424,9 @@ func TestGetUnitFieldSet(t *testing.T) {
func unit(path, modulePath, version string, readme *internal.Readme, pkg *internal.Package) *internal.Unit {
u := &internal.Unit{
- DirectoryMeta: internal.DirectoryMeta{
- ModuleInfo: *sample.ModuleInfo(modulePath, version),
+ PathInfo: internal.PathInfo{
+ ModulePath: modulePath,
+ Version: version,
Path: path,
IsRedistributable: true,
Licenses: sample.LicenseMetadata,
@@ -512,7 +500,6 @@ func TestGetUnitBypass(t *testing.T) {
func findDirectory(m *internal.Module, path string) *internal.Unit {
for _, d := range m.Units {
- d.ModuleInfo = m.ModuleInfo
if d.Path == path {
return d
}