From 9e29ce893f05f8ccf26bf21f5bab11f95a000d4d Mon Sep 17 00:00:00 2001 From: "dr.max" Date: Tue, 9 Jul 2019 11:44:55 -0700 Subject: [PATCH] test(commands): backfills gotest.tools tests for version & completion cmds (#223) --- pkg/kn/commands/completion_test.go | 62 +++++++++++ .../{test_helper.go => help_testing.go} | 50 +++++++++ pkg/kn/commands/version_test.go | 103 ++++++++++++++++++ vendor/modules.txt | 2 +- 4 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 pkg/kn/commands/completion_test.go rename pkg/kn/commands/{test_helper.go => help_testing.go} (70%) create mode 100644 pkg/kn/commands/version_test.go diff --git a/pkg/kn/commands/completion_test.go b/pkg/kn/commands/completion_test.go new file mode 100644 index 0000000000..f108c74220 --- /dev/null +++ b/pkg/kn/commands/completion_test.go @@ -0,0 +1,62 @@ +// Copyright © 2018 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. + +package commands + +import ( + "testing" + + "github.com/spf13/cobra" + "gotest.tools/assert" +) + +func TestCompletion(t *testing.T) { + var ( + fakeRootCmd, completionCmd *cobra.Command + knParams *KnParams + ) + + setup := func() { + knParams = &KnParams{} + completionCmd = NewCompletionCommand(knParams) + + fakeRootCmd = &cobra.Command{} + fakeRootCmd.AddCommand(completionCmd) + } + + t.Run("creates a CompletionCommand", func(t *testing.T) { + setup() + assert.Equal(t, completionCmd.Use, "completion") + assert.Equal(t, completionCmd.Short, "Output shell completion code (default Bash)") + assert.Assert(t, completionCmd.RunE == nil) + }) + + t.Run("returns completion code for BASH", func(t *testing.T) { + setup() + CaptureStdout(t) + defer ReleaseStdout(t) + + completionCmd.Run(fakeRootCmd, []string{}) + assert.Assert(t, ReadStdout(t) != "") + }) + + t.Run("returns completion code for ZSH", func(t *testing.T) { + setup() + CaptureStdout(t) + defer ReleaseStdout(t) + + completionCmd.Run(fakeRootCmd, []string{"--zsh"}) + assert.Assert(t, ReadStdout(t) != "") + }) +} diff --git a/pkg/kn/commands/test_helper.go b/pkg/kn/commands/help_testing.go similarity index 70% rename from pkg/kn/commands/test_helper.go rename to pkg/kn/commands/help_testing.go index f4979dc924..0f3a84ded5 100644 --- a/pkg/kn/commands/test_helper.go +++ b/pkg/kn/commands/help_testing.go @@ -17,15 +17,30 @@ package commands import ( "bytes" "flag" + "io" + "os" + "testing" "github.com/knative/client/pkg/serving/v1alpha1" "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake" "github.com/spf13/cobra" + "gotest.tools/assert" client_testing "k8s.io/client-go/testing" ) const FakeNamespace = "current" +var ( + oldStdout *os.File + stdout *os.File + output string + + readFile, writeFile *os.File + + origArgs []string +) + +// CreateTestKnCommand helper for creating test commands func CreateTestKnCommand(cmd *cobra.Command, knParams *KnParams) (*cobra.Command, *fake.FakeServingV1alpha1, *bytes.Buffer) { buf := new(bytes.Buffer) fakeServing := &fake.FakeServingV1alpha1{&client_testing.Fake{}} @@ -38,6 +53,41 @@ func CreateTestKnCommand(cmd *cobra.Command, knParams *KnParams) (*cobra.Command return knCommand, fakeServing, buf } +// CaptureStdout collects the current content of os.Stdout +func CaptureStdout(t *testing.T) { + oldStdout = os.Stdout + var err error + readFile, writeFile, err = os.Pipe() + assert.Assert(t, err == nil) + stdout = writeFile + os.Stdout = writeFile +} + +// ReleaseStdout releases the os.Stdout and restores to original +func ReleaseStdout(t *testing.T) { + output = ReadStdout(t) + os.Stdout = oldStdout +} + +// ReadStdout returns the collected os.Stdout content +func ReadStdout(t *testing.T) string { + outC := make(chan string) + go func() { + var buf bytes.Buffer + io.Copy(&buf, readFile) + outC <- buf.String() + }() + writeFile.Close() + output = <-outC + + CaptureStdout(t) + + return output +} + +// Private + +// newKnCommand needed since calling the one in core would cause a import cycle func newKnCommand(subCommand *cobra.Command, params *KnParams) *cobra.Command { rootCmd := &cobra.Command{ Use: "kn", diff --git a/pkg/kn/commands/version_test.go b/pkg/kn/commands/version_test.go new file mode 100644 index 0000000000..921b67ea0d --- /dev/null +++ b/pkg/kn/commands/version_test.go @@ -0,0 +1,103 @@ +// Copyright © 2018 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. + +package commands + +import ( + "bytes" + "testing" + "text/template" + + "github.com/spf13/cobra" + "gotest.tools/assert" +) + +type versionOutput struct { + Version string + BuildDate string + GitRevision string + ServingVersion string +} + +var versionOutputTemplate = `Version: {{.Version}} +Build Date: {{.BuildDate}} +Git Revision: {{.GitRevision}} +Dependencies: +- serving: {{.ServingVersion}} +` + +const ( + fakeVersion = "fake-version" + fakeBuildDate = "fake-build-date" + fakeGitRevision = "fake-git-revision" + fakeServingVersion = "fake-serving-version" +) + +func TestVersion(t *testing.T) { + var ( + versionCmd *cobra.Command + knParams *KnParams + expectedVersionOutput string + ) + + setup := func() { + Version = fakeVersion + BuildDate = fakeBuildDate + GitRevision = fakeGitRevision + ServingVersion = fakeServingVersion + + expectedVersionOutput = genVersionOuput(t, versionOutputTemplate, + versionOutput{ + Version: fakeVersion, + BuildDate: fakeBuildDate, + GitRevision: fakeGitRevision, + ServingVersion: fakeServingVersion}) + + knParams = &KnParams{} + versionCmd = NewVersionCommand(knParams) + } + + t.Run("creates a VersionCommand", func(t *testing.T) { + setup() + CaptureStdout(t) + defer ReleaseStdout(t) + + assert.Equal(t, versionCmd.Use, "version") + assert.Equal(t, versionCmd.Short, "Prints the client version") + assert.Assert(t, versionCmd.RunE != nil) + }) + + t.Run("prints version, build date, git revision, and serving version string", func(t *testing.T) { + setup() + CaptureStdout(t) + defer ReleaseStdout(t) + + err := versionCmd.RunE(nil, []string{}) + assert.Assert(t, err == nil) + assert.Equal(t, ReadStdout(t), expectedVersionOutput) + }) +} + +// Private + +func genVersionOuput(t *testing.T, templ string, vOutput versionOutput) string { + tmpl, err := template.New("versionOutput").Parse(versionOutputTemplate) + assert.Assert(t, err == nil) + + buf := bytes.Buffer{} + err = tmpl.Execute(&buf, vOutput) + assert.Assert(t, err == nil) + + return buf.String() +} diff --git a/vendor/modules.txt b/vendor/modules.txt index d58400cdd1..d761a43c0e 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -167,8 +167,8 @@ gopkg.in/inf.v0 # gopkg.in/yaml.v2 v2.2.2 gopkg.in/yaml.v2 # gotest.tools v2.2.0+incompatible -gotest.tools/assert/cmp gotest.tools/assert +gotest.tools/assert/cmp gotest.tools/internal/format gotest.tools/internal/source gotest.tools/internal/difflib