Skip to content

Commit 7e7f50b

Browse files
authored
Add package functions to manage vars in context (#4)
1 parent 8e9e3fb commit 7e7f50b

11 files changed

Lines changed: 183 additions & 51 deletions

File tree

.github/workflows/cloc.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
name: cloc
33
on:
44
pull_request:
5+
6+
# Cancel the workflow in progress in newer build is about to start.
7+
concurrency:
8+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
9+
cancel-in-progress: true
10+
511
jobs:
612
cloc:
713
runs-on: ubuntu-latest
@@ -18,7 +24,7 @@ jobs:
1824
- name: Count Lines Of Code
1925
id: loc
2026
run: |
21-
curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.1/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz
27+
curl -sLO https://github.com/vearutop/sccdiff/releases/download/v1.0.2/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && echo "b17e76bede22af0206b4918d3b3c4e7357f2a21b57f8de9e7c9dc0eb56b676c0 sccdiff" | shasum -c
2228
OUTPUT=$(cd pr && ../sccdiff -basedir ../base)
2329
echo "${OUTPUT}"
2430
OUTPUT="${OUTPUT//$'\n'/%0A}"

.github/workflows/golangci-lint.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,26 @@ on:
88
- master
99
- main
1010
pull_request:
11+
12+
# Cancel the workflow in progress in newer build is about to start.
13+
concurrency:
14+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
15+
cancel-in-progress: true
16+
1117
jobs:
1218
golangci:
1319
name: golangci-lint
1420
runs-on: ubuntu-latest
1521
steps:
22+
- uses: actions/setup-go@v3
23+
with:
24+
go-version: 1.18.x
1625
- uses: actions/checkout@v2
1726
- name: golangci-lint
18-
uses: golangci/golangci-lint-action@v2.5.2
27+
uses: golangci/golangci-lint-action@v3.1.0
1928
with:
2029
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
21-
version: v1.43.0
30+
version: v1.46.2
2231

2332
# Optional: working directory, useful for monorepos
2433
# working-directory: somedir

.github/workflows/gorelease.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22
name: gorelease
33
on:
44
pull_request:
5+
6+
# Cancel the workflow in progress in newer build is about to start.
7+
concurrency:
8+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
9+
cancel-in-progress: true
10+
511
env:
6-
GO_VERSION: 1.17.x
12+
GO_VERSION: 1.18.x
713
jobs:
814
gorelease:
915
runs-on: ubuntu-latest
1016
steps:
1117
- name: Install Go stable
1218
if: env.GO_VERSION != 'tip'
13-
uses: actions/setup-go@v2
19+
uses: actions/setup-go@v3
1420
with:
1521
go-version: ${{ env.GO_VERSION }}
1622
- name: Install Go tip
@@ -29,12 +35,12 @@ jobs:
2935
with:
3036
path: |
3137
~/go/bin/gorelease
32-
key: ${{ runner.os }}-gorelease
38+
key: ${{ runner.os }}-gorelease-generic
3339
- name: Gorelease
3440
id: gorelease
3541
run: |
3642
test -e ~/go/bin/gorelease || go install golang.org/x/exp/cmd/gorelease@latest
37-
OUTPUT=$(gorelease || exit 0)
43+
OUTPUT=$(gorelease 2>&1 || exit 0)
3844
echo "${OUTPUT}"
3945
OUTPUT="${OUTPUT//$'\n'/%0A}"
4046
echo "::set-output name=report::$OUTPUT"

.github/workflows/test-unit.yml

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,30 @@ on:
66
- master
77
- main
88
pull_request:
9+
10+
# Cancel the workflow in progress in newer build is about to start.
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
13+
cancel-in-progress: true
14+
915
env:
1016
GO111MODULE: "on"
1117
RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing.
12-
COV_GO_VERSION: 1.17.x # Version of Go to collect coverage
18+
COV_GO_VERSION: 1.18.x # Version of Go to collect coverage
19+
TARGET_DELTA_COV: 90 # Target coverage of changed lines, in percents
1320
jobs:
1421
test:
1522
strategy:
1623
matrix:
17-
go-version: [ 1.16.x, 1.17.x, tip ]
24+
go-version: [ 1.16.x, 1.17.x, 1.18.x ]
1825
runs-on: ubuntu-latest
1926
steps:
2027
- name: Install Go stable
2128
if: matrix.go-version != 'tip'
22-
uses: actions/setup-go@v2
29+
uses: actions/setup-go@v3
2330
with:
2431
go-version: ${{ matrix.go-version }}
32+
2533
- name: Install Go tip
2634
if: matrix.go-version == 'tip'
2735
run: |
@@ -31,8 +39,10 @@ jobs:
3139
tar -C ~/sdk/gotip -xzf gotip.tar.gz
3240
~/sdk/gotip/bin/go version
3341
echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV
42+
3443
- name: Checkout code
3544
uses: actions/checkout@v2
45+
3646
- name: Go cache
3747
uses: actions/cache@v2
3848
with:
@@ -45,58 +55,79 @@ jobs:
4555
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }}
4656
restore-keys: |
4757
${{ runner.os }}-go-cache
58+
4859
- name: Restore base test coverage
4960
id: base-coverage
50-
if: matrix.go-version == env.COV_GO_VERSION
61+
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
5162
uses: actions/cache@v2
5263
with:
5364
path: |
5465
unit-base.txt
5566
# Use base sha for PR or new commit hash for master/main push in test result key.
5667
key: ${{ runner.os }}-unit-test-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
57-
- name: Checkout base code
58-
if: matrix.go-version == env.COV_GO_VERSION && env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
59-
uses: actions/checkout@v2
60-
with:
61-
ref: ${{ github.event.pull_request.base.sha }}
62-
path: __base
68+
6369
- name: Run test for base code
6470
if: matrix.go-version == env.COV_GO_VERSION && env.RUN_BASE_COVERAGE == 'on' && steps.base-coverage.outputs.cache-hit != 'true' && github.event.pull_request.base.sha != ''
6571
run: |
66-
cd __base
67-
make | grep test-unit && (make test-unit && go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > ../unit-base.txt) || echo "No test-unit in base"
72+
git fetch origin master ${{ github.event.pull_request.base.sha }}
73+
HEAD=$(git rev-parse HEAD)
74+
git reset --hard ${{ github.event.pull_request.base.sha }}
75+
(make test-unit && go tool cover -func=./unit.coverprofile > unit-base.txt) || echo "No test-unit in base"
76+
git reset --hard $HEAD
77+
6878
- name: Test
6979
id: test
7080
run: |
7181
make test-unit
72-
go tool cover -func=./unit.coverprofile | sed -e 's/.go:[0-9]*:\t/.go\t/g' | sed -e 's/\t\t*/\t/g' > unit.txt
73-
OUTPUT=$(test -e unit-base.txt && (diff unit-base.txt unit.txt || exit 0) || cat unit.txt)
74-
echo "${OUTPUT}"
75-
OUTPUT="${OUTPUT//$'\n'/%0A}"
82+
go tool cover -func=./unit.coverprofile > unit.txt
7683
TOTAL=$(grep 'total:' unit.txt)
7784
echo "${TOTAL}"
78-
echo "::set-output name=diff::$OUTPUT"
7985
echo "::set-output name=total::$TOTAL"
80-
- name: Store base coverage
81-
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
82-
run: cp unit.txt unit-base.txt
86+
87+
- name: Annotate missing test coverage
88+
id: annotate
89+
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
90+
run: |
91+
git fetch origin master ${{ github.event.pull_request.base.sha }}
92+
curl -sLO https://github.com/vearutop/gocovdiff/releases/download/v1.3.4/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && shasum -a 256 gocovdiff && echo "b351c67526eefeb0671c82e9271ae984875865eed19e911f40f78348cb98347c gocovdiff" | shasum -c
93+
REP=$(./gocovdiff -cov unit.coverprofile -gha-annotations gha-unit.txt -delta-cov-file delta-cov-unit.txt -target-delta-cov ${TARGET_DELTA_COV})
94+
echo "${REP}"
95+
REP="${REP//$'\n'/%0A}"
96+
cat gha-unit.txt
97+
DIFF=$(test -e unit-base.txt && ./gocovdiff -func-cov unit.txt -func-base-cov unit-base.txt || echo "Missing base coverage file")
98+
DIFF="${DIFF//$'\n'/%0A}"
99+
TOTAL=$(cat delta-cov-unit.txt)
100+
echo "::set-output name=rep::$REP"
101+
echo "::set-output name=diff::$DIFF"
102+
echo "::set-output name=total::$TOTAL"
103+
83104
- name: Comment Test Coverage
84105
continue-on-error: true
85-
if: matrix.go-version == env.COV_GO_VERSION
106+
if: matrix.go-version == env.COV_GO_VERSION && github.event.pull_request.base.sha != ''
86107
uses: marocchino/sticky-pull-request-comment@v2
87108
with:
88109
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
89110
header: unit-test
90111
message: |
91112
### Unit Test Coverage
92113
${{ steps.test.outputs.total }}
114+
${{ steps.annotate.outputs.total }}
115+
<details><summary>Coverage of changed lines</summary>
116+
117+
${{ steps.annotate.outputs.rep }}
118+
119+
</details>
120+
93121
<details><summary>Coverage diff with base branch</summary>
94122
95-
```diff
96-
${{ steps.test.outputs.diff }}
97-
```
123+
${{ steps.annotate.outputs.diff }}
124+
98125
</details>
99126
127+
- name: Store base coverage
128+
if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
129+
run: cp unit.txt unit-base.txt
130+
100131
- name: Upload code coverage
101132
if: matrix.go-version == env.COV_GO_VERSION
102133
uses: codecov/codecov-action@v1

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ linters:
3737
- tagliatelle
3838
- errname
3939
- ireturn
40+
- exhaustruct
41+
- nonamedreturns
4042

