test(commands): backfills gotest.tools tests for version & completion cmds (#223)

This commit is contained in:
dr.max 2019-07-09 11:44:55 -07:00 committed by Knative Prow Robot
parent 2ae037f3be
commit 9e29ce893f
4 changed files with 216 additions and 1 deletions

View File

@ -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) != "")
})
}

View File

@ -17,15 +17,30 @@ package commands
import ( import (
"bytes" "bytes"
"flag" "flag"
"io"
"os"
"testing"
"github.com/knative/client/pkg/serving/v1alpha1" "github.com/knative/client/pkg/serving/v1alpha1"
"github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake" "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1/fake"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gotest.tools/assert"
client_testing "k8s.io/client-go/testing" client_testing "k8s.io/client-go/testing"
) )
const FakeNamespace = "current" 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) { func CreateTestKnCommand(cmd *cobra.Command, knParams *KnParams) (*cobra.Command, *fake.FakeServingV1alpha1, *bytes.Buffer) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
fakeServing := &fake.FakeServingV1alpha1{&client_testing.Fake{}} fakeServing := &fake.FakeServingV1alpha1{&client_testing.Fake{}}
@ -38,6 +53,41 @@ func CreateTestKnCommand(cmd *cobra.Command, knParams *KnParams) (*cobra.Command
return knCommand, fakeServing, buf 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 { func newKnCommand(subCommand *cobra.Command, params *KnParams) *cobra.Command {
rootCmd := &cobra.Command{ rootCmd := &cobra.Command{
Use: "kn", Use: "kn",

View File

@ -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()
}

2
vendor/modules.txt vendored
View File

@ -167,8 +167,8 @@ gopkg.in/inf.v0
# gopkg.in/yaml.v2 v2.2.2 # gopkg.in/yaml.v2 v2.2.2
gopkg.in/yaml.v2 gopkg.in/yaml.v2
# gotest.tools v2.2.0+incompatible # gotest.tools v2.2.0+incompatible
gotest.tools/assert/cmp
gotest.tools/assert gotest.tools/assert
gotest.tools/assert/cmp
gotest.tools/internal/format gotest.tools/internal/format
gotest.tools/internal/source gotest.tools/internal/source
gotest.tools/internal/difflib gotest.tools/internal/difflib