Skip to content

Commit

Permalink
Merge branch 'master' into kn-plugins4
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilien committed Jul 9, 2019
2 parents a4c94ae + 9e29ce8 commit b7941e4
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 10 deletions.
62 changes: 62 additions & 0 deletions pkg/kn/commands/completion_test.go
Original file line number Diff line number Diff line change
@@ -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) != "")
})
}
10 changes: 0 additions & 10 deletions pkg/kn/commands/test_helper.go → pkg/kn/commands/help_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ func CreateTestKnCommand(cmd *cobra.Command, knParams *KnParams) (*cobra.Command
return knCommand, fakeServing, buf
}

// TestContains is a test helper function, checking if a substring is present in given
// output string
func TestContains(t *testing.T, output string, sub []string, element string) {
for _, each := range sub {
if !strings.Contains(output, each) {
t.Errorf("Missing %s: %s", element, each)
}
}
}

// CaptureStdout collects the current content of os.Stdout
func CaptureStdout(t *testing.T) {
oldStdout = os.Stdout
Expand Down
103 changes: 103 additions & 0 deletions pkg/kn/commands/version_test.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit b7941e4

Please sign in to comment.