Skip to content

Commit

Permalink
Fix tests with Once singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed Jul 24, 2024
1 parent 374d77d commit 49c08f3
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 190 deletions.
11 changes: 11 additions & 0 deletions cmd/heartbeat/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"

"github.com/wakatime/wakatime-cli/cmd"
cmdheartbeat "github.com/wakatime/wakatime-cli/cmd/heartbeat"
cmdparams "github.com/wakatime/wakatime-cli/cmd/params"
"github.com/wakatime/wakatime-cli/pkg/api"
"github.com/wakatime/wakatime-cli/pkg/heartbeat"
"github.com/wakatime/wakatime-cli/pkg/ini"
Expand Down Expand Up @@ -290,6 +292,8 @@ 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 @@ -372,6 +376,8 @@ 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 @@ -512,6 +518,7 @@ func TestSendHeartbeats_NonExistingEntity(t *testing.T) {
}

func TestSendHeartbeats_IsUnsavedEntity(t *testing.T) {

Check failure on line 520 in cmd/heartbeat/heartbeat_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests

block should not start with a whitespace (wsl)

Check failure on line 520 in cmd/heartbeat/heartbeat_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests macOS

block should not start with a whitespace (wsl)

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

Expand Down Expand Up @@ -580,6 +587,8 @@ 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 @@ -710,6 +719,8 @@ 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
6 changes: 4 additions & 2 deletions cmd/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,12 @@ func readAPIKeyFromCommand(cmdStr string) (string, error) {
}

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

// Once prevents reading from stdin twice
var Once sync.Once // nolint:gochecknoglobals

func readExtraHeartbeats() []heartbeat.Heartbeat {
once.Do(func() {
Once.Do(func() {
in := bufio.NewReader(os.Stdin)

input, err := in.ReadString('\n')
Expand Down
Loading

0 comments on commit 49c08f3

Please sign in to comment.