-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yaml
86 lines (86 loc) · 2.84 KB
/
action.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
name: "go testcoverage action"
description: "Run tests and get coverage on your Go code"
author: "Jonnatan Oyarzun"
inputs:
install-go:
description: |
By default the action install a version of Go appropriate for building and running.
If set to false, you will need to have installed Go.
required: true
default: true
go-version:
description: |
Version of Go
required: false
default: 1.19.x
cache-key:
description: |
Custom string to include in the cache key. This is useful when using multiple Go versions.
required: false
coverage-threshold:
description: |
The threshold in percentage to consider the test successful. Default is 0 so any value will pass
required: true
default: 0
use-ginkgo:
description: |
Use ginkgo to run tests and get the coverage
required: false
default: false
workdir:
description: |
Go base directory
required: false
default: .
should-fail:
description: |
Util var to test if fail
required: false
default: false
runs:
using: "composite"
steps:
- id: install_go
if: ${{ inputs.install-go != 'false' }}
uses: WillAbides/setup-go-faster@v1.8.0
with:
go-version: ${{ inputs.go-version }}
- uses: actions/cache@v3
if: ${{ inputs.merge-files == '' }}
with:
path: |
${{ inputs.install-go != 'false' && steps.install_go.outputs.GOCACHE || '' }}
key: testcoverage-${{ runner.os }}-${{ inputs.cache-key }}-${{ github.sha }}
restore-keys: |
testcoverage-${{ runner.os }}-${{ inputs.cache-key }}-
- env:
COVERAGE_THRESHOLD: ${{ inputs.coverage-threshold }}
USE_GINKGO: ${{ inputs.use-ginkgo }}
WORKDIR: ${{ inputs.workdir }}
SHOULD_FAIL: ${{ inputs.should-fail }}
run: |
cd "$WORKDIR"
if $USE_GINKGO -eq "true"; then
go install github.com/onsi/ginkgo/v2/ginkgo
ginkgo -v -cover -covermode=count -coverpkg=./... ./...
else
go test -v -coverprofile=coverprofile.out ./...
fi
echo "Checking test coverage is above threshold ..."
echo "Threshold : $COVERAGE_THRESHOLD %"
totalCoverage=`go tool cover -func=coverprofile.out | grep total | grep -Eo '[0-9]+\.[0-9]+'`
echo "Current test coverage : $totalCoverage %"
if (( $(echo "$totalCoverage $COVERAGE_THRESHOLD" | awk '{print ($1 >= $2)}') )); then
echo "Coverage OK"
if "$SHOULD_FAIL" -eq "true"; then
exit 1
fi
else
echo "Current test coverage is below threshold. Please add more unit tests or adjust threshold to a lower value."
echo "Failed"
if "$SHOULD_FAIL" -eq "true"; then
exit 0
fi
exit 1
fi
shell: bash