Skip to content

Commit 50affbb

Browse files
authoredJan 29, 2023
Tolerate nil primary error in multi error, fixes #17 (#18)
1 parent 79ab0c3 commit 50affbb

13 files changed

+103
-40
lines changed
 

‎.github/ISSUE_TEMPLATE/bug_report.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
If feasible/relevant, please provide a code snippet (inline or with Go playground) to reproduce the issue.
15+
16+
17+
**Expected behavior**
18+
A clear and concise description of what you expected to happen.
19+
20+
**Additional context**
21+
Add any other context about the problem here.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
Please use discussions https://github.com/bool64/ctxd/discussions/categories/ideas to share feature ideas.

‎.github/ISSUE_TEMPLATE/question.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
name: Question
3+
about: Any question about features or usage
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
Please use discussions https://github.com/bool64/ctxd/discussions/categories/q-a to make your question more discoverable by other folks.

‎.github/workflows/bench.yml

+32-19
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
uses: actions/setup-go@v3
3232
with:
3333
go-version: ${{ env.GO_VERSION }}
34+
3435
- name: Install Go tip
3536
if: env.GO_VERSION == 'tip'
3637
run: |
@@ -40,10 +41,12 @@ jobs:
4041
tar -C ~/sdk/gotip -xzf gotip.tar.gz
4142
~/sdk/gotip/bin/go version
4243
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
44+
4345
- name: Checkout code
4446
uses: actions/checkout@v2
4547
with:
4648
ref: ${{ (github.event.inputs.new != '') && github.event.inputs.new || github.event.ref }}
49+
4750
- name: Go cache
4851
uses: actions/cache@v2
4952
with:
@@ -56,47 +59,57 @@ jobs:
5659
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
5760
restore-keys: |
5861
${{ runner.os }}-go-cache
62+
5963
- name: Restore benchstat
6064
uses: actions/cache@v2
6165
with:
6266
path: ~/go/bin/benchstat
63-
key: ${{ runner.os }}-benchstat
67+
key: ${{ runner.os }}-benchstat-legacy
68+
6469
- name: Restore base benchmark result
70+
id: base-benchmark
6571
if: env.CACHE_BENCHMARK == 'on'
66-
id: benchmark-base
6772
uses: actions/cache@v2
6873
with:
6974
path: |
7075
bench-master.txt
7176
bench-main.txt
7277
# Use base sha for PR or new commit hash for master/main push in benchmark result key.
7378
key: ${{ runner.os }}-bench-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
74-
- name: Checkout base code
75-
if: env.RUN_BASE_BENCHMARK == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && (github.event.pull_request.base.sha != '' || github.event.inputs.old != '')
76-
uses: actions/checkout@v2
77-
with:
78-
ref: ${{ (github.event.pull_request.base.sha != '' ) && github.event.pull_request.base.sha || github.event.inputs.old }}
79-
path: __base
80-
- name: Run base benchmark
81-
if: env.RUN_BASE_BENCHMARK == 'on' && steps.benchmark-base.outputs.cache-hit != 'true' && (github.event.pull_request.base.sha != '' || github.event.inputs.old != '')
79+
80+
- name: Run benchmark
8281
run: |
82+
export REF_NAME=new
83+
make bench
84+
OUTPUT=$(make bench-stat-diff)
85+
echo "${OUTPUT}"
86+
echo "diff<<EOF" >> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
87+
OUTPUT=$(make bench-stat)
88+
echo "${OUTPUT}"
89+
echo "result<<EOF" >> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
90+
91+
- name: Run benchmark for base code
92+
if: env.RUN_BASE_BENCHMARK == 'on' && steps.base-benchmark.outputs.cache-hit != 'true' && (github.event.pull_request.base.sha != '' || github.event.inputs.old != '')
93+
run: |
94+
git fetch origin master ${{ github.event.pull_request.base.sha }}
95+
HEAD=$(git rev-parse HEAD)
96+
git reset --hard ${{ github.event.pull_request.base.sha }}
8397
export REF_NAME=master
84-
cd __base
85-
make | grep bench-run && (BENCH_COUNT=5 make bench-run bench-stat && cp bench-master.txt ../bench-master.txt) || echo "No benchmarks in base"
86-
- name: Benchmark
98+
make bench-run bench-stat
99+
git reset --hard $HEAD
100+
101+
- name: Benchmark stats
87102
id: bench
88103
run: |
89104
export REF_NAME=new
90-
BENCH_COUNT=5 make bench
91105
OUTPUT=$(make bench-stat-diff)
92106
echo "${OUTPUT}"
93-
OUTPUT="${OUTPUT//$'\n'/%0A}"
94-
echo "::set-output name=diff::$OUTPUT"
107+
echo "diff<<EOF" >> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
95108
OUTPUT=$(make bench-stat)
96109
echo "${OUTPUT}"
97-
OUTPUT="${OUTPUT//$'\n'/%0A}"
98-
echo "::set-output name=result::$OUTPUT"
99-
- name: Comment Benchmark Result
110+
echo "result<<EOF" >> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
111+
112+
- name: Comment benchmark result
100113
continue-on-error: true
101114
uses: marocchino/sticky-pull-request-comment@v2
102115
with:

