Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip #1078

Closed
wants to merge 21 commits into from
50 changes: 23 additions & 27 deletions cmd/fileexperts/fileexperts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"testing"
"time"

"github.com/wakatime/wakatime-cli/cmd"
"github.com/wakatime/wakatime-cli/cmd/fileexperts"
"github.com/wakatime/wakatime-cli/pkg/api"
"github.com/wakatime/wakatime-cli/pkg/log"
Expand Down Expand Up @@ -86,7 +85,6 @@
v.Set("plugin", plugin)
v.Set("project", "wakatime-cli")
v.Set("entity", "testdata/main.go")
v.Set("file-experts", true)

output, err := fileexperts.FileExperts(v)
require.NoError(t, err)
Expand All @@ -97,39 +95,27 @@
}

func TestFileExperts_NonExistingEntity(t *testing.T) {
tmpDir := t.TempDir()

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

defer logFile.Close()

// logs := bytes.NewBuffer(nil)

// teardownLogCapture := captureLogs(logs)
// defer teardownLogCapture()

v := viper.New()
v.Set("key", "00000000-0000-4000-8000-000000000000")
v.Set("api-url", "https://example.org")
v.Set("entity", "nonexisting")
v.Set("file-experts", true)
v.Set("key", "00000000-0000-4000-8000-000000000000")
v.Set("log-file", logFile.Name())
v.Set("verbose", true)

cmd.SetupLogging(v)

defer func() {
if file, ok := log.Output().(*os.File); ok {
_ = file.Sync()
file.Close()
} else if handler, ok := log.Output().(io.Closer); ok {
handler.Close()
}
}()
log.SetVerbose(true)

_, err = fileexperts.FileExperts(v)
require.NoError(t, err)

output, err := io.ReadAll(logFile)
_, err := fileexperts.FileExperts(v)

Check failure on line 115 in cmd/fileexperts/fileexperts_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests Windows

no new variables on left side of :=

Check failure on line 115 in cmd/fileexperts/fileexperts_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests

no new variables on left side of :=

Check failure on line 115 in cmd/fileexperts/fileexperts_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests macOS

no new variables on left side of :=
require.NoError(t, err)

assert.Contains(t, string(output), "skipping because of non-existing file")
assert.Contains(t, logs.String(), "skipping because of non-existing file")

Check failure on line 118 in cmd/fileexperts/fileexperts_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests Windows

undefined: logs

Check failure on line 118 in cmd/fileexperts/fileexperts_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests

undefined: logs

Check failure on line 118 in cmd/fileexperts/fileexperts_test.go

View workflow job for this annotation

GitHub Actions / Unit Tests macOS

undefined: logs
}

func TestFileExperts_ErrApi(t *testing.T) {
Expand All @@ -148,7 +134,6 @@
v.Set("key", "00000000-0000-4000-8000-000000000000")
v.Set("api-url", testServerURL)
v.Set("entity", "testdata/main.go")
v.Set("file-experts", true)

_, err := fileexperts.FileExperts(v)
require.Error(t, err)
Expand Down Expand Up @@ -183,7 +168,6 @@
v.Set("key", "00000000-0000-4000-8000-000000000000")
v.Set("api-url", testServerURL)
v.Set("entity", "testdata/main.go")
v.Set("file-experts", true)

_, err := fileexperts.FileExperts(v)
require.Error(t, err)
Expand Down Expand Up @@ -217,7 +201,6 @@
v.Set("key", "00000000-0000-4000-8000-000000000000")
v.Set("api-url", testServerURL)
v.Set("entity", "testdata/main.go")
v.Set("file-experts", true)

_, err := fileexperts.FileExperts(v)
require.Error(t, err)
Expand All @@ -242,3 +225,16 @@

return srv.URL, router, func() { srv.Close() }
}

func captureLogs(dest io.Writer) func() {
logOutput := log.Output()

// will write to log output and dest
mw := io.MultiWriter(logOutput, dest)

log.SetOutput(mw)

return func() {
log.SetOutput(logOutput)
}
}
Loading
Loading