From ca899ec04882df9a148bc6c15f48e76af3cf7223 Mon Sep 17 00:00:00 2001 From: Alexander Campbell Date: Fri, 19 May 2017 11:24:53 -0700 Subject: [PATCH] kubectl: write unit test for deprecatedAlias() --- pkg/kubectl/cmd/cmd_test.go | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pkg/kubectl/cmd/cmd_test.go b/pkg/kubectl/cmd/cmd_test.go index 19514f2fa1..efa359bc1b 100644 --- a/pkg/kubectl/cmd/cmd_test.go +++ b/pkg/kubectl/cmd/cmd_test.go @@ -25,9 +25,12 @@ import ( "net/http" "os" "reflect" + stdstrings "strings" "testing" "time" + "github.com/spf13/cobra" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -659,3 +662,38 @@ func genResponseWithJsonEncodedBody(bodyStruct interface{}) (*http.Response, err } return &http.Response{StatusCode: 200, Header: defaultHeader(), Body: bytesBody(jsonBytes)}, nil } + +func Test_deprecatedAlias(t *testing.T) { + makeCobraCommand := func() *cobra.Command { + cobraCmd := new(cobra.Command) + cobraCmd.Use = "print five lines" + return cobraCmd + } + + original := makeCobraCommand() + alias := deprecatedAlias("echo", makeCobraCommand()) + + if len(alias.Deprecated) == 0 { + t.Error("deprecatedAlias should always have a non-empty .Deprecated") + } + if !stdstrings.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") + } + + // 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. +}