mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: use yargs for mocha runner (#11045)
This commit is contained in:
parent
03b22ab456
commit
30bd030078
@ -18,11 +18,13 @@
|
|||||||
|
|
||||||
import {randomUUID} from 'crypto';
|
import {randomUUID} from 'crypto';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import {spawn, type SpawnOptions} from 'node:child_process';
|
import {spawn} from 'node:child_process';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
|
||||||
import {globSync} from 'glob';
|
import {globSync} from 'glob';
|
||||||
|
import yargs from 'yargs';
|
||||||
|
import {hideBin} from 'yargs/helpers';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
zPlatform,
|
zPlatform,
|
||||||
@ -44,17 +46,63 @@ import {
|
|||||||
type RecommendedExpectation,
|
type RecommendedExpectation,
|
||||||
} from './utils.js';
|
} from './utils.js';
|
||||||
|
|
||||||
|
const {
|
||||||
|
_: mochaArgs,
|
||||||
|
testSuite: testSuiteId,
|
||||||
|
saveStatsTo,
|
||||||
|
cdpTests: includeCdpTests,
|
||||||
|
suggestions: provideSuggestions,
|
||||||
|
coverage: useCoverage,
|
||||||
|
minTests,
|
||||||
|
shard,
|
||||||
|
reporter,
|
||||||
|
} = yargs(hideBin(process.argv))
|
||||||
|
.parserConfiguration({'unknown-options-as-args': true})
|
||||||
|
.scriptName('@puppeteer/mocha-runner')
|
||||||
|
.option('coverage', {
|
||||||
|
boolean: true,
|
||||||
|
default: true,
|
||||||
|
})
|
||||||
|
.option('suggestions', {
|
||||||
|
boolean: true,
|
||||||
|
default: true,
|
||||||
|
})
|
||||||
|
.option('cdp-tests', {
|
||||||
|
boolean: true,
|
||||||
|
default: true,
|
||||||
|
})
|
||||||
|
.option('save-stats-to', {
|
||||||
|
string: true,
|
||||||
|
requiresArg: true,
|
||||||
|
})
|
||||||
|
.option('min-tests', {
|
||||||
|
number: true,
|
||||||
|
default: 0,
|
||||||
|
requiresArg: true,
|
||||||
|
})
|
||||||
|
.option('test-suite', {
|
||||||
|
string: true,
|
||||||
|
requiresArg: true,
|
||||||
|
})
|
||||||
|
.option('shard', {
|
||||||
|
string: true,
|
||||||
|
requiresArg: true,
|
||||||
|
})
|
||||||
|
.option('reporter', {
|
||||||
|
string: true,
|
||||||
|
requiresArg: true,
|
||||||
|
})
|
||||||
|
.parseSync();
|
||||||
|
|
||||||
function getApplicableTestSuites(
|
function getApplicableTestSuites(
|
||||||
parsedSuitesFile: TestSuiteFile,
|
parsedSuitesFile: TestSuiteFile,
|
||||||
platform: Platform
|
platform: Platform
|
||||||
): TestSuite[] {
|
): TestSuite[] {
|
||||||
const testSuiteArgIdx = process.argv.indexOf('--test-suite');
|
|
||||||
let applicableSuites: TestSuite[] = [];
|
let applicableSuites: TestSuite[] = [];
|
||||||
|
|
||||||
if (testSuiteArgIdx === -1) {
|
if (!testSuiteId) {
|
||||||
applicableSuites = filterByPlatform(parsedSuitesFile.testSuites, platform);
|
applicableSuites = filterByPlatform(parsedSuitesFile.testSuites, platform);
|
||||||
} else {
|
} else {
|
||||||
const testSuiteId = process.argv[testSuiteArgIdx + 1];
|
|
||||||
const testSuite = parsedSuitesFile.testSuites.find(suite => {
|
const testSuite = parsedSuitesFile.testSuites.find(suite => {
|
||||||
return suite.id === testSuiteId;
|
return suite.id === testSuiteId;
|
||||||
});
|
});
|
||||||
@ -77,29 +125,9 @@ function getApplicableTestSuites(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const noCoverage = process.argv.indexOf('--no-coverage') !== -1;
|
let statsPath = saveStatsTo;
|
||||||
const noSuggestions = process.argv.indexOf('--no-suggestions') !== -1;
|
if (statsPath && statsPath.includes('INSERTID')) {
|
||||||
const excludeCDPOnly = process.argv.indexOf('--no-cdp-tests') !== -1;
|
statsPath = statsPath.replace(/INSERTID/gi, randomUUID());
|
||||||
|
|
||||||
const statsFilenameIdx = process.argv.indexOf('--save-stats-to');
|
|
||||||
let statsFilename = '';
|
|
||||||
if (statsFilenameIdx !== -1) {
|
|
||||||
statsFilename = process.argv[statsFilenameIdx + 1] as string;
|
|
||||||
if (statsFilename.includes('INSERTID')) {
|
|
||||||
statsFilename = statsFilename.replace(/INSERTID/gi, randomUUID());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const minTestsIdx = process.argv.indexOf('--min-tests');
|
|
||||||
let minTests = 0;
|
|
||||||
if (minTestsIdx !== -1) {
|
|
||||||
minTests = Number(process.argv[minTestsIdx + 1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const shardIdx = process.argv.indexOf('--shard');
|
|
||||||
let shard = null;
|
|
||||||
if (shardIdx !== -1) {
|
|
||||||
shard = String(process.argv[shardIdx + 1]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const platform = zPlatform.parse(os.platform());
|
const platform = zPlatform.parse(os.platform());
|
||||||
@ -115,8 +143,8 @@ async function main() {
|
|||||||
const applicableSuites = getApplicableTestSuites(parsedSuitesFile, platform);
|
const applicableSuites = getApplicableTestSuites(parsedSuitesFile, platform);
|
||||||
|
|
||||||
console.log('Planning to run the following test suites', applicableSuites);
|
console.log('Planning to run the following test suites', applicableSuites);
|
||||||
if (statsFilename) {
|
if (statsPath) {
|
||||||
console.log('Test stats will be saved to', statsFilename);
|
console.log('Test stats will be saved to', statsPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
let fail = false;
|
let fail = false;
|
||||||
@ -164,39 +192,22 @@ async function main() {
|
|||||||
const tmpDir = fs.mkdtempSync(
|
const tmpDir = fs.mkdtempSync(
|
||||||
path.join(os.tmpdir(), 'puppeteer-test-runner-')
|
path.join(os.tmpdir(), 'puppeteer-test-runner-')
|
||||||
);
|
);
|
||||||
const tmpFilename = statsFilename
|
const tmpFilename = statsPath
|
||||||
? statsFilename
|
? statsPath
|
||||||
: path.join(tmpDir, 'output.json');
|
: path.join(tmpDir, 'output.json');
|
||||||
console.log('Running', JSON.stringify(parameters), tmpFilename);
|
console.log('Running', JSON.stringify(parameters), tmpFilename);
|
||||||
const reporterArgumentIndex = process.argv.indexOf('--reporter');
|
|
||||||
const args = [
|
const args = [
|
||||||
'-u',
|
'-u',
|
||||||
path.join(__dirname, 'interface.js'),
|
path.join(__dirname, 'interface.js'),
|
||||||
'-R',
|
'-R',
|
||||||
reporterArgumentIndex === -1
|
!reporter ? path.join(__dirname, 'reporter.js') : reporter,
|
||||||
? path.join(__dirname, 'reporter.js')
|
|
||||||
: process.argv[reporterArgumentIndex + 1] || '',
|
|
||||||
'-O',
|
'-O',
|
||||||
'output=' + tmpFilename,
|
`output=${tmpFilename}`,
|
||||||
];
|
];
|
||||||
const retriesArgumentIndex = process.argv.indexOf('--retries');
|
|
||||||
const timeoutArgumentIndex = process.argv.indexOf('--timeout');
|
|
||||||
if (retriesArgumentIndex > -1) {
|
|
||||||
args.push('--retries', process.argv[retriesArgumentIndex + 1] || '');
|
|
||||||
}
|
|
||||||
if (timeoutArgumentIndex > -1) {
|
|
||||||
args.push('--timeout', process.argv[timeoutArgumentIndex + 1] || '');
|
|
||||||
}
|
|
||||||
if (process.argv.indexOf('--no-parallel')) {
|
|
||||||
args.push('--no-parallel');
|
|
||||||
}
|
|
||||||
if (process.argv.indexOf('--fullTrace')) {
|
|
||||||
args.push('--full-trace');
|
|
||||||
}
|
|
||||||
|
|
||||||
const specPattern = 'test/build/**/*.spec.js';
|
const specPattern = 'test/build/**/*.spec.js';
|
||||||
const specs = globSync(specPattern, {
|
const specs = globSync(specPattern, {
|
||||||
ignore: excludeCDPOnly ? 'test/build/cdp/**/*.spec.js' : undefined,
|
ignore: !includeCdpTests ? 'test/build/cdp/**/*.spec.js' : undefined,
|
||||||
}).sort((a, b) => {
|
}).sort((a, b) => {
|
||||||
return a.localeCompare(b);
|
return a.localeCompare(b);
|
||||||
});
|
});
|
||||||
@ -222,26 +233,29 @@ async function main() {
|
|||||||
} else {
|
} else {
|
||||||
args.push(...specs);
|
args.push(...specs);
|
||||||
}
|
}
|
||||||
const spawnArgs: SpawnOptions = {
|
const handle = spawn(
|
||||||
shell: true,
|
'npx',
|
||||||
cwd: process.cwd(),
|
[
|
||||||
stdio: 'inherit',
|
...(useCoverage
|
||||||
env,
|
? [
|
||||||
};
|
'c8',
|
||||||
const handle = noCoverage
|
'--check-coverage',
|
||||||
? spawn('npx', ['mocha', ...args], spawnArgs)
|
'--lines',
|
||||||
: spawn(
|
String(suite.expectedLineCoverage),
|
||||||
'npx',
|
'npx',
|
||||||
[
|
]
|
||||||
'c8',
|
: []),
|
||||||
'--check-coverage',
|
'mocha',
|
||||||
'--lines',
|
...mochaArgs.map(String),
|
||||||
String(suite.expectedLineCoverage),
|
...args,
|
||||||
'npx mocha',
|
],
|
||||||
...args,
|
{
|
||||||
],
|
shell: true,
|
||||||
spawnArgs
|
cwd: process.cwd(),
|
||||||
);
|
stdio: 'inherit',
|
||||||
|
env,
|
||||||
|
}
|
||||||
|
);
|
||||||
await new Promise<void>((resolve, reject) => {
|
await new Promise<void>((resolve, reject) => {
|
||||||
handle.on('error', err => {
|
handle.on('error', err => {
|
||||||
reject(err);
|
reject(err);
|
||||||
@ -288,7 +302,7 @@ async function main() {
|
|||||||
fail = true;
|
fail = true;
|
||||||
console.error(err);
|
console.error(err);
|
||||||
} finally {
|
} finally {
|
||||||
if (!noSuggestions) {
|
if (!!provideSuggestions) {
|
||||||
printSuggestions(
|
printSuggestions(
|
||||||
recommendations,
|
recommendations,
|
||||||
'add',
|
'add',
|
||||||
|
Loading…
Reference in New Issue
Block a user