当代码覆盖率小于 x% 时使构建失败

Make the build failure when code coverage is less than x%

我正在使用 github 操作,在我的测试中,当我的代码覆盖率低于 80% 时,我需要让 myt 构建失败。我在 github 市场中查找了一些 github 操作,但没有找到任何东西。我可以做吗 ?如果有帮助,我正在链接我的工作流文件

---
name: lint build and test
on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  build-and-test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.15
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}  

      - name: Super-Linter
        uses: github/super-linter@v3.14.0
        env:
          VALIDATE_GO: false
          VALIDATE_JSCPD: false
          VALIDATE_ALL_CODEBASE: true
          DEFAULT_BRANCH: master          
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  

      - name: golangci-lint
        uses: golangci/golangci-lint-action@v2
        with:
          version: v1.29

      - name: Build
        run: go build -o apiDateTime -v ./...    

      - name: Test 
        run: go test ./... -coverprofile cover.out -covermode atomic

      - name: Coverage result
        run: go tool cover -func cover.out

我会将 go test 的调用替换为 shell 脚本的调用,如 here 所述。

shell 脚本看起来像这样

!#/bin/sh

set -e -u

go test ./... -coverprofile cover.out -covermode atomic

perc=`go tool cover -func=cover.out | tail -n 1 | sed -Ee 's!^[^[:digit:]]+([[:digit:]]+(\.[[:digit:]]+)?)%$!!'`
res=`echo "$perc >= 80.0" | bc`
test "$res" -eq 1 && exit 0
echo "Insufficient coverage: $perc" >&2
exit 1

其中:

  1. 涉及sed的咒语提取覆盖率(见here)。
  2. 下一行要求计算器将百分比与您配置的阈值进行比较。
  3. 如果测试通过,则下一行使脚本成功退出。
  4. 如果不满足覆盖要求,脚本的其余部分就会崩溃。

此脚本需要 bc tool 安装在那个 ubuntu-latest 包中(我不知道)。
如果不是,则整个过程可以用图像中可用的任何语言编写脚本,例如 Perl 或 Python.