diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7af6886..b66f75d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -42,6 +42,7 @@ jobs: path-to-profile: /tmp/coverage/combined.txt flag-name: Go-${{ matrix.go }} parallel: true + if: ${{ github.event.repository.fork == false }} # 仅在非 fork 时上传覆盖率 check-coverage: name: Check coverage @@ -51,14 +52,15 @@ jobs: - uses: shogo82148/actions-goveralls@v1 with: parallel-finished: true + if: ${{ github.event.repository.fork == false }} # 仅在非 fork 时检查覆盖率 # 发布 Release release: name: Release a new version needs: [ lint, test ] runs-on: ubuntu-latest - # 仅在推送标签时执行 - if: ${{ success() && startsWith(github.ref, 'refs/tags/v') }} + # 仅在推送标签时执行 - && - 仅在非 fork 时执行发布 + if: ${{ github.event.repository.fork == false && success() && startsWith(github.ref, 'refs/tags/v') }} steps: # 1. 检出代码 - name: Checkout code diff --git a/check_value.go b/check_value.go index 51bf9a8..c8cb863 100644 --- a/check_value.go +++ b/check_value.go @@ -60,13 +60,13 @@ func None[V comparable](v V) { } } -func Null[V *any](v any) { +func Null[T any](v *T) { if v != nil { panic(errors.New("SHOULD BE NULL BUT IS FULL")) } } -func Full[V *any](v any) { +func Full[T any](v *T) { if v == nil { panic(errors.New("SHOULD BE FULL BUT IS NULL")) } diff --git a/check_value_test.go b/check_value_test.go index 4ad1c0a..7f7ff68 100644 --- a/check_value_test.go +++ b/check_value_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "github.com/yyle88/done/internal/tests" ) func TestGood(t *testing.T) { @@ -27,6 +28,24 @@ type Example struct { S string } -func newExample2x() (*Example, error) { +func newExample() (*Example, error) { return &Example{S: "xyz"}, nil } + +func TestNull(t *testing.T) { + var example *Example + Null(example) + + tests.ExpectPanic(t, func() { + Null(&Example{S: "abc"}) + }) +} + +func TestFull(t *testing.T) { + Full(&Example{S: "abc"}) + + tests.ExpectPanic(t, func() { + var example *Example + Full(example) + }) +} diff --git a/check_vce_test.go b/check_vce_test.go index 373a800..150a1fc 100644 --- a/check_vce_test.go +++ b/check_vce_test.go @@ -5,21 +5,21 @@ import ( ) func TestVE(t *testing.T) { - ve := VCE(newExample2x()) + ve := VCE(newExample()) t.Log(ve.V) } func TestVe_Done(t *testing.T) { - example := VCE(newExample2x()).Done() + example := VCE(newExample()).Done() t.Log(example.S) } func TestVe_Good(t *testing.T) { - VCE(newExample2x()).Good() + VCE(newExample()).Good() } func TestVe_Nice(t *testing.T) { - example := VCE(newExample2x()).Nice() + example := VCE(newExample()).Nice() t.Log(example.S) } diff --git a/go.mod b/go.mod index 9c2734b..dd7b40f 100644 --- a/go.mod +++ b/go.mod @@ -5,14 +5,14 @@ go 1.22.6 require ( github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.10.0 - github.com/yyle88/zaplog v0.0.16 + github.com/yyle88/zaplog v0.0.17 go.uber.org/zap v1.27.0 ) require ( github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect - github.com/yyle88/mutexmap v1.0.8 // indirect + github.com/yyle88/mutexmap v1.0.9 // indirect go.uber.org/multierr v1.11.0 // indirect gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 636e758..34f7f7e 100644 --- a/go.sum +++ b/go.sum @@ -12,10 +12,10 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/yyle88/mutexmap v1.0.8 h1:VntAdXID5wbk211LZEPVK96jQBxIcfVIbQuk9cv3P/8= -github.com/yyle88/mutexmap v1.0.8/go.mod h1:QUYDuARLPlGj414kHewQ5tt8jkDxQXoai8H3C4Gg+yc= -github.com/yyle88/zaplog v0.0.16 h1:ZCxQhq3+nWeWMAXIzeA1EA4exRq5Pn8pXTpEw1GjyD4= -github.com/yyle88/zaplog v0.0.16/go.mod h1:0ct8Rh6uE5i9RG+xbH6d4/pyDBt9JmxBqHNCI+T4wiM= +github.com/yyle88/mutexmap v1.0.9 h1:g1nfNmsReU/AgcGkLgPEKrBeMThJJBrJuvU7i3DOVC0= +github.com/yyle88/mutexmap v1.0.9/go.mod h1:QUYDuARLPlGj414kHewQ5tt8jkDxQXoai8H3C4Gg+yc= +github.com/yyle88/zaplog v0.0.17 h1:2exjnXROfkNRvcKZDAFUM2KDpca1n2OKrEH73pTvrX0= +github.com/yyle88/zaplog v0.0.17/go.mod h1:SJvxdHW7zWu1r+K66oBPBWljiGnDjCV8le3SzQnJpBw= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= diff --git a/internal/tests/tests.go b/internal/tests/tests.go new file mode 100644 index 0000000..efdb68d --- /dev/null +++ b/internal/tests/tests.go @@ -0,0 +1,19 @@ +package tests + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func ExpectPanic(t *testing.T, run func()) { + defer func() { + if recoverFrom := recover(); recoverFrom != nil { + t.Logf("expect panic then catch panic [%v] -> [success]", recoverFrom) + return + } + require.Fail(t, "expect panic while not panic -> [failure]") + }() + + run() +} diff --git a/internal/tests/tests_test.go b/internal/tests/tests_test.go new file mode 100644 index 0000000..e8a4700 --- /dev/null +++ b/internal/tests/tests_test.go @@ -0,0 +1,14 @@ +package tests_test + +import ( + "testing" + + "github.com/pkg/errors" + "github.com/yyle88/must/internal/tests" +) + +func TestExpectPanic(t *testing.T) { + tests.ExpectPanic(t, func() { + panic(errors.New("expect-panic")) + }) +}