diff --git a/commands/commands.go b/commands/commands.go index 97e6d21134d..d55a4e9aab3 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -281,7 +281,7 @@ func (cc *hugoBuilderCommon) handleCommonBuilderFlags(cmd *cobra.Command) { cmd.PersistentFlags().StringVarP(&cc.environment, "environment", "e", "", "build environment") cmd.PersistentFlags().StringP("themesDir", "", "", "filesystem path to themes directory") cmd.PersistentFlags().StringP("ignoreVendorPaths", "", "", "ignores any _vendor for module paths matching the given Glob pattern") - cmd.PersistentFlags().StringVar(&cc.clock, "clock", "", "set clock inside hugo, e.g. --clock 2021-11-06T22:30:00.00+09:00") + cmd.PersistentFlags().StringVar(&cc.clock, "clock", "", "set the clock used by Hugo, e.g. --clock 2021-11-06T22:30:00.00+09:00") } func (cc *hugoBuilderCommon) handleFlags(cmd *cobra.Command) { diff --git a/commands/hugo_test.go b/commands/hugo_test.go index 61160b7dd06..e5b62edd11f 100644 --- a/commands/hugo_test.go +++ b/commands/hugo_test.go @@ -21,7 +21,9 @@ import ( "strings" "testing" + "github.com/bep/clock" qt "github.com/frankban/quicktest" + "github.com/gohugoio/hugo/common/htime" "github.com/gohugoio/hugo/hugofs" "github.com/spf13/afero" "golang.org/x/tools/txtar" @@ -29,6 +31,7 @@ import ( // Issue #5662 func TestHugoWithContentDirOverride(t *testing.T) { + t.Parallel() c := qt.New(t) files := ` @@ -50,6 +53,7 @@ Page: {{ .Title }}| // Issue #9794 func TestHugoStaticFilesMultipleStaticAndManyFolders(t *testing.T) { + t.Parallel() c := qt.New(t) files := ` @@ -95,6 +99,8 @@ Home. // Issue #8787 func TestHugoListCommandsWithClockFlag(t *testing.T) { + t.Cleanup(func() { htime.Clock = clock.System() }) + c := qt.New(t) files := ` @@ -115,7 +121,9 @@ date: 2200-11-06 Page: {{ .Title }}| ` - s := newTestHugoCmdBuilder(c, files, []string{"list", "future"}).Build() + s := newTestHugoCmdBuilder(c, files, []string{"list", "future"}) + s.captureOut = true + s.Build() p := filepath.Join("content", "future.md") s.AssertStdout(p + ",2200-11-06T00:00:00Z") @@ -130,7 +138,9 @@ type testHugoCmdBuilder struct { dir string files string args []string - out string + + captureOut bool + out string } func newTestHugoCmdBuilder(c *qt.C, files string, args []string) *testHugoCmdBuilder { @@ -156,12 +166,17 @@ func (s *testHugoCmdBuilder) Build() *testHugoCmdBuilder { args := append(s.args, "-s="+s.dir, "--quiet") cmd.SetArgs(args) - out, err := captureStdout(func() error { + if s.captureOut { + out, err := captureStdout(func() error { + _, err := cmd.ExecuteC() + return err + }) + s.Assert(err, qt.IsNil) + s.out = out + } else { _, err := cmd.ExecuteC() - return err - }) - s.Assert(err, qt.IsNil) - s.out = out + s.Assert(err, qt.IsNil) + } return s } diff --git a/common/htime/time.go b/common/htime/time.go index d854e9312d8..052a45ed1dd 100644 --- a/common/htime/time.go +++ b/common/htime/time.go @@ -75,7 +75,8 @@ var ( "November", "December", } - Clock = clock.Start(time.Now()) + + Clock = clock.System() ) func NewTimeFormatter(ltr locales.Translator) TimeFormatter { @@ -151,12 +152,12 @@ func ToTimeInDefaultLocationE(i any, location *time.Location) (tim time.Time, er return cast.ToTimeInDefaultLocationE(i, location) } -// Now returns time.Now() or time value based on`clock` flag. +// Now returns time.Now() or time value based on the `clock` flag. // Use this function to fake time inside hugo. func Now() time.Time { return Clock.Now() } func Since(t time.Time) time.Duration { - return Clock.Now().Sub(t) + return Clock.Since(t) } diff --git a/go.mod b/go.mod index 2637fcc0a73..25ae2b9f440 100644 --- a/go.mod +++ b/go.mod @@ -91,7 +91,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/sso v1.4.0 // indirect github.com/aws/aws-sdk-go-v2/service/sts v1.7.0 // indirect github.com/aws/smithy-go v1.8.0 // indirect - github.com/bep/clock v0.1.0 // indirect + github.com/bep/clock v0.2.1-0.20220507124130-0a9d9c79927c // indirect github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect github.com/dlclark/regexp2 v1.4.0 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect diff --git a/go.sum b/go.sum index 9d0e2b812b9..267d2300652 100644 --- a/go.sum +++ b/go.sum @@ -214,6 +214,12 @@ github.com/aws/smithy-go v1.8.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAm github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/bep/clock v0.1.0 h1:sdvJ08SsgwTY/ejA705YKBlktFfj2uVpmQSSQspZJ2c= github.com/bep/clock v0.1.0/go.mod h1:shVP9tZ3cXpbVj60SnlU1pMwKjFxECBRm9vZfpoA0Gs= +github.com/bep/clock v0.2.0 h1:Uiv+P2wRVBy/g9Gybh645PgEOSC1BCXIQ7DOIc2bE64= +github.com/bep/clock v0.2.0/go.mod h1:6Gz2lapnJ9vxpvPxQ2u6FcXFRoj4kkiqQ6pm0ERZlwk= +github.com/bep/clock v0.2.1-0.20220507123307-c7e9c1bdae6f h1:dZlHVwWUCnS4P1Yk57qppK81fAUVV8DzDAo1lZA85hk= +github.com/bep/clock v0.2.1-0.20220507123307-c7e9c1bdae6f/go.mod h1:6Gz2lapnJ9vxpvPxQ2u6FcXFRoj4kkiqQ6pm0ERZlwk= +github.com/bep/clock v0.2.1-0.20220507124130-0a9d9c79927c h1:EPIuutNKs0tBieB1Z46GhNhiaYCs8bqSwwuo08xJHqs= +github.com/bep/clock v0.2.1-0.20220507124130-0a9d9c79927c/go.mod h1:6Gz2lapnJ9vxpvPxQ2u6FcXFRoj4kkiqQ6pm0ERZlwk= github.com/bep/debounce v1.2.0 h1:wXds8Kq8qRfwAOpAxHrJDbCXgC5aHSzgQb/0gKsHQqo= github.com/bep/debounce v1.2.0/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0= github.com/bep/gitmap v1.1.2 h1:zk04w1qc1COTZPPYWDQHvns3y1afOsdRfraFQ3qI840= diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 05a1c3d776f..b931731313c 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -1585,7 +1585,7 @@ func TestShouldBuild(t *testing.T) { func TestShouldBuildWithClock(t *testing.T) { htime.Clock = clock.Start(time.Date(2021, 11, 17, 20, 34, 58, 651387237, time.UTC)) - t.Cleanup(func() { htime.Clock = clock.Start(time.Now()) }) + t.Cleanup(func() { htime.Clock = clock.System() }) past := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) future := time.Date(2037, 11, 17, 20, 34, 58, 651387237, time.UTC) zero := time.Time{} diff --git a/tpl/time/init.go b/tpl/time/init.go index 7dd71f2105d..4bb2ddf67bf 100644 --- a/tpl/time/init.go +++ b/tpl/time/init.go @@ -28,7 +28,6 @@ func init() { if d.Language == nil { panic("Language must be set") } - ctx := New(langs.GetTimeFormatter(d.Language), langs.GetLocation(d.Language)) ns := &internal.TemplateFuncsNamespace{