Skip to content

Commit 8ce3e4e

Browse files
committed
epoch: fix unit test when SOURCE_DATE_EPOCH is set
Fixes #8200 Signed-off-by: Samuel Karp <[email protected]>
1 parent 7a77da2 commit 8ce3e4e

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

pkg/epoch/epoch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const SourceDateEpochEnv = "SOURCE_DATE_EPOCH"
3434
// If the env var is not set, SourceDateEpoch returns nil without an error.
3535
func SourceDateEpoch() (*time.Time, error) {
3636
v, ok := os.LookupEnv(SourceDateEpochEnv)
37-
if !ok {
37+
if !ok || v == "" {
3838
return nil, nil // not an error
3939
}
4040
i64, err := strconv.ParseInt(v, 10, 64)

pkg/epoch/epoch_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ func rightAfter(t1, t2 time.Time) bool {
3636
func TestSourceDateEpoch(t *testing.T) {
3737
if s, ok := os.LookupEnv(SourceDateEpochEnv); ok {
3838
t.Logf("%s is already set to %q, unsetting", SourceDateEpochEnv, s)
39+
// see https://github.com/golang/go/issues/52817#issuecomment-1131339120
3940
t.Setenv(SourceDateEpochEnv, "")
41+
os.Unsetenv(SourceDateEpochEnv)
4042
}
4143

4244
t.Run("WithoutSourceDateEpoch", func(t *testing.T) {
@@ -49,6 +51,18 @@ func TestSourceDateEpoch(t *testing.T) {
4951
require.True(t, rightAfter(now, v))
5052
})
5153

54+
t.Run("WithEmptySourceDateEpoch", func(t *testing.T) {
55+
t.Setenv(SourceDateEpochEnv, "")
56+
57+
vp, err := SourceDateEpoch()
58+
require.NoError(t, err)
59+
require.Nil(t, vp)
60+
61+
now := time.Now()
62+
v := SourceDateEpochOrNow()
63+
require.True(t, rightAfter(now, v))
64+
})
65+
5266
t.Run("WithSourceDateEpoch", func(t *testing.T) {
5367
sourceDateEpoch, err := time.Parse(time.RFC3339, "2022-01-23T12:34:56Z")
5468
require.NoError(t, err)

0 commit comments

Comments
 (0)