diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml new file mode 100644 index 0000000..4f7ddf8 --- /dev/null +++ b/.github/workflows/go.yaml @@ -0,0 +1,41 @@ +name: Go + +on: + pull_request: + branches: + - main + - master + - release/v* + - test-workflow-main + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v5 + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version: "1.22.12" + + - name: Lint with golangci-lint + uses: golangci/golangci-lint-action@v8 + with: + version: v2.5.0 + only-new-issues: true + + test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v5 + + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version: "1.22.12" + + - name: Run go test + run: go test -v ./... diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..8891809 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,101 @@ +# https://golangci-lint.run/docs/configuration/file/ +version: "2" + +run: + timeout: 5m + +issues: + new: true + whole-files: true + new-from-rev: HEAD~ + max-same-issues: 50 + +severity: + default: error + # rules: + # - linters: + # - dupl + # severity: error + +formatters: + enable: + - gci + - gofmt + - gofumpt + - goimports + - golines + - swaggo + settings: + gofmt: + rewrite-rules: + - pattern: "interface{}" + replacement: "any" + gofumpt: + extra-rules: true + exclusions: + paths: [] + +linters: + default: none + enable: + # Standard + - errcheck + - govet + - ineffassign + - staticcheck + - unused + + # Additional + - bodyclose + - depguard + - errname + - errorlint + - gocognit + - goconst + - goheader + - gomodguard + - goprintffuncname + - gosec + - nakedret + - nilerr + - nilnil + - noctx + - nolintlint + - prealloc + - promlinter + - revive + - testifylint + - testpackage + - thelper + - usestdlibvars + - wastedassign + - wsl_v5 + + disable: [] + + settings: + depguard: + rules: + main: + deny: + - pkg: github.com/pkg/errors + desc: Should be replaced by standard lib errors package + - pkg: golang.org/x/exp/slices + desc: Should be replaced by standard lib slices package + - pkg: github.com/go-resty/resty$ + desc: Use github.com/go-resty/resty/v2 instead + - pkg: github.com/aws/smithy-go/ptr$ + desc: Use github.com/aws/aws-sdk-go-v2/aws instead + govet: + enable: + - nilness + + # exclusions: + # paths: + # - (.+)_test\.go + # rules: + # - path: (.+)_test\.go + # linters: + # - dupl + # - mnd + # - lll diff --git a/.tool-versions b/.tool-versions index 76c9a78..a0cffcc 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1,2 @@ -golang 1.22.7 -golangci-lint 1.60.3 +golang 1.22.12 +golangci-lint 2.5.0 diff --git a/Makefile b/Makefile index 25982e5..ae95376 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,27 @@ -MODULE_DIRS = . ./tools +MODULE_DIRS = . gowork: - go work init . ./tools + go work init . tidy: - $(foreach dir,$(MODULE_DIRS), \ - (cd $(dir) && go mod tidy) &&) true + go mod tidy -install: tidy - cd tools && go install \ - mvdan.cc/gofumpt +install-asdf: + asdf install + +install: install-asdf tidy +# curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v2.5.0 fmt: install - gofumpt -l -w -extra . + golangci-lint fmt -v ./... + +fix: install + golangci-lint run -v --fix ./... lint: install - golangci-lint run ./... + golangci-lint run -v ./... -test: +test: install go test ./... -check: fmt lint test +check: fix lint test diff --git a/errgroup.go b/errgroup.go index fa37351..287bd40 100644 --- a/errgroup.go +++ b/errgroup.go @@ -1,7 +1,10 @@ +// Package errgroup provides a more robust error group implementation +// that extends golang.org/x/sync/errgroup with panic recovery. package errgroup import ( "context" + "errors" "golang.org/x/sync/errgroup" ) @@ -61,7 +64,9 @@ func (g *Group) TryGo(f func() error) bool { // returns the first non-nil error (if any) from them. func (g *Group) Wait() error { err := g.g.Wait() - if p, ok := err.(panicked); ok { + + var p panicked + if errors.As(err, &p) { // re-panic to keep the original stack trace panic(p.panic) } diff --git a/errgroup_test.go b/errgroup_test.go index e6dec8b..7a76f0a 100644 --- a/errgroup_test.go +++ b/errgroup_test.go @@ -8,6 +8,7 @@ import ( "github.com/min0625/errgroup" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func Test_Group(t *testing.T) { @@ -32,9 +33,9 @@ func Test_Group(t *testing.T) { return nil }) - assert.NoError(t, g.Wait()) - assert.Equal(t, jobXIsDone, true) - assert.Equal(t, jobYIsDone, true) + require.NoError(t, g.Wait()) + assert.True(t, jobXIsDone) + assert.True(t, jobYIsDone) } func Test_WithContext(t *testing.T) { @@ -61,8 +62,8 @@ func Test_WithContext(t *testing.T) { return nil }) - assert.ErrorIs(t, g.Wait(), myErr) - assert.Equal(t, jobIsCanceled, true) + require.ErrorIs(t, g.Wait(), myErr) + assert.True(t, jobIsCanceled, true) } func Test_Group_Error(t *testing.T) { diff --git a/example_test.go b/example_test.go index ff3c766..020020f 100644 --- a/example_test.go +++ b/example_test.go @@ -35,6 +35,8 @@ func Example() { if err := g.Wait(); err != nil { // Handle error + fmt.Println(err) + return } // Output: oops diff --git a/go.mod b/go.mod index 278c38f..5c897d3 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/min0625/errgroup -go 1.22.7 +go 1.22.12 require ( github.com/stretchr/testify v1.9.0 diff --git a/tools/go.mod b/tools/go.mod deleted file mode 100644 index 8b24bcb..0000000 --- a/tools/go.mod +++ /dev/null @@ -1,12 +0,0 @@ -module github.com/min0625/json-arr/tools - -go 1.22.7 - -require mvdan.cc/gofumpt v0.7.0 - -require ( - github.com/google/go-cmp v0.6.0 // indirect - golang.org/x/mod v0.14.0 // indirect - golang.org/x/sync v0.6.0 // indirect - golang.org/x/tools v0.17.0 // indirect -) diff --git a/tools/go.sum b/tools/go.sum deleted file mode 100644 index 856e4a5..0000000 --- a/tools/go.sum +++ /dev/null @@ -1,20 +0,0 @@ -github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI= -github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow= -github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= -github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= -golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= -golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= -golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= -golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= -golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps= -mvdan.cc/gofumpt v0.7.0 h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU= -mvdan.cc/gofumpt v0.7.0/go.mod h1:txVFJy/Sc/mvaycET54pV8SW8gWxTlUuGHVEcncmNUo= diff --git a/tools/tools.go b/tools/tools.go deleted file mode 100644 index de6f5cf..0000000 --- a/tools/tools.go +++ /dev/null @@ -1,8 +0,0 @@ -//go:build tools -// +build tools - -package tools - -import ( - _ "mvdan.cc/gofumpt" -)