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

chore: Lint tests #5709

Closed
wants to merge 35 commits into from
Closed

chore: Lint tests #5709

wants to merge 35 commits into from

Conversation

faddat
Copy link
Contributor

@faddat faddat commented Aug 6, 2023

THis PR lints the tests in caddy, too.

  • adds thelper linter (gives additional context in your editor when tests fail)
  • adds revive linter
  • lints the entire codebase
  • adds nolintlint so that nolint statements don't hang around where they're not needed

@CLAassistant
Copy link

CLAassistant commented Aug 6, 2023

CLA assistant check
All committers have signed the CLA.

@faddat faddat mentioned this pull request Aug 6, 2023
Copy link
Contributor Author

@faddat faddat left a comment

Choose a reason for hiding this comment

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

reviews sometimes can be quite not ergonomic

(this comment was required to complete a review)

@mholt
Copy link
Member

mholt commented Aug 14, 2023

Hmmm. I disagree with the linter in some places here. I think a field name like publicKey is perfect because it's the final result of processing the PublicKeys field after provisioning from config.

@faddat
Copy link
Contributor Author

faddat commented Aug 14, 2023

@mholt -- indeed, I disagreed with it too :).

Please lmk if I made any changes based on its disagreeable choices.

I basically finished this one out by disabling a few things:

  • confusing-naming
  • unhandled-error
  • import-shadowing

I'f you'd like any of them on, please let me know that too :)

Ah, this one still has a few traces of unused-parameter in it, I'll get those out too.

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

What's the problem with turning unused parameters into parameters named _? I do that sometimes, maybe it's good to be consistent...

I guess we do lose the self-documenting code a bit though.

.github/dependabot.yml Outdated Show resolved Hide resolved
modules/caddyhttp/push/handler.go Outdated Show resolved Hide resolved
@faddat faddat mentioned this pull request Aug 14, 2023
@faddat
Copy link
Contributor Author

faddat commented Aug 14, 2023

@mholt -- those _'s were kinda the result of a misconfiguration.

