From c28a780a386ff9852b361bf8aa2fd4acdf8afe3f Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Mon, 29 Jul 2024 09:53:41 +0200 Subject: [PATCH] Unit test for stdin singleton --- cmd/heartbeat/heartbeat_test.go | 7 ++- cmd/params/params_test.go | 79 +++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/cmd/heartbeat/heartbeat_test.go b/cmd/heartbeat/heartbeat_test.go index 818e88b3..2adb6996 100644 --- a/cmd/heartbeat/heartbeat_test.go +++ b/cmd/heartbeat/heartbeat_test.go @@ -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) @@ -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) diff --git a/cmd/params/params_test.go b/cmd/params/params_test.go index 4738f53f..7138cb1f 100644 --- a/cmd/params/params_test.go +++ b/cmd/params/params_test.go @@ -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)