4143
issues:
4244
exclude-use-default: false

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#GOLANGCI_LINT_VERSION := "v1.43.0" # Optional configuration to pinpoint golangci-lint version.
1+
#GOLANGCI_LINT_VERSION := "v1.46.2" # 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

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
![Code lines](https://sloc.xyz/github/bool64/shared/?category=code)
88
![Comments](https://sloc.xyz/github/bool64/shared/?category=comments)
99

10-
Shared provides space and contract to share variables between test helpers.
10+
Shared provides space and contract to share variables between test helpers and libraries.

go.mod

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
module github.com/bool64/shared
22

3-
go 1.11
3+
go 1.17
44

55
require (
6-
github.com/bool64/dev v0.2.5
6+
github.com/bool64/dev v0.2.17
77
github.com/stretchr/testify v1.4.0
88
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
gopkg.in/yaml.v2 v2.2.2 // indirect
14+
)

go.sum

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
github.com/bool64/dev v0.1.43 h1:McAhPS6dBrNSN53K9+4dj4ntCjEf9pw/r8AwaXRKgX8=
2-
github.com/bool64/dev v0.1.43/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
3-
github.com/bool64/dev v0.2.5 h1:H0bylghwcjDBBhEwSFTjArEO9Dr8cCaB54QSOF7esOA=
4-
github.com/bool64/dev v0.2.5/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
5-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
1+
github.com/bool64/dev v0.2.17 h1:jE+T92oazAIV8fvMDJrKjsF1bzfr5XezZ8bM5GS1Cl0=
2+
github.com/bool64/dev v0.2.17/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
63
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
76
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
87
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
98
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

vars.go

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,40 @@ func (v *Vars) Reset() {
2929

3030
// Fork instruments context with a copy of Vars or returns Vars that are already in context.
3131
func (v *Vars) Fork(ctx context.Context) (context.Context, *Vars) {
32-
if vc, ok := ctx.Value(ctxKey{}).(*Vars); ok {
32+
if vc, ok := ctx.Value(ctxKeyInstance{}).(*Vars); ok {
3333
return ctx, vc
3434
}
3535

36-
v.mu.Lock()
37-
defer v.mu.Unlock()
36+
vc := &Vars{}
37+
38+
if v != nil {
39+
v.mu.Lock()
40+
defer v.mu.Unlock()
3841

39-
vc := &Vars{
40-
VarPrefix: v.VarPrefix,
41-
onSet: v.onSet,
42+
vc.VarPrefix = v.VarPrefix
43+
vc.onSet = v.onSet
44+
45+
if len(v.vars) > 0 {
46+
vc.vars = make(map[string]interface{})
47+
48+
for k, v := range v.vars {
49+
vc.vars[k] = v
50+
}
51+
}
4252
}
4353

44-
if len(v.vars) > 0 {
45-
vc.vars = make(map[string]interface{})
54+
gv := VarsFromContext(ctx)
55+
if len(gv) > 0 {
56+
if vc.vars == nil {
57+
vc.vars = make(map[string]interface{})
58+
}
4659

47-
for k, v := range v.vars {
60+
for k, v := range VarsFromContext(ctx) {
4861
vc.vars[k] = v
4962
}
5063
}
5164

52-
return context.WithValue(ctx, ctxKey{}, vc), vc
65+
return context.WithValue(ctx, ctxKeyInstance{}, vc), vc
5366
}
5467

5568
// IsVar checks if string looks like a variable name.
@@ -62,7 +75,10 @@ func (v *Vars) IsVar(s string) bool {
6275
return strings.HasPrefix(s, varPrefix)
6376
}
6477

65-
type ctxKey struct{}
78+
type (
79+
ctxKeyInstance struct{}
80+
ctxKey struct{}
81+
)
6682

6783
// Get returns variable value if is exists.
6884
func (v *Vars) Get(s string) (interface{}, bool) {
@@ -112,3 +128,37 @@ func (v *Vars) GetAll() map[string]interface{} {
112128

113129
return res
114130
}
131+
132+
// VarToContext adds variable to context.
133+
func VarToContext(ctx context.Context, key string, value interface{}) context.Context {
134+
if vc, ok := ctx.Value(ctxKeyInstance{}).(*Vars); ok {
135+
vc.Set(key, value)
136+
137+
return ctx
138+
}
139+
140+
m, ok := ctx.Value(ctxKey{}).(map[string]interface{})
141+
if !ok {
142+
m = make(map[string]interface{})
143+
144+
ctx = context.WithValue(ctx, ctxKey{}, m)
145+
}
146+
147+
m[key] = value
148+
149+
return ctx
150+
}
151+
152+
// VarsFromContext returns variables from context.
153+
func VarsFromContext(ctx context.Context) map[string]interface{} {
154+
if vc, ok := ctx.Value(ctxKeyInstance{}).(*Vars); ok {
155+
return vc.vars
156+
}
157+
158+
m, ok := ctx.Value(ctxKey{}).(map[string]interface{})
159+
if !ok {
160+
return nil
161+
}
162+
163+
return m
164+
}

0 commit comments

Comments
 (0)