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

Exclude generated code from coverage #23

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

breml
Copy link

@breml breml commented Mar 28, 2022

Fixes #15

if err != nil {
return errors.WithStack(err)
}
if !doNotEditRe.Match(body) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm, maybe it would be good to make the check a bit more thorough, similar to the isGenerated from the lint tool?
just to be a bit more strict / thorough and not exclude some frankenstein file with a comment floating around somewhere mid-file?

https://pkg.go.dev/cmd/go#hdr-Generate_Go_files_by_processing_source specifies:

This line must appear before the first non-comment, non-blank text in the file.

you can see the test case as well from here which things should be valid: https://github.com/golang/lint/blob/85993ffd0a6cd043291f3f63d45d656d97b165bd/lint_test.go#L292

Copy link
Author

Choose a reason for hiding this comment

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

@rubensayshi You are right and I am aware of this specification (I linked it myself in hexdigest/gowrap#20). My issue is, that one of my main use-cases is exactly with gowrap and this tool generates a valid comment, but does not (yet) comply with strict requirement for the position, because the comment is inserted after the package statement.

If you agree, I can change the detection, such that it complies with the test set you linked, which would be sufficient for me.

Copy link
Author

Choose a reason for hiding this comment

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

@rubensayshi I added the isGenerated function you linked to in a separate commit.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ehm, I suppose it's fine to adjust isGenerated to also accept package before the comment as an exception?

Copy link
Author

Choose a reason for hiding this comment

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

This is already the case in the "original" version of isGenerated, see:

{"package foo\n// Code generated by some tool. DO NOT EDIT.\ntype foo int\n", true},

I assume, this because this code predates the decision in golang/go#41196

@imsodin
Copy link

imsodin commented Mar 29, 2022

Should there be a flag/config option for this? There could be projects interested in coverage of generated code.

@dave
Copy link
Owner

dave commented Mar 29, 2022

Yeah I think running tests on generated code is common... I do it in some of my projects.

@breml
Copy link
Author

breml commented Mar 29, 2022

@dave No problem, I can add a flag for this. Do you have any preference on the name of the flag? I assume, the default would be to keep the existing behavior for backwards compatibility reasons.

@breml
Copy link
Author

breml commented Mar 29, 2022

@dave Another question, there are several places, where gofmt (respectively goimports and gofumpt) would change the formatting of the code. I did not commit these changes so far. How do you feel about this?

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.

Option to exclude all generated files
4 participants