Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parallel test fail #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 19 additions & 16 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,12 +121,20 @@ date: 2200-11-06
Page: {{ .Title }}|

`
s := newTestHugoCmdBuilder(c, files, []string{"list", "future"}).Build()
out, err := captureStdout(func() error {
newTestHugoCmdBuilder(c, files, []string{"list", "future"}).Build()
return nil
})
c.Assert(err, qt.IsNil)
p := filepath.Join("content", "future.md")
s.AssertStdout(p + ",2200-11-06T00:00:00Z")
c.Assert(out, qt.Contains, p+",2200-11-06T00:00:00Z")

s = newTestHugoCmdBuilder(c, files, []string{"list", "future", "--clock", "2300-11-06"}).Build()
s.AssertStdout("")
out, err = captureStdout(func() error {
newTestHugoCmdBuilder(c, files, []string{"list", "future", "--clock", "2300-11-06"}).Build()
return nil
})
c.Assert(err, qt.IsNil)
c.Assert(out, qt.Equals, "")
}

type testHugoCmdBuilder struct {
Expand All @@ -130,7 +144,6 @@ type testHugoCmdBuilder struct {
dir string
files string
args []string
out string
}

func newTestHugoCmdBuilder(c *qt.C, files string, args []string) *testHugoCmdBuilder {
Expand All @@ -156,12 +169,8 @@ func (s *testHugoCmdBuilder) Build() *testHugoCmdBuilder {
args := append(s.args, "-s="+s.dir, "--quiet")
cmd.SetArgs(args)

out, err := captureStdout(func() error {
_, err := cmd.ExecuteC()
return err
})
_, err := cmd.ExecuteC()
s.Assert(err, qt.IsNil)
s.out = out

return s
}
Expand All @@ -182,9 +191,3 @@ func (s *testHugoCmdBuilder) AssertFileContent(filename string, matches ...strin
}
}
}

func (s *testHugoCmdBuilder) AssertStdout(match string) {
s.Helper()
content := strings.TrimSpace(s.out)
s.Assert(content, qt.Contains, strings.TrimSpace(match))
}
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