Skip to content

Commit

Permalink
Moved CaptureOutput to "test" package for reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
rhuss committed Jun 18, 2020
1 parent a30f9b4 commit fdc9704
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 55 deletions.
6 changes: 3 additions & 3 deletions cmd/kn/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/spf13/cobra"
"gotest.tools/assert"

"knative.dev/client/pkg/kn/commands"
"knative.dev/client/lib/test"
"knative.dev/client/pkg/kn/root"
"knative.dev/client/pkg/util"
)
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestRunWithError(t *testing.T) {
},
}
for _, d := range data {
capture := commands.CaptureOutput(t)
capture := test.CaptureOutput(t)
printError(errors.New(d.given))
stdOut, errOut := capture.Close()

Expand All @@ -261,7 +261,7 @@ func TestRun(t *testing.T) {
os.Args = oldArgs
})()

capture := commands.CaptureOutput(t)
capture := test.CaptureOutput(t)
err := run(os.Args[1:])
out, _ := capture.Close()

Expand Down
77 changes: 77 additions & 0 deletions lib/test/capture_output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2020 The Knative Authors

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// OutputCapture allows to capture any text written to standard out or standard error
// which is especially useful during testing.
//
// Call it like:
//
// capture := CaptureOutput(t)
// doSomeActionThatWritesToStdOutAndStdErr()
// stdOut, stdErr := capture.Close()
//
// CaptureOutpu() and capture.Close() should always come in pairs as Close() also
// restores the old streams
package test

import (
"io/ioutil"
"os"
"testing"

"gotest.tools/assert"
)

type OutputCapture struct {
outRead, outWrite *os.File
errorRead, errorWrite *os.File
t *testing.T

oldStdout *os.File
oldStderr *os.File
}

// CaptureOutput sets up standard our and standard error to capture any
// output which
func CaptureOutput(t *testing.T) OutputCapture {
ret := OutputCapture{
oldStdout: os.Stdout,
oldStderr: os.Stderr,
t: t,
}
var err error
ret.outRead, ret.outWrite, err = os.Pipe()
assert.NilError(t, err)
os.Stdout = ret.outWrite
ret.errorRead, ret.errorWrite, err = os.Pipe()
assert.NilError(t, err)
os.Stderr = ret.errorWrite
return ret
}

