Skip to content

Commit

Permalink
Merge pull request #1075 from wakatime/misc/singleton-stdin-unit-test
Browse files Browse the repository at this point in the history
Unit test for stdin singleton
  • Loading branch information
alanhamlett authored Jul 29, 2024
2 parents 9dd13b0 + c28a780 commit 65ec92a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
7 changes: 3 additions & 4 deletions cmd/heartbeat/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ func TestSendHeartbeats_RateLimited(t *testing.T) {
tmpFile, err := os.CreateTemp(t.TempDir(), "wakatime")
require.NoError(t, err)

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

v := viper.New()
v.SetDefault("sync-offline-activity", 1000)
Expand All @@ -162,11 +163,9 @@ func TestSendHeartbeats_RateLimited(t *testing.T) {
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)

Expand Down
79 changes: 79 additions & 0 deletions cmd/params/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2630,6 +2630,85 @@ func TestStatusBar_String(t *testing.T) {
)
}

func TestLoadHeartbeatParams_ExtraHeartbeats_StdinReadOnlyOnce(t *testing.T) {
r, w, err := os.Pipe()
require.NoError(t, err)

defer func() {
r.Close()
w.Close()
}()

origStdin := os.Stdin

defer func() { os.Stdin = origStdin }()

os.Stdin = r

cmdparams.Once = sync.Once{}

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

go func() {
_, err := w.Write(data)
require.NoError(t, err)

w.Close()
}()

v := viper.New()
v.Set("entity", "/path/to/file")
v.Set("extra-heartbeats", true)

params, err := cmdparams.LoadHeartbeatParams(v)
require.NoError(t, err)

assert.Len(t, params.ExtraHeartbeats, 2)
assert.Equal(t, "Golang", params.ExtraHeartbeats[0].LanguageAlternate)

r.Close()
w.Close()

// change stdin and make sure loading params uses old stdin
r, w, err = os.Pipe()
require.NoError(t, err)

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

go func() {
_, err := w.Write(data)
require.NoError(t, err)

w.Close()
}()

os.Stdin = r

v = viper.New()
v.Set("entity", "/path/to/file")
v.Set("extra-heartbeats", true)

params, err = cmdparams.LoadHeartbeatParams(v)
require.NoError(t, err)

assert.Len(t, params.ExtraHeartbeats, 2)
assert.Equal(t, "Golang", params.ExtraHeartbeats[0].LanguageAlternate)

v = viper.New()
v.Set("entity", "/path/to/file")
v.Set("extra-heartbeats", true)

cmdparams.Once = sync.Once{}

params, err = cmdparams.LoadHeartbeatParams(v)
require.NoError(t, err)

assert.Len(t, params.ExtraHeartbeats, 2)
assert.Empty(t, params.ExtraHeartbeats[0].LanguageAlternate)
}

func captureLogs(dest io.Writer) func() {
// set verbose
log.SetVerbose(true)
Expand Down

0 comments on commit 65ec92a

Please sign in to comment.