Add --coverage option to build scripts for tests (#54499)

This commit is contained in:
Ron Buckton
2023-06-02 16:00:47 -04:00
committed by GitHub
parent e8c7927641
commit 913e556373
7 changed files with 448 additions and 21 deletions

View File

@@ -4,7 +4,7 @@ import os from "os";
const ci = ["1", "true"].includes(process.env.CI ?? "");
const parsed = minimist(process.argv.slice(2), {
boolean: ["dirty", "light", "colors", "lkg", "soft", "fix", "failed", "keepFailed", "force", "built", "ci", "bundle", "typecheck", "lint"],
boolean: ["dirty", "light", "colors", "lkg", "soft", "fix", "failed", "keepFailed", "force", "built", "ci", "bundle", "typecheck", "lint", "coverage"],
string: ["browser", "tests", "break", "host", "reporter", "stackTraceLimit", "timeout", "shards", "shardId"],
alias: {
/* eslint-disable quote-props */
@@ -42,6 +42,7 @@ const parsed = minimist(process.argv.slice(2), {
bundle: true,
typecheck: true,
lint: true,
coverage: false,
}
});
@@ -88,5 +89,6 @@ export default options;
* @property {boolean} bundle
* @property {boolean} typecheck
* @property {boolean} lint
* @property {boolean} coverage
*/
void 0;

View File

@@ -7,13 +7,14 @@ import path from "path";
import { findUpFile, findUpRoot } from "./findUpDir.mjs";
import cmdLineOptions from "./options.mjs";
import { exec } from "./utils.mjs";
import { exec, ExecError } from "./utils.mjs";
const mochaJs = path.resolve(findUpRoot(), "node_modules", "mocha", "bin", "_mocha");
export const localBaseline = "tests/baselines/local/";
export const refBaseline = "tests/baselines/reference/";
export const localRwcBaseline = "internal/baselines/rwc/local";
export const refRwcBaseline = "internal/baselines/rwc/reference";
export const coverageDir = "coverage";
/**
* @param {string} runJs
@@ -35,11 +36,13 @@ export async function runConsoleTests(runJs, defaultReporter, runInParallel, opt
const keepFailed = cmdLineOptions.keepFailed;
const shards = +cmdLineOptions.shards || undefined;
const shardId = +cmdLineOptions.shardId || undefined;
const coverage = cmdLineOptions.coverage;
if (!cmdLineOptions.dirty) {
if (options.watching) {
console.log(chalk.yellowBright(`[watch] cleaning test directories...`));
}
await cleanTestDirs();
await cleanCoverageDir();
if (options.token?.signaled) {
return;
@@ -128,21 +131,28 @@ export async function runConsoleTests(runJs, defaultReporter, runInParallel, opt
/** @type {Error | undefined} */
let error;
const savedNodeEnv = process.env.NODE_ENV;
const savedNodeV8Coverage = process.env.NODE_V8_COVERAGE;
try {
setNodeEnvToDevelopment();
process.env.NODE_ENV = "development";
if (coverage) {
process.env.NODE_V8_COVERAGE = path.resolve(coverageDir, "tmp");
}
const { exitCode } = await exec(process.execPath, args, { token: options.token });
if (exitCode !== 0) {
errorStatus = exitCode;
error = new Error(`Process exited with status code ${errorStatus}.`);
await exec(process.execPath, args, { token: options.token });
if (coverage) {
await exec("npm", ["--prefer-offline", "exec", "--", "c8", "report"], { token: options.token });
}
}
catch (e) {
errorStatus = undefined;
errorStatus = e instanceof ExecError ? e.exitCode ?? undefined : undefined;
error = /** @type {Error} */ (e);
}
finally {
restoreSavedNodeEnv();
if (coverage) {
process.env.NODE_V8_COVERAGE = savedNodeV8Coverage;
}
process.env.NODE_ENV = savedNodeEnv;
}
await del("test.config");
@@ -169,6 +179,10 @@ export async function cleanTestDirs() {
await fs.promises.mkdir(localBaseline, { recursive: true });
}
async function cleanCoverageDir() {
await del([coverageDir]);
}
/**
* used to pass data from command line directly to run.js
* @param {string} tests
@@ -200,17 +214,6 @@ export function writeTestConfigFile(tests, runners, light, taskConfigsFolder, wo
fs.writeFileSync("test.config", testConfigContents);
}
/** @type {string | undefined} */
let savedNodeEnv;
function setNodeEnvToDevelopment() {
savedNodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = "development";
}
function restoreSavedNodeEnv() {
process.env.NODE_ENV = savedNodeEnv;
}
function deleteTemporaryProjectOutput() {
return del(path.join(localBaseline, "projectOutput/"));
}

View File

@@ -35,7 +35,7 @@ export async function exec(cmd, args, options = {}) {
}
else {
const reason = options.token?.signaled ? options.token.reason ?? new CancelError() :
new Error(`Process exited with code: ${exitCode}`);
new ExecError(exitCode);
reject(reason);
}
subscription?.unsubscribe();
@@ -53,6 +53,19 @@ export async function exec(cmd, args, options = {}) {
}));
}
export class ExecError extends Error {
exitCode;
/**
* @param {number | null} exitCode
* @param {string} message
*/
constructor(exitCode, message = `Process exited with code: ${exitCode}`) {
super(message);
this.exitCode = exitCode;
}
}
/**
* Reads JSON data with optional comments using the LKG TypeScript compiler
* @param {string} jsonPath