Skip to content

Commit

Permalink
lint: Add testableexamples linter to have examples correctly tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matovidlo committed Jun 4, 2024
1 parent 14f9030 commit 51b7c18
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions build/ci/golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ linters:
- staticcheck
- stylecheck
- tagliatelle
- testableexamples
- thelper
- tparallel
- paralleltest
Expand Down
24 changes: 22 additions & 2 deletions internal/pkg/utils/errors/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errors_test

import (
"fmt"
"regexp"

"github.com/keboola/keboola-as-code/internal/pkg/utils/errors"
)
Expand Down Expand Up @@ -38,7 +39,14 @@ func ExampleWrap() {
func ExampleWithStack() {
originalErr := errors.New("original error")
err := errors.WithStack(originalErr)
fmt.Println(errors.Format(err, errors.FormatWithStack()))
re, rErr := regexp.Compile("\\[.*/internal")

Check failure on line 42 in internal/pkg/utils/errors/example_test.go

View workflow job for this annotation

GitHub Actions / Lint / lint

regexpMust: for const patterns like "\\[.*/internal", use regexp.MustCompile (gocritic)
if rErr != nil {
panic("unable to compile regex")
}

fmt.Println(string(re.ReplaceAll([]byte(errors.Format(err, errors.FormatWithStack())), []byte("["))))
// output:
// original error [/pkg/utils/errors/example_test.go:40]
}

func ExampleFormatWithStack() {
Expand All @@ -48,7 +56,19 @@ func ExampleFormatWithStack() {
fmt.Println(errors.Format(wrappedErr))
fmt.Println()
fmt.Println("FormatWithStack:")
fmt.Println(errors.Format(wrappedErr, errors.FormatWithStack()))
re, rErr := regexp.Compile("\\[.*/internal")

Check failure on line 59 in internal/pkg/utils/errors/example_test.go

View workflow job for this annotation

GitHub Actions / Lint / lint

regexpMust: for const patterns like "\\[.*/internal", use regexp.MustCompile (gocritic)
if rErr != nil {
panic("unable to compile regex")
}

fmt.Println(string(re.ReplaceAll([]byte(errors.Format(wrappedErr, errors.FormatWithStack())), []byte("["))))
// output:
// Standard output:
// new error message
//
// FormatWithStack:
// new error message [/pkg/utils/errors/example_test.go:54] (*errors.wrappedError):
// - original error [/pkg/utils/errors/example_test.go:53]
}

func ExampleFormatWithUnwrap() {
Expand Down

0 comments on commit 51b7c18

Please sign in to comment.