From 5f19f7d6d7d968634054b1ee53ff41a050b5740e Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Thu, 2 Feb 2023 22:02:48 +0100 Subject: [PATCH] Fix errors.Is(status.X) (#16) --- .github/ISSUE_TEMPLATE/bug_report.md | 21 +++++++++++++++++++++ .github/ISSUE_TEMPLATE/feature_request.md | 10 ++++++++++ .github/ISSUE_TEMPLATE/question.md | 10 ++++++++++ .github/workflows/cloc.yml | 7 +++---- .github/workflows/golangci-lint.yml | 4 ++-- .github/workflows/gorelease.yml | 5 ++--- .github/workflows/test-unit.yml | 12 +++++------- .golangci.yml | 3 +++ Makefile | 2 +- go.mod | 2 +- go.sum | 4 ++-- status/error.go | 13 ++++++++++++- status/error_test.go | 2 ++ 13 files changed, 74 insertions(+), 21 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE/bug_report.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md create mode 100644 .github/ISSUE_TEMPLATE/question.md diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..b3c2870 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,21 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +If feasible/relevant, please provide a code snippet (inline or with Go playground) to reproduce the issue. + + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..b9d3f65 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,10 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' + +--- + +Please use discussions https://github.com/swaggest/usecase/discussions/categories/ideas to share feature ideas. diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md new file mode 100644 index 0000000..770689b --- /dev/null +++ b/.github/ISSUE_TEMPLATE/question.md @@ -0,0 +1,10 @@ +--- +name: Question +about: Any question about features or usage +title: '' +labels: '' +assignees: '' + +--- + +Please use discussions https://github.com/swaggest/usecase/discussions/categories/q-a to make your question more discoverable by other folks. diff --git a/.github/workflows/cloc.yml b/.github/workflows/cloc.yml index 619ca74..927e099 100644 --- a/.github/workflows/cloc.yml +++ b/.github/workflows/cloc.yml @@ -21,7 +21,7 @@ jobs: with: ref: ${{ github.event.pull_request.base.sha }} path: base - - name: Count Lines Of Code + - name: Count lines of code id: loc run: | curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.3/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz @@ -29,10 +29,9 @@ jobs: [ "$sccdiff_hash" == "ae8a07b687bd3dba60861584efe724351aa7ff63" ] || (echo "::error::unexpected hash for sccdiff, possible tampering: $sccdiff_hash" && exit 1) OUTPUT=$(cd pr && ../sccdiff -basedir ../base) echo "${OUTPUT}" - OUTPUT="${OUTPUT//$'\n'/%0A}" - echo "::set-output name=diff::$OUTPUT" + echo "diff<> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT - - name: Comment Code Lines + - name: Comment lines of code continue-on-error: true uses: marocchino/sticky-pull-request-comment@v2 with: diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index a38a33d..fbf8167 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -24,10 +24,10 @@ jobs: go-version: 1.19.x - uses: actions/checkout@v2 - name: golangci-lint - uses: golangci/golangci-lint-action@v3.1.0 + uses: golangci/golangci-lint-action@v3.3.1 with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.49.0 + version: v1.50.1 # Optional: working directory, useful for monorepos # working-directory: somedir diff --git a/.github/workflows/gorelease.yml b/.github/workflows/gorelease.yml index 6267500..5d9fae0 100644 --- a/.github/workflows/gorelease.yml +++ b/.github/workflows/gorelease.yml @@ -42,9 +42,8 @@ jobs: test -e ~/go/bin/gorelease || go install golang.org/x/exp/cmd/gorelease@latest OUTPUT=$(gorelease 2>&1 || exit 0) echo "${OUTPUT}" - OUTPUT="${OUTPUT//$'\n'/%0A}" - echo "::set-output name=report::$OUTPUT" - - name: Comment Report + echo "report<> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT + - name: Comment report continue-on-error: true uses: marocchino/sticky-pull-request-comment@v2 with: diff --git a/.github/workflows/test-unit.yml b/.github/workflows/test-unit.yml index ed31598..c3110ce 100644 --- a/.github/workflows/test-unit.yml +++ b/.github/workflows/test-unit.yml @@ -82,7 +82,7 @@ jobs: go tool cover -func=./unit.coverprofile > unit.txt TOTAL=$(grep 'total:' unit.txt) echo "${TOTAL}" - echo "::set-output name=total::$TOTAL" + echo "total=$TOTAL" >> $GITHUB_OUTPUT - name: Annotate missing test coverage id: annotate @@ -94,16 +94,14 @@ jobs: git fetch origin master ${{ github.event.pull_request.base.sha }} REP=$(./gocovdiff -cov unit.coverprofile -gha-annotations gha-unit.txt -delta-cov-file delta-cov-unit.txt -target-delta-cov ${TARGET_DELTA_COV}) echo "${REP}" - REP="${REP//$'\n'/%0A}" cat gha-unit.txt DIFF=$(test -e unit-base.txt && ./gocovdiff -func-cov unit.txt -func-base-cov unit-base.txt || echo "Missing base coverage file") - DIFF="${DIFF//$'\n'/%0A}" TOTAL=$(cat delta-cov-unit.txt) - echo "::set-output name=rep::$REP" - echo "::set-output name=diff::$DIFF" - echo "::set-output name=total::$TOTAL" + echo "rep<> $GITHUB_OUTPUT && echo "$REP" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT + echo "diff<> $GITHUB_OUTPUT && echo "$DIFF" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT + echo "total<> $GITHUB_OUTPUT && echo "$TOTAL" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT - - name: Comment Test Coverage + - name: Comment test coverage continue-on-error: true if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != '' uses: marocchino/sticky-pull-request-comment@v2 diff --git a/.golangci.yml b/.golangci.yml index 15ca9a8..c0b7f03 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -43,6 +43,8 @@ linters: - structcheck - varcheck - deadcode + - testableexamples + - dupword issues: exclude-use-default: false @@ -65,5 +67,6 @@ issues: path: "_test.go" - linters: - errcheck # Error checking omitted for brevity. + - gosec path: "example_" diff --git a/Makefile b/Makefile index a7af41b..b3034ff 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -#GOLANGCI_LINT_VERSION := "v1.49.0" # Optional configuration to pinpoint golangci-lint version. +#GOLANGCI_LINT_VERSION := "v1.50.1" # Optional configuration to pinpoint golangci-lint version. # The head of Makefile determines location of dev-go to include standard targets. GO ?= go diff --git a/go.mod b/go.mod index ca97964..f3b54b1 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/swaggest/usecase go 1.18 require ( - github.com/bool64/dev v0.2.20 + github.com/bool64/dev v0.2.24 github.com/stretchr/testify v1.8.0 ) diff --git a/go.sum b/go.sum index 8af0d46..e628e2e 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/bool64/dev v0.2.20 h1:9eIRGdcg2kQW2NGza++QbOKidNNaK+KfWuUXcZFDejE= -github.com/bool64/dev v0.2.20/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg= +github.com/bool64/dev v0.2.24 h1:xptlKivPh870W3Xc9szPcM7wkFmTMuHT8rc0nu7dITk= +github.com/bool64/dev v0.2.24/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/status/error.go b/status/error.go index 58b52a9..2bb94a3 100644 --- a/status/error.go +++ b/status/error.go @@ -1,6 +1,9 @@ package status -import "strings" +import ( + "errors" + "strings" +) var codeToMsg = func() map[Code]string { res := make(map[Code]string, len(strToCode)) @@ -29,6 +32,14 @@ func (e errorWithStatus) Unwrap() error { return e.err } +func (e errorWithStatus) Is(target error) bool { + if target == e.code { //nolint:goerr113 // Target is expected to be plain status error. + return true + } + + return errors.Is(e.err, target) +} + func (e errorWithStatus) Status() Code { return e.code } diff --git a/status/error_test.go b/status/error_test.go index 68288f1..898f392 100644 --- a/status/error_test.go +++ b/status/error_test.go @@ -11,6 +11,8 @@ import ( func TestWrap(t *testing.T) { err := status.Wrap(errors.New("failed"), status.AlreadyExists) assert.EqualError(t, err, "already exists: failed") + assert.True(t, errors.Is(err, status.AlreadyExists)) + assert.False(t, errors.Is(err, status.NotFound)) assert.EqualError(t, err.(interface{ Unwrap() error }).Unwrap(), "failed") assert.Equal(t, status.AlreadyExists, err.(interface{ Status() status.Code }).Status()) }