-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enabled golinter errcheck
option
#1014
Conversation
and fixed all the resulting linter errors.
{[]string{}, "expecting exactly one argument - \"instances\"", nil}, // 3 | ||
{[]string{"somethingelse"}, "expecting \"instances\" and not \"somethingelse\"", nil}, // 4 | ||
{[]string{"instances"}, "expecting \"instances\" and not \"somethingelse\"", []string{"test"}}, // 5 | ||
{[]string{"test"}}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these test cases already exist above in TestValidate
and were effectively a no-op here.
if err.Error() != tt.err { | ||
t.Errorf("Expecting error message '%s' but got '%s'", tt.err, err) | ||
} | ||
if len(tt.err) > 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a pattern that we use in a few places and it is wrong: on the left, we fail to test the case where the resulting err == nil
but should be something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Can we test for != ""
here instead? I associate len
with an array and while a string is an array of chars, this isn't the first thing that comes to mind when seeing this
if len(tt.err) > 0 { | |
if tt.err != "" { |
and fixed `kubeconfig` location for `windows`
@@ -17,27 +18,24 @@ func TestUpdateCommand_Validation(t *testing.T) { | |||
name string | |||
args []string | |||
instanceName string | |||
parameters map[string]string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
parameters
were effectively not tested here.
@@ -74,9 +73,5 @@ and serves as an API aggregation layer. | |||
func initGlobalFlags(cmd *cobra.Command, out io.Writer) { | |||
flags := cmd.PersistentFlags() | |||
Settings.AddFlags(flags) | |||
clog.Init(flags, out) | |||
// FIXME: add error handling | |||
cmd.ParseFlags(os.Args[1:]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ParseFlags()
call had no effect. Env var overrides are now taken directly as defaults when adding the cobra flag.
// AddFlags binds flags to the given flagset. | ||
func (s *Settings) AddFlags(fs *pflag.FlagSet) { | ||
fs.StringVar((*string)(&s.Home), "home", DefaultKudoHome, "location of your KUDO config.") | ||
fs.StringVar(&s.KubeConfig, "kubeconfig", os.Getenv("HOME")+"/.kube/config", "Path to your Kubernetes configuration file.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kubeconfig
location on GOOS == "windows"
was broken since there is no os.Getenv("HOME")
there.
@@ -584,36 +590,48 @@ func WithNamespace(obj runtime.Object, namespace string) runtime.Object { | |||
} | |||
|
|||
// WithSpec applies the provided spec to the Kubernetes object. | |||
func WithSpec(obj runtime.Object, spec map[string]interface{}) runtime.Object { | |||
return WithKeyValue(obj, "spec", spec) | |||
func WithSpec(t *testing.T, obj runtime.Object, spec map[string]interface{}) runtime.Object { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not very proud of this solution: passing in t *testing.T
and failing the test here. But, the way WithSpec
, WithStatus
and WithLabels
methods are used to construct testing tables, makes it hard to do anything else meaningfully. At least we actually fail now if something goes wrong here.
} | ||
|
||
// WithKeyValue sets key in the provided object to value. | ||
func WithKeyValue(obj runtime.Object, key string, value map[string]interface{}) runtime.Object { | ||
func WithKeyValue(obj runtime.Object, key string, value map[string]interface{}) (runtime.Object, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was fun: we would ignore two ways this method could fail return the copy of the passed object instead. Which does not make the debugging of tests using this method easy.
// AddFlags binds flags to the given flagset. | ||
func (s *Settings) AddFlags(fs *pflag.FlagSet) { | ||
fs.StringVar((*string)(&s.Home), "home", DefaultKudoHome, "location of your KUDO config.") | ||
fs.StringVar(&s.KubeConfig, "kubeconfig", os.Getenv("HOME")+"/.kube/config", "Path to your Kubernetes configuration file.") | ||
fs.StringVar((*string)(&s.Home), "home", kudoHome(), "location of your KUDO config.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The env vars overrides (if exist) are used as the default for the cobra flag.
This is a huge improvement, well done. It's nice that we can be much more confident in our tests and everything as a whole. This is such a far-reaching PR that there can always be other refactoring to do, but want to keep this specific to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks for fixing all these missing cases and enabled errcheck
! Just a few comments regarding the use of an additional assertion library. testify
, which is already used, should be able to do this as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loving this cleanup, thanks!
var DefaultKudoHome = filepath.Join(homedir.HomeDir(), ".kudo") | ||
var DefaultKubeConfig = filepath.Join(homedir.HomeDir(), "/.kube/config") | ||
|
||
func kudoHome() string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yaaay!
and fixed all the resulting linter errors.
Co-authored-by: Alena Varkockova avarkockova@mesosphere.com