Skip to content

Commit

Permalink
Cleanup tests and remove unused pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed Jul 29, 2024
1 parent 35e88b4 commit 2ace324
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
58 changes: 50 additions & 8 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,11 @@ func TestSendHeartbeats_RateLimited(t *testing.T) {
numCalls++
})

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

defer tmpFile.Close()

v := viper.New()
v.SetDefault("sync-offline-activity", 1000)
v.Set("api-url", testServerURL)
Expand All @@ -152,6 +161,7 @@ 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("internal.heartbeats_last_sent_at", time.Now().Add(-time.Minute).Format(time.RFC3339))

offlineQueueFile, err := os.CreateTemp(t.TempDir(), "")
Expand All @@ -164,6 +174,8 @@ func TestSendHeartbeats_RateLimited(t *testing.T) {
}

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

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

Expand Down Expand Up @@ -198,6 +210,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 +306,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 +352,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 +390,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 +488,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 +532,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 +602,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 +666,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 +734,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 +790,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 +827,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 +892,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 +958,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 +1044,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 +1131,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 +1142,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 +1153,8 @@ func TestRateLimited_NotLimited(t *testing.T) {
}

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

p := cmdheartbeat.RateLimitParams{
Disabled: true,
}
Expand All @@ -1133,6 +1163,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 +1173,8 @@ func TestRateLimited_TimeoutZero(t *testing.T) {
}

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

p := cmdheartbeat.RateLimitParams{
Timeout: 0,
}
Expand All @@ -1149,6 +1183,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 +1252,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{}
}
20 changes: 20 additions & 0 deletions cmd/offlinesync/offlinesync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"net/http/httptest"
"os"
"strings"
"sync"
"testing"
"time"

"github.com/wakatime/wakatime-cli/cmd/offlinesync"
cmdparams "github.com/wakatime/wakatime-cli/cmd/params"
"github.com/wakatime/wakatime-cli/pkg/exitcode"
"github.com/wakatime/wakatime-cli/pkg/heartbeat"

Expand All @@ -22,6 +24,8 @@ import (
)

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

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

Expand Down Expand Up @@ -112,6 +116,8 @@ func TestRunWithRateLimiting(t *testing.T) {
}

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

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

Expand Down Expand Up @@ -202,6 +208,8 @@ func TestRunWithoutRateLimiting(t *testing.T) {
}

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

v := viper.New()
v.Set("key", "00000000-0000-4000-8000-000000000000")
v.Set("heartbeat-rate-limit-seconds", 500)
Expand All @@ -214,6 +222,8 @@ func TestRunWithRateLimiting_RateLimited(t *testing.T) {
}

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

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

Expand Down Expand Up @@ -302,6 +312,8 @@ func TestSyncOfflineActivity(t *testing.T) {
}

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

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

Expand Down Expand Up @@ -391,6 +403,8 @@ func TestSyncOfflineActivity_QueueFileFromConfig(t *testing.T) {
}

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

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

Expand Down Expand Up @@ -515,3 +529,9 @@ func insertHeartbeatRecord(t *testing.T, db *bolt.DB, bucket string, h heartbeat
})
require.NoError(t, err)
}

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

cmdparams.Once = sync.Once{}
}
6 changes: 3 additions & 3 deletions cmd/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ func readAPIKeyFromCommand(cmdStr string) (string, error) {
return strings.TrimSpace(string(out)), nil
}

var extraHeartbeatsCache *[]heartbeat.Heartbeat // nolint:gochecknoglobals
var extraHeartbeatsCache []heartbeat.Heartbeat // nolint:gochecknoglobals

// Once prevents reading from stdin twice.
var Once sync.Once // nolint:gochecknoglobals
Expand All @@ -769,10 +769,10 @@ func readExtraHeartbeats() []heartbeat.Heartbeat {
log.Errorf("failed parsing: %s", err)
}

extraHeartbeatsCache = &heartbeats
extraHeartbeatsCache = heartbeats
})

return *extraHeartbeatsCache
return extraHeartbeatsCache
}

func parseExtraHeartbeats(data string) ([]heartbeat.Heartbeat, error) {
Expand Down

0 comments on commit 2ace324

Please sign in to comment.