Skip to content

Commit

Permalink
fixup! Cleanup tests and remove unused pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Jul 28, 2024
1 parent deafaf7 commit 232adb7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 33 deletions.
14 changes: 12 additions & 2 deletions cmd/heartbeat/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,13 @@ func TestSendHeartbeats_ErrBackoff(t *testing.T) {

defer logFile.Close()

v := viper.New()
tmpFile, err := os.CreateTemp(tmpDir, "wakatime")
require.NoError(t, err)

defer tmpFile.Close()

v := viper.New()
v.Set("internal-config", tmpFile.Name())
v.Set("internal.backoff_at", time.Now().Add(10*time.Minute).Format(ini.DateFormat))
v.Set("internal.backoff_retries", "1")
v.SetDefault("sync-offline-activity", 1000)
Expand Down Expand Up @@ -912,8 +917,13 @@ func TestSendHeartbeats_ErrBackoff_Verbose(t *testing.T) {

defer logFile.Close()

v := viper.New()
tmpFile, err := os.CreateTemp(tmpDir, "wakatime")
require.NoError(t, err)

defer tmpFile.Close()

v := viper.New()
v.Set("internal-config", tmpFile.Name())
v.Set("internal.backoff_at", time.Now().Add(10*time.Minute).Format(ini.DateFormat))
v.Set("internal.backoff_retries", "1")
v.SetDefault("sync-offline-activity", 1000)
Expand Down
6 changes: 6 additions & 0 deletions cmd/offlinesync/offlinesync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,15 @@ func TestRunWithoutRateLimiting(t *testing.T) {
func TestRunWithRateLimiting_RateLimited(t *testing.T) {
resetSingleton(t)

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

defer tmpFile.Close()

v := viper.New()
v.Set("key", "00000000-0000-4000-8000-000000000000")
v.Set("heartbeat-rate-limit-seconds", 500)
v.Set("internal-config", tmpFile.Name())
v.Set("internal.heartbeats_last_sent_at", time.Now().Add(-time.Minute).Format(time.RFC3339))

code, err := offlinesync.RunWithRateLimiting(v)
Expand Down
2 changes: 2 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func RunE(cmd *cobra.Command, v *viper.Viper) error {
log.Fatalf("failed to setup logging: %s", err)
}

defer log.Close()

Check failure on line 56 in cmd/run.go

View workflow job for this annotation

GitHub Actions / Unit Tests

Error return value of `log.Close` is not checked (errcheck)

Check failure on line 56 in cmd/run.go

View workflow job for this annotation

GitHub Actions / Unit Tests macOS

Error return value of `log.Close` is not checked (errcheck)

err = parseConfigFiles(v)
if err != nil {
log.Errorf("failed to parse config files: %s", err)
Expand Down
28 changes: 18 additions & 10 deletions cmd/run_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"
"net/http/httptest"
"os"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -111,10 +110,6 @@ func TestRunCmd_ErrOfflineEnqueue(t *testing.T) {
}

func TestRunCmd_BackoffLoggedWithVerbose(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping because OS is windows.")
}

verbose := true

testServerURL, router, tearDown := setupTestServer()
Expand All @@ -135,6 +130,11 @@ func TestRunCmd_BackoffLoggedWithVerbose(t *testing.T) {

defer logFile.Close()

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

defer tmpFile.Close()

offlineQueueFile, err := os.CreateTemp(tmpDir, "")
require.NoError(t, err)

Expand All @@ -145,12 +145,19 @@ func TestRunCmd_BackoffLoggedWithVerbose(t *testing.T) {

defer entity.Close()

t.Logf("log file: %s", logFile.Name())
t.Logf("tmp file: %s", tmpFile.Name())
t.Logf("offline queue file: %s", offlineQueueFile.Name())
t.Logf("entity file: %s", entity.Name())

v := viper.New()
v.Set("api-url", testServerURL)
v.Set("entity", entity.Name())
v.Set("key", "00000000-0000-4000-8000-000000000000")
v.Set("log-file", logFile.Name())
v.Set("offline-queue-file", offlineQueueFile.Name())
v.Set("config", tmpFile.Name())
v.Set("internal-config", tmpFile.Name())
v.Set("internal.backoff_at", time.Now().Add(10*time.Minute).Format(ini.DateFormat))
v.Set("internal.backoff_retries", "1")
v.Set("verbose", verbose)
Expand All @@ -164,7 +171,6 @@ func TestRunCmd_BackoffLoggedWithVerbose(t *testing.T) {
require.ErrorAs(t, err, &errexitcode)

assert.Equal(t, exitcode.ErrBackoff, err.(exitcode.Err).Code)

assert.Equal(t, 0, numCalls)

output, err := io.ReadAll(logFile)
Expand All @@ -174,10 +180,6 @@ func TestRunCmd_BackoffLoggedWithVerbose(t *testing.T) {
}

func TestRunCmd_BackoffNotLogged(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping because OS is windows.")
}

verbose := false

testServerURL, router, tearDown := setupTestServer()
Expand All @@ -203,6 +205,11 @@ func TestRunCmd_BackoffNotLogged(t *testing.T) {

defer logFile.Close()

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

defer tmpFile.Close()

entity, err := os.CreateTemp(tmpDir, "")
require.NoError(t, err)

Expand All @@ -214,6 +221,7 @@ func TestRunCmd_BackoffNotLogged(t *testing.T) {
v.Set("key", "00000000-0000-4000-8000-000000000000")
v.Set("log-file", logFile.Name())
v.Set("offline-queue-file", offlineQueueFile.Name())
v.Set("internal-config", tmpFile.Name())
v.Set("internal.backoff_at", time.Now().Add(10*time.Minute).Format(ini.DateFormat))
v.Set("internal.backoff_retries", "1")
v.Set("verbose", verbose)
Expand Down
4 changes: 4 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func new() *l.Entry {
return entry
}

func Close() error {

Check warning on line 80 in pkg/log/log.go

View workflow job for this annotation

GitHub Actions / Unit Tests

exported: exported function Close should have comment or be unexported (revive)

Check warning on line 80 in pkg/log/log.go

View workflow job for this annotation

GitHub Actions / Unit Tests macOS

exported: exported function Close should have comment or be unexported (revive)
return logEntry.Logger.Out.(*os.File).Close()
}

// Output returns the current log output.
func Output() io.Writer {
return logEntry.Logger.Out
Expand Down
4 changes: 0 additions & 4 deletions pkg/project/tfvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
)

func TestTfvc_Detect(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping because OS is windows.")
}

fp := setupTestTfvc(t, ".tf")

s := project.Tfvc{
Expand Down
17 changes: 0 additions & 17 deletions pkg/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -64,10 +63,6 @@ func TestNewClient_Err(t *testing.T) {
}

func TestWithDetection_SshConfig_Hostname(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping because OS is Windows.")
}

shutdown, host, port := testServer(t, false)
defer shutdown()

Expand Down Expand Up @@ -133,10 +128,6 @@ func TestWithDetection_SshConfig_Hostname(t *testing.T) {
}

func TestWithDetection_SshConfig_UserKnownHostsFile_Mismatch(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping because OS is Windows.")
}

logs := bytes.NewBuffer(nil)

teardownLogCapture := captureLogs(logs)
Expand Down Expand Up @@ -199,10 +190,6 @@ func TestWithDetection_SshConfig_UserKnownHostsFile_Mismatch(t *testing.T) {
}

func TestWithDetection_SshConfig_UserKnownHostsFile_Match(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping because OS is Windows.")
}

logs := bytes.NewBuffer(nil)

teardownLogCapture := captureLogs(logs)
Expand Down Expand Up @@ -282,10 +269,6 @@ func TestWithDetection_SshConfig_UserKnownHostsFile_Match(t *testing.T) {
}

func TestWithDetection_Filtered(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping because OS is Windows.")
}

logs := bytes.NewBuffer(nil)

teardownLogCapture := captureLogs(logs)
Expand Down

0 comments on commit 232adb7

Please sign in to comment.