Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint: Add testableexamples linter to have examples correctly tested. #1812

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`)
fmt.Println(string(re.ReplaceAll([]byte(errors.Format(err, errors.FormatWithStack())), []byte("["))))
// output:
// original error [/pkg/utils/errors/example_test.go:40]
}
Comment on lines -41 to 46
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples without tests also make sense, for documentation.

Is it possible to define the output for each example?


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`)
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
Loading