mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-03 19:58:17 +00:00 
			
		
		
		
	kubectl: write unit test for deprecatedAlias()
This commit is contained in:
		@@ -25,9 +25,12 @@ import (
 | 
				
			|||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"reflect"
 | 
						"reflect"
 | 
				
			||||||
 | 
						stdstrings "strings"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/spf13/cobra"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
						metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/runtime"
 | 
						"k8s.io/apimachinery/pkg/runtime"
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/runtime/schema"
 | 
						"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
 | 
						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.
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user