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 f2cf032
Show file tree
Hide file tree
Showing 2 changed files with 15 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
16 changes: 14 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,10 @@ func ExampleWrap() {
func ExampleWithStack() {
originalErr := errors.New("original error")
err := errors.WithStack(originalErr)
fmt.Println(errors.Format(err, errors.FormatWithStack()))
re := regexp.MustCompile("\\[.*/internal")

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

View workflow job for this annotation

GitHub Actions / Lint / lint

S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple)
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 +52,15 @@ func ExampleFormatWithStack() {
fmt.Println(errors.Format(wrappedErr))
fmt.Println()
fmt.Println("FormatWithStack:")
fmt.Println(errors.Format(wrappedErr, errors.FormatWithStack()))
re := regexp.MustCompile("\\[.*/internal")

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

View workflow job for this annotation

GitHub Actions / Lint / lint

S1007: should use raw string (`...`) with regexp.MustCompile to avoid having to escape twice (gosimple)
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:50] (*errors.wrappedError):
// - original error [/pkg/utils/errors/example_test.go:49]
}

func ExampleFormatWithUnwrap() {
Expand Down

0 comments on commit f2cf032

Please sign in to comment.