From 54d6cbe12529d267ffc2be526f9ce9f647d1ff84 Mon Sep 17 00:00:00 2001 From: Marat Reymers Date: Sun, 28 Aug 2022 23:40:15 +0300 Subject: [PATCH] Add usage via golangci-lint to readme --- README.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/README.md b/README.md index c39738d..ba86079 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Linter checks if examples are testable (have an expected output). > Author of idea is [Jamie Tanna](https://github.com/jamietanna) (see [this issue](https://github.com/golangci/golangci-lint/issues/3084)). + ## Description Example functions without output comments are compiled but not executed by `go test`, see [doc](https://pkg.go.dev/testing#hdr-Examples). @@ -22,6 +23,62 @@ func Example_good() { ``` +## Usage + +The best way is to use [golangci-lint](https://golangci-lint.run/). +It includes [testableexamples](https://golangci-lint.run/usage/linters/#list-item-testableexamples) and a lot of other great linters. + +### Install + +See [official site](https://golangci-lint.run/usage/install/). + +### Enable + +`testableexamples` is disabled by default. +To enable it, add the following to your `.golangci.yml`: + +```yaml +linters: + enable: + testableexamples +``` + +### Run + +```shell +golangci-lint run +``` + +### Directive `//nolint:testableexamples` + +Here is incorrect usage of `//nolint` directive. +`golangci-lint` understands it correctly, but `godoc` will include comment to the code. + +```go +// Description +func Example_nolintIncorrect() { //nolint:testableexamples // that's why + fmt.Println("hello") +} +``` + +Here are two examples of correct usage of `//nolint` directive. +`godoc` will ignore comment. + +```go +//nolint:testableexamples // that's why +func Example_nolintCorrect() { + fmt.Println("hello") +} + +// Description +// +//nolint:testableexamples // that's why +func Example_nolintCorrectWithDescription() { + fmt.Println("hello") +} +``` + + ## Usage as standalone linter ### Install @@ -35,6 +92,16 @@ go install github.com/maratori/testableexamples@latest testableexamples ./... ``` +### Nolint + +Standalone linter doesn't support `//nolint` directive. +And there is no alternative for that, please use `golangci-lint`. + + +## License + +[MIT License][license-url] + [ci-img]: https://github.com/maratori/testableexamples/actions/workflows/ci.yml/badge.svg [ci-url]: https://github.com/maratori/testableexamples/actions/workflows/ci.yml