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

add flag to ignore test files #5

Merged
merged 3 commits into from
Mar 6, 2020
Merged

add flag to ignore test files #5

merged 3 commits into from
Mar 6, 2020

Conversation

mweb
Copy link
Contributor

@mweb mweb commented Feb 21, 2020

This add a flag to ignore all test files and test packages for the linter. See Ignore Tests #4.

@fatih
Copy link
Owner

fatih commented Feb 21, 2020

Can you add a new folder with a _test.go file. Let's make sure it doesn't get annotated.

@mweb
Copy link
Contributor Author

mweb commented Feb 22, 2020

I added three tests. One with a test file with a test package, one with a _test.go file and one with a _test package but the ignoretests flag not set.

In addition, I had to rewrite the faillint analyzer a bit since the global instance made it difficult to run tests where the ignoretests flag was not set. If it was set once it was always set.
I hope this is OK, otherwise I could move the h test before the f and g tests then it would work as well.

@mweb
Copy link
Contributor Author

mweb commented Feb 22, 2020

During the change I though maybe instead of a boolean to ignore the test file we could add another string with separate rules for the test files. If the string is empty then use the normal rules as specified.

Example:
faillint --paths="reflect,errors" --testpaths="errors"

The problem of this solution is that it would not be possible to set an empty string for the tests to have no checks at all. But it would still be possible to set a package that does not exist.

Which version would you prefer?

I added a branch with a possible rewrite as described above. If you prefer this solution I could change the pull request accordingly and add documentation for this.
https://github.com/mweb/faillint/tree/testpaths

@fatih
Copy link
Owner

fatih commented Mar 6, 2020

Thank you for your work. Much appreciated. Couple of comments:

  • Using paths Is not user friendly, I think ignoretests is better
  • Let's use --ignore-tests instead of --ignoretests
  • I like the new NewAnalyzer() function, however it would break existing package because we removed the variable Analyzer. I don't want to release a v2 because of that so I think we can still use NewAnalyzer() but should also keep the global variable, i.e: var Analyzer = NewAnalyzer() should do the trick.

RunDespiteErrors: true,
}
a.Flags.StringVar(&f.paths, "paths", "", "import paths to fail")
a.Flags.BoolVar(&f.ignoretests, "ignoretests", false, "ignore all _test.go files and packages.")
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
a.Flags.BoolVar(&f.ignoretests, "ignoretests", false, "ignore all _test.go files and packages.")
a.Flags.BoolVar(&f.ignoretests, "ignore-tests", false, "ignore all _test.go files and packages.")i

I think this is better than ignoretests

if f.paths == "" {
return nil, nil
}
if f.ignoretests && strings.Contains(pass.Pkg.Name(), "_test") {
Copy link
Owner

Choose a reason for hiding this comment

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

Hm I wonder if we should we remove this line. We shouldn't look at the package name. I think checking just the files is better to avoid any surprises.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I don't think both checks are necessary but the file name check is more important since not all tests are within a test package. I will remove it and push a new version.

@mweb
Copy link
Contributor Author

mweb commented Mar 6, 2020

Ok I readded the global instance. But since I had the new function I was still able to remove the init function.

}

a.Flags.StringVar(&f.paths, "paths", "", "import paths to fail")
a.Flags.BoolVar(&f.ignoretests, "ignore-tests", false, "ignore all _test.go files and packages.")
Copy link
Owner

Choose a reason for hiding this comment

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

This should be now only ignore all _test.go files

@fatih
Copy link
Owner

fatih commented Mar 6, 2020

@mweb this looks great. Thanks again working on this. There is one comment after that please make sure to squash all commits into a single commit and then it's ready to be merged 👍

@fatih fatih merged commit a79d10e into fatih:master Mar 6, 2020
@fatih
Copy link
Owner

fatih commented Mar 6, 2020

I went and merged it. Thank you again @mweb 👍

@fatih fatih mentioned this pull request Mar 7, 2020
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.

2 participants