|
| 1 | +# This script is provided by github.com/bool64/dev. |
1 | 2 | name: test-unit
|
2 | 3 | on:
|
3 | 4 | push:
|
|
7 | 8 | pull_request:
|
8 | 9 | env:
|
9 | 10 | GO111MODULE: "on"
|
| 11 | + 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 |
10 | 13 | jobs:
|
11 | 14 | test:
|
12 | 15 | strategy:
|
13 | 16 | matrix:
|
14 |
| - go-version: [ 1.13.x, 1.14.x, 1.15.x, 1.16.x ] |
| 17 | + go-version: [ 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, tip ] |
15 | 18 | runs-on: ubuntu-latest
|
16 | 19 | steps:
|
17 |
| - - name: Install Go |
| 20 | + - name: Install Go stable |
| 21 | + if: matrix.go-version != 'tip' |
18 | 22 | uses: actions/setup-go@v2
|
19 | 23 | with:
|
20 | 24 | go-version: ${{ matrix.go-version }}
|
| 25 | + - name: Install Go tip |
| 26 | + if: matrix.go-version == 'tip' |
| 27 | + run: | |
| 28 | + curl -sL https://storage.googleapis.com/go-build-snap/go/linux-amd64/$(git ls-remote https://github.com/golang/go.git HEAD | awk '{print $1;}').tar.gz -o gotip.tar.gz |
| 29 | + ls -lah gotip.tar.gz |
| 30 | + mkdir -p ~/sdk/gotip |
| 31 | + tar -C ~/sdk/gotip -xzf gotip.tar.gz |
| 32 | + ~/sdk/gotip/bin/go version |
| 33 | + echo "PATH=$HOME/go/bin:$HOME/sdk/gotip/bin/:$PATH" >> $GITHUB_ENV |
21 | 34 | - name: Checkout code
|
22 | 35 | uses: actions/checkout@v2
|
| 36 | + - name: Go cache |
| 37 | + uses: actions/cache@v2 |
| 38 | + with: |
| 39 | + # In order: |
| 40 | + # * Module download cache |
| 41 | + # * Build cache (Linux) |
| 42 | + path: | |
| 43 | + ~/go/pkg/mod |
| 44 | + ~/.cache/go-build |
| 45 | + key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }} |
| 46 | + restore-keys: | |
| 47 | + ${{ runner.os }}-go-cache |
23 | 48 | - name: Restore base test coverage
|
| 49 | + id: base-coverage |
| 50 | + if: matrix.go-version == env.COV_GO_VERSION |
24 | 51 | uses: actions/cache@v2
|
25 | 52 | with:
|
26 | 53 | path: |
|
27 | 54 | unit-base.txt
|
28 | 55 | # Use base sha for PR or new commit hash for master/main push in test result key.
|
29 | 56 | key: ${{ runner.os }}-unit-test-coverage-${{ (github.event.pull_request.base.sha != github.event.after) && github.event.pull_request.base.sha || github.event.after }}
|
30 |
| - - name: Restore vendor |
31 |
| - uses: actions/cache@v2 |
| 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 |
32 | 60 | with:
|
33 |
| - path: | |
34 |
| - vendor |
35 |
| - key: ${{ runner.os }}-go${{ matrix.go-version }}-vendor-${{ hashFiles('**/go.mod') }} |
36 |
| - - name: Populate dependencies |
| 61 | + ref: ${{ github.event.pull_request.base.sha }} |
| 62 | + path: __base |
| 63 | + - name: Run test for base code |
| 64 | + 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 != '' |
37 | 65 | run: |
|
38 |
| - (test -d vendor && echo vendor found) || (go mod vendor && du -sh vendor && du -sh ~/go/pkg/mod) |
| 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" |
39 | 68 | - name: Test
|
40 | 69 | id: test
|
41 | 70 | run: |
|
42 | 71 | make test-unit
|
43 | 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
|
44 | 73 | OUTPUT=$(test -e unit-base.txt && (diff unit-base.txt unit.txt || exit 0) || cat unit.txt)
|
45 |
| - OUTPUT="${OUTPUT//'%'/'%25'}" |
46 |
| - OUTPUT="${OUTPUT//$'\n'/'%0A'}" |
47 |
| - OUTPUT="${OUTPUT//$'\r'/'%0D'}" |
| 74 | + echo "${OUTPUT}" |
| 75 | + OUTPUT="${OUTPUT//$'\n'/%0A}" |
48 | 76 | TOTAL=$(grep 'total:' unit.txt)
|
| 77 | + echo "${TOTAL}" |
49 | 78 | echo "::set-output name=diff::$OUTPUT"
|
50 | 79 | echo "::set-output name=total::$TOTAL"
|
51 | 80 | - name: Store base coverage
|
52 | 81 | if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/main' }}
|
53 | 82 | run: cp unit.txt unit-base.txt
|
54 | 83 | - name: Comment Test Coverage
|
55 |
| - if: matrix.go-version == '1.16.x' |
| 84 | + continue-on-error: true |
| 85 | + if: matrix.go-version == env.COV_GO_VERSION |
56 | 86 | uses: marocchino/sticky-pull-request-comment@v2
|
57 | 87 | with:
|
58 | 88 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
68 | 98 | </details>
|
69 | 99 |
|
70 | 100 | - name: Upload code coverage
|
71 |
| - if: matrix.go-version == '1.16.x' |
| 101 | + if: matrix.go-version == env.COV_GO_VERSION |
72 | 102 | uses: codecov/codecov-action@v1
|
73 | 103 | with:
|
74 | 104 | file: ./unit.coverprofile
|
|
0 commit comments