Skip to content

Commit

Permalink
Fix unit tests on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
maraino committed Jul 3, 2024
1 parent ee3ab80 commit ca6ad2a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
22 changes: 11 additions & 11 deletions internal/templates/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ import (
//
// The "toTime" function receives a time or a Unix epoch and returns a time.Time
// in UTC. The "formatTime" function uses "toTime" and formats the resulting
// time using RFC3339. The functions "parseTime" and "mustParseTime" parses a
// string and returns the time.Time it represents. The "toLayout" function
// time using RFC3339. The functions "parseTime" and "mustParseTime" parse a
// string and return the time.Time it represents. The "toLayout" function
// converts strings like "time.RFC3339" or "time.UnixDate" to the actual layout
// represented by the Go constant with the same name. The "fail" function sets
// represented by the Go constant with the same nameß. The "fail" function sets
// the provided message, so that template errors are reported directly to the
// template without having the wrapper that text/template adds.
//
// {{ toTime }}
// => time.Now().UTC()
// => time.Now().UTC()
// {{ .Token.nbf | toTime }}
// => time.Unix(.Token.nbf, 0).UTC()
// => time.Unix(.Token.nbf, 0).UTC()
// {{ .Token.nbf | formatTime }}
// => time.Unix(.Token.nbf, 0).UTC().Format(time.RFC3339)
// => time.Unix(.Token.nbf, 0).UTC().Format(time.RFC3339)
// {{ "2024-07-02T23:16:02Z" | parseTime }}
// => time.Parse(time.RFC3339, "2024-07-02T23:16:02Z")
// => time.Parse(time.RFC3339, "2024-07-02T23:16:02Z")
// {{ parseTime "time.RFC339" "2024-07-02T23:16:02Z" }}
// => time.Parse(time.RFC3339, "2024-07-02T23:16:02Z")
// => time.Parse(time.RFC3339, "2024-07-02T23:16:02Z")
// {{ parseTime "time.UnixDate" "Tue Jul 2 16:20:48 PDT 2024" "America/Los_Angeles" }}
// => loc, _ := time.LoadLocation("America/Los_Angeles")
// time.ParseInLocation(time.UnixDate, "Tue Jul 2 16:20:48 PDT 2024", loc)
// => loc, _ := time.LoadLocation("America/Los_Angeles")
// time.ParseInLocation(time.UnixDate, "Tue Jul 2 16:20:48 PDT 2024", loc)
// {{ toLayout "time.RFC3339" }}
// => time.RFC3339
// => time.RFC3339
//
// sprig "env" and "expandenv" functions are removed to avoid the leak of
// information.
Expand Down
19 changes: 12 additions & 7 deletions internal/templates/funcmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ func TestGetFuncMap_toTime_formatTime(t *testing.T) {

func TestGetFuncMap_parseTime_mustParseTime(t *testing.T) {
now := time.Now().Truncate(time.Second)
loc, err := time.LoadLocation("America/Los_Angeles")
loc := time.Local
if zone, _ := now.Zone(); zone == "UTC" {
loc = time.UTC
}

losAngeles, err := time.LoadLocation("America/Los_Angeles")
require.NoError(t, err)

type args struct {
Expand All @@ -96,15 +101,15 @@ func TestGetFuncMap_parseTime_mustParseTime(t *testing.T) {
want time.Time
assertion assert.ErrorAssertionFunc
}{
{"now", args{[]string{now.Format(time.RFC3339)}}, now.Local(), assert.NoError},
{"now", args{[]string{now.Format(time.RFC3339)}}, now.In(loc), assert.NoError},
{"with real layout", args{[]string{time.UnixDate, now.UTC().Format(time.UnixDate)}}, now.UTC(), assert.NoError},
{"with name layout", args{[]string{"time.UnixDate", now.Format(time.UnixDate)}}, now.Local(), assert.NoError},
{"with name layout", args{[]string{"time.UnixDate", now.Format(time.UnixDate)}}, now.In(loc), assert.NoError},
{"with locale UTC", args{[]string{"time.UnixDate", now.UTC().Format(time.UnixDate), "UTC"}}, now.UTC(), assert.NoError},
{"with locale other", args{[]string{"time.UnixDate", now.In(loc).Format(time.UnixDate), "America/Los_Angeles"}}, now.In(loc), assert.NoError},
{"with locale other", args{[]string{"time.UnixDate", now.In(losAngeles).Format(time.UnixDate), "America/Los_Angeles"}}, now.In(losAngeles), assert.NoError},
{"fail parse", args{[]string{now.Format(time.UnixDate)}}, time.Time{}, assert.Error},
{"fail parse with layout", args{[]string{"time.UnixDate", now.Format(time.RFC3339)}}, time.Time{}, assert.Error},
{"fail parse with locale", args{[]string{"time.UnixDate", now.Format(time.RFC3339), "america/Los_Angeles"}}, time.Time{}, assert.Error},
{"fail load locale", args{[]string{"time.UnixDate", now.In(loc).Format(time.UnixDate), "America/The_Angels"}}, time.Time{}, assert.Error},
{"fail load locale", args{[]string{"time.UnixDate", now.In(losAngeles).Format(time.UnixDate), "America/The_Angels"}}, time.Time{}, assert.Error},
{"fail arguments", args{[]string{"time.Layout", now.Format(time.Layout), "America/The_Angels", "extra"}}, time.Time{}, assert.Error},
}
for _, tt := range tests {
Expand Down Expand Up @@ -205,8 +210,8 @@ func TestTemplates(t *testing.T) {
errorAssertion assert.ErrorAssertionFunc
failAssertion assert.ValueAssertionFunc
}{
{"toTime", args{`{{ .nbf | toTime }}`}, now.String(), assert.NoError, assert.Empty},
{"toTime toJson", args{`{{ .nbf | toTime | toJson }}`}, strconv.Quote(now.Format(time.RFC3339)), assert.NoError, assert.Empty},
{"toTime int64", args{`{{ .nbf | toTime }}`}, now.String(), assert.NoError, assert.Empty},
{"toTime int64 toJson", args{`{{ .nbf | toTime | toJson }}`}, strconv.Quote(now.Format(time.RFC3339)), assert.NoError, assert.Empty},
{"toTime float64 toJson", args{`{{ .float64 | toTime | toJson }}`}, strconv.Quote(now.Format(time.RFC3339)), assert.NoError, assert.Empty},
{"formatTime", args{`{{ .nbf | formatTime }}`}, now.Format(time.RFC3339), assert.NoError, assert.Empty},
{"formatTime float64", args{`{{ .float64 | formatTime }}`}, now.Format(time.RFC3339), assert.NoError, assert.Empty},
Expand Down

0 comments on commit ca6ad2a

Please sign in to comment.