From 5e1a3fd020a864ff065f997c873e5c4197f65e8d Mon Sep 17 00:00:00 2001 From: Alexander Campbell Date: Fri, 19 May 2017 14:49:03 -0700 Subject: [PATCH] kubectl: improve deprecatedAlias unit test Two new behaviors are tested: 1. The output message that deprecatedAlias gives when it is called must include the word "deprecatated" and the name of the new function that the user should use instead. 2. The correct function must be called by the alias (alias should "fall back" to the functionality of the original. --- pkg/kubectl/cmd/cmd_test.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/kubectl/cmd/cmd_test.go b/pkg/kubectl/cmd/cmd_test.go index efa359bc1b..6fec9f56fa 100644 --- a/pkg/kubectl/cmd/cmd_test.go +++ b/pkg/kubectl/cmd/cmd_test.go @@ -664,9 +664,13 @@ func genResponseWithJsonEncodedBody(bodyStruct interface{}) (*http.Response, err } 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 } @@ -692,8 +696,19 @@ func Test_deprecatedAlias(t *testing.T) { original.Name(), "print") } + buffer := new(bytes.Buffer) + alias.SetOutput(buffer) + alias.Execute() + str := buffer.String() + if !stdstrings.Contains(str, "deprecated") || !stdstrings.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. + // 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") + } }