diff --git a/.github/actions/cargo-code-coverage/action.yaml b/.github/actions/cargo-code-coverage/action.yaml new file mode 100644 index 0000000..402720a --- /dev/null +++ b/.github/actions/cargo-code-coverage/action.yaml @@ -0,0 +1,57 @@ +name: cargo-code-coverage +description: "An action for generating code-coverage for a project using cargo-llvm-cov" + +inputs: + output-file: + required: true + description: "The output file to write the coverage information to, which gets cached." + minimum-requirement: + required: false + description: "A decimal value indicating the minimum required coverage requirements. 0 disables this." + default: "0.0" + +runs: + using: "composite" + steps: + - name: Cache Coverage + id: cargo-coverage + uses: actions/cache@v3 + with: + path: ${{ inputs.output-file }} + key: cargo-coverage-${{ runner.os }}-${{ hashFiles('**/*.rs', '**/Cargo.toml', 'Cargo.toml') }} + + - name: Install cargo-llvm-cov + if: steps.cargo-coverage.outputs.cache-hit != 'true' + uses: taiki-e/install-action@cargo-llvm-cov + + - name: Generate Coverage Coverage + if: steps.cargo-coverage.outputs.cache-hit != 'true' + shell: bash + run: cargo llvm-cov --json --output-path ${{ inputs.output-file }} + + - name: Emit 'coverage' output + id: report + shell: bash + run: echo "coverage=$(cat ${{ inputs.output-file }})" >> "${GITHUB_OUTPUT}" + + - name: Check Coverage Requirement + if: inputs.minimum-requirement > 0.0 + shell: bash + run: | + coverage=${{fromJson(steps.report.outputs.coverage).data[0].totals.lines.percent}} + + if [[ ${{inputs.minimum-requirement}} > ${coverage} ]]; then + echo \ + "::error::Project code coverage fell below minimum desired '${{inputs.minimum-requirement}}%'!" \ + "The current coverage is '${COVERAGE}%.'" \ + "Please either add more tests, or lower the requirements." + exit 1 + fi + + - name: Upload Coverage Artifact + uses: actions/upload-artifact@v1 + if: steps.report.outcome == 'success' + continue-on-error: true + with: + name: ${{ runner.os }}-codecov + path: ${{ inputs.output-file }} diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index a5b23b0..1b065a0 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -158,9 +158,6 @@ jobs: defaults: run: shell: bash - env: - OUTPUT_FILE: ${{ matrix.os }}-codecov.json - COVERAGE_MINIMUM: ${{ 0.0 }} steps: - name: Checkout @@ -174,9 +171,6 @@ jobs: with: components: llvm-tools-preview - - name: Install cargo-llvm-cov - uses: taiki-e/install-action@cargo-llvm-cov - - name: Build id: build uses: ./.github/actions/cargo-build @@ -185,28 +179,8 @@ jobs: id: test run: cargo test --verbose - - name: Generate Coverage Report - id: report - run: | - cargo llvm-cov --json --output-path ${{ env.OUTPUT_FILE }} - echo "coverage=$(cat ${{ env.OUTPUT_FILE }})" >> "${GITHUB_OUTPUT}" - - - name: Check Coverage Requirement - if: env.COVERAGE_MINIMUM > 0.0 - env: - COVERAGE: ${{fromJson(steps.report.outputs.coverage).data[0].totals.lines.percent}} - run: | - if [[ ${{env.COVERAGE_MINIMUM}} > ${{env.COVERAGE}} ]]; then - echo "Error: Project code coverage fell below minimum desired '${COVERAGE_MINIMUM}%'!" >&2 - echo "The current coverage is '${COVERAGE}%.'" >&2 - echo "Please either add more tests, or lower the requirements." >&2 - exit 1 - fi - - - name: Upload Coverage Artifact - uses: actions/upload-artifact@v1 - if: steps.report.outcome == 'success' - continue-on-error: true + - name: Coverage + uses: ./.github/actions/cargo-code-coverage with: - name: ${{matrix.os}}-codecov - path: ${{env.OUTPUT_FILE}} + output-file: ${{ matrix.os }}-codecov.json + minimum-required: 0.0