Skip to content
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

Merged
merged 5 commits into from
Oct 30, 2019
Merged

Enabled golinter errcheck option #1014

merged 5 commits into from
Oct 30, 2019

Conversation

zen-dog
Copy link
Contributor

@zen-dog zen-dog commented Oct 29, 2019

and fixed all the resulting linter errors.

Co-authored-by: Alena Varkockova avarkockova@mesosphere.com

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"}},
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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.

Copy link
Member

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

Suggested change
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
Copy link
Contributor Author

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:])
Copy link
Contributor Author

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.")
Copy link
Contributor Author

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 {
Copy link
Contributor Author

@zen-dog zen-dog Oct 29, 2019

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) {
Copy link
Contributor Author

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.")
Copy link
Contributor Author

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.

@gerred
Copy link
Member

gerred commented Oct 29, 2019

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 errcheck unless there was anything glaring - and there's not. 👍

Copy link
Member

@nfnt nfnt left a 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.

pkg/kudoctl/cmd/get/get_test.go Show resolved Hide resolved
pkg/kudoctl/cmd/install/install_test.go Show resolved Hide resolved
Copy link
Contributor

@alenkacz alenkacz left a 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!

pkg/kudoctl/cmd/get/get_test.go Show resolved Hide resolved
var DefaultKudoHome = filepath.Join(homedir.HomeDir(), ".kudo")
var DefaultKubeConfig = filepath.Join(homedir.HomeDir(), "/.kube/config")

func kudoHome() string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaaay!

@zen-dog zen-dog merged commit 95877c1 into master Oct 30, 2019
@zen-dog zen-dog deleted the ad/enable-errcheck branch October 30, 2019 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants