Skip to content

Commit

Permalink
Merge pull request #1113 from wakatime/develop
Browse files Browse the repository at this point in the history
Release v1.102.4
  • Loading branch information
alanhamlett authored Oct 29, 2024
2 parents 75e1749 + 8153a4b commit fad2841
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
14 changes: 12 additions & 2 deletions cmd/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func LoadAPIParams(v *viper.Viper) (API, error) {

backoffAtStr := vipertools.GetString(v, "internal.backoff_at")
if backoffAtStr != "" {
parsed, err := time.Parse(ini.DateFormat, backoffAtStr)
parsed, err := safeTimeParse(ini.DateFormat, backoffAtStr)
// nolint:gocritic
if err != nil {
log.Warnf("failed to parse backoff_at: %s", err)
Expand Down Expand Up @@ -666,7 +666,7 @@ func LoadOfflineParams(v *viper.Viper) Offline {

lastSentAtStr := vipertools.GetString(v, "internal.heartbeats_last_sent_at")
if lastSentAtStr != "" {
parsed, err := time.Parse(ini.DateFormat, lastSentAtStr)
parsed, err := safeTimeParse(ini.DateFormat, lastSentAtStr)
// nolint:gocritic
if err != nil {
log.Warnf("failed to parse heartbeats_last_sent_at: %s", err)
Expand Down Expand Up @@ -720,6 +720,16 @@ func LoadStatusBarParams(v *viper.Viper) (StatusBar, error) {
}, nil
}

func safeTimeParse(format string, s string) (parsed time.Time, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("time.Parse panic: %v", r)
}
}()

return time.Parse(format, s)
}

func readAPIKeyFromCommand(cmdStr string) (string, error) {
if cmdStr == "" {
return "", nil
Expand Down
34 changes: 34 additions & 0 deletions cmd/params/params_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package params
import (
"regexp"
"testing"
"time"

"github.com/wakatime/wakatime-cli/pkg/ini"
"github.com/wakatime/wakatime-cli/pkg/regex"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -59,3 +61,35 @@ func TestParseBoolOrRegexList(t *testing.T) {
})
}
}

func TestSafeTimeParse(t *testing.T) {
parsed, err := safeTimeParse(ini.DateFormat, "2024-01-13T13:35:58Z")
require.NoError(t, err)

assert.Equal(t, time.Date(2024, 1, 13, 13, 35, 58, 0, time.UTC), parsed)
}

func TestSafeTimeParse_Err(t *testing.T) {
tests := map[string]struct {
Input string
Expected string
}{
"empty string": {
Input: "",
Expected: `parsing time "" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "2006"`,
},
"invalid time": {
Input: "invalid",
Expected: `parsing time "invalid" as "2006-01-02T15:04:05Z07:00": cannot parse "invalid" as "2006"`,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
parsed, err := safeTimeParse(ini.DateFormat, test.Input)
require.Equal(t, time.Time{}, parsed)

assert.EqualError(t, err, test.Expected)
})
}
}

0 comments on commit fad2841

Please sign in to comment.