Merge pull request #59 from golangci/feature/enabled-tests-linting-by-default
#52: #36: lint test files by default
This commit is contained in:
		
						commit
						68e1295340
					
				| @ -1,6 +1,3 @@ | |||||||
| run: |  | ||||||
|   tests: true |  | ||||||
| 
 |  | ||||||
| linters-settings: | linters-settings: | ||||||
|   govet: |   govet: | ||||||
|     check-shadowing: true |     check-shadowing: true | ||||||
|  | |||||||
| @ -223,7 +223,7 @@ Flags: | |||||||
|       --issues-exit-code int        Exit code when issues were found (default 1) |       --issues-exit-code int        Exit code when issues were found (default 1) | ||||||
|       --build-tags strings          Build tags (not all linters support them) |       --build-tags strings          Build tags (not all linters support them) | ||||||
|       --deadline duration           Deadline for total work (default 1m0s) |       --deadline duration           Deadline for total work (default 1m0s) | ||||||
|       --tests                       Analyze tests (*_test.go) |       --tests                       Analyze tests (*_test.go) (default true) | ||||||
|       --print-resources-usage       Print avg and max memory usage of golangci-lint and total time |       --print-resources-usage       Print avg and max memory usage of golangci-lint and total time | ||||||
|   -c, --config PATH                 Read config from file path PATH |   -c, --config PATH                 Read config from file path PATH | ||||||
|       --no-config                   Don't read config |       --no-config                   Don't read config | ||||||
| @ -293,9 +293,6 @@ There is a [`.golangci.yml`](https://github.com/golangci/golangci-lint/blob/mast | |||||||
| 
 | 
 | ||||||
| It's a [.golangci.yml](https://github.com/golangci/golangci-lint/blob/master/.golangci.yml) of this repo: we enable more linters than by default and make their settings more strict: | It's a [.golangci.yml](https://github.com/golangci/golangci-lint/blob/master/.golangci.yml) of this repo: we enable more linters than by default and make their settings more strict: | ||||||
| ```yaml | ```yaml | ||||||
| run: |  | ||||||
|   tests: true |  | ||||||
| 
 |  | ||||||
| linters-settings: | linters-settings: | ||||||
|   govet: |   govet: | ||||||
|     check-shadowing: true |     check-shadowing: true | ||||||
|  | |||||||
| @ -69,7 +69,7 @@ func (e *Executor) initFlagSet(fs *pflag.FlagSet) { | |||||||
| 		1, wh("Exit code when issues were found")) | 		1, wh("Exit code when issues were found")) | ||||||
| 	fs.StringSliceVar(&rc.BuildTags, "build-tags", []string{}, wh("Build tags (not all linters support them)")) | 	fs.StringSliceVar(&rc.BuildTags, "build-tags", []string{}, wh("Build tags (not all linters support them)")) | ||||||
| 	fs.DurationVar(&rc.Deadline, "deadline", time.Minute, wh("Deadline for total work")) | 	fs.DurationVar(&rc.Deadline, "deadline", time.Minute, wh("Deadline for total work")) | ||||||
| 	fs.BoolVar(&rc.AnalyzeTests, "tests", false, wh("Analyze tests (*_test.go)")) | 	fs.BoolVar(&rc.AnalyzeTests, "tests", true, wh("Analyze tests (*_test.go)")) | ||||||
| 	fs.BoolVar(&rc.PrintResourcesUsage, "print-resources-usage", false, wh("Print avg and max memory usage of golangci-lint and total time")) | 	fs.BoolVar(&rc.PrintResourcesUsage, "print-resources-usage", false, wh("Print avg and max memory usage of golangci-lint and total time")) | ||||||
| 	fs.StringVarP(&rc.Config, "config", "c", "", wh("Read config from file path `PATH`")) | 	fs.StringVarP(&rc.Config, "config", "c", "", wh("Read config from file path `PATH`")) | ||||||
| 	fs.BoolVar(&rc.NoConfig, "no-config", false, wh("Don't read config")) | 	fs.BoolVar(&rc.NoConfig, "no-config", false, wh("Don't read config")) | ||||||
|  | |||||||
| @ -20,22 +20,20 @@ func installBinary(t assert.TestingT) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestCongratsMessageIfNoIssues(t *testing.T) { | func TestCongratsMessageIfNoIssues(t *testing.T) { | ||||||
| 	installBinary(t) |  | ||||||
| 
 |  | ||||||
| 	out, exitCode := runGolangciLint(t, "../...") | 	out, exitCode := runGolangciLint(t, "../...") | ||||||
| 	assert.Equal(t, 0, exitCode) | 	assert.Equal(t, 0, exitCode) | ||||||
| 	assert.Equal(t, "Congrats! No issues were found.\n", out) | 	assert.Equal(t, "Congrats! No issues were found.\n", out) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestDeadline(t *testing.T) { | func TestDeadline(t *testing.T) { | ||||||
| 	installBinary(t) |  | ||||||
| 
 |  | ||||||
| 	out, exitCode := runGolangciLint(t, "--no-config", "--deadline=1ms", "../...") | 	out, exitCode := runGolangciLint(t, "--no-config", "--deadline=1ms", "../...") | ||||||
| 	assert.Equal(t, 4, exitCode) | 	assert.Equal(t, 4, exitCode) | ||||||
| 	assert.Equal(t, "", out) // no 'Congrats! No issues were found.' | 	assert.Equal(t, "", out) // no 'Congrats! No issues were found.' | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func runGolangciLint(t *testing.T, args ...string) (string, int) { | func runGolangciLint(t *testing.T, args ...string) (string, int) { | ||||||
|  | 	installBinary(t) | ||||||
|  | 
 | ||||||
| 	runArgs := append([]string{"run"}, args...) | 	runArgs := append([]string{"run"}, args...) | ||||||
| 	cmd := exec.Command("golangci-lint", runArgs...) | 	cmd := exec.Command("golangci-lint", runArgs...) | ||||||
| 	out, err := cmd.Output() | 	out, err := cmd.Output() | ||||||
| @ -54,3 +52,8 @@ func runGolangciLint(t *testing.T, args ...string) (string, int) { | |||||||
| 	ws := cmd.ProcessState.Sys().(syscall.WaitStatus) | 	ws := cmd.ProcessState.Sys().(syscall.WaitStatus) | ||||||
| 	return string(out), ws.ExitStatus() | 	return string(out), ws.ExitStatus() | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestTestsAreLintedByDefault(t *testing.T) { | ||||||
|  | 	out, exitCode := runGolangciLint(t, "--no-config", "./testdata/withtests") | ||||||
|  | 	assert.Equal(t, 0, exitCode, out) | ||||||
|  | } | ||||||
|  | |||||||
							
								
								
									
										23
									
								
								test/testdata/withtests/p.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								test/testdata/withtests/p.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,23 @@ | |||||||
|  | package withtests | ||||||
|  | 
 | ||||||
|  | import "fmt" | ||||||
|  | 
 | ||||||
|  | var varUsedOnlyInTests bool | ||||||
|  | 
 | ||||||
|  | func usedOnlyInTests() {} | ||||||
|  | 
 | ||||||
|  | type someType struct { | ||||||
|  | 	fieldUsedOnlyInTests bool | ||||||
|  | 	fieldUsedHere        bool | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func usedHere() { | ||||||
|  | 	v := someType{ | ||||||
|  | 		fieldUsedHere: true, | ||||||
|  | 	} | ||||||
|  | 	fmt.Println(v) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func init() { | ||||||
|  | 	usedHere() | ||||||
|  | } | ||||||
							
								
								
									
										14
									
								
								test/testdata/withtests/p_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								test/testdata/withtests/p_test.go
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | |||||||
|  | package withtests | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"testing" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func TestSomething(t *testing.T) { | ||||||
|  | 	v := someType{ | ||||||
|  | 		fieldUsedOnlyInTests: true, | ||||||
|  | 	} | ||||||
|  | 	fmt.Println(v, varUsedOnlyInTests) | ||||||
|  | 	usedOnlyInTests() | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Isaev Denis
						Isaev Denis