Skip to content

Commit e6657e8

Browse files
committed
don't analyze tests by default
1 parent 511b04d commit e6657e8

File tree

6 files changed

+45
-14
lines changed

6 files changed

+45
-14
lines changed

internal/commands/run.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (e *Executor) initRun() {
8888
runCmd.Flags().BoolVarP(&rc.Diff, "new", "n", false, "Show only new issues: if there are unstaged changes or untracked files, only those changes are shown, else only changes in HEAD~ are shown")
8989
runCmd.Flags().StringVar(&rc.DiffFromRevision, "new-from-rev", "", "Show only new issues created after git revision `REV`")
9090
runCmd.Flags().StringVar(&rc.DiffPatchFilePath, "new-from-patch", "", "Show only new issues created in git patch with file path `PATH`")
91+
runCmd.Flags().BoolVar(&rc.AnalyzeTests, "tests", false, "Analyze tests (*_test.go)")
9192
}
9293

9394
func isFullImportNeeded(linters []pkg.Linter) bool {
@@ -128,8 +129,7 @@ func loadWholeAppIfNeeded(ctx context.Context, linters []pkg.Linter, cfg *config
128129
Build: &bctx,
129130
AllowErrors: true, // Try to analyze event partially
130131
}
131-
const needTests = true // TODO: configure and take into account in paths resolver
132-
rest, err := loadcfg.FromArgs(paths.MixedPaths(), needTests)
132+
rest, err := loadcfg.FromArgs(paths.MixedPaths(), cfg.AnalyzeTests)
133133
if err != nil {
134134
return nil, nil, fmt.Errorf("can't parepare load config with paths: %s", err)
135135
}
@@ -162,7 +162,7 @@ func buildLintCtx(ctx context.Context, linters []pkg.Linter, cfg *config.Config)
162162
args = []string{"./..."}
163163
}
164164

165-
paths, err := fsutils.GetPathsForAnalysis(ctx, args)
165+
paths, err := fsutils.GetPathsForAnalysis(ctx, args, cfg.Run.AnalyzeTests)
166166
if err != nil {
167167
return nil, err
168168
}

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ type Run struct { // nolint:maligned
9292
DiffFromRevision string
9393
DiffPatchFilePath string
9494
Diff bool
95+
96+
AnalyzeTests bool
9597
}
9698

