Skip to content

Commit 42c8900

Browse files
authored
test(core): merge all package test reports (#6261)
## Problem - CI test reporting only captures the last package's results - Running `npm run testE2E` only preserves toolkit's report.xml, losing other package results ## Solution - Generate individual report.xml files per subproject - Consolidate all existing package reports into root .test-reports/report.xml - This is done in the buildspecs themselves rather than directly in the package.json (the original approach to solve this problem) because mac/linux and windows have two different ways of getting the last error code, resulting in a complicated package.json for something thats only needed in our codebuilds --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 7e788c5 commit 42c8900

File tree

6 files changed

+78
-5
lines changed

6 files changed

+78
-5
lines changed

buildspec/linuxE2ETests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ phases:
3737
commands:
3838
- export HOME=/home/codebuild-user
3939
# Ignore failure until throttling issues are fixed.
40-
- xvfb-run npm run testE2E
40+
- xvfb-run npm run testE2E; npm run mergeReports -- "$?"
4141
- VCS_COMMIT_ID="${CODEBUILD_RESOLVED_SOURCE_VERSION}"
4242
- CI_BUILD_URL=$(echo $CODEBUILD_BUILD_URL | sed 's/#/%23/g')
4343
- CI_BUILD_ID="${CODEBUILD_BUILD_ID}"

buildspec/linuxIntegrationTests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ phases:
9292
build:
9393
commands:
9494
- export HOME=/home/codebuild-user
95-
- xvfb-run npm run testInteg
95+
- xvfb-run npm run testInteg; npm run mergeReports -- "$?"
9696
- VCS_COMMIT_ID="${CODEBUILD_RESOLVED_SOURCE_VERSION}"
9797
- CI_BUILD_URL=$(echo $CODEBUILD_BUILD_URL | sed 's/#/%23/g')
9898
- CI_BUILD_ID="${CODEBUILD_BUILD_ID}"

buildspec/linuxTests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ phases:
4141
# Ensure that "foo | run_and_report" fails correctly.
4242
set -o pipefail
4343
. buildspec/shared/common.sh
44-
2>&1 xvfb-run npm test --silent | run_and_report 2 \
44+
{ 2>&1 xvfb-run npm test --silent; npm run mergeReports -- "$?"; } | run_and_report 2 \
4545
'rejected promise not handled' \
4646
'This typically indicates a bug. Read https://developer.mozilla.org/docs/Web/JavaScript/Guide/Using_promises#error_handling'
4747
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"lintfix": "eslint -c .eslintrc.js --ignore-path .gitignore --ignore-pattern '**/*.json' --ignore-pattern '**/*.gen.ts' --ignore-pattern '**/types/*.d.ts' --ignore-pattern '**/src/testFixtures/**' --ignore-pattern '**/resources/js/graphStateMachine.js' --fix --ext .ts packages plugins",
3737
"clean": "npm run clean -w packages/ -w plugins/",
3838
"reset": "npm run clean && ts-node ./scripts/clean.ts node_modules && npm install",
39-
"generateNonCodeFiles": "npm run generateNonCodeFiles -w packages/ --if-present"
39+
"generateNonCodeFiles": "npm run generateNonCodeFiles -w packages/ --if-present",
40+
"mergeReports": "ts-node ./scripts/mergeReports.ts"
4041
},
4142
"devDependencies": {
4243
"@aws-toolkits/telemetry": "^1.0.289",

packages/core/src/test/testRunner.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ export async function runTests(
6565
}
6666

6767
const root = getRoot()
68-
const outputFile = path.resolve(root, '../../', '.test-reports', 'report.xml')
68+
// output the report to the individual package
69+
const outputFile = path.resolve(root, '.test-reports', 'report.xml')
6970
const colorOutput = !process.env['AWS_TOOLKIT_TEST_NO_COLOR']
7071

7172
// Create the mocha test

scripts/mergeReports.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import * as fs from 'fs'
7+
import * as path from 'path'
8+
import * as xml2js from 'xml2js'
9+
10+
/**
11+
* Merge all of the packages/ test reports into a single directory
12+
*/
13+
async function mergeReports() {
14+
console.log('Merging test reports')
15+
16+
const packagesDir = `${__dirname}/../packages`
17+
18+
// Get all packages/* directories
19+
const packageDirs = fs.readdirSync(packagesDir).map((dir) => path.join(packagesDir, dir))
20+
21+
// Find report.xml files in .test-reports subdirectories
22+
const testReports = packageDirs
23+
.map((dir) => `${dir}/.test-reports/report.xml`)
24+
.filter((file) => fs.existsSync(file))
25+
26+
const mergedReport = {
27+
testsuites: {
28+
testsuite: [],
29+
},
30+
}
31+
32+
// Collect all test reports into a single merged test report object
33+
for (const file of testReports) {
34+
const content = fs.readFileSync(file)
35+
const result: { testsuites: { testsuite: [] } } = await xml2js.parseStringPromise(content)
36+
if (result.testsuites && result.testsuites.testsuite) {
37+
mergedReport.testsuites.testsuite.push(...result.testsuites.testsuite)
38+
}
39+
}
40+
41+
const builder = new xml2js.Builder()
42+
const xml = builder.buildObject(mergedReport)
43+
44+
/**
45+
* Create the new test reports directory and write the test report
46+
*/
47+
const reportsDir = path.join(__dirname, '..', '.test-reports')
48+
49+
// Create reports directory if it doesn't exist
50+
if (!fs.existsSync(reportsDir)) {
51+
fs.mkdirSync(reportsDir, { recursive: true })
52+
}
53+
54+
fs.writeFileSync(`${reportsDir}/report.xml`, xml)
55+
56+
const exitCodeArg = process.argv[2]
57+
if (exitCodeArg) {
58+
/**
59+
* Retrieves the exit code from the previous test run execution.
60+
*
61+
* This allows us to:
62+
* 1. Merge and upload test reports regardless of the test execution status
63+
* 2. Preserve the original test run exit code
64+
* 3. Report the test status back to CI
65+
*/
66+
const exitCode = parseInt(exitCodeArg || '0', 10)
67+
process.exit(exitCode)
68+
}
69+
}
70+
71+
mergeReports()

0 commit comments

Comments
 (0)