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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/testableexamples.go

Lines changed: 19 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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).
Lines changed: 23 additions & 0 deletions
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)