mirror of https://github.com/k3s-io/k3s
remove unused func deprecatedAlias
parent
27fca554e1
commit
c1243ddd05
|
@ -79,7 +79,6 @@ go_test(
|
||||||
deps = [
|
deps = [
|
||||||
"//pkg/kubectl/cmd/util:go_default_library",
|
"//pkg/kubectl/cmd/util:go_default_library",
|
||||||
"//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library",
|
"//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library",
|
||||||
"//vendor/github.com/spf13/cobra:go_default_library",
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -555,18 +555,3 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
|
||||||
func runHelp(cmd *cobra.Command, args []string) {
|
func runHelp(cmd *cobra.Command, args []string) {
|
||||||
cmd.Help()
|
cmd.Help()
|
||||||
}
|
}
|
||||||
|
|
||||||
// deprecatedAlias is intended to be used to create a "wrapper" command around
|
|
||||||
// an existing command. The wrapper works the same but prints a deprecation
|
|
||||||
// message before running. This command is identical functionality.
|
|
||||||
func deprecatedAlias(deprecatedVersion string, cmd *cobra.Command) *cobra.Command {
|
|
||||||
// Have to be careful here because Cobra automatically extracts the name
|
|
||||||
// of the command from the .Use field.
|
|
||||||
originalName := cmd.Name()
|
|
||||||
|
|
||||||
cmd.Use = deprecatedVersion
|
|
||||||
cmd.Deprecated = fmt.Sprintf("use %q instead", originalName)
|
|
||||||
cmd.Short = fmt.Sprintf("%s. This command is deprecated, use %q instead", cmd.Short, originalName)
|
|
||||||
cmd.Hidden = true
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
|
@ -17,16 +17,12 @@ limitations under the License.
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
|
|
||||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||||
)
|
)
|
||||||
|
@ -57,56 +53,6 @@ func TestNormalizationFuncGlobalExistence(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_deprecatedAlias(t *testing.T) {
|
|
||||||
var correctCommandCalled bool
|
|
||||||
makeCobraCommand := func() *cobra.Command {
|
|
||||||
cobraCmd := new(cobra.Command)
|
|
||||||
cobraCmd.Use = "print five lines"
|
|
||||||
cobraCmd.Run = func(*cobra.Command, []string) {
|
|
||||||
correctCommandCalled = true
|
|
||||||
}
|
|
||||||
return cobraCmd
|
|
||||||
}
|
|
||||||
|
|
||||||
original := makeCobraCommand()
|
|
||||||
alias := deprecatedAlias("echo", makeCobraCommand())
|
|
||||||
|
|
||||||
if len(alias.Deprecated) == 0 {
|
|
||||||
t.Error("deprecatedAlias should always have a non-empty .Deprecated")
|
|
||||||
}
|
|
||||||
if !strings.Contains(alias.Deprecated, "print") {
|
|
||||||
t.Error("deprecatedAlias should give the name of the new function in its .Deprecated field")
|
|
||||||
}
|
|
||||||
if !alias.Hidden {
|
|
||||||
t.Error("deprecatedAlias should never have .Hidden == false (deprecated aliases should be hidden)")
|
|
||||||
}
|
|
||||||
|
|
||||||
if alias.Name() != "echo" {
|
|
||||||
t.Errorf("deprecatedAlias has name %q, expected %q",
|
|
||||||
alias.Name(), "echo")
|
|
||||||
}
|
|
||||||
if original.Name() != "print" {
|
|
||||||
t.Errorf("original command has name %q, expected %q",
|
|
||||||
original.Name(), "print")
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer := new(bytes.Buffer)
|
|
||||||
alias.SetOutput(buffer)
|
|
||||||
alias.Execute()
|
|
||||||
str := buffer.String()
|
|
||||||
if !strings.Contains(str, "deprecated") || !strings.Contains(str, "print") {
|
|
||||||
t.Errorf("deprecation warning %q does not include enough information", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// It would be nice to test to see that original.Run == alias.Run
|
|
||||||
// Unfortunately Golang does not allow comparing functions. I could do
|
|
||||||
// this with reflect, but that's technically invoking undefined
|
|
||||||
// behavior. Best we can do is make sure that the function is called.
|
|
||||||
if !correctCommandCalled {
|
|
||||||
t.Errorf("original function doesn't appear to have been called by alias")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestKubectlCommandHandlesPlugins(t *testing.T) {
|
func TestKubectlCommandHandlesPlugins(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
Loading…
Reference in New Issue