|
| 1 | +'use strict'; |
| 2 | +const { |
| 3 | + ArrayFrom, |
| 4 | + ArrayPrototypeFilter, |
| 5 | + ArrayPrototypeIncludes, |
| 6 | + ArrayPrototypePush, |
| 7 | + ArrayPrototypeSlice, |
| 8 | + ArrayPrototypeSort, |
| 9 | + Promise, |
| 10 | + SafeSet, |
| 11 | +} = primordials; |
| 12 | +const { |
| 13 | + prepareMainThreadExecution, |
| 14 | +} = require('internal/bootstrap/pre_execution'); |
| 15 | +const { spawn } = require('child_process'); |
| 16 | +const { readdirSync, statSync } = require('fs'); |
| 17 | +const console = require('internal/console/global'); |
| 18 | +const { |
| 19 | + codes: { |
| 20 | + ERR_TEST_FAILURE, |
| 21 | + }, |
| 22 | +} = require('internal/errors'); |
| 23 | +const test = require('internal/test_runner/harness'); |
| 24 | +const { kSubtestsFailed } = require('internal/test_runner/test'); |
| 25 | +const { |
| 26 | + isSupportedFileType, |
| 27 | + doesPathMatchFilter, |
| 28 | +} = require('internal/test_runner/utils'); |
| 29 | +const { basename, join, resolve } = require('path'); |
| 30 | +const kFilterArgs = ['--test']; |
| 31 | + |
| 32 | +prepareMainThreadExecution(false); |
| 33 | +markBootstrapComplete(); |
| 34 | + |
| 35 | +// TODO(cjihrig): Replace this with recursive readdir once it lands. |
| 36 | +function processPath(path, testFiles, options) { |
| 37 | + const stats = statSync(path); |
| 38 | + |
| 39 | + if (stats.isFile()) { |
| 40 | + if (options.userSupplied || |
| 41 | + (options.underTestDir && isSupportedFileType(path)) || |
| 42 | + doesPathMatchFilter(path)) { |
| 43 | + testFiles.add(path); |
| 44 | + } |
| 45 | + } else if (stats.isDirectory()) { |
| 46 | + const name = basename(path); |
| 47 | + |
| 48 | + if (!options.userSupplied && name === 'node_modules') { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // 'test' directories get special treatment. Recursively add all .js, |
| 53 | + // .cjs, and .mjs files in the 'test' directory. |
| 54 | + const isTestDir = name === 'test'; |
| 55 | + const { underTestDir } = options; |
| 56 | + const entries = readdirSync(path); |
| 57 | + |
| 58 | + if (isTestDir) { |
| 59 | + options.underTestDir = true; |
| 60 | + } |
| 61 | + |
| 62 | + options.userSupplied = false; |
| 63 | + |
| 64 | + for (let i = 0; i < entries.length; i++) { |
| 65 | + processPath(join(path, entries[i]), testFiles, options); |
| 66 | + } |
| 67 | + |
| 68 | + options.underTestDir = underTestDir; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +function createTestFileList() { |
| 73 | + const cwd = process.cwd(); |
| 74 | + const hasUserSuppliedPaths = process.argv.length > 1; |
| 75 | + const testPaths = hasUserSuppliedPaths ? |
| 76 | + ArrayPrototypeSlice(process.argv, 1) : [cwd]; |
| 77 | + const testFiles = new SafeSet(); |
| 78 | + |
| 79 | + try { |
| 80 | + for (let i = 0; i < testPaths.length; i++) { |
| 81 | + const absolutePath = resolve(testPaths[i]); |
| 82 | + |
| 83 | + processPath(absolutePath, testFiles, { userSupplied: true }); |
| 84 | + } |
| 85 | + } catch (err) { |
| 86 | + if (err?.code === 'ENOENT') { |
| 87 | + console.error(`Could not find '${err.path}'`); |
| 88 | + process.exit(1); |
| 89 | + } |
| 90 | + |
| 91 | + throw err; |
| 92 | + } |
| 93 | + |
| 94 | + return ArrayPrototypeSort(ArrayFrom(testFiles)); |
| 95 | +} |
| 96 | + |
| 97 | +function filterExecArgv(arg) { |
| 98 | + return !ArrayPrototypeIncludes(kFilterArgs, arg); |
| 99 | +} |
| 100 | + |
| 101 | +function runTestFile(path) { |
| 102 | + return test(path, () => { |
| 103 | + return new Promise((resolve, reject) => { |
| 104 | + const args = ArrayPrototypeFilter(process.execArgv, filterExecArgv); |
| 105 | + ArrayPrototypePush(args, path); |
| 106 | + |
| 107 | + const child = spawn(process.execPath, args); |
| 108 | + // TODO(cjihrig): Implement a TAP parser to read the child's stdout |
| 109 | + // instead of just displaying it all if the child fails. |
| 110 | + let stdout = ''; |
| 111 | + let stderr = ''; |
| 112 | + let err; |
| 113 | + |
| 114 | + child.on('error', (error) => { |
| 115 | + err = error; |
| 116 | + }); |
| 117 | + |
| 118 | + child.stdout.setEncoding('utf8'); |
| 119 | + child.stderr.setEncoding('utf8'); |
| 120 | + |
| 121 | + child.stdout.on('data', (chunk) => { |
| 122 | + stdout += chunk; |
| 123 | + }); |
| 124 | + |
| 125 | + child.stderr.on('data', (chunk) => { |
| 126 | + stderr += chunk; |
| 127 | + }); |
| 128 | + |
| 129 | + child.once('exit', (code, signal) => { |
| 130 | + if (code !== 0 || signal !== null) { |
| 131 | + if (!err) { |
| 132 | + err = new ERR_TEST_FAILURE('test failed', kSubtestsFailed); |
| 133 | + err.exitCode = code; |
| 134 | + err.signal = signal; |
| 135 | + err.stdout = stdout; |
| 136 | + err.stderr = stderr; |
| 137 | + // The stack will not be useful since the failures came from tests |
| 138 | + // in a child process. |
| 139 | + err.stack = undefined; |
| 140 | + } |
| 141 | + |
| 142 | + return reject(err); |
| 143 | + } |
| 144 | + |
| 145 | + resolve(); |
| 146 | + }); |
| 147 | + }); |
| 148 | + }); |
| 149 | +} |
| 150 | + |
| 151 | +(async function main() { |
| 152 | + const testFiles = createTestFileList(); |
| 153 | + |
| 154 | + for (let i = 0; i < testFiles.length; i++) { |
| 155 | + runTestFile(testFiles[i]); |
| 156 | + } |
| 157 | +})(); |
0 commit comments