// Close return the output collected and restores the original standard out and error streams
// (i.e. those that were present before the call to CaptureOutput).
func (c OutputCapture) Close() (string, string) {
err := c.outWrite.Close()
assert.NilError(c.t, err)
err = c.errorWrite.Close()
assert.NilError(c.t, err)
outOutput, err := ioutil.ReadAll(c.outRead)
assert.NilError(c.t, err)
errOutput, err := ioutil.ReadAll(c.errorRead)
assert.NilError(c.t, err)
os.Stdout = c.oldStdout
os.Stderr = c.oldStderr
return string(outOutput), string(errOutput)
}
3 changes: 2 additions & 1 deletion pkg/kn/commands/completion/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package completion
import (
"testing"

"knative.dev/client/lib/test"
"knative.dev/client/pkg/kn/commands"
"knative.dev/client/pkg/util"

Expand All @@ -35,7 +36,7 @@ func TestCompletionUsage(t *testing.T) {
func TestCompletionGeneration(t *testing.T) {
for _, shell := range []string{"bash", "zsh"} {
completionCmd := NewCompletionCommand(&commands.KnParams{})
c := commands.CaptureOutput(t)
c := test.CaptureOutput(t)
err := completionCmd.RunE(&cobra.Command{}, []string{shell})
assert.NilError(t, err)
stdOut, stdErr := c.Close()
Expand Down
43 changes: 0 additions & 43 deletions pkg/kn/commands/testing_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ package commands

import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/spf13/cobra"
"gotest.tools/assert"
"k8s.io/apimachinery/pkg/runtime"
clienttesting "k8s.io/client-go/testing"
servingv1fake "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1/fake"
Expand Down Expand Up @@ -93,46 +90,6 @@ func CreateDynamicTestKnCommand(cmd *cobra.Command, knParams *KnParams, objects

}

type OutputCapture struct {
outRead, outWrite *os.File
errorRead, errorWrite *os.File
t *testing.T

oldStdout *os.File
oldStderr *os.File
}

func CaptureOutput(t *testing.T) OutputCapture {
ret := OutputCapture{
oldStdout: os.Stdout,
oldStderr: os.Stderr,
t: t,
}
var err error
ret.outRead, ret.outWrite, err = os.Pipe()
assert.NilError(t, err)
os.Stdout = ret.outWrite
ret.errorRead, ret.errorWrite, err = os.Pipe()
assert.NilError(t, err)
os.Stderr = ret.errorWrite
return ret
}

// CaptureOutput collects the current content of os.Stdout
func (c OutputCapture) Close() (string, string) {
err := c.outWrite.Close()
assert.NilError(c.t, err)
err = c.errorWrite.Close()
assert.NilError(c.t, err)
outOutput, err := ioutil.ReadAll(c.outRead)
assert.NilError(c.t, err)
errOutput, err := ioutil.ReadAll(c.errorRead)
assert.NilError(c.t, err)
os.Stdout = c.oldStdout
os.Stderr = c.oldStderr
return string(outOutput), string(errOutput)
}

// NewTestCommand can be used by tes
func NewTestCommand(subCommand *cobra.Command, params *KnParams) *cobra.Command {
rootCmd := &cobra.Command{
Expand Down
4 changes: 3 additions & 1 deletion pkg/kn/commands/testing_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (

"github.com/spf13/cobra"
"gotest.tools/assert"

"knative.dev/client/lib/test"
)

func TestCreateTestKnCommand(t *testing.T) {
Expand Down Expand Up @@ -56,7 +58,7 @@ func TestCreateDynamicTestKnCommand(t *testing.T) {
}

func TestCaptureStdout(t *testing.T) {
c := CaptureOutput(t)
c := test.CaptureOutput(t)
fmt.Print("Hello World !")
stdOut, stdErr := c.Close()
assert.Equal(t, stdErr, "")
Expand Down
6 changes: 3 additions & 3 deletions pkg/templates/command_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/spf13/cobra"
"gotest.tools/assert"

"knative.dev/client/pkg/kn/commands"
"knative.dev/client/lib/test"
"knative.dev/client/pkg/util"
)

Expand Down Expand Up @@ -54,14 +54,14 @@ func TestSetUsage(t *testing.T) {
assert.Assert(t, cmd.DisableFlagsInUseLine)
}

capture := commands.CaptureOutput(t)
capture := test.CaptureOutput(t)
err := (rootCmd.UsageFunc())(rootCmd)
assert.NilError(t, err)
stdOut, stdErr := capture.Close()
assert.Equal(t, stdErr, "")
assert.Assert(t, util.ContainsAll(stdOut, "header-1", "header-2"))

capture = commands.CaptureOutput(t)
capture = test.CaptureOutput(t)
(rootCmd.HelpFunc())(rootCmd, nil)
stdOut, stdErr = capture.Close()
assert.Equal(t, stdErr, "")
Expand Down
8 changes: 4 additions & 4 deletions pkg/templates/template_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/spf13/cobra"
"gotest.tools/assert"

"knative.dev/client/pkg/kn/commands"
"knative.dev/client/lib/test"
"knative.dev/client/pkg/util"
)

Expand Down Expand Up @@ -60,7 +60,7 @@ func TestUsageFunc(t *testing.T) {
},
}
for _, d := range data {
capture := commands.CaptureOutput(t)
capture := test.CaptureOutput(t)
err := (engine.usageFunc())(d.cmd)
assert.NilError(t, err)
stdOut, stdErr := capture.Close()
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestHelpFunc(t *testing.T) {
},
}
for _, d := range data {
capture := commands.CaptureOutput(t)
capture := test.CaptureOutput(t)
(engine.helpFunc())(d.cmd, []string{})
stdOut, stdErr := capture.Close()

Expand All @@ -103,7 +103,7 @@ func TestHelpFunc(t *testing.T) {
func TestOptionsFunc(t *testing.T) {
rootCmd, _ := newTemplateEngine()
subCmd := rootCmd.Commands()[0]
capture := commands.CaptureOutput(t)
capture := test.CaptureOutput(t)
err := NewGlobalOptionsFunc()(subCmd)
assert.NilError(t, err)
stdOut, stdErr := capture.Close()
Expand Down

0 comments on commit fdc9704

Please sign in to comment.