9799
type Config struct {

pkg/fsutils/fsutils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func processPaths(root string, paths []string, maxPaths int) ([]string, error) {
5555
return ret, nil
5656
}
5757

58-
func GetPathsForAnalysis(ctx context.Context, inputPaths []string) (ret *ProjectPaths, err error) {
58+
func GetPathsForAnalysis(ctx context.Context, inputPaths []string, includeTests bool) (ret *ProjectPaths, err error) {
5959
defer func(startedAt time.Time) {
6060
if ret != nil {
6161
logrus.Infof("Found paths for analysis for %s: %s", time.Since(startedAt), ret.MixedPaths())
@@ -69,7 +69,7 @@ func GetPathsForAnalysis(ctx context.Context, inputPaths []string) (ret *Project
6969
}
7070

7171
excludeDirs := []string{"vendor", "testdata", "examples", "Godeps", "builtin"}
72-
pr := NewPathResolver(excludeDirs, []string{".go"})
72+
pr := NewPathResolver(excludeDirs, []string{".go"}, includeTests)
7373
paths, err := pr.Resolve(inputPaths...)
7474
if err != nil {
7575
return nil, fmt.Errorf("can't resolve paths: %s", err)

pkg/fsutils/path_resolver.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
type PathResolver struct {
1212
excludeDirs map[string]bool
1313
allowedFileExtensions map[string]bool
14+
includeTests bool
1415
}
1516

1617
type pathResolveState struct {
@@ -56,7 +57,7 @@ func (s pathResolveState) toResult() *PathResolveResult {
5657
return res
5758
}
5859

59-
func NewPathResolver(excludeDirs, allowedFileExtensions []string) *PathResolver {
60+
func NewPathResolver(excludeDirs, allowedFileExtensions []string, includeTests bool) *PathResolver {
6061
excludeDirsMap := map[string]bool{}
6162
for _, dir := range excludeDirs {
6263
excludeDirsMap[dir] = true
@@ -70,6 +71,7 @@ func NewPathResolver(excludeDirs, allowedFileExtensions []string) *PathResolver
7071
return &PathResolver{
7172
excludeDirs: excludeDirsMap,
7273
allowedFileExtensions: allowedFileExtensionsMap,
74+
includeTests: includeTests,
7375
}
7476
}
7577

@@ -89,6 +91,10 @@ func (pr PathResolver) isIgnoredDir(dir string) bool {
8991
}
9092

9193
func (pr PathResolver) isAllowedFile(path string) bool {
94+
if !pr.includeTests && strings.HasSuffix(path, "_test.go") {
95+
return false
96+
}
97+
9298
return pr.allowedFileExtensions[filepath.Ext(path)]
9399
}
94100

pkg/fsutils/path_resolver_test.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func prepareFS(t *testing.T, paths ...string) *fsPreparer {
5454
}
5555

5656
func newPR() *PathResolver {
57-
return NewPathResolver([]string{}, []string{})
57+
return NewPathResolver([]string{}, []string{}, false)
5858
}
5959

6060
func TestPathResolverNoPaths(t *testing.T) {
@@ -72,11 +72,12 @@ func TestPathResolverNotExistingPath(t *testing.T) {
7272

7373
func TestPathResolverCommonCases(t *testing.T) {
7474
type testCase struct {
75-
name string
76-
prepare []string
77-
resolve []string
78-
expFiles []string
79-
expDirs []string
75+
name string
76+
prepare []string
77+
resolve []string
78+
expFiles []string
79+
expDirs []string
80+
includeTests bool
8081
}
8182

8283
testCases := []testCase{
@@ -154,14 +155,36 @@ func TestPathResolverCommonCases(t *testing.T) {
154155
resolve: []string{"./..."},
155156
expDirs: []string{"."},
156157
},
158+
{
159+
name: "include tests",
160+
prepare: []string{"a/b.go", "a/b_test.go"},
161+
resolve: []string{"./..."},
162+
expDirs: []string{".", "a"},
163+
expFiles: []string{"a/b.go", "a/b_test.go"},
164+
includeTests: true,
165+
},
166+
{
167+
name: "exclude tests",
168+
prepare: []string{"a/b.go", "a/b_test.go"},
169+
resolve: []string{"./..."},
170+
expDirs: []string{".", "a"},
171+
expFiles: []string{"a/b.go"},
172+
},
173+
{
174+
name: "exclude tests except explicitly set",
175+
prepare: []string{"a/b.go", "a/b_test.go", "a/c_test.go"},
176+
resolve: []string{"./...", "a/c_test.go"},
177+
expDirs: []string{".", "a"},
178+
expFiles: []string{"a/b.go", "a/c_test.go"},
179+
},
157180
}
158181

159182
for _, tc := range testCases {
160183
t.Run(tc.name, func(t *testing.T) {
161184
fp := prepareFS(t, tc.prepare...)
162185
defer fp.clean()
163186

164-
pr := NewPathResolver([]string{"vendor"}, []string{".go"})
187+
pr := NewPathResolver([]string{"vendor"}, []string{".go"}, tc.includeTests)
165188
res, err := pr.Resolve(tc.resolve...)
166189
assert.NoError(t, err)
167190

pkg/golinters/golint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (Golint) Name() string {
2020
func (g Golint) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) {
2121
var issues []result.Issue
2222
if lintCtx.Paths.IsDirsRun {
23-
for _, path := range lintCtx.Paths.Dirs {
23+
for _, path := range lintCtx.Paths.Dirs { // TODO: support exclusion of test files
2424
i, err := lintDir(path, lintCtx.RunCfg().Golint.MinConfidence)
2525
if err != nil {
2626
// TODO: skip and warn

0 commit comments

Comments
 (0)