diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..4183383 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,37 @@ +name: Go tests + +on: + push: + branches: [master] + pull_request: + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + - uses: actions/setup-go@v7 + with: + go-version-file: go.mod + cache-dependency-path: go.sum + - run: go vet ./... + - run: go test -cover -coverprofile=coverage.out ./... + - name: Check coverage # TODO: increase minimum after adding more tests + run: | + awk -v minimum=0 ' + NR > 1 { + total += $2 + if ($3 > 0) covered += $2 + } + END { + if (total == 0) { + print "No statement coverage data" + exit 1 + } + printf "Total statement coverage: %.2f%% (minimum: %d%%)\n", 100 * covered / total, minimum + exit (100 * covered < minimum * total) + } + ' coverage.out diff --git a/format_test.go b/format_test.go index 10c9b15..d2ce7e8 100644 --- a/format_test.go +++ b/format_test.go @@ -27,7 +27,7 @@ func TestFormatNew(t *testing.T) { "%+v", "error\n" + "github.com/pingcap/errors.TestFormatNew\n" + - "\t.+/pingcap/errors/format_test.go:26", + "\t.+/format_test.go:26", }, { New("error"), "%q", @@ -57,7 +57,7 @@ func TestFormatErrorf(t *testing.T) { "%+v", "error\n" + "github.com/pingcap/errors.TestFormatErrorf\n" + - "\t.+/pingcap/errors/format_test.go:56", + "\t.+/format_test.go:56", }} for i, tt := range tests { @@ -83,7 +83,7 @@ func TestFormatWrap(t *testing.T) { "%+v", "error\n" + "github.com/pingcap/errors.TestFormatWrap\n" + - "\t.+/pingcap/errors/format_test.go:82", + "\t.+/format_test.go:82", }, { Annotate(io.EOF, "error"), "%s", @@ -98,14 +98,14 @@ func TestFormatWrap(t *testing.T) { "EOF\n" + "error\n" + "github.com/pingcap/errors.TestFormatWrap\n" + - "\t.+/pingcap/errors/format_test.go:96", + "\t.+/format_test.go:96", }, { Annotate(Annotate(io.EOF, "error1"), "error2"), "%+v", "EOF\n" + "error1\n" + "github.com/pingcap/errors.TestFormatWrap\n" + - "\t.+/pingcap/errors/format_test.go:103\n", + "\t.+/format_test.go:103\n", }, { Annotate(New("error with space"), "context"), "%q", @@ -136,7 +136,7 @@ func TestFormatWrapf(t *testing.T) { "EOF\n" + "error2\n" + "github.com/pingcap/errors.TestFormatWrapf\n" + - "\t.+/pingcap/errors/format_test.go:134", + "\t.+/format_test.go:134", }, { Annotatef(New("error"), "error%d", 2), "%s", @@ -150,7 +150,7 @@ func TestFormatWrapf(t *testing.T) { "%+v", "error\n" + "github.com/pingcap/errors.TestFormatWrapf\n" + - "\t.+/pingcap/errors/format_test.go:149", + "\t.+/format_test.go:149", }} for i, tt := range tests { @@ -176,7 +176,7 @@ func TestFormatWithStack(t *testing.T) { "%+v", []string{"EOF", "github.com/pingcap/errors.TestFormatWithStack\n" + - "\t.+/pingcap/errors/format_test.go:175"}, + "\t.+/format_test.go:175"}, }, { WithStack(New("error")), "%s", @@ -190,36 +190,36 @@ func TestFormatWithStack(t *testing.T) { "%+v", []string{"error", "github.com/pingcap/errors.TestFormatWithStack\n" + - "\t.+/pingcap/errors/format_test.go:189", + "\t.+/format_test.go:189", "github.com/pingcap/errors.TestFormatWithStack\n" + - "\t.+/pingcap/errors/format_test.go:189"}, + "\t.+/format_test.go:189"}, }, { WithStack(WithStack(io.EOF)), "%+v", []string{"EOF", "github.com/pingcap/errors.TestFormatWithStack\n" + - "\t.+/pingcap/errors/format_test.go:197", + "\t.+/format_test.go:197", "github.com/pingcap/errors.TestFormatWithStack\n" + - "\t.+/pingcap/errors/format_test.go:197"}, + "\t.+/format_test.go:197"}, }, { WithStack(WithStack(Annotatef(io.EOF, "message"))), "%+v", []string{"EOF", "message", "github.com/pingcap/errors.TestFormatWithStack\n" + - "\t.+/pingcap/errors/format_test.go:205", + "\t.+/format_test.go:205", "github.com/pingcap/errors.TestFormatWithStack\n" + - "\t.+/pingcap/errors/format_test.go:205", + "\t.+/format_test.go:205", "github.com/pingcap/errors.TestFormatWithStack\n" + - "\t.+/pingcap/errors/format_test.go:205"}, + "\t.+/format_test.go:205"}, }, { WithStack(Errorf("error%d", 1)), "%+v", []string{"error1", "github.com/pingcap/errors.TestFormatWithStack\n" + - "\t.+/pingcap/errors/format_test.go:216", + "\t.+/format_test.go:216", "github.com/pingcap/errors.TestFormatWithStack\n" + - "\t.+/pingcap/errors/format_test.go:216"}, + "\t.+/format_test.go:216"}, }} for i, tt := range tests { @@ -246,7 +246,7 @@ func TestFormatWithMessage(t *testing.T) { []string{ "error", "github.com/pingcap/errors.TestFormatWithMessage\n" + - "\t.+/pingcap/errors/format_test.go:244", + "\t.+/format_test.go:244", "error2"}, }, { WithMessage(io.EOF, "addition1"), @@ -273,13 +273,13 @@ func TestFormatWithMessage(t *testing.T) { "%+v", []string{"EOF", "error1", "error2", "github.com/pingcap/errors.TestFormatWithMessage\n" + - "\t.+/pingcap/errors/format_test.go:272"}, + "\t.+/format_test.go:272"}, }, { WithMessage(Errorf("error%d", 1), "error2"), "%+v", []string{"error1", "github.com/pingcap/errors.TestFormatWithMessage\n" + - "\t.+/pingcap/errors/format_test.go:278", + "\t.+/format_test.go:278", "error2"}, }, { WithMessage(WithStack(io.EOF), "error"), @@ -287,7 +287,7 @@ func TestFormatWithMessage(t *testing.T) { []string{ "EOF", "github.com/pingcap/errors.TestFormatWithMessage\n" + - "\t.+/pingcap/errors/format_test.go:285", + "\t.+/format_test.go:285", "error"}, }, { WithMessage(Annotate(WithStack(io.EOF), "inside-error"), "outside-error"), @@ -295,7 +295,7 @@ func TestFormatWithMessage(t *testing.T) { []string{ "EOF", "github.com/pingcap/errors.TestFormatWithMessage\n" + - "\t.+/pingcap/errors/format_test.go:293", + "\t.+/format_test.go:293", "inside-error", "outside-error"}, }} diff --git a/stack_test.go b/stack_test.go index d6d855e..413f91e 100644 --- a/stack_test.go +++ b/stack_test.go @@ -68,7 +68,7 @@ func TestFrameFormat(t *testing.T) { Frame(initpc), "%+s", "github.com/pingcap/errors.init\n" + - "\t.+/pingcap/errors/stack_test.go", + "\t.+/stack_test.go", }, { Frame(0), "%s", @@ -115,7 +115,7 @@ func TestFrameFormat(t *testing.T) { Frame(initpc), "%+v", "github.com/pingcap/errors.init\n" + - "\t.+/pingcap/errors/stack_test.go:11", + "\t.+/stack_test.go:11", }, { Frame(0), "%v", @@ -155,24 +155,24 @@ func TestStackTrace(t *testing.T) { }{{ New("ooh"), []string{ "github.com/pingcap/errors.TestStackTrace\n" + - "\t.+/pingcap/errors/stack_test.go", + "\t.+/stack_test.go", }, }, { Annotate(New("ooh"), "ahh"), []string{ "github.com/pingcap/errors.TestStackTrace\n" + - "\t.+/pingcap/errors/stack_test.go", // this is the stack of Wrap, not New + "\t.+/stack_test.go", // this is the stack of Wrap, not New }, }, { Cause(Annotate(New("ooh"), "ahh")), []string{ "github.com/pingcap/errors.TestStackTrace\n" + - "\t.+/pingcap/errors/stack_test.go", // this is the stack of New + "\t.+/stack_test.go", // this is the stack of New }, }, { func() error { return New("ooh") }(), []string{ `github.com/pingcap/errors.(func·009|TestStackTrace.func1)` + - "\n\t.+/pingcap/errors/stack_test.go", // this is the stack of New + "\n\t.+/stack_test.go", // this is the stack of New "github.com/pingcap/errors.TestStackTrace\n" + - "\t.+/pingcap/errors/stack_test.go", // this is the stack of New's caller + "\t.+/stack_test.go", // this is the stack of New's caller }, }, { Cause(func() error { @@ -183,11 +183,11 @@ func TestStackTrace(t *testing.T) { // go 1.23 when Debug its suffix is "TestStackTrace.func2.1", it's // "TestStackTrace.TestStackTrace.func2.func3" when Run `github.com/pingcap/errors.(func·010|TestStackTrace.func2.1|TestStackTrace.TestStackTrace.func2.func3)` + - "\n\t.+/pingcap/errors/stack_test.go", // this is the stack of Errorf + "\n\t.+/stack_test.go", // this is the stack of Errorf `github.com/pingcap/errors.(func·011|TestStackTrace.func2)` + - "\n\t.+/pingcap/errors/stack_test.go", // this is the stack of Errorf's caller + "\n\t.+/stack_test.go", // this is the stack of Errorf's caller "github.com/pingcap/errors.TestStackTrace\n" + - "\t.+/pingcap/errors/stack_test.go", // this is the stack of Errorf's caller's caller + "\t.+/stack_test.go", // this is the stack of Errorf's caller's caller }, }} for i, tt := range tests { @@ -266,9 +266,9 @@ func TestStackTraceFormat(t *testing.T) { "%+v", "\n" + "github.com/pingcap/errors.stackTrace\n" + - "\t.+/pingcap/errors/stack_test.go:\\d+\n" + + "\t.+/stack_test.go:\\d+\n" + "github.com/pingcap/errors.TestStackTraceFormat\n" + - "\t.+/pingcap/errors/stack_test.go:\\d+", + "\t.+/stack_test.go:\\d+", }, { stackTrace()[:2], "%#v",