‎.github/workflows/cloc.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@ jobs:
2121
with:
2222
ref: ${{ github.event.pull_request.base.sha }}
2323
path: base
24-
- name: Count Lines Of Code
24+
- name: Count lines of code
2525
id: loc
2626
run: |
2727
curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.3/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
2828
sccdiff_hash=$(git hash-object ./sccdiff)
2929
[ "$sccdiff_hash" == "ae8a07b687bd3dba60861584efe724351aa7ff63" ] || (echo "::error::unexpected hash for sccdiff, possible tampering: $sccdiff_hash" && exit 1)
3030
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
3131
echo "${OUTPUT}"
32-
OUTPUT="${OUTPUT//$'\n'/%0A}"
33-
echo "::set-output name=diff::$OUTPUT"
32+
echo "diff<<EOF" >> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
3433
35-
- name: Comment Code Lines
34+
- name: Comment lines of code
3635
continue-on-error: true
3736
uses: marocchino/sticky-pull-request-comment@v2
3837
with:

‎.github/workflows/golangci-lint.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ jobs:
2424
go-version: 1.19.x
2525
- uses: actions/checkout@v2
2626
- name: golangci-lint
27-
uses: golangci/golangci-lint-action@v3.2.0
27+
uses: golangci/golangci-lint-action@v3.3.1
2828
with:
2929
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
30-
version: v1.50.0
30+
version: v1.50.1
3131

3232
# Optional: working directory, useful for monorepos
3333
# working-directory: somedir

‎.github/workflows/gorelease.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ jobs:
4242
test -e ~/go/bin/gorelease || go install golang.org/x/exp/cmd/gorelease@latest
4343
OUTPUT=$(gorelease 2>&1 || exit 0)
4444
echo "${OUTPUT}"
45-
OUTPUT="${OUTPUT//$'\n'/%0A}"
46-
echo "::set-output name=report::$OUTPUT"
47-
- name: Comment Report
45+
echo "report<<EOF" >> $GITHUB_OUTPUT && echo "$OUTPUT" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
46+
- name: Comment report
4847
continue-on-error: true
4948
uses: marocchino/sticky-pull-request-comment@v2
5049
with:

‎.github/workflows/test-unit.yml

