summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Olheiser <john.olheiser@gmail.com>2021-10-18 16:48:23 -0500
committerGitHub <noreply@github.com>2021-10-18 16:48:23 -0500
commiteb748ff79e22cdb04bb727d51a2669696d191d9c (patch)
treef6c7a6c8599998c36825336b830b586bc784efbf
parentc5770195d9fef850c8567e30b43624d3bdd10e6f (diff)
downloadgitea-eb748ff79e22cdb04bb727d51a2669696d191d9c.tar.gz
gitea-eb748ff79e22cdb04bb727d51a2669696d191d9c.zip
Allow mocking timeutil (#17354) (#17356)
Signed-off-by: jolheiser <john.olheiser@gmail.com>
-rw-r--r--models/user_heatmap_test.go7
-rw-r--r--modules/timeutil/timestamp.go16
2 files changed, 23 insertions, 0 deletions
diff --git a/models/user_heatmap_test.go b/models/user_heatmap_test.go
index b2aaea6499..be44833789 100644
--- a/models/user_heatmap_test.go
+++ b/models/user_heatmap_test.go
@@ -7,6 +7,9 @@ package models
import (
"fmt"
"testing"
+ "time"
+
+ "code.gitea.io/gitea/modules/timeutil"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
@@ -37,6 +40,10 @@ func TestGetUserHeatmapDataByUser(t *testing.T) {
// Prepare
assert.NoError(t, PrepareTestDatabase())
+ // Mock time
+ timeutil.Set(time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
+ defer timeutil.Unset()
+
for i, tc := range testCases {
user := AssertExistsAndLoadBean(t, &User{ID: tc.userID}).(*User)
diff --git a/modules/timeutil/timestamp.go b/modules/timeutil/timestamp.go
index b1c60c3084..1fe8d4fcb1 100644
--- a/modules/timeutil/timestamp.go
+++ b/modules/timeutil/timestamp.go
@@ -13,8 +13,24 @@ import (
// TimeStamp defines a timestamp
type TimeStamp int64
+// mock is NOT concurrency-safe!!
+var mock time.Time
+
+// Set sets the time to a mocked time.Time
+func Set(now time.Time) {
+ mock = now
+}
+
+// Unset will unset the mocked time.Time
+func Unset() {
+ mock = time.Time{}
+}
+
// TimeStampNow returns now int64
func TimeStampNow() TimeStamp {
+ if !mock.IsZero() {
+ return TimeStamp(mock.Unix())
+ }
return TimeStamp(time.Now().Unix())
}