From 51b7c1866637da24a2f13cd988a595b09283cc65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Va=C5=A1ko?= Date: Mon, 3 Jun 2024 19:30:36 +0200 Subject: [PATCH] lint: Add testableexamples linter to have examples correctly tested. --- build/ci/golangci.yml | 1 + internal/pkg/utils/errors/example_test.go | 24 +++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/build/ci/golangci.yml b/build/ci/golangci.yml index 8b3200b7bc..f86492cb59 100644 --- a/build/ci/golangci.yml +++ b/build/ci/golangci.yml @@ -135,6 +135,7 @@ linters: - staticcheck - stylecheck - tagliatelle + - testableexamples - thelper - tparallel - paralleltest diff --git a/internal/pkg/utils/errors/example_test.go b/internal/pkg/utils/errors/example_test.go index 5d32f98e3b..a0a76e4bb3 100644 --- a/internal/pkg/utils/errors/example_test.go +++ b/internal/pkg/utils/errors/example_test.go @@ -2,6 +2,7 @@ package errors_test import ( "fmt" + "regexp" "github.com/keboola/keboola-as-code/internal/pkg/utils/errors" ) @@ -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") + 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() { @@ -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") + 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() {