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 25, 2024
1 parent 35e88b4 commit c1b00fd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
52 changes: 44 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) {
resetSingletons(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) {
resetSingletons(t)

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

Expand Down Expand Up @@ -164,6 +168,8 @@ func TestSendHeartbeats_RateLimited(t *testing.T) {
}

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

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

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

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

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

Expand Down Expand Up @@ -292,8 +300,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 +346,8 @@ func TestSendHeartbeats_ExtraHeartbeats(t *testing.T) {
}

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

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

Expand Down Expand Up @@ -376,8 +384,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 +482,8 @@ func TestSendHeartbeats_ExtraHeartbeats_Sanitize(t *testing.T) {
}

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

tmpDir := t.TempDir()

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

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

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

Expand Down Expand Up @@ -586,8 +596,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 +660,8 @@ func TestSendHeartbeats_IsUnsavedEntity(t *testing.T) {
}

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

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

Expand Down Expand Up @@ -718,8 +728,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 +784,8 @@ func TestSendHeartbeats_NonExistingExtraHeartbeatsEntity(t *testing.T) {
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func TestRateLimited_NotLimited(t *testing.T) {
resetSingletons(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 +1147,8 @@ func TestRateLimited_NotLimited(t *testing.T) {
}

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

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

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

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

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

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

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

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

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

func resetSingletons(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) {
resetSingletons(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) {
resetSingletons(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) {
resetSingletons(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) {
resetSingletons(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) {
resetSingletons(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) {
resetSingletons(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 resetSingletons(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 c1b00fd

Please sign in to comment.