+5-7
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
go tool cover -func=./unit.coverprofile > unit.txt
8383
TOTAL=$(grep 'total:' unit.txt)
8484
echo "${TOTAL}"
85-
echo "::set-output name=total::$TOTAL"
85+
echo "total=$TOTAL" >> $GITHUB_OUTPUT
8686
8787
- name: Annotate missing test coverage
8888
id: annotate
@@ -94,16 +94,14 @@ jobs:
9494
git fetch origin master ${{ github.event.pull_request.base.sha }}
9595
REP=$(./gocovdiff -cov unit.coverprofile -gha-annotations gha-unit.txt -delta-cov-file delta-cov-unit.txt -target-delta-cov ${TARGET_DELTA_COV})
9696
echo "${REP}"
97-
REP="${REP//$'\n'/%0A}"
9897
cat gha-unit.txt
9998
DIFF=$(test -e unit-base.txt && ./gocovdiff -func-cov unit.txt -func-base-cov unit-base.txt || echo "Missing base coverage file")
100-
DIFF="${DIFF//$'\n'/%0A}"
10199
TOTAL=$(cat delta-cov-unit.txt)
102-
echo "::set-output name=rep::$REP"
103-
echo "::set-output name=diff::$DIFF"
104-
echo "::set-output name=total::$TOTAL"
100+
echo "rep<<EOF" >> $GITHUB_OUTPUT && echo "$REP" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
101+
echo "diff<<EOF" >> $GITHUB_OUTPUT && echo "$DIFF" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
102+
echo "total<<EOF" >> $GITHUB_OUTPUT && echo "$TOTAL" >> $GITHUB_OUTPUT && echo "EOF" >> $GITHUB_OUTPUT
105103
106-
- name: Comment Test Coverage
104+
- name: Comment test coverage
107105
continue-on-error: true
108106
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
109107
uses: marocchino/sticky-pull-request-comment@v2

‎Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#GOLANGCI_LINT_VERSION := "v1.50.0" # Optional configuration to pinpoint golangci-lint version.
1+
#GOLANGCI_LINT_VERSION := "v1.50.1" # Optional configuration to pinpoint golangci-lint version.
22

33
# The head of Makefile determines location of dev-go to include standard targets.
44
GO ?= go

‎error.go

+8
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ type multi struct {
324324

325325
// Error returns message.
326326
func (le multi) Error() string {
327+
if le.primary == nil {
328+
if len(le.secondary) > 0 {
329+
return le.secondary[0].Error()
330+
}
331+
332+
return "empty multi error"
333+
}
334+
327335
return le.primary.Error()
328336
}
329337

‎error_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func TestTuples_Fields(t *testing.T) {
260260
ctxd.Tuples{"a", 123, "b", 456}.Fields()) // All good.
261261
}
262262

263-
func TestNewMulti(t *testing.T) {
263+
func TestMultiError(t *testing.T) {
264264
errPrimary := errors.New("failed")
265265
errSecondary1 := ctxd.SentinelError("miserably")
266266
errSecondary2 := ctxd.SentinelError("hopelessly")
@@ -282,3 +282,8 @@ func TestNewMulti(t *testing.T) {
282282
assert.True(t, errors.As(err, &errSentinel))
283283
assert.Equal(t, "miserably", string(errSentinel))
284284
}
285+
286+
func TestMultiError_invalid(t *testing.T) {
287+
assert.Equal(t, "empty multi error", ctxd.MultiError(nil).Error())
288+
assert.Equal(t, "secondary fail", ctxd.MultiError(nil, errors.New("secondary fail")).Error())
289+
}

‎go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/bool64/ctxd
33
go 1.17
44

55
require (
6-
github.com/bool64/dev v0.2.22
6+
github.com/bool64/dev v0.2.24
77
github.com/stretchr/testify v1.8.0
88
github.com/swaggest/usecase v1.2.0
99
)

‎go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
github.com/bool64/dev v0.2.20/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
2-
github.com/bool64/dev v0.2.22 h1:YJFKBRKplkt+0Emq/5Xk1Z5QRmMNzc1UOJkR3rxJksA=
3-
github.com/bool64/dev v0.2.22/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
2+
github.com/bool64/dev v0.2.24 h1:xptlKivPh870W3Xc9szPcM7wkFmTMuHT8rc0nu7dITk=
3+
github.com/bool64/dev v0.2.24/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
44
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
66
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

0 commit comments

Comments
 (0)
Please sign in to comment.