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

fix go vet error with go1.11 #954

Merged
merged 1 commit into from
Jul 31, 2018
Merged

fix go vet error with go1.11 #954

merged 1 commit into from
Jul 31, 2018

Conversation

dmitris
Copy link
Contributor

@dmitris dmitris commented Jul 20, 2018

currently builds of the package with go1.11 fail due to using %q qualifier with *string type in github/repos_contents_test.go:

$ go1.11beta2 tool vet .
github/repos_contents_test.go:59: T.Errorf format %q has arg tt.encoding of wrong type *string
github/repos_contents_test.go:62: T.Errorf format %q has arg tt.encoding of wrong type *string

In Travis, a number of PR builds fail with Go:master ("Allowed failures"), ex. https://travis-ci.org/google/go-github/builds/406237120

PR fixes the issue by treating the cases where tt.enconding and tt.content are nil or not nil separately and adding extra varaibles (encoding and content) for printing in error messages. It also adds a workaround for gofmt difference between go1.10 and go1.11 (as in golang/go#26228, Go commit: golang/go@542ea5a) by adding an empty line.

@googlebot googlebot added the cla: yes Indication that the PR author has signed a Google Contributor License Agreement. label Jul 20, 2018
@gmlewis
Copy link
Collaborator

gmlewis commented Jul 20, 2018

Since this affects only a test, why not just change the %qs to %vs?

@dmitris
Copy link
Contributor Author

dmitris commented Jul 21, 2018

%v does not show the string, only the pointer address - I thought it would be useful to see the actual string value (if not nil) in the error message. Let me know if you prefer to change it to %v, I can change it.

@gmlewis
Copy link
Collaborator

gmlewis commented Jul 21, 2018

Oh, right... doh. It seems like we could come up with a solution that has less repetition... like taking advantage of the same logic in the github-accessors.go file... for example:

...
got, err := r.GetContent()

stringOrNil := func(s *string) string {
  if s == nil {
    return "<nil>"
  }
  return *s
}
encoding := stringOrNil(tt.encoding)
content := stringOrNil(tt.content)
...

What do you think?

@dmitshur
Copy link
Member

dmitshur commented Jul 22, 2018

We'll need to think about a better long term way to deal with gofmt changes.

I think the best future-proof solution is to have the diff -u <(echo -n) <(gofmt -d -s .) check run only for one of the Go versions, not all of them. It might not be possible to make a single .go file compatible with gofmt of many Go versions (or possible only by sacrificing readability).

@dmitris
Copy link
Contributor Author

dmitris commented Jul 22, 2018

@gmlewis - changed to use stringOrNil as you suggested

return *s
}
encoding := stringOrNil(tt.encoding)
content := stringOrNil(tt.content)
Copy link
Member

@dmitshur dmitshur Jul 23, 2018

Choose a reason for hiding this comment

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

I don't think it's appropriate to add this much code in between got, err := ... and the if err != nil ... statements while keeping the variable name just err (see here). A future reader may think there's a missing error check after the got, err := ... line.

I would suggest factoring out the definition of stringOrNil to the very top of TestRepositoryContent_GetContent, or even outside to the package scope, below TestRepositoryContent_GetContent. And the stringOrNil(...) calls can just be inlined into the corresponding t.Errorf calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree, done with 2c7b329

Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

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

Thank you, @dmitris and @dmitshur!
I like this a lot better.
One tiny nit, one random musing (feel free to ignore), and then LGTM.
After that, awaiting second LGTM before merging.

if s == nil {
return "<nil>"
}
return *s
Copy link
Collaborator

Choose a reason for hiding this comment

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

random musing - what about making this line return fmt.Sprintf("%q", *s) to preserve the quoting?

Copy link
Contributor Author

@dmitris dmitris Jul 23, 2018

Choose a reason for hiding this comment

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

if we want the function call to have quotes around the parameters as in a "real" one, I think we should add them to the t.Errorf's format, and change the surrounding quotes markers:

		if err == nil && tt.wantErr {
			t.Errorf(`RepositoryContent("%s", "%s") did not return unexpected error`,
				stringOrNil(tt.encoding), stringOrNil(tt.content))
		}

- this would allow to keep stringOrNil doing straightforward conversion to string or "<nil>" without adding additional formatting (surrounding quotes).

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, this is not correct because <nil> would then be quoted like "<nil>" which is not right.
It should either be <nil> or "string-value".
But no big deal... we can ignore it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you are right; changed stringOrNil to use fmt.Sprintf("%q", *s) for quoting

}
if want := tt.want; got != want {
t.Errorf("RepositoryContent.GetContent returned %+v, want %+v", got, want)
}
}
}

// stringOrNil converts a potentially null string pointer to string
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add period to end of sentence.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@dmitris dmitris force-pushed the go111-fixvet branch 3 times, most recently from 83fed73 to 633f1e9 Compare July 24, 2018 08:44
github/repos_contents_test.go - use helper function to display string
or <nil> using string pointer.

github/misc_test.go - add a line break to allow passing gofmt check
with both go1.10 and go.11.
@dmitris
Copy link
Contributor Author

dmitris commented Jul 31, 2018

@gmlewis - could you merge this, or do you need the second approval? Thanks.

@gmlewis gmlewis requested review from juliaferraioli and removed request for juliaferraioli July 31, 2018 13:01
@gmlewis
Copy link
Collaborator

gmlewis commented Jul 31, 2018

@dmitris - Sorry for the delay. I think this is good-to-go and doesn't need a second LGTM now.
Merging.

@gmlewis gmlewis merged commit bffe4f4 into google:master Jul 31, 2018
@dmitris dmitris deleted the go111-fixvet branch July 31, 2018 13:30
gmlewis pushed a commit to gmlewis/go-github that referenced this pull request Aug 10, 2018
n1lesh pushed a commit to n1lesh/go-github that referenced this pull request Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Indication that the PR author has signed a Google Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants