|
| 1 | +const cp = require('child_process') |
| 2 | +const fs = require('fs') |
| 3 | +const path = require('path') |
| 4 | +const rimraf = require('rimraf') |
| 5 | + |
| 6 | +const SIMPLE_MAIN_RE = /^main\.\d+\.m?js$/ |
| 7 | +const MAIN_RE = /^main\.(.+)\.m?js$/ |
| 8 | +const MAIN_ALL = 'main.all.js' |
| 9 | + |
| 10 | +/** |
| 11 | + * Generates fixtures |
| 12 | + * |
| 13 | + * Each fixture is a directory with a `lib.js` file and a few `main.X.js` |
| 14 | + * files where `X` is an integer. |
| 15 | + * For each main file, it will generate a `cov.X.json` file containing coverage |
| 16 | + * for `lib.mjs` when using the corresponding main as the entry point. |
| 17 | + * It also generates a `cov.all.json` obtained by executing (importing) all |
| 18 | + * the entry points. |
| 19 | + * |
| 20 | + * Merging all the `cov.X.json` coverages should result in `cov.all.json`. |
| 21 | + */ |
| 22 | +async function main () { |
| 23 | + for (const fixtureName of getFixtures()) { |
| 24 | + await generateFixture(fixtureName) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function getFixtures () { |
| 29 | + return fs.readdirSync(__dirname) |
| 30 | + .filter((child) => fs.statSync(path.join(__dirname, child)).isDirectory()) |
| 31 | +} |
| 32 | + |
| 33 | +async function generateFixture (name) { |
| 34 | + const dir = path.join(__dirname, name) |
| 35 | + const simpleMains = getSimpleMains(dir) |
| 36 | + const mainAll = generateMainAll(simpleMains) |
| 37 | + await fs.promises.writeFile(path.join(dir, MAIN_ALL), mainAll) |
| 38 | + for (const simpleMain of [...simpleMains, MAIN_ALL]) { |
| 39 | + const id = MAIN_RE.exec(simpleMain)[1] |
| 40 | + const libCoverage = await getLibCoverage(dir, simpleMain) |
| 41 | + await fs.promises.writeFile(path.join(dir, `cov.${id}.json`), JSON.stringify(libCoverage, null, 2)) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function getSimpleMains (dir) { |
| 46 | + return fs.readdirSync(dir) |
| 47 | + .filter((child) => SIMPLE_MAIN_RE.test(child)) |
| 48 | + .sort((a, b) => { |
| 49 | + a = parseInt(MAIN_RE.exec(a)[1]) |
| 50 | + b = parseInt(MAIN_RE.exec(b)[1]) |
| 51 | + return a - b |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +function generateMainAll (mains) { |
| 56 | + return [ |
| 57 | + '// DO NOT EDIT: generated by `generate-fixtures.js`', |
| 58 | + 'const lib = require.resolve(\'./lib.js\')', |
| 59 | + 'function main () {', |
| 60 | + ...mains.map(main => ` require(${JSON.stringify(`./${main}`)})\n delete require.cache[lib]`), |
| 61 | + '}', |
| 62 | + 'main()', |
| 63 | + '', |
| 64 | + ].join('\n') |
| 65 | +} |
| 66 | + |
| 67 | +async function getLibCoverage (dir, main) { |
| 68 | + const covDirName = `cov-${main}` |
| 69 | + const covDir = path.join(dir, covDirName) |
| 70 | + await rmDir(covDir) |
| 71 | + await spawnWithCoverage(dir, main, covDir) |
| 72 | + const coverages = await readCoverage(covDir) |
| 73 | + await rmDir(covDir) |
| 74 | + const urlEnd = path.join('test', 'fixtures', path.basename(dir), 'lib.js') |
| 75 | + for (const coverage of coverages) { |
| 76 | + if (coverage.url.endsWith(urlEnd)) { |
| 77 | + return { |
| 78 | + scriptId: '1', |
| 79 | + url: path.posix.join('/', 'test', 'fixtures', path.basename(dir), 'lib.js'), |
| 80 | + functions: coverage.functions |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + throw new Error(`Coverage not found for \`lib.js\` for ${path.join(dir, main)}`) |
| 85 | +} |
| 86 | + |
| 87 | +async function spawnWithCoverage (dir, main, covDir) { |
| 88 | + return new Promise((resolve, reject) => { |
| 89 | + const proc = cp.spawn( |
| 90 | + process.execPath, |
| 91 | + [main], |
| 92 | + { |
| 93 | + cwd: dir, |
| 94 | + env: {NODE_V8_COVERAGE: covDir}, |
| 95 | + }, |
| 96 | + ) |
| 97 | + proc.once('close', (code, signal) => { |
| 98 | + if (code === 0) { |
| 99 | + resolve() |
| 100 | + } else { |
| 101 | + reject(new Error(`Non-zero return code for: ${path.join(dir, main)}`)) |
| 102 | + } |
| 103 | + }) |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +async function rmDir (dir) { |
| 108 | + return new Promise((resolve, reject) => { |
| 109 | + rimraf(dir, (err) => { |
| 110 | + if (err !== null) { |
| 111 | + reject(err) |
| 112 | + } else { |
| 113 | + resolve() |
| 114 | + } |
| 115 | + }) |
| 116 | + }) |
| 117 | +} |
| 118 | + |
| 119 | +async function readCoverage (covDir) { |
| 120 | + const covChildren = fs.readdirSync(covDir) |
| 121 | + if (covChildren.length !== 1) { |
| 122 | + throw new Error(`Expected a single file in: ${covDir}`) |
| 123 | + } |
| 124 | + return JSON.parse(await fs.promises.readFile(path.join(covDir, covChildren[0]), {encoding: 'UTF-8'})).result |
| 125 | +} |
| 126 | + |
| 127 | +main() |
0 commit comments