diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 38a2cbce5e40..19cd49ad6cad 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -2011,6 +2011,7 @@ linters: - stylecheck - tagliatelle - tenv + - testableexamples - testpackage - thelper - tparallel @@ -2116,6 +2117,7 @@ linters: - stylecheck - tagliatelle - tenv + - testableexamples - testpackage - thelper - tparallel diff --git a/go.mod b/go.mod index 71e7d6af3c87..9a1de649a8a6 100644 --- a/go.mod +++ b/go.mod @@ -58,6 +58,7 @@ require ( github.com/ldez/tagliatelle v0.3.1 github.com/leonklingele/grouper v1.1.0 github.com/lufeee/execinquery v1.2.1 + github.com/maratori/testableexamples v1.0.0 github.com/maratori/testpackage v1.1.0 github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0 github.com/mattn/go-colorable v0.1.13 diff --git a/go.sum b/go.sum index ec573e621a99..60e0b7a66462 100644 --- a/go.sum +++ b/go.sum @@ -341,6 +341,8 @@ github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= +github.com/maratori/testableexamples v1.0.0 h1:dU5alXRrD8WKSjOUnmJZuzdxWOEQ57+7s93SLMxb2vI= +github.com/maratori/testableexamples v1.0.0/go.mod h1:4rhjL1n20TUTT4vdh3RDqSizKLyXp7K2u6HgraZCGzE= github.com/maratori/testpackage v1.1.0 h1:GJY4wlzQhuBusMF1oahQCBtUV/AQ/k69IZ68vxaac2Q= github.com/maratori/testpackage v1.1.0/go.mod h1:PeAhzU8qkCwdGEMTEupsHJNlQu2gZopMC6RjbhmHeDc= github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 h1:pWxk9e//NbPwfxat7RXkts09K+dEBJWakUWwICVqYbA= diff --git a/pkg/golinters/testableexamples.go b/pkg/golinters/testableexamples.go new file mode 100644 index 000000000000..3333593a62eb --- /dev/null +++ b/pkg/golinters/testableexamples.go @@ -0,0 +1,19 @@ +package golinters + +import ( + "github.com/maratori/testableexamples/pkg/testableexamples" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewTestableexamples() *goanalysis.Linter { + a := testableexamples.NewAnalyzer() + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + nil, + ).WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index e963719dc451..a270e4f776ce 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -750,6 +750,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithLoadForGoAnalysis(). WithURL("https://github.com/sivchari/tenv"), + linter.NewConfig(golinters.NewTestableexamples()). + WithSince("v1.50.0"). + WithPresets(linter.PresetTest). + WithURL("https://github.com/maratori/testableexamples"), + linter.NewConfig(golinters.NewTestpackage(testpackageCfg)). WithSince("v1.25.0"). WithPresets(linter.PresetStyle, linter.PresetTest). diff --git a/test/testdata/testableexamples_test.go b/test/testdata/testableexamples_test.go new file mode 100644 index 000000000000..606ee68e59da --- /dev/null +++ b/test/testdata/testableexamples_test.go @@ -0,0 +1,23 @@ +//golangcitest:args -Etestableexamples +package testdata + +import "fmt" + +func Example_good() { + fmt.Println("hello") + // Output: hello +} + +func Example_goodEmptyOutput() { + fmt.Println("") + // Output: +} + +func Example_bad() { // want `^missing output for example, go test can't validate it$` + fmt.Println("hello") +} + +//nolint:testableexamples +func Example_nolint() { + fmt.Println("hello") +}