From b3f9788280608b1bf7af794d97880ffa477b4ab8 Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Thu, 29 Aug 2024 23:01:08 -0400 Subject: [PATCH] Fix: correctly combine test coverage. (#541) Signed-off-by: dblock --- .github/workflows/test-spec.yml | 15 ++++++++------- tools/src/tester/TestResults.ts | 24 ++++++++++++++---------- tools/src/tester/types/test.types.ts | 12 +++++++++--- tools/tests/tester/TestResults.test.ts | 19 ++++++++++++++++--- 4 files changed, 47 insertions(+), 23 deletions(-) diff --git a/.github/workflows/test-spec.yml b/.github/workflows/test-spec.yml index a9b0963b..b4745a4e 100644 --- a/.github/workflows/test-spec.yml +++ b/.github/workflows/test-spec.yml @@ -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 diff --git a/tools/src/tester/TestResults.ts b/tools/src/tester/TestResults.ts index 792699e2..864ea05d 100644 --- a/tools/src/tester/TestResults.ts +++ b/tools/src/tester/TestResults.ts @@ -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"; @@ -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 } @@ -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() } } diff --git a/tools/src/tester/types/test.types.ts b/tools/src/tester/types/test.types.ts index 174f207c..f98c93e9 100644 --- a/tools/src/tester/types/test.types.ts +++ b/tools/src/tester/types/test.types.ts @@ -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[] } diff --git a/tools/tests/tester/TestResults.test.ts b/tools/tests/tester/TestResults.test.ts index d4abe847..4ca6aac5 100644 --- a/tools/tests/tester/TestResults.test.ts +++ b/tools/tests/tester/TestResults.test.ts @@ -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) })