internal/frontend: fix commitTime for tests
CL 268638 added `.In(time.UTC)` to the commitTime for several tests.
This was causing tests to fail when run locally, due to different
outputs of absoluteTime.
absoluteTime for these commit times are now computed within the tests.
sample.UnitMeta also now sets the CommitTime to NowTruncated().
Change-Id: I3187c9a5eacd5d136c73080b2803241cb2022729
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/271808
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/server_test.go b/internal/frontend/server_test.go
index 6e8c390..c9f1895 100644
--- a/internal/frontend/server_test.go
+++ b/internal/frontend/server_test.go
@@ -353,6 +353,7 @@
LicenseFilePath: sample.LicenseFilePath,
UnitURLFormat: "/" + sample.ModulePath + "%s/" + sample.Suffix,
ModuleURL: "/" + sample.ModulePath,
+ CommitTime: absoluteTime(sample.NowTruncated()),
}
v2pkgV100 := &pagecheck.Page{
@@ -370,6 +371,7 @@
LicenseFilePath: sample.LicenseFilePath,
UnitURLFormat: "/github.com/v2major/module_name%s/bar",
ModuleURL: "/github.com/v2major/module_name",
+ CommitTime: absoluteTime(sample.NowTruncated()),
}
v2pkgV1Buz := *v2pkgV100
@@ -396,6 +398,7 @@
LicenseFilePath: sample.LicenseFilePath,
UnitURLFormat: "/github.com/v2major/module_name/v2%s/bar",
ModuleURL: "/github.com/v2major/module_name/v2",
+ CommitTime: absoluteTime(sample.NowTruncated()),
}
p9 := *pkgV100
@@ -426,6 +429,7 @@
LicenseFilePath: "LICENSE",
UnitURLFormat: "/github.com/incompatible%s/dir/inc",
ModuleURL: "/github.com/incompatible",
+ CommitTime: absoluteTime(sample.NowTruncated()),
}
pkgNonRedist := &pagecheck.Page{
@@ -441,6 +445,7 @@
LicenseType: "",
UnitURLFormat: "/github.com/non_redistributable%s/bar",
ModuleURL: "/github.com/non_redistributable",
+ CommitTime: absoluteTime(sample.NowTruncated()),
}
dir := &pagecheck.Page{
@@ -457,6 +462,7 @@
UnitURLFormat: "/" + sample.ModulePath + "%s/foo/directory",
LatestMajorVersionLink: "/github.com/valid/module_name/foo/directory",
LatestLink: "/github.com/valid/module_name/foo/directory",
+ CommitTime: absoluteTime(sample.NowTruncated()),
}
mod := &pagecheck.Page{
@@ -471,6 +477,7 @@
IsLatestMajor: true,
LatestLink: "/" + sample.ModulePath + "@v1.0.0",
LatestMajorVersionLink: "/" + sample.ModulePath,
+ CommitTime: absoluteTime(sample.NowTruncated()),
}
mp := *mod
mp.Version = pseudoVersion
@@ -492,6 +499,7 @@
IsLatestMinor: true,
IsLatestMajor: true,
UnitURLFormat: "/github.com/pseudo%s/dir",
+ CommitTime: absoluteTime(sample.NowTruncated()),
}
dirCmd := &pagecheck.Page{
@@ -508,6 +516,7 @@
LatestLink: "/cmd",
UnitURLFormat: "/cmd%s",
LatestMajorVersionLink: "/cmd",
+ CommitTime: absoluteTime(sample.NowTruncated()),
}
netHttp := &pagecheck.Page{
@@ -523,6 +532,7 @@
IsLatestMajor: true,
LatestLink: "/net/http",
LatestMajorVersionLink: "/net/http",
+ CommitTime: absoluteTime(sample.NowTruncated()),
}
pubsubliteDir := &pagecheck.Page{
diff --git a/internal/frontend/versions_test.go b/internal/frontend/versions_test.go
index d4b0fd2..4d10686 100644
--- a/internal/frontend/versions_test.go
+++ b/internal/frontend/versions_test.go
@@ -181,6 +181,16 @@
if err != nil {
t.Fatalf("fetchVersionsDetails(ctx, db, %q, %q): %v", tc.pkg.Path, tc.pkg.ModulePath, err)
}
+ for _, vl := range tc.wantDetails.ThisModule {
+ for _, v := range vl.Versions {
+ v.CommitTime = absoluteTime(tc.modules[0].CommitTime)
+ }
+ }
+ for _, vl := range tc.wantDetails.OtherModules {
+ for _, v := range vl.Versions {
+ v.CommitTime = absoluteTime(tc.modules[0].CommitTime)
+ }
+ }
if diff := cmp.Diff(tc.wantDetails, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
diff --git a/internal/postgres/unit_test.go b/internal/postgres/unit_test.go
index 72255e7..3ac4539 100644
--- a/internal/postgres/unit_test.go
+++ b/internal/postgres/unit_test.go
@@ -160,6 +160,7 @@
test.want.Name,
test.want.IsRedistributable,
)
+ test.want.CommitTime = um.CommitTime
checkUnit(ctx, t, um, test.want)
})
}
@@ -180,8 +181,7 @@
want.SourceInfo = um.SourceInfo
want.NumImports = len(want.Imports)
opts = append(opts,
- cmpopts.IgnoreFields(internal.Unit{}, "Imports"),
- cmpopts.IgnoreFields(internal.Unit{}, "LicenseContents"),
+ cmpopts.IgnoreFields(internal.Unit{}, "Imports", "LicenseContents"),
)
if diff := cmp.Diff(want, got, opts...); diff != "" {
t.Errorf("mismatch (-want, +got):\n%s", diff)
@@ -252,14 +252,14 @@
},
} {
t.Run(test.name, func(t *testing.T) {
- pathInfo := sample.UnitMeta(
+ um := sample.UnitMeta(
test.want.Path,
test.want.ModulePath,
test.want.Version,
test.want.Name,
test.want.IsRedistributable,
)
- got, err := testDB.GetUnit(ctx, pathInfo, test.fields)
+ got, err := testDB.GetUnit(ctx, um, test.fields)
if err != nil {
t.Fatal(err)
}
@@ -268,7 +268,8 @@
// The packages table only includes partial license information; it omits the Coverage field.
cmpopts.IgnoreFields(licenses.Metadata{}, "Coverage"),
}
- test.want.SourceInfo = pathInfo.SourceInfo
+ test.want.CommitTime = um.CommitTime
+ test.want.SourceInfo = um.SourceInfo
cleanFields(test.want, test.fields)
if diff := cmp.Diff(test.want, got, opts...); diff != "" {
t.Errorf("mismatch (-want, +got):\n%s", diff)
diff --git a/internal/testing/pagecheck/pagecheck.go b/internal/testing/pagecheck/pagecheck.go
index a9a26e3..2225e05 100644
--- a/internal/testing/pagecheck/pagecheck.go
+++ b/internal/testing/pagecheck/pagecheck.go
@@ -11,7 +11,6 @@
"fmt"
"path"
"regexp"
- "time"
"golang.org/x/pkgsite/internal/testing/htmlcheck"
)
@@ -104,11 +103,6 @@
importsDetails = nil
}
- commitTime := p.CommitTime
- if commitTime == "" {
- commitTime = time.Now().In(time.UTC).Format("Jan _2, 2006")
- }
-
majorVersionBannerClass := "UnitHeader-majorVersionBanner"
if p.IsLatestMajor {
majorVersionBannerClass += " DetailsHeader-banner--latest"
@@ -133,7 +127,7 @@
href("?tab=versions"),
exactText("Version "+p.FormattedVersion))),
in(`[data-test-id="UnitHeader-commitTime"]`,
- text(commitTime)),
+ text(p.CommitTime)),
in(`[data-test-id="UnitHeader-licenses"]`,
in("a",
href(licenseLink),
diff --git a/internal/testing/sample/sample.go b/internal/testing/sample/sample.go
index 1a0f3ca..d39eb60 100644
--- a/internal/testing/sample/sample.go
+++ b/internal/testing/sample/sample.go
@@ -243,6 +243,7 @@
Version: version,
Path: path,
Name: name,
+ CommitTime: NowTruncated(),
IsRedistributable: isRedistributable,
Licenses: LicenseMetadata,
SourceInfo: source.NewGitHubInfo("https://"+modulePath, "", version),