Skip to content

Commit

Permalink
Merge pull request #1074 from wakatime/bugfix/fix-tests-with-once
Browse files Browse the repository at this point in the history
Fix tests with Once singleton
  • Loading branch information
alanhamlett authored Jul 24, 2024
2 parents 374d77d + ac9acf6 commit 35e88b4
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 190 deletions.
10 changes: 10 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 @@ -580,6 +586,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 +718,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 35e88b4

Please sign in to comment.