1
0
mirror of https://github.com/regclient/regclient.git synced 2025-04-18 22:44:00 +03:00
regclient/mod/time_test.go
Brandon Mitchell cc0ae63f8f
Replace t.Error and return with t.Fatal
Signed-off-by: Brandon Mitchell <git@bmitch.net>
2024-02-27 15:33:52 -05:00

35 lines
809 B
Go

package mod
import (
"fmt"
"os"
"testing"
"time"
)
func TestTimeNow(t *testing.T) {
t.Run("NoEnv", func(t *testing.T) {
curEnv, envIsSet := os.LookupEnv(epocEnv)
if envIsSet {
err := os.Unsetenv(epocEnv)
if err != nil {
t.Fatalf("failed to unset %s", epocEnv)
}
defer os.Setenv(epocEnv, curEnv)
}
curTimeNow := timeNow()
if curTimeNow.After(time.Now()) {
t.Error("timeNow reported a time after OS time now")
}
})
t.Run("WithEnv", func(t *testing.T) {
timePrev := time.Now().Add(-1 * time.Hour).Round(time.Second)
timeSec := fmt.Sprintf("%d", timePrev.Unix())
t.Setenv(epocEnv, timeSec)
curTimeNow := timeNow()
if !curTimeNow.Equal(timePrev) {
t.Errorf("timeNow did not use the epoc, expected %d, received %d", timePrev.Unix(), curTimeNow.Unix())
}
})
}