From 1e8979f49c97131597b41c6a904af05c8df97786 Mon Sep 17 00:00:00 2001 From: Nedyalko Andreev Date: Sun, 6 Mar 2022 17:00:12 +0200 Subject: [PATCH] Test that --logformat is deprecated but still works --- cmd/root.go | 11 +++++++---- cmd/root_test.go | 30 ++++++++++++++++++++++++++++-- cmd/run_test.go | 12 +++--------- lib/testutils/logrus_hook.go | 12 ++++++++++++ 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 1d2c70428a28..49b30d06f5c2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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) @@ -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), @@ -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 } diff --git a/cmd/root_test.go b/cmd/root_test.go index f8ecaeb6f6cf..c620a8c5a1d3 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -3,7 +3,6 @@ package cmd import ( "bytes" "context" - "os" "os/signal" "runtime" "sync" @@ -11,6 +10,7 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/afero" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.k6.io/k6/lib/testutils" ) @@ -63,7 +63,7 @@ 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, @@ -71,3 +71,29 @@ func newGlobalTestState(t *testing.T) *globalTestState { } 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`) +} diff --git a/cmd/run_test.go b/cmd/run_test.go index a11e9800de20..ba1af0cfc7f8 100644 --- a/cmd/run_test.go +++ b/cmd/run_test.go @@ -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" @@ -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 { @@ -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)) } }) } diff --git a/lib/testutils/logrus_hook.go b/lib/testutils/logrus_hook.go index 5c41d855639b..2436e9ffb98f 100644 --- a/lib/testutils/logrus_hook.go +++ b/lib/testutils/logrus_hook.go @@ -21,6 +21,7 @@ package testutils import ( + "strings" "sync" "github.com/sirupsen/logrus" @@ -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 +}