diff --git a/pkg/kn-source-pkg/test/e2e/basic_workflow_test.go b/pkg/kn-source-pkg/test/e2e/basic_workflow_test.go index c4960fe2..e984a692 100644 --- a/pkg/kn-source-pkg/test/e2e/basic_workflow_test.go +++ b/pkg/kn-source-pkg/test/e2e/basic_workflow_test.go @@ -24,7 +24,7 @@ import ( "gotest.tools/v3/assert" "knative.dev/client-pkg/pkg/util" - "knative.dev/client-pkg/pkg/util/lib/test" + "knative.dev/client-pkg/pkg/util/test" ) func TestBasicWorkflow(t *testing.T) { diff --git a/pkg/kn-source-pkg/test/e2e/common.go b/pkg/kn-source-pkg/test/e2e/common.go index a2c96fbb..d55423c0 100644 --- a/pkg/kn-source-pkg/test/e2e/common.go +++ b/pkg/kn-source-pkg/test/e2e/common.go @@ -15,7 +15,7 @@ package e2e import ( - "knative.dev/client-pkg/pkg/util/lib/test" + "knative.dev/client-pkg/pkg/util/test" ) type E2ETest struct { diff --git a/pkg/kn-source-pkg/test/e2e/kn_plugin.go b/pkg/kn-source-pkg/test/e2e/kn_plugin.go index 6a60a667..f23ca49f 100644 --- a/pkg/kn-source-pkg/test/e2e/kn_plugin.go +++ b/pkg/kn-source-pkg/test/e2e/kn_plugin.go @@ -22,23 +22,23 @@ import ( "strings" homedir "github.com/mitchellh/go-homedir" - "knative.dev/client-pkg/pkg/util/lib/test" + test2 "knative.dev/client-pkg/pkg/util/test" ) type knPlugin struct { - kn test.Kn + kn test2.Kn pluginName string pluginPath string install bool } // Run the KnPlugin returning a KnRunResult -func (kp *knPlugin) Run(args ...string) test.KnRunResult { +func (kp *knPlugin) Run(args ...string) test2.KnRunResult { if kp.install { err := kp.Install() if err != nil { fmt.Printf("error installing kn plugin: %s\n", err.Error()) - return test.KnRunResult{} + return test2.KnRunResult{} } defer kp.Uninstall() } @@ -46,7 +46,7 @@ func (kp *knPlugin) Run(args ...string) test.KnRunResult { } // Kn object to run `kn` -func (kp *knPlugin) Kn() test.Kn { +func (kp *knPlugin) Kn() test2.Kn { return kp.kn } @@ -153,9 +153,9 @@ func pluginArgs(pluginName string) []string { return pluginParts[1:] } -func RunKnPlugin(namespace string, pluginName string, args []string) test.KnRunResult { +func RunKnPlugin(namespace string, pluginName string, args []string) test2.KnRunResult { pluginArgs := pluginArgs(pluginName) args = append(args, []string{"--namespace", namespace}...) argsWithPlugin := append(pluginArgs, args...) - return test.RunKn(namespace, argsWithPlugin) + return test2.RunKn(namespace, argsWithPlugin) } diff --git a/pkg/kn/commands/namespaced_test.go b/pkg/kn/commands/namespaced_test.go index f5160865..d88dd1b4 100644 --- a/pkg/kn/commands/namespaced_test.go +++ b/pkg/kn/commands/namespaced_test.go @@ -19,7 +19,7 @@ import ( "path/filepath" "testing" - "knative.dev/client-pkg/pkg/util/lib/test" + "knative.dev/client-pkg/pkg/util/test" "k8s.io/client-go/tools/clientcmd" diff --git a/pkg/kn/commands/types_test.go b/pkg/kn/commands/types_test.go index a124b749..5b6e0fc9 100644 --- a/pkg/kn/commands/types_test.go +++ b/pkg/kn/commands/types_test.go @@ -21,7 +21,7 @@ import ( "strings" "testing" - "knative.dev/client-pkg/pkg/util/lib/test" + "knative.dev/client-pkg/pkg/util/test" "gotest.tools/v3/assert" "k8s.io/client-go/tools/clientcmd" diff --git a/pkg/kn/flags/podspec_helper_test.go b/pkg/kn/flags/podspec_helper_test.go index 977fdb62..04eafd8f 100644 --- a/pkg/kn/flags/podspec_helper_test.go +++ b/pkg/kn/flags/podspec_helper_test.go @@ -25,12 +25,11 @@ import ( "testing" "k8s.io/apimachinery/pkg/util/intstr" - - "k8s.io/apimachinery/pkg/api/resource" - "knative.dev/client-pkg/pkg/util/lib/test" + "knative.dev/client-pkg/pkg/util/test" "gotest.tools/v3/assert" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" "knative.dev/client-pkg/pkg/util" "knative.dev/pkg/ptr" ) diff --git a/pkg/serving/v1/gitops_test.go b/pkg/serving/v1/gitops_test.go index 4f4f1376..390f60d1 100644 --- a/pkg/serving/v1/gitops_test.go +++ b/pkg/serving/v1/gitops_test.go @@ -21,13 +21,13 @@ import ( "time" "gotest.tools/v3/assert" + libtest "knative.dev/client-pkg/pkg/util/test" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" servingtest "knative.dev/serving/pkg/testing/v1" - libtest "knative.dev/client-pkg/pkg/util/lib/test" "knative.dev/pkg/ptr" servingv1 "knative.dev/serving/pkg/apis/serving/v1" ) diff --git a/pkg/util/test/capture_output.go b/pkg/util/test/capture_output.go new file mode 100644 index 00000000..7a12b479 --- /dev/null +++ b/pkg/util/test/capture_output.go @@ -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() +// +// CaptureOutput() and capture.Close() should always come in pairs as Close() also +// restores the old streams +package test + +import ( + "io" + "os" + "testing" + + "gotest.tools/v3/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 := io.ReadAll(c.outRead) + assert.NilError(c.t, err) + errOutput, err := io.ReadAll(c.errorRead) + assert.NilError(c.t, err) + os.Stdout = c.oldStdout + os.Stderr = c.oldStderr + return string(outOutput), string(errOutput) +} diff --git a/pkg/util/lib/test/cli.go b/pkg/util/test/cli.go similarity index 100% rename from pkg/util/lib/test/cli.go rename to pkg/util/test/cli.go diff --git a/pkg/util/lib/test/flags.go b/pkg/util/test/flags.go similarity index 100% rename from pkg/util/lib/test/flags.go rename to pkg/util/test/flags.go diff --git a/pkg/util/lib/test/integration.go b/pkg/util/test/integration.go similarity index 100% rename from pkg/util/lib/test/integration.go rename to pkg/util/test/integration.go diff --git a/pkg/util/lib/test/result_collector.go b/pkg/util/test/result_collector.go similarity index 100% rename from pkg/util/lib/test/result_collector.go rename to pkg/util/test/result_collector.go diff --git a/pkg/util/lib/test/service.go b/pkg/util/test/service.go similarity index 100% rename from pkg/util/lib/test/service.go rename to pkg/util/test/service.go diff --git a/pkg/util/lib/test/utils.go b/pkg/util/test/utils.go similarity index 100% rename from pkg/util/lib/test/utils.go rename to pkg/util/test/utils.go