Skip to content

Commit

Permalink
Fix: correctly combine test coverage. (#541)
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Aug 30, 2024
1 parent 14d819b commit b3f9788
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/test-spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ jobs:
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
(map(.operations) | add | unique | length) as $total_operations_count |
(map(.evaluated_operations) | add | unique | length) as $evaluated_operations_count |
{
total_operations_count: $total_operations_count,
evaluated_operations_count: $evaluated_operations_count,
evaluated_paths_pct: $evaluated_operations_count | (10000 * . / $total_operations_count | round / 100)
}
' $(find ./coverage -name "test-spec-coverage-*.json") > ./coverage/coverage.json
cat ./coverage/coverage.json
- name: Construct Comment Data Payload
Expand Down
24 changes: 14 additions & 10 deletions tools/src/tester/TestResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* compatible open source license.
*/

import _ from "lodash";
import _, { isEqual } from "lodash";
import MergedOpenApiSpec from "./MergedOpenApiSpec";
import { Operation, StoryEvaluations } from "./types/eval.types";
import { SpecTestCoverage } from "./types/test.types";
Expand All @@ -27,9 +27,9 @@ export default class TestResults {

evaluated_operations(): Operation[] {
if (this._evaluated_operations !== undefined) return this._evaluated_operations
this._evaluated_operations = _.uniq(_.compact(_.flatMap(this._evaluations.evaluations, (evaluation) =>
this._evaluated_operations = _.uniqWith(_.compact(_.flatMap(this._evaluations.evaluations, (evaluation) =>
_.map(evaluation.chapters, (chapter) => chapter.operation)
)))
)), isEqual)
return this._evaluated_operations
}

Expand All @@ -47,22 +47,26 @@ export default class TestResults {

operations(): Operation[] {
if (this._operations !== undefined) return this._operations
this._operations = _.uniq(Object.entries(this._spec.paths()).flatMap(([path, path_item]) => {
this._operations = _.uniqWith(Object.entries(this._spec.paths()).flatMap(([path, path_item]) => {
return Object.values(path_item).map((method) => {
return { method: method.toUpperCase(), path }
})
}))
}), isEqual)

return this._operations
}

test_coverage(): SpecTestCoverage {
return {
evaluated_operations_count: this.evaluated_operations().length,
total_operations_count: this.operations().length,
evaluated_paths_pct: this.operations().length > 0 ? Math.round(
this.evaluated_operations().length / this.operations().length * 100 * 100
) / 100 : 0,
summary: {
evaluated_operations_count: this.evaluated_operations().length,
total_operations_count: this.operations().length,
evaluated_paths_pct: this.operations().length > 0 ? Math.round(
this.evaluated_operations().length / this.operations().length * 100 * 100
) / 100 : 0
},
operations: this.operations(),
evaluated_operations: this.evaluated_operations()
}
}

Expand Down
12 changes: 9 additions & 3 deletions tools/src/tester/types/test.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
* compatible open source license.
*/

import { Operation } from "./eval.types"

export interface SpecTestCoverage {
total_operations_count: number
evaluated_operations_count: number,
evaluated_paths_pct: number
summary: {
total_operations_count: number
evaluated_operations_count: number,
evaluated_paths_pct: number
},
operations: Operation[],
evaluated_operations: Operation[]
}
19 changes: 16 additions & 3 deletions tools/tests/tester/TestResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,22 @@ describe('TestResults', () => {
const filename = 'coverage.json'
test_results.write_coverage(filename)
expect(JSON.parse(fs.readFileSync(filename, 'utf8'))).toEqual({
evaluated_operations_count: 1,
evaluated_paths_pct: 16.67,
total_operations_count: 6
summary: {
evaluated_operations_count: 1,
evaluated_paths_pct: 16.67,
total_operations_count: 6
},
evaluated_operations: [
{ method: 'PUT', path: '/{index}' },
],
operations: [
{ method: 'GET', path: '/_nodes/{id}' },
{ method: 'POST', path: '/_nodes/{id}' },
{ method: 'GET', path: '/cluster_manager' },
{ method: 'POST', path: '/cluster_manager' },
{ method: 'GET', path: '/index' },
{ method: 'GET', path: '/nodes' }
]
})
fs.unlinkSync(filename)
})
Expand Down

0 comments on commit b3f9788

Please sign in to comment.