Skip to content

Commit

Permalink
Misc clock adjustments
Browse files Browse the repository at this point in the history
Updates #8787
  • Loading branch information
bep committed May 8, 2022
1 parent e232cce commit 7726c15
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 22 additions & 7 deletions commands/hugo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ 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"
)

// Issue #5662
func TestHugoWithContentDirOverride(t *testing.T) {
t.Parallel()
c := qt.New(t)

files := `
Expand All @@ -50,6 +53,7 @@ Page: {{ .Title }}|

// Issue #9794
func TestHugoStaticFilesMultipleStaticAndManyFolders(t *testing.T) {
t.Parallel()
c := qt.New(t)

files := `
Expand Down Expand Up @@ -95,6 +99,8 @@ Home.

// Issue #8787
func TestHugoListCommandsWithClockFlag(t *testing.T) {
t.Cleanup(func() { htime.Clock = clock.System() })

c := qt.New(t)

files := `
Expand All @@ -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")

Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions common/htime/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ var (
"November",
"December",
}
Clock = clock.Start(time.Now())

Clock = clock.System()
)

func NewTimeFormatter(ltr locales.Translator) TimeFormatter {
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion hugolib/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
1 change: 0 additions & 1 deletion tpl/time/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 7726c15

Please sign in to comment.