I think that their clarity (eg: we don't do anything with this) is less-good than the self-documenting code. I did this on ibc-go a while ago and ended up having to seek out names a bunch.

Overall, I wanted to let the maintainers here know, the idea behind this set of PR's was to make writing code easier by transferring as much of the formatting work to a machine as possible. In the long run I'd recommend progressively tightening the linters, because you can catch small/easy issues (that can be maddening) that way, and also ensure that the code is really rigorously formatted.

The reason I came to do this now, was the tweet about the feature freeze -- that's pretty much a perfect time to do this kind of work, because when linting we don't want to change what the code does at all (except maybe adding some tests, the linters are pretty good at finding places where we could add additional tests).

Ah, and about linting the tests -- so that's not default behavior for golangci-lint but it probably should be. Once you're linting all of the code including the tests, you can get good consistency on the whole codebase, really fast.

You can think of the linter configuration, as the thing that handles all of the formatting of the code, and in the future, you can use it to make sometimes surprisingly granular changes even to small stuff.

@faddat faddat marked this pull request as ready for review August 14, 2023 17:51
@@ -392,11 +392,11 @@ func SplitNetworkAddress(a string) (network, host, port string, err error) {
}
if IsUnixNetwork(network) {
host = a
return
return network, host, port, nil
Copy link
Member

Choose a reason for hiding this comment

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

This seems like a mistake -- the returns are named.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahhh, so it isn't a mistake actually, it's designed to make the code clearer. If you'd like me to change it back the way it was, just let me know.

@@ -132,7 +132,7 @@ type HeaderOps struct {
}

// Provision sets up the header operations.
func (ops *HeaderOps) Provision(_ caddy.Context) error {
func (ops *HeaderOps) Provision(ctx caddy.Context) error {
Copy link
Member

@mholt mholt Sep 1, 2023

Choose a reason for hiding this comment

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

This variable isn't used, so I'm not sure why it needs a name. Naming it falsely implies that we're using it, whereas _ makes it clear we're satisfying an interface IMO. I know we talked about it before but it still seems misleading to me. Especially for an argument where the type makes things obvious; if this was a string or int, with a very vague function name, maybe a different matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I can change that setting, and that'd get applied universally.

Copy link
Member

Choose a reason for hiding this comment

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

Universally for context args, or for every arg?

Because I think it could make sense for some args.

If it can't be configured in that way, I don't think that linter is helpful for us.

@@ -284,12 +284,12 @@ func cmdReverseProxy(fs caddycmd.Flags) (int, error) {
appsRaw["tls"] = caddyconfig.JSON(tlsApp, nil)
}

var false bool
var falseBool bool
Copy link
Member

Choose a reason for hiding this comment

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

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it was shadowing the name "false"

@@ -142,13 +142,13 @@ func (c *client) Do(p map[string]string, req io.Reader) (r io.Reader, err error)

err = writer.writeBeginRequest(uint16(Responder), 0)
if err != nil {
return
return nil, err
Copy link
Member

Choose a reason for hiding this comment

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

More named returns

@francislavoie francislavoie changed the title lint tests chore: Lint tests Sep 8, 2023
Copy link
Contributor

@AnomalRoil AnomalRoil left a comment

Choose a reason for hiding this comment

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

Here are a few comments.

I think I agree with @mholt that keeping the _ instead of using named but unused parameters is clearer.

But I also think that if you're replacing named return with explicit returns, you might as well drop the named returns most of the time.
(Despite @mholt clearly loving these named returns, I think it doesn't help with readability of long functions, so it's probably better to have explicit return values.)

@@ -884,11 +884,11 @@ func Version() (simple, full string) {
if CustomVersion != "" {
full = CustomVersion
simple = CustomVersion
return
return full, simple
Copy link
Contributor

Choose a reason for hiding this comment

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

This is permuting both strings, no?

Suggested change
return full, simple
return simple, full

Not that it matters here, but better be consistent.

}
full = "unknown"
simple = "unknown"
return
return full, simple
Copy link
Contributor

Choose a reason for hiding this comment

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

This is permuting both strings, no?

Suggested change
return full, simple
return simple, full

Not that it matters here, but better be consistent.

@@ -961,9 +961,11 @@ func handleConfig(w http.ResponseWriter, r *http.Request) error {
// Set the ETag as a trailer header.
// The alternative is to write the config to a buffer, and
// then hash that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why the extra line here?
The comment is related to the next line.

Suggested change

@@ -1,4 +1,74 @@
linters-settings:
revive:
enable-all-rules: true
# Do NOT whine about the following, full explanation found in:
Copy link
Contributor

Choose a reason for hiding this comment

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

Not convinced anybody "whines".

Suggested change
# Do NOT whine about the following, full explanation found in:
# Had to disable these out of the 77 rules. Full explanation of all rules found in:

Copy link
Member

Choose a reason for hiding this comment

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

Oh I think they're actually referring to the linter complaining when we don't really care about these things or they aren't relevant in our project. :) Not a person. But yeah, maybe the proposed change here is better.

- name: unhandled-error
disabled: true
# Arguments added below do not trigger the rule.
# arguments =["os\.(Create|WriteFile|Chmod)", "fmt\.Print", "myFunction", "net\..*", "bytes\.Buffer\.Write"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# arguments =["os\.(Create|WriteFile|Chmod)", "fmt\.Print", "myFunction", "net\..*", "bytes\.Buffer\.Write"]

@@ -95,7 +95,7 @@ func (e *StaticError) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
return nil
}

func (e StaticError) ServeHTTP(w http.ResponseWriter, r *http.Request, _ Handler) error {
func (e StaticError) ServeHTTP(w http.ResponseWriter, r *http.Request, h Handler) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

this feels like it hurts readability. I agree with what @mholt was saying in an above comment, better keep the _ for these function whose goal is to fulfill an interface.

Comment on lines 46 to 48
resp := w.Result()
resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
resp := w.Result()
resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
resp := w.Result()
respBody, _ := io.ReadAll(resp.Body)
resp.Body.Close()

You want to read the Body before closing it ;)

@@ -52,7 +52,7 @@ func (ts Tailscale) GetCertificate(ctx context.Context, hello *tls.ClientHelloIn
}

// canHazCertificate returns true if Tailscale reports it can get a certificate for the given ClientHello.
func (ts Tailscale) canHazCertificate(ctx context.Context, hello *tls.ClientHelloInfo) (bool, error) {
func (Tailscale) canHazCertificate(ctx context.Context, hello *tls.ClientHelloInfo) (bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

😆 Very tempted to git blame who came up with that naming, fitting of Tailscale being the cool kids tho!

Copy link
Member

Choose a reason for hiding this comment

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

That might have been me, being commissioned by Tailscale (to dev the feature, not to name it that, lol). 😶

@@ -73,7 +73,7 @@ func (FileWriter) CaddyModule() caddy.ModuleInfo {
}

// Provision sets up the module
func (fw *FileWriter) Provision(ctx caddy.Context) error {
func (fw *FileWriter) Provision(caddy.Context) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
func (fw *FileWriter) Provision(caddy.Context) error {
func (fw *FileWriter) Provision(_ caddy.Context) error {

return
return 0, err2
Copy link
Contributor

Choose a reason for hiding this comment

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

This changes the logic, but maybe in a good way.

Might be worth considering returning errors.Join of both err and err2 or maybe a new error wrapping both, tho.

@francislavoie
Copy link
Member

Thanks for the review comments @AnomalRoil, appreciated!

This PR will need rebasing, it's fallen a bit behind.

@mholt
Copy link
Member

mholt commented Dec 1, 2023

Yes, thank you from me too, @AnomalRoil !

But I also think that if you're replacing named return with explicit returns, you might as well drop the named returns most of the time.
(Despite @mholt clearly loving these named returns, I think it doesn't help with readability of long functions, so it's probably better to have explicit return values.)

If I read you right, I think I agree. Named returns can be very elegant sometimes and even help improve correctness / reduce error-proneness, but if most/all of the return statements end up using variable names, the names should be removed from the return signature.

@faddat Any interest in finishing this up? Apologies if it's overwhelming. That's just what happens sometimes with big changes 😅 We appreciate it!

@mholt
Copy link
Member

mholt commented Mar 5, 2024

Closing due to inactivity -- however, we'd be open to finishing this when you are @faddat (or anyone else who would like to pick this up)!

Maybe it would be easier to break it down into smaller changes if possible.

@mholt mholt closed this Mar 5, 2024
@mholt mholt added the needs info 📭 Requires more information label Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs info 📭 Requires more information
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants