From e77ca3c105bd64c5077d823d2127f6f812a4f681 Mon Sep 17 00:00:00 2001 From: satotake Date: Wed, 27 Apr 2022 02:57:04 +0900 Subject: Add `clock` cli flag Close #8787 --- hugolib/content_factory.go | 3 ++- hugolib/page_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++- hugolib/site.go | 6 ++++-- 3 files changed, 56 insertions(+), 4 deletions(-) (limited to 'hugolib') diff --git a/hugolib/content_factory.go b/hugolib/content_factory.go index e6e82979f..0a4d0aa0a 100644 --- a/hugolib/content_factory.go +++ b/hugolib/content_factory.go @@ -20,6 +20,7 @@ import ( "strings" "time" + "github.com/gohugoio/hugo/common/htime" "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/source" @@ -70,7 +71,7 @@ func (f ContentFactory) ApplyArchetypeTemplate(w io.Writer, p page.Page, archety d := &archetypeFileData{ Type: archetypeKind, - Date: time.Now().Format(time.RFC3339), + Date: htime.Now().Format(time.RFC3339), Page: p, File: p.File(), } diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 84e54306a..05a1c3d77 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -22,12 +22,14 @@ import ( "testing" "time" + "github.com/bep/clock" "github.com/gohugoio/hugo/htesting" "github.com/gohugoio/hugo/markup/asciidocext" "github.com/gohugoio/hugo/markup/rst" "github.com/gohugoio/hugo/config" + "github.com/gohugoio/hugo/common/htime" "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/hugofs" @@ -1536,7 +1538,6 @@ Content. } func TestShouldBuild(t *testing.T) { - t.Parallel() 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{} @@ -1582,6 +1583,54 @@ 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()) }) + 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{} + + publishSettings := []struct { + buildFuture bool + buildExpired bool + buildDrafts bool + draft bool + publishDate time.Time + expiryDate time.Time + out bool + }{ + // publishDate and expiryDate + {false, false, false, false, zero, zero, true}, + {false, false, false, false, zero, future, true}, + {false, false, false, false, past, zero, true}, + {false, false, false, false, past, future, true}, + {false, false, false, false, past, past, false}, + {false, false, false, false, future, future, false}, + {false, false, false, false, future, past, false}, + + // buildFuture and buildExpired + {false, true, false, false, past, past, true}, + {true, true, false, false, past, past, true}, + {true, false, false, false, past, past, false}, + {true, false, false, false, future, future, true}, + {true, true, false, false, future, future, true}, + {false, true, false, false, future, past, false}, + + // buildDrafts and draft + {true, true, false, true, past, future, false}, + {true, true, true, true, past, future, true}, + {true, true, true, true, past, future, true}, + } + + for _, ps := range publishSettings { + s := shouldBuild(ps.buildFuture, ps.buildExpired, ps.buildDrafts, ps.draft, + ps.publishDate, ps.expiryDate) + if s != ps.out { + t.Errorf("AssertShouldBuildWithClock unexpected output with params: %+v", ps) + } + } +} + // "dot" in path: #1885 and #2110 // disablePathToLower regression: #3374 func TestPathIssues(t *testing.T) { diff --git a/hugolib/site.go b/hugolib/site.go index cf7f0ff82..cbfc4d836 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -30,6 +30,7 @@ import ( "strings" "time" + "github.com/gohugoio/hugo/common/htime" "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/common/types" "github.com/gohugoio/hugo/modules" @@ -1910,10 +1911,11 @@ func shouldBuild(buildFuture bool, buildExpired bool, buildDrafts bool, Draft bo if !(buildDrafts || !Draft) { return false } - if !buildFuture && !publishDate.IsZero() && publishDate.After(time.Now()) { + hnow := htime.Now() + if !buildFuture && !publishDate.IsZero() && publishDate.After(hnow) { return false } - if !buildExpired && !expiryDate.IsZero() && expiryDate.Before(time.Now()) { + if !buildExpired && !expiryDate.IsZero() && expiryDate.Before(hnow) { return false } return true -- cgit v1.2.3