Skip to content

Commit

Permalink
Report test coverage in a PR comment.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Jul 19, 2024
1 parent 1236f77 commit e6e32a8
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 10 deletions.
1 change: 1 addition & 0 deletions .cspell
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ aarch
actiongroup
actiongroups
aggregatable
argjson
asciifolding
authc
authinfo
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Spec Test Coverage Analysis
{{with .test_coverage}}

| Total | Tested |
|-------------------|-----------------------------|
| {{.paths_count}} | {{.evaluated_paths_count}} |

{{end}}
3 changes: 2 additions & 1 deletion .github/workflows/pr-comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
workflow_run:
workflows:
- Analyze PR Changes
- Test Spec
types:
- completed

Expand Down Expand Up @@ -71,4 +72,4 @@ jobs:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ env.PR_NUMBER }}
body: ${{ steps.render.outputs.result }}
edit-mode: replace
edit-mode: replace
57 changes: 54 additions & 3 deletions .github/workflows/test-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,61 @@ jobs:
run: docker-compose up -d

- name: Run Tests
run: npm run test:spec -- --opensearch-insecure --coverage test-spec-coverage-${{ matrix.entry.version }}.json
run: |
npm run test:spec -- --opensearch-insecure --coverage coverage/test-spec-coverage-${{ matrix.entry.version }}.json
- name: Upload Test Coverage Results
uses: actions/upload-artifact@v4
with:
name: test-spec-coverage-${{ matrix.entry.version }}
path: test-spec-coverage-${{ matrix.entry.version }}.json
name: coverage-${{ matrix.entry.version }}
path: coverage/test-spec-coverage-${{ matrix.entry.version }}.json

merge-coverage:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
needs: test-opensearch-spec
steps:
- uses: actions/checkout@v3

- name: Download Spec Coverage Data
uses: actions/download-artifact@v4
with:
path: coverage

- name: Combine Test Coverage Data
shell: bash -eo pipefail {0}
run: |
jq -sc '
map(to_entries) |
flatten |
group_by(.key) |
map({key: .[0].key, value: map(.value) | max}) |
from_entries |
.' $(find ./coverage -name "test-spec-coverage-*.json") > ./coverage/coverage.json
cat ./coverage/coverage.json
- name: Construct Comment Data Payload
shell: bash -eo pipefail {0}
run: |
jq \
--arg pr_number ${PR_NUMBER} \
--slurpfile test_coverage ./coverage/coverage.json \
--null-input '
{
"pr_number": ($pr_number),
"comment_identifier": "# Test Coverage Analysis",
"template_name": "pr-test-coverage-analysis",
"template_data": {
"test_coverage": ($test_coverage[0])
}
}
' | tee pr-comment.json
env:
PR_NUMBER: ${{ github.event.pull_request.number }}

- name: Upload PR Comment Payload
uses: actions/upload-artifact@v4
with:
name: pr-comment
path: pr-comment.json
6 changes: 4 additions & 2 deletions tools/src/tester/TestResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ export default class TestResults {

test_coverage(): SpecTestCoverage {
return {
evaluated_paths_count: this.evaluated_paths_count(),
paths_count: this.spec_paths_count()
test_coverage: {
evaluated_paths_count: this.evaluated_paths_count(),
paths_count: this.spec_paths_count()
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions tools/src/tester/types/test.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/

export interface SpecTestCoverage {
paths_count: number
evaluated_paths_count: number
test_coverage: {
paths_count: number
evaluated_paths_count: number
}
}
6 changes: 4 additions & 2 deletions tools/tests/tester/TestResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ describe('TestResults', () => {
const filename = 'coverage.json'
test_results.write_coverage(filename)
expect(JSON.parse(fs.readFileSync(filename, 'utf8'))).toEqual({
evaluated_paths_count: 1,
paths_count: 4
test_coverage: {
evaluated_paths_count: 1,
paths_count: 4
}
})
fs.unlinkSync(filename)
})
Expand Down

0 comments on commit e6e32a8

Please sign in to comment.