internal/frontend: fix TestFetchDirectoryDetails
TestFetchDirectoryDetails wasn't checking the output of
fetchDirectoryDetails. Tests for this function are fixed.
TestFetchDirectoryDetailsInvalidArguments is also updated to test
fetchDirectoryDetails.
For golang/go#39629
Change-Id: Ib21f58790cfa8ddca31cf539869c4989df950c0f
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/258222
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>
diff --git a/internal/frontend/directory_test.go b/internal/frontend/directory_test.go
index 71256fe..84c68dc 100644
--- a/internal/frontend/directory_test.go
+++ b/internal/frontend/directory_test.go
@@ -8,6 +8,7 @@
"context"
"errors"
"fmt"
+ "path"
"testing"
"github.com/google/go-cmp/cmp"
@@ -32,8 +33,11 @@
mi := sample.ModuleInfo(modulePath, version)
var wantPkgs []*Package
for _, suffix := range suffixes {
- sp := sample.LegacyPackage(modulePath, suffix)
- pm := packageMetaFromLegacyPackage(sp)
+ fullPath := suffix
+ if modulePath != stdlib.ModulePath {
+ fullPath = path.Join(modulePath, suffix)
+ }
+ pm := sample.PackageMeta(fullPath)
pkg, err := createPackage(pm, mi, false)
if err != nil {
t.Fatal(err)
@@ -42,10 +46,10 @@
if pkg.PathAfterDirectory == "" {
pkg.PathAfterDirectory = effectiveName(pm.Path, pm.Name) + " (root)"
}
- pkg.Synopsis = sp.Synopsis
- pkg.PathAfterDirectory = internal.Suffix(sp.Path, dirPath)
+ pkg.Synopsis = pm.Synopsis
+ pkg.PathAfterDirectory = internal.Suffix(pm.Path, dirPath)
if pkg.PathAfterDirectory == "" {
- pkg.PathAfterDirectory = fmt.Sprintf("%s (root)", effectiveName(sp.Path, sp.Name))
+ pkg.PathAfterDirectory = fmt.Sprintf("%s (root)", effectiveName(pm.Path, pm.Name))
}
wantPkgs = append(wantPkgs, pkg)
}
@@ -75,37 +79,10 @@
includeDirPath: true,
dirPath: "github.com/hashicorp/vault/api",
modulePath: "github.com/hashicorp/vault/api",
- version: internal.LatestVersion,
- wantModulePath: "github.com/hashicorp/vault/api",
- wantVersion: "v1.1.2",
- wantPkgSuffixes: []string{""},
- },
- {
- name: "only dirPath provided, includeDirPath = false, want longest module path",
- dirPath: "github.com/hashicorp/vault/api",
- modulePath: internal.UnknownModulePath,
- version: internal.LatestVersion,
- wantModulePath: "github.com/hashicorp/vault/api",
- wantVersion: "v1.1.2",
- wantPkgSuffixes: nil,
- },
- {
- name: "dirPath@version, includeDirPath = false, want longest module path",
- dirPath: "github.com/hashicorp/vault/api",
- modulePath: internal.UnknownModulePath,
version: "v1.1.2",
wantModulePath: "github.com/hashicorp/vault/api",
wantVersion: "v1.1.2",
- wantPkgSuffixes: nil,
- },
- {
- name: "dirPath@version, includeDirPath = false, version only exists for shorter module path",
- dirPath: "github.com/hashicorp/vault/api",
- modulePath: internal.UnknownModulePath,
- version: "v1.0.3",
- wantModulePath: "github.com/hashicorp/vault",
- wantVersion: "v1.0.3",
- wantPkgSuffixes: nil,
+ wantPkgSuffixes: []string{""},
},
{
name: "valid directory for modulePath@version/suffix, includeDirPath = false",
@@ -151,7 +128,6 @@
},
} {
t.Run(tc.name, func(t *testing.T) {
- mi := sample.ModuleInfoReleaseType(tc.modulePath, tc.version)
var (
got *Directory
err error
@@ -162,17 +138,24 @@
ModulePath: tc.modulePath,
Version: tc.version,
IsRedistributable: true,
+ CommitTime: sample.CommitTime,
+ Licenses: sample.LicenseMetadata,
}
got, err = fetchDirectoryDetails(ctx, testDB, um, tc.includeDirPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ checkDirectory(got, tc.dirPath, tc.wantModulePath, tc.wantVersion, tc.wantPkgSuffixes)
})
t.Run("legacy", func(t *testing.T) {
+ mi := sample.ModuleInfoReleaseType(tc.modulePath, tc.version)
got, err = legacyFetchDirectoryDetails(ctx, testDB,
tc.dirPath, mi, sample.LicenseMetadata, tc.includeDirPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ checkDirectory(got, tc.dirPath, tc.wantModulePath, tc.wantVersion, tc.wantPkgSuffixes)
})
- if err != nil {
- t.Fatal(err)
- }
- checkDirectory(got, tc.dirPath, tc.wantModulePath, tc.wantVersion, tc.wantPkgSuffixes)
})
}
}
@@ -190,24 +173,6 @@
wantPkgPaths []string
}{
{
- name: "dirPath is empty",
- dirPath: "github.com/hashicorp/vault/api",
- modulePath: "",
- version: internal.LatestVersion,
- },
- {
- name: "modulePath is empty",
- dirPath: "github.com/hashicorp/vault/api",
- modulePath: "",
- version: internal.LatestVersion,
- },
- {
- name: "version is empty",
- dirPath: "github.com/hashicorp/vault/api",
- modulePath: internal.UnknownModulePath,
- version: "",
- },
- {
name: "dirPath is not modulePath, includeDirPath = true",
dirPath: "github.com/hashicorp/vault/api",
modulePath: "github.com/hashicorp/vault",
@@ -217,8 +182,15 @@
} {
t.Run(tc.name, func(t *testing.T) {
mi := sample.ModuleInfoReleaseType(tc.modulePath, tc.version)
- got, err := legacyFetchDirectoryDetails(ctx, testDB,
- tc.dirPath, mi, sample.LicenseMetadata, tc.includeDirPath)
+ um := &internal.UnitMeta{
+ Path: tc.dirPath,
+ ModulePath: mi.ModulePath,
+ Version: mi.Version,
+ IsRedistributable: true,
+ CommitTime: sample.CommitTime,
+ Licenses: sample.LicenseMetadata,
+ }
+ got, err := fetchDirectoryDetails(ctx, testDB, um, tc.includeDirPath)
if !errors.Is(err, derrors.InvalidArgument) {
t.Fatalf("expected err; got = \n%+v, %v", got, err)
}
diff --git a/internal/testing/sample/sample.go b/internal/testing/sample/sample.go
index 2ff09bf..b3fb6ea 100644
--- a/internal/testing/sample/sample.go
+++ b/internal/testing/sample/sample.go
@@ -189,6 +189,7 @@
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