Skip to content

Commit 281e184

Browse files
authored
feat: add testableexamples linter (#3170)
1 parent 3a2ad90 commit 281e184

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed

.golangci.reference.yml

+2
Original file line numberDiff line numberDiff line change
@@ -2011,6 +2011,7 @@ linters:
20112011
- stylecheck
20122012
- tagliatelle
20132013
- tenv
2014+
- testableexamples
20142015
- testpackage
20152016
- thelper
20162017
- tparallel
@@ -2116,6 +2117,7 @@ linters:
21162117
- stylecheck
21172118
- tagliatelle
21182119
- tenv
2120+
- testableexamples
21192121
- testpackage
21202122
- thelper
21212123
- tparallel

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ require (
5858
github.com/ldez/tagliatelle v0.3.1
5959
github.com/leonklingele/grouper v1.1.0
6060
github.com/lufeee/execinquery v1.2.1
61+
github.com/maratori/testableexamples v1.0.0
6162
github.com/maratori/testpackage v1.1.0
6263
github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0
6364
github.com/mattn/go-colorable v0.1.13

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/testableexamples.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package golinters
2+
3+
import (
4+
"github.com/maratori/testableexamples/pkg/testableexamples"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewTestableexamples() *goanalysis.Linter {
11+
a := testableexamples.NewAnalyzer()
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeSyntax)
19+
}

pkg/lint/lintersdb/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
750750
WithLoadForGoAnalysis().
751751
WithURL("https://github.com/sivchari/tenv"),
752752

753+
linter.NewConfig(golinters.NewTestableexamples()).
754+
WithSince("v1.50.0").
755+
WithPresets(linter.PresetTest).
756+
WithURL("https://github.com/maratori/testableexamples"),
757+
753758
linter.NewConfig(golinters.NewTestpackage(testpackageCfg)).
754759
WithSince("v1.25.0").
755760
WithPresets(linter.PresetStyle, linter.PresetTest).
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//golangcitest:args -Etestableexamples
2+
package testdata
3+
4+
import "fmt"
5+
6+
func Example_good() {
7+
fmt.Println("hello")
8+
// Output: hello
9+
}
10+
11+
func Example_goodEmptyOutput() {
12+
fmt.Println("")
13+
// Output:
14+
}
15+
16+
func Example_bad() { // want `^missing output for example, go test can't validate it$`
17+
fmt.Println("hello")
18+
}
19+
20+
//nolint:testableexamples
21+
func Example_nolint() {
22+
fmt.Println("hello")
23+
}

0 commit comments

Comments
 (0)