Merge pull request #8732 from thaJeztah/epoch_export_parse

pkg/epoch: extract parsing SOURCE_DATE_EPOCH to a function
This commit is contained in:
Phil Estes 2023-06-23 17:06:21 -04:00 committed by GitHub
commit 1a5eaa9ad0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 17 deletions

View File

@ -37,12 +37,11 @@ func SourceDateEpoch() (*time.Time, error) {
if !ok || v == "" {
return nil, nil // not an error
}
i64, err := strconv.ParseInt(v, 10, 64)
t, err := ParseSourceDateEpoch(v)
if err != nil {
return nil, fmt.Errorf("invalid %s value %q: %w", SourceDateEpochEnv, v, err)
return nil, fmt.Errorf("invalid %s value: %w", SourceDateEpochEnv, err)
}
unix := time.Unix(i64, 0).UTC()
return &unix, nil
return t, nil
}
// SourceDateEpochOrNow returns the SOURCE_DATE_EPOCH time if available,
@ -58,12 +57,26 @@ func SourceDateEpochOrNow() time.Time {
return time.Now().UTC()
}
// ParseSourceDateEpoch parses the given source date epoch, as *time.Time.
// It returns an error if sourceDateEpoch is empty or not well-formatted.
func ParseSourceDateEpoch(sourceDateEpoch string) (*time.Time, error) {
if sourceDateEpoch == "" {
return nil, fmt.Errorf("value is empty")
}
i64, err := strconv.ParseInt(sourceDateEpoch, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid value: %w", err)
}
unix := time.Unix(i64, 0).UTC()
return &unix, nil
}
// SetSourceDateEpoch sets the SOURCE_DATE_EPOCH env var.
func SetSourceDateEpoch(tm time.Time) {
os.Setenv(SourceDateEpochEnv, fmt.Sprintf("%d", tm.Unix()))
_ = os.Setenv(SourceDateEpochEnv, strconv.Itoa(int(tm.Unix())))
}
// UnsetSourceDateEpoch unsets the SOURCE_DATE_EPOCH env var.
func UnsetSourceDateEpoch() {
os.Unsetenv(SourceDateEpochEnv)
_ = os.Unsetenv(SourceDateEpochEnv)
}

View File

@ -19,6 +19,7 @@ package epoch
import (
"os"
"runtime"
"strconv"
"testing"
"time"
@ -26,11 +27,15 @@ import (
)
func rightAfter(t1, t2 time.Time) bool {
if t2.Equal(t1) {
return true
}
threshold := 10 * time.Millisecond
if runtime.GOOS == "windows" {
// Low timer resolution on Windows
return (t2.After(t1) && t2.Before(t1.Add(100*time.Millisecond))) || t2.Equal(t1)
threshold *= 10
}
return t2.After(t1) && t2.Before(t1.Add(10*time.Millisecond))
return t2.After(t1) && t2.Before(t1.Add(threshold))
}
func TestSourceDateEpoch(t *testing.T) {
@ -46,25 +51,31 @@ func TestSourceDateEpoch(t *testing.T) {
require.NoError(t, err)
require.Nil(t, vp)
now := time.Now()
now := time.Now().UTC()
v := SourceDateEpochOrNow()
require.True(t, rightAfter(now, v))
require.True(t, rightAfter(now, v), "now: %s, v: %s", now, v)
})
t.Run("WithEmptySourceDateEpoch", func(t *testing.T) {
t.Setenv(SourceDateEpochEnv, "")
const emptyValue = ""
t.Setenv(SourceDateEpochEnv, emptyValue)
vp, err := SourceDateEpoch()
require.NoError(t, err)
require.Nil(t, vp)
now := time.Now()
vp, err = ParseSourceDateEpoch(emptyValue)
require.Error(t, err, "value is empty")
require.Nil(t, vp)
now := time.Now().UTC()
v := SourceDateEpochOrNow()
require.True(t, rightAfter(now, v))
require.True(t, rightAfter(now, v), "now: %s, v: %s", now, v)
})
t.Run("WithSourceDateEpoch", func(t *testing.T) {
sourceDateEpoch, err := time.Parse(time.RFC3339, "2022-01-23T12:34:56Z")
const rfc3339Str = "2022-01-23T12:34:56Z"
sourceDateEpoch, err := time.Parse(time.RFC3339, rfc3339Str)
require.NoError(t, err)
SetSourceDateEpoch(sourceDateEpoch)
@ -72,6 +83,10 @@ func TestSourceDateEpoch(t *testing.T) {
vp, err := SourceDateEpoch()
require.NoError(t, err)
require.True(t, vp.Equal(sourceDateEpoch.UTC()))
vp, err = ParseSourceDateEpoch(strconv.Itoa(int(sourceDateEpoch.Unix())))
require.NoError(t, err)
require.True(t, vp.Equal(sourceDateEpoch))
v := SourceDateEpochOrNow()
@ -79,14 +94,19 @@ func TestSourceDateEpoch(t *testing.T) {
})
t.Run("WithInvalidSourceDateEpoch", func(t *testing.T) {
t.Setenv(SourceDateEpochEnv, "foo")
const invalidValue = "foo"
t.Setenv(SourceDateEpochEnv, invalidValue)
vp, err := SourceDateEpoch()
require.ErrorContains(t, err, "invalid SOURCE_DATE_EPOCH value")
require.Nil(t, vp)
now := time.Now()
vp, err = ParseSourceDateEpoch(invalidValue)
require.ErrorContains(t, err, "invalid value:")
require.Nil(t, vp)
now := time.Now().UTC()
v := SourceDateEpochOrNow()
require.True(t, rightAfter(now, v))
require.True(t, rightAfter(now, v), "now: %s, v: %s", now, v)
})
}