mirror of https://github.com/k3s-io/k3s
Merge pull request #71906 from Pingan2017/remove-deprecatedAlias
remove unused func deprecatedAliask3s-v1.15.3
commit
7511e93371
|
@ -80,7 +80,6 @@ go_test(
|
|||
deps = [
|
||||
"//pkg/kubectl/cmd/util:go_default_library",
|
||||
"//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library",
|
||||
"//vendor/github.com/spf13/cobra:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
|
|
|
@ -572,18 +572,3 @@ func NewKubectlCommand(in io.Reader, out, err io.Writer) *cobra.Command {
|
|||
func runHelp(cmd *cobra.Command, args []string) {
|
||||
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
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
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) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
Loading…
Reference in New Issue