-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into kn-plugins4
- Loading branch information
Showing
7 changed files
with
397 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) != "") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// 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 plugin | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"gotest.tools/assert" | ||
) | ||
|
||
func TestPluginHandler(t *testing.T) { | ||
var ( | ||
pluginHandler PluginHandler | ||
pluginPath, pluginName, tmpPathDir string | ||
err error | ||
) | ||
|
||
setup := func(t *testing.T) { | ||
pluginHandler = &DefaultPluginHandler{ | ||
ValidPrefixes: []string{"kn"}, | ||
} | ||
assert.Assert(t, pluginHandler != nil) | ||
|
||
tmpPathDir, err = ioutil.TempDir("", "plugin_list") | ||
assert.Assert(t, err == nil) | ||
} | ||
|
||
cleanup := func(t *testing.T) { | ||
err = os.RemoveAll(tmpPathDir) | ||
assert.Assert(t, err == nil) | ||
} | ||
|
||
beforeEach := func(t *testing.T) { | ||
pluginName = "fake-plugin-name" | ||
pluginPath = CreateTestPluginInPath(t, "kn-"+pluginName, KnTestPluginScript, FileModeExecutable, tmpPathDir) | ||
assert.Assert(t, pluginPath != "") | ||
|
||
err = os.Setenv("PATH", tmpPathDir) | ||
assert.Assert(t, err == nil) | ||
} | ||
|
||
t.Run("#NewDefaultPluginHandler", func(t *testing.T) { | ||
setup(t) | ||
defer cleanup(t) | ||
|
||
pHandler := NewDefaultPluginHandler([]string{"kn"}) | ||
assert.Assert(t, pHandler != nil) | ||
}) | ||
|
||
t.Run("#Lookup", func(t *testing.T) { | ||
t.Run("returns the first filepath matching prefix", func(t *testing.T) { | ||
setup(t) | ||
defer cleanup(t) | ||
beforeEach(t) | ||
|
||
path, exists := pluginHandler.Lookup(pluginName) | ||
assert.Assert(t, path != "", fmt.Sprintf("no path when Lookup(%s)", pluginName)) | ||
assert.Assert(t, exists == true, fmt.Sprintf("could not Lookup(%s)", pluginName)) | ||
}) | ||
|
||
t.Run("returns empty filepath when no matching prefix found", func(t *testing.T) { | ||
setup(t) | ||
defer cleanup(t) | ||
|
||
path, exists := pluginHandler.Lookup("bogus-plugin-name") | ||
assert.Assert(t, path == "", fmt.Sprintf("unexpected plugin: kn-bogus-plugin-name")) | ||
assert.Assert(t, exists == false, fmt.Sprintf("unexpected plugin: kn-bogus-plugin-name")) | ||
}) | ||
}) | ||
|
||
t.Run("#Execute", func(t *testing.T) { | ||
t.Run("success while executing the executable with args and environment", func(t *testing.T) { | ||
setup(t) | ||
defer cleanup(t) | ||
beforeEach(t) | ||
|
||
err = pluginHandler.Execute(pluginPath, []string{pluginPath}, os.Environ()) | ||
assert.Assert(t, err == nil, fmt.Sprintf("test plugin %s failed executing", pluginName)) | ||
}) | ||
|
||
t.Run("fails while executing the executable with args and environment", func(t *testing.T) { | ||
setup(t) | ||
defer cleanup(t) | ||
beforeEach(t) | ||
|
||
bogusPath := filepath.Join(filepath.Dir(pluginPath), "kn-bogus-plugin-name") | ||
err = pluginHandler.Execute(bogusPath, []string{bogusPath}, os.Environ()) | ||
assert.Assert(t, err != nil, fmt.Sprintf("bogus plugin in path %s unexpectedly executed OK", bogusPath)) | ||
}) | ||
}) | ||
|
||
t.Run("HandlePluginCommand", func(t *testing.T) { | ||
t.Run("sucess handling", func(t *testing.T) { | ||
setup(t) | ||
defer cleanup(t) | ||
beforeEach(t) | ||
|
||
err = HandlePluginCommand(pluginHandler, []string{pluginPath, pluginName}) | ||
assert.Assert(t, err == nil, fmt.Sprintf("test plugin %s failed executing", pluginName)) | ||
}) | ||
|
||
t.Run("fails handling", func(t *testing.T) { | ||
setup(t) | ||
defer cleanup(t) | ||
|
||
err = HandlePluginCommand(pluginHandler, []string{pluginPath, "kn-bogus-plugin-name"}) | ||
assert.Assert(t, err != nil, fmt.Sprintf("test plugin %s expected to fail executing", pluginName)) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Oops, something went wrong.