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.
pull/6/head
Alexander Campbell 2017-05-19 14:49:03 -07:00
parent 487398ebf9
commit 5e1a3fd020
1 changed files with 16 additions and 1 deletions

View File

@ -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")
}
}