Skip to content

Commit

Permalink
Merge pull request #1081 from wakatime/develop
Browse files Browse the repository at this point in the history
Release v1.98.2
  • Loading branch information
alanhamlett authored Jul 29, 2024
2 parents 72aa4f3 + 4b4c192 commit 7d81a02
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 187 deletions.
6 changes: 1 addition & 5 deletions cmd/heartbeat/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

// Run executes the heartbeat command.
func Run(v *viper.Viper) (int, error) {
queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)
if err != nil {
log.Warnf("failed to load offline queue filepath: %s", err)
}
Expand Down Expand Up @@ -115,10 +115,6 @@ func SendHeartbeats(v *viper.Viper, queueFilepath string) error {
handleOpts := initHandleOptions(params)

if !params.Offline.Disabled {
if params.Offline.QueueFile != "" {
queueFilepath = params.Offline.QueueFile
}

handleOpts = append(handleOpts, offline.WithQueue(queueFilepath))
}

Expand Down
63 changes: 52 additions & 11 deletions cmd/heartbeat/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
)

func TestSendHeartbeats(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
defer tearDown()

Expand Down Expand Up @@ -120,6 +122,8 @@ func TestSendHeartbeats(t *testing.T) {
}

func TestSendHeartbeats_RateLimited(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
defer tearDown()

Expand All @@ -133,6 +137,12 @@ func TestSendHeartbeats_RateLimited(t *testing.T) {
numCalls++
})

tmpFile, err := os.CreateTemp(t.TempDir(), "wakatime")
require.NoError(t, err)

offlineQueueFile, err := os.CreateTemp(t.TempDir(), "")
require.NoError(t, err)

v := viper.New()
v.SetDefault("sync-offline-activity", 1000)
v.Set("api-url", testServerURL)
Expand All @@ -152,18 +162,19 @@ func TestSendHeartbeats_RateLimited(t *testing.T) {
v.Set("timeout", 5)
v.Set("write", true)
v.Set("heartbeat-rate-limit-seconds", 500)
v.Set("internal-config", tmpFile.Name())
v.Set("offline-queue-file", offlineQueueFile.Name())
v.Set("internal.heartbeats_last_sent_at", time.Now().Add(-time.Minute).Format(time.RFC3339))

offlineQueueFile, err := os.CreateTemp(t.TempDir(), "")
require.NoError(t, err)

err = cmdheartbeat.SendHeartbeats(v, offlineQueueFile.Name())
require.NoError(t, err)

assert.Zero(t, numCalls)
}

func TestSendHeartbeats_WithFiltering_Exclude(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
defer tearDown()

Expand Down Expand Up @@ -198,6 +209,8 @@ func TestSendHeartbeats_WithFiltering_Exclude(t *testing.T) {
}

func TestSendHeartbeats_ExtraHeartbeats(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
defer tearDown()

Expand Down Expand Up @@ -292,8 +305,6 @@ func TestSendHeartbeats_ExtraHeartbeats(t *testing.T) {

os.Stdin = r

cmdparams.Once = sync.Once{}

data, err := os.ReadFile("testdata/extra_heartbeats.json")
require.NoError(t, err)

Expand Down Expand Up @@ -340,6 +351,8 @@ func TestSendHeartbeats_ExtraHeartbeats(t *testing.T) {
}

func TestSendHeartbeats_ExtraHeartbeats_Sanitize(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
defer tearDown()

Expand Down Expand Up @@ -376,8 +389,6 @@ func TestSendHeartbeats_ExtraHeartbeats_Sanitize(t *testing.T) {

os.Stdin = r

cmdparams.Once = sync.Once{}

data, err := os.ReadFile("testdata/extra_heartbeats.json")
require.NoError(t, err)

Expand Down Expand Up @@ -476,6 +487,8 @@ func TestSendHeartbeats_ExtraHeartbeats_Sanitize(t *testing.T) {
}

func TestSendHeartbeats_NonExistingEntity(t *testing.T) {
resetSingleton(t)

tmpDir := t.TempDir()

logFile, err := os.CreateTemp(tmpDir, "")
Expand Down Expand Up @@ -518,6 +531,8 @@ func TestSendHeartbeats_NonExistingEntity(t *testing.T) {
}

func TestSendHeartbeats_IsUnsavedEntity(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
defer tearDown()

Expand Down Expand Up @@ -586,8 +601,6 @@ func TestSendHeartbeats_IsUnsavedEntity(t *testing.T) {

os.Stdin = inr

cmdparams.Once = sync.Once{}

data, err := os.ReadFile("testdata/extra_heartbeats_is_unsaved_entity.json")
require.NoError(t, err)

Expand Down Expand Up @@ -652,6 +665,8 @@ func TestSendHeartbeats_IsUnsavedEntity(t *testing.T) {
}

func TestSendHeartbeats_NonExistingExtraHeartbeatsEntity(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
defer tearDown()

Expand Down Expand Up @@ -718,8 +733,6 @@ func TestSendHeartbeats_NonExistingExtraHeartbeatsEntity(t *testing.T) {

os.Stdin = inr

cmdparams.Once = sync.Once{}

data, err := os.ReadFile("testdata/extra_heartbeats_nonexisting_entity.json")
require.NoError(t, err)

Expand Down Expand Up @@ -776,6 +789,8 @@ func TestSendHeartbeats_NonExistingExtraHeartbeatsEntity(t *testing.T) {
}

func TestSendHeartbeats_ErrAuth_UnsetAPIKey(t *testing.T) {
resetSingleton(t)

_, router, tearDown := setupTestServer()
defer tearDown()

Expand Down Expand Up @@ -811,6 +826,8 @@ func TestSendHeartbeats_ErrAuth_UnsetAPIKey(t *testing.T) {
}

func TestSendHeartbeats_ErrBackoff(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
defer tearDown()

Expand Down Expand Up @@ -874,6 +891,8 @@ func TestSendHeartbeats_ErrBackoff(t *testing.T) {
}

func TestSendHeartbeats_ErrBackoff_Verbose(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
defer tearDown()

Expand Down Expand Up @@ -938,6 +957,8 @@ func TestSendHeartbeats_ErrBackoff_Verbose(t *testing.T) {
}

func TestSendHeartbeats_ObfuscateProject(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
defer tearDown()

Expand Down Expand Up @@ -1022,6 +1043,8 @@ func TestSendHeartbeats_ObfuscateProject(t *testing.T) {
}

func TestSendHeartbeats_ObfuscateProjectNotBranch(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
defer tearDown()

Expand Down Expand Up @@ -1107,6 +1130,8 @@ func TestSendHeartbeats_ObfuscateProjectNotBranch(t *testing.T) {
}

func TestRateLimited(t *testing.T) {
resetSingleton(t)

p := cmdheartbeat.RateLimitParams{
Timeout: time.Duration(offline.RateLimitDefaultSeconds) * time.Second,
LastSentAt: time.Now(),
Expand All @@ -1116,6 +1141,8 @@ func TestRateLimited(t *testing.T) {
}

func TestRateLimited_NotLimited(t *testing.T) {
resetSingleton(t)

p := cmdheartbeat.RateLimitParams{
LastSentAt: time.Now().Add(time.Duration(-offline.RateLimitDefaultSeconds*2) * time.Second),
Timeout: time.Duration(offline.RateLimitDefaultSeconds) * time.Second,
Expand All @@ -1125,6 +1152,8 @@ func TestRateLimited_NotLimited(t *testing.T) {
}

func TestRateLimited_Disabled(t *testing.T) {
resetSingleton(t)

p := cmdheartbeat.RateLimitParams{
Disabled: true,
}
Expand All @@ -1133,6 +1162,8 @@ func TestRateLimited_Disabled(t *testing.T) {
}

func TestRateLimited_TimeoutZero(t *testing.T) {
resetSingleton(t)

p := cmdheartbeat.RateLimitParams{
LastSentAt: time.Time{},
}
Expand All @@ -1141,6 +1172,8 @@ func TestRateLimited_TimeoutZero(t *testing.T) {
}

func TestRateLimited_LastSentAtZero(t *testing.T) {
resetSingleton(t)

p := cmdheartbeat.RateLimitParams{
Timeout: 0,
}
Expand All @@ -1149,6 +1182,8 @@ func TestRateLimited_LastSentAtZero(t *testing.T) {
}

func TestResetRateLimit(t *testing.T) {
resetSingleton(t)

tmpFile, err := os.CreateTemp(t.TempDir(), "wakatime")
require.NoError(t, err)

Expand Down Expand Up @@ -1216,3 +1251,9 @@ func copyFile(t *testing.T, source, destination string) {
err = os.WriteFile(destination, input, 0600)
require.NoError(t, err)
}

func resetSingleton(t *testing.T) {
t.Helper()

cmdparams.Once = sync.Once{}
}
4 changes: 0 additions & 4 deletions cmd/offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ func SaveHeartbeats(v *viper.Viper, heartbeats []heartbeat.Heartbeat, queueFilep

handleOpts := initHandleOptions(params)

if params.Offline.QueueFile != "" {
queueFilepath = params.Offline.QueueFile
}

handleOpts = append(handleOpts, offline.WithQueue(queueFilepath))

sender := offline.Noop{}
Expand Down
9 changes: 1 addition & 8 deletions cmd/offlinecount/offlinecount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package offlinecount
import (
"fmt"

"github.com/wakatime/wakatime-cli/cmd/params"
"github.com/wakatime/wakatime-cli/pkg/exitcode"
"github.com/wakatime/wakatime-cli/pkg/offline"

Expand All @@ -12,20 +11,14 @@ import (

// Run executes the offline-count command.
func Run(v *viper.Viper) (int, error) {
queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)
if err != nil {
return exitcode.ErrGeneric, fmt.Errorf(
"failed to load offline queue filepath: %s",
err,
)
}

p := params.LoadOfflineParams(v)

if p.QueueFile != "" {
queueFilepath = p.QueueFile
}

count, err := offline.CountHeartbeats(queueFilepath)
if err != nil {
fmt.Println(err)
Expand Down
6 changes: 1 addition & 5 deletions cmd/offlineprint/offlineprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// Run executes the print-offline-heartbeats command.
func Run(v *viper.Viper) (int, error) {
queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)
if err != nil {
return exitcode.ErrGeneric, fmt.Errorf(
"failed to load offline queue filepath: %s",
Expand All @@ -25,10 +25,6 @@ func Run(v *viper.Viper) (int, error) {

p := params.LoadOfflineParams(v)

if p.QueueFile != "" {
queueFilepath = p.QueueFile
}

hh, err := offline.ReadHeartbeats(queueFilepath, p.PrintMax)
if err != nil {
fmt.Println(err)
Expand Down
22 changes: 12 additions & 10 deletions cmd/offlinesync/offlinesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ func run(v *viper.Viper) (int, error) {
return exitcode.Success, nil
}

queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)
if err != nil {
return exitcode.ErrGeneric, fmt.Errorf(
"offline sync failed: failed to load offline queue filepath: %s",
err,
)
}

queueFilepathLegacy, err := offline.QueueFilepathLegacy()
queueFilepathLegacy, err := offline.QueueFilepathLegacy(v)
if err != nil {
log.Warnf("legacy offline sync failed: failed to load offline queue filepath: %s", err)
}
Expand Down Expand Up @@ -84,6 +84,10 @@ func syncOfflineActivityLegacy(v *viper.Viper, queueFilepath string) error {
return nil
}

if !fileExists(queueFilepath) {
return nil
}

paramOffline := params.LoadOfflineParams(v)

paramAPI, err := params.LoadAPIParams(v)
Expand All @@ -96,10 +100,6 @@ func syncOfflineActivityLegacy(v *viper.Viper, queueFilepath string) error {
return fmt.Errorf("failed to initialize api client: %w", err)
}

if paramOffline.QueueFileLegacy != "" {
queueFilepath = paramOffline.QueueFileLegacy
}

handle := heartbeat.NewHandle(apiClient,
offline.WithSync(queueFilepath, paramOffline.SyncMax),
apikey.WithReplacing(apikey.Config{
Expand Down Expand Up @@ -135,10 +135,6 @@ func SyncOfflineActivity(v *viper.Viper, queueFilepath string) error {

paramOffline := params.LoadOfflineParams(v)

if paramOffline.QueueFile != "" {
queueFilepath = paramOffline.QueueFile
}

handle := heartbeat.NewHandle(apiClient,
offline.WithSync(queueFilepath, paramOffline.SyncMax),
apikey.WithReplacing(apikey.Config{
Expand All @@ -158,3 +154,9 @@ func SyncOfflineActivity(v *viper.Viper, queueFilepath string) error {

return nil
}

// fileExists checks if a file or directory exist.
func fileExists(fp string) bool {
_, err := os.Stat(fp)
return err == nil || os.IsExist(err)
}
Loading

0 comments on commit 7d81a02

Please sign in to comment.