Skip to content

Commit

Permalink
Test that --logformat is deprecated but still works
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Mar 7, 2022
1 parent 66897c5 commit 1e8979f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
11 changes: 7 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type globalState struct {

outMutex *sync.Mutex
stdOut, stdErr *consoleWriter
stdIn *os.File
stdIn io.Reader

signalNotify func(chan<- os.Signal, ...os.Signal)
signalStop func(chan<- os.Signal)
Expand Down Expand Up @@ -205,6 +205,12 @@ func newRootCommand(gs *globalState) *rootCommand {
PersistentPreRunE: c.persistentPreRunE,
}

rootCmd.PersistentFlags().AddFlagSet(rootCmdPersistentFlagSet(gs))
rootCmd.SetArgs(gs.args[1:])
rootCmd.SetOut(gs.stdOut)
rootCmd.SetErr(gs.stdErr) // TODO: use gs.logger.WriterLevel(logrus.ErrorLevel)?
rootCmd.SetIn(gs.stdIn)

loginCmd := getLoginCmd()
loginCmd.AddCommand(
getLoginCloudCommand(gs),
Expand All @@ -216,10 +222,7 @@ func newRootCommand(gs *globalState) *rootCommand {
getStatsCmd(gs), getStatusCmd(gs), getVersionCmd(gs),
)

rootCmd.PersistentFlags().AddFlagSet(rootCmdPersistentFlagSet(gs))
rootCmd.SetArgs(gs.args[1:])
c.cmd = rootCmd

return c
}

Expand Down
30 changes: 28 additions & 2 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package cmd
import (
"bytes"
"context"
"os"
"os/signal"
"runtime"
"sync"
"testing"

"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/lib/testutils"
)
Expand Down Expand Up @@ -63,11 +63,37 @@ func newGlobalTestState(t *testing.T) *globalTestState {
outMutex: outMutex,
stdOut: &consoleWriter{nil, ts.stdOut, false, outMutex, nil},
stdErr: &consoleWriter{nil, ts.stdErr, false, outMutex, nil},
stdIn: os.Stdin, // TODO: spoof?
stdIn: new(bytes.Buffer),
signalNotify: signal.Notify,
signalStop: signal.Stop,
logger: logger,
fallbackLogger: testutils.NewLogger(t).WithField("fallback", true),
}
return ts
}

func TestDeprecatedOptionWarning(t *testing.T) {
t.Parallel()

ts := newGlobalTestState(t)
ts.args = []string{"k6", "--logformat", "json", "run", "-"}
ts.stdIn = bytes.NewBuffer([]byte(`
console.log('foo');
export default function() { console.log('bar'); };
`))

root := newRootCommand(ts.globalState)

require.NoError(t, root.cmd.Execute())

logMsgs := ts.loggerHook.Drain()
assert.True(t, testutils.LogContains(logMsgs, logrus.InfoLevel, "foo"))
assert.True(t, testutils.LogContains(logMsgs, logrus.InfoLevel, "bar"))
assert.Contains(t, ts.stdErr.String(), `"level":"info","msg":"foo","source":"console"`)
assert.Contains(t, ts.stdErr.String(), `"level":"info","msg":"bar","source":"console"`)

// TODO: after we get rid of cobra, actually emit this message to stderr
// and, ideally, through the log, not just print it...
assert.False(t, testutils.LogContains(logMsgs, logrus.InfoLevel, "logformat"))
assert.Contains(t, ts.stdOut.String(), `--logformat has been deprecated`)
}
12 changes: 3 additions & 9 deletions cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
"path"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -41,6 +41,7 @@ import (
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/js/common"
"go.k6.io/k6/lib/fsext"
"go.k6.io/k6/lib/testutils"
)

type mockWriter struct {
Expand Down Expand Up @@ -212,14 +213,7 @@ func TestRunScriptErrorsAndAbort(t *testing.T) {
}

if tc.expLogOutput != "" {
var gotMsg bool
for _, entry := range testState.loggerHook.Drain() {
if strings.Contains(entry.Message, tc.expLogOutput) {
gotMsg = true
break
}
}
assert.True(t, gotMsg)
assert.True(t, testutils.LogContains(testState.loggerHook.Drain(), logrus.InfoLevel, tc.expLogOutput))
}
})
}
Expand Down
12 changes: 12 additions & 0 deletions lib/testutils/logrus_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package testutils

import (
"strings"
"sync"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -57,3 +58,14 @@ func (smh *SimpleLogrusHook) Drain() []logrus.Entry {
}

var _ logrus.Hook = &SimpleLogrusHook{}

// LogContains is a helper function that checks the provided list of log entries
// for a message matching the provided level and contents.
func LogContains(logEntries []logrus.Entry, expLevel logrus.Level, expContents string) bool {
for _, entry := range logEntries {
if entry.Level == expLevel && strings.Contains(entry.Message, expContents) {
return true
}
}
return false
}

0 comments on commit 1e8979f

Please sign in to comment.