diff options
| author | Jonathan Amsterdam <jba@google.com> | 2020-04-27 16:12:09 -0400 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2020-04-29 20:56:42 +0000 |
| commit | 0f995de2a3635609f7f95c2efc86e2dab4fcadf0 (patch) | |
| tree | 9c453c709f35cc11365ae3543e86f97049d4b7f9 /internal/postgres/path_test.go | |
| parent | 21584e30257c7768c25dbe8a2dec7279ace4f84b (diff) | |
| download | go-x-pkgsite-0f995de2a3635609f7f95c2efc86e2dab4fcadf0.tar.xz | |
internal/postgres: support for getting path info
Add GetPathInfo, a function that gets information about a path. The
main handler would call either GetPathInfo(path, "unknown", "latest")
if the incoming URL path had no "@", or GetPathInfo(path, "unknown",
version) if the incoming path looked like "foo@v".
Based on the returned path (or absence of one), it would decide whether
to call GetDirectory, GetPackage or GetModule.
Change-Id: Ifc09202d8ec110960e2c3bbaf04b79fc7a7c72df
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/728098
Reviewed-by: Julie Qiu <julieqiu@google.com>
Diffstat (limited to 'internal/postgres/path_test.go')
| -rw-r--r-- | internal/postgres/path_test.go | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/internal/postgres/path_test.go b/internal/postgres/path_test.go new file mode 100644 index 00000000..0129fe3e --- /dev/null +++ b/internal/postgres/path_test.go @@ -0,0 +1,155 @@ +// Copyright 2020 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 postgres + +import ( + "context" + "path" + "testing" + "time" + + "golang.org/x/pkgsite/internal" + "golang.org/x/pkgsite/internal/experiment" + "golang.org/x/pkgsite/internal/version" +) + +func TestGetPathInfo(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), testTimeout) + defer cancel() + ctx = experiment.NewContext(ctx, experiment.NewSet(map[string]bool{ + internal.ExperimentInsertDirectories: true, + })) + + defer ResetTestDB(testDB, t) + + for _, testModule := range []struct { + module, version, packageSuffix string + }{ + {"m.com", "v1.0.0", "a"}, + {"m.com", "v1.0.1", "dir/a"}, + {"m.com", "v1.1.0", "a/b"}, + {"m.com", "v1.2.0-pre", "a"}, + {"m.com/a", "v1.1.0", "b"}, + } { + vtype, err := version.ParseType(testModule.version) + if err != nil { + t.Fatal(err) + } + + pkgName := path.Base(testModule.packageSuffix) + pkgPath := path.Join(testModule.module, testModule.packageSuffix) + m := &internal.Module{ + ModuleInfo: internal.ModuleInfo{ + ModulePath: testModule.module, + Version: testModule.version, + VersionType: vtype, + CommitTime: time.Now(), + }, + Packages: []*internal.Package{{ + Name: pkgName, + Path: pkgPath, + }}, + } + for d := pkgPath; d != "." && len(d) >= len(testModule.module); d = path.Dir(d) { + dir := &internal.DirectoryNew{Path: d} + if d == pkgPath { + dir.Package = &internal.PackageNew{ + Path: pkgPath, + Name: pkgName, + Documentation: &internal.Documentation{}, + } + } + m.Directories = append(m.Directories, dir) + } + if err := testDB.InsertModule(ctx, m); err != nil { + t.Fatal(err) + } + } + + for _, test := range []struct { + name string + path, module, version string + wantModule, wantVersion string + wantIsPackage bool + }{ + { + name: "known module and version", + path: "m.com/a", + module: "m.com", + version: "v1.2.0-pre", + wantModule: "m.com", + wantVersion: "v1.2.0-pre", + wantIsPackage: true, + }, + { + name: "unknown module, known version", + path: "m.com/a/b", + version: "v1.1.0", + // The path is in two modules at v1.1.0. Prefer the longer one. + wantModule: "m.com/a", + wantVersion: "v1.1.0", + wantIsPackage: true, + }, + { + name: "known module, unknown version", + path: "m.com/a", + module: "m.com", + // Choose the latest release version. + wantModule: "m.com", + wantVersion: "v1.1.0", + wantIsPackage: false, + }, + { + name: "unknown module and version", + path: "m.com/a/b", + // Select the latest release version, longest module. + wantModule: "m.com/a", + wantVersion: "v1.1.0", + wantIsPackage: true, + }, + { + name: "module", + path: "m.com", + // Select the latest version of the module. + wantModule: "m.com", + wantVersion: "v1.1.0", + wantIsPackage: false, + }, + { + name: "longest module", + path: "m.com/a", + version: "v1.1.0", + // Prefer module m/a over module m, directory a. + wantModule: "m.com/a", + wantVersion: "v1.1.0", + wantIsPackage: false, // m/a is a module + }, + { + name: "directory", + path: "m.com/dir", + wantModule: "m.com", + wantVersion: "v1.0.1", + wantIsPackage: false, // m/dir is a directory + }, + } { + t.Run(test.name, func(t *testing.T) { + if test.module == "" { + test.module = internal.UnknownModulePath + } + if test.version == "" { + test.version = internal.LatestVersion + } + gotModule, gotVersion, gotIsPackage, err := testDB.GetPathInfo(ctx, test.path, test.module, test.version) + if err != nil { + t.Fatal(err) + } + if gotModule != test.wantModule || gotVersion != test.wantVersion || gotIsPackage != test.wantIsPackage { + t.Errorf("got (%q, %q, %t), want (%q, %q, %t)", + gotModule, gotVersion, gotIsPackage, + test.wantModule, test.wantVersion, test.wantIsPackage) + } + }) + } +} |
