@babel/helper-transform-fixture-test-runner
Advanced tools
Comparing version
314
lib/index.js
@@ -6,5 +6,9 @@ "use strict"; | ||
}); | ||
exports.buildParallelProcessTests = buildParallelProcessTests; | ||
exports.buildProcessTests = buildProcessTests; | ||
exports.createTestContext = createTestContext; | ||
exports.default = _default; | ||
exports.runCodeInTestContext = runCodeInTestContext; | ||
var babel = require("@babel/core"); | ||
var _core = require("@babel/core"); | ||
var babel = _core; | ||
var _helperFixtures = require("@babel/helper-fixtures"); | ||
@@ -15,3 +19,3 @@ var _codeFrame = require("@babel/code-frame"); | ||
var _assert = require("assert"); | ||
var _fs = require("fs"); | ||
var _fs = _interopRequireWildcard(require("fs"), true); | ||
var _path = require("path"); | ||
@@ -22,6 +26,14 @@ var _vm = require("vm"); | ||
var _jestDiff = require("jest-diff"); | ||
var _child_process = require("child_process"); | ||
var _os = require("os"); | ||
var _makeDir = require("make-dir"); | ||
var _fsReaddirRecursive = require("fs-readdir-recursive"); | ||
var _module = require("module"); | ||
var _helperCheckDuplicateNodes = require("@babel/helper-check-duplicate-nodes"); | ||
var _crypto = require("crypto"); | ||
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } | ||
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } | ||
const dirname = _path.dirname(__filename); | ||
{ | ||
@@ -46,3 +58,3 @@ if (!_assert.rejects) { | ||
const contextModuleCache = new WeakMap(); | ||
const sharedTestContext = createContext(); | ||
const sharedTestContext = createTestContext(); | ||
function transformWithoutConfigFile(code, opts) { | ||
@@ -60,3 +72,3 @@ return babel.transformSync(code, Object.assign({ | ||
} | ||
function createContext() { | ||
function createTestContext() { | ||
const context = _vm.createContext(Object.assign({}, helpers, { | ||
@@ -73,3 +85,3 @@ process: process, | ||
contextModuleCache.set(context, moduleCache); | ||
runCacheableScriptInTestContext(_path.join(_path.dirname(__filename), "babel-helpers-in-memory.js"), babel.buildExternalHelpers, context, moduleCache); | ||
runCacheableScriptInTestContext(_path.join(_path.dirname(__filename), "babel-helpers-in-memory.js"), _core.buildExternalHelpers, context, moduleCache); | ||
return context; | ||
@@ -123,3 +135,3 @@ } | ||
if (moduleCache[filename]) return moduleCache[filename].exports; | ||
return runCacheableScriptInTestContext(filename, () => _fs.readFileSync(filename, "utf8"), context, moduleCache).exports; | ||
return runCacheableScriptInTestContext(filename, () => _fs.default.readFileSync(filename, "utf8"), context, moduleCache).exports; | ||
} | ||
@@ -216,3 +228,3 @@ function runCodeInTestContext(code, opts, context = sharedTestContext) { | ||
if (execCode) { | ||
const context = createContext(); | ||
const context = createTestContext(); | ||
const execOpts = getOpts(exec); | ||
@@ -242,9 +254,9 @@ ({ | ||
if (!ignoreOutput) { | ||
if (!expected.code && outputCode && !opts.throws && _fs.statSync(_path.dirname(expected.loc)).isDirectory() && !process.env.CI) { | ||
if (!expected.code && outputCode && !opts.throws && _fs.default.statSync(_path.dirname(expected.loc)).isDirectory() && !process.env.CI) { | ||
const expectedFile = expected.loc.replace(/\.m?js$/, result.sourceType === "module" ? ".mjs" : ".js"); | ||
console.log(`New test file created: ${expectedFile}`); | ||
_fs.writeFileSync(expectedFile, `${outputCode}\n`); | ||
_fs.default.writeFileSync(expectedFile, `${outputCode}\n`); | ||
if (expected.loc !== expectedFile) { | ||
try { | ||
_fs.unlinkSync(expected.loc); | ||
_fs.default.unlinkSync(expected.loc); | ||
} catch (e) {} | ||
@@ -276,3 +288,3 @@ } | ||
console.log(`Updated test file: ${task.sourceMapVisual.loc}`); | ||
_fs.writeFileSync((_task$sourceMapVisual = task.sourceMapVisual.loc) != null ? _task$sourceMapVisual : task.taskDir + "/source-map-visual.txt", visual + "\n"); | ||
_fs.default.writeFileSync((_task$sourceMapVisual = task.sourceMapVisual.loc) != null ? _task$sourceMapVisual : task.taskDir + "/source-map-visual.txt", visual + "\n"); | ||
} | ||
@@ -286,3 +298,3 @@ } | ||
console.log(`Updated test file: ${task.sourceMapFile.loc}`); | ||
_fs.writeFileSync(task.sourceMapFile.loc, JSON.stringify(result.map, null, 2)); | ||
_fs.default.writeFileSync(task.sourceMapFile.loc, JSON.stringify(result.map, null, 2)); | ||
} | ||
@@ -300,3 +312,3 @@ } | ||
console.log(`Updated test file: ${expectedLoc}`); | ||
_fs.writeFileSync(expectedLoc, `${actualCode}\n`); | ||
_fs.default.writeFileSync(expectedLoc, `${actualCode}\n`); | ||
return; | ||
@@ -380,3 +392,279 @@ } | ||
} | ||
const nodeGte8 = parseInt(process.versions.node, 10) >= 8; | ||
const tmpDir = (0, _fs.realpathSync)(_os.tmpdir()); | ||
const readDir = function (loc, filter) { | ||
const files = {}; | ||
if (_fs.default.existsSync(loc)) { | ||
_fsReaddirRecursive(loc, filter).forEach(function (filename) { | ||
files[filename] = (0, _helperFixtures.readFile)(_path.join(loc, filename)); | ||
}); | ||
} | ||
return files; | ||
}; | ||
const outputFileSync = function (filePath, data) { | ||
(0, _makeDir.sync)(_path.dirname(filePath)); | ||
_fs.default.writeFileSync(filePath, data); | ||
}; | ||
function deleteDir(path) { | ||
if (_fs.default.existsSync(path)) { | ||
_fs.default.readdirSync(path).forEach(function (file) { | ||
const curPath = path + "/" + file; | ||
if (_fs.default.lstatSync(curPath).isDirectory()) { | ||
deleteDir(curPath); | ||
} else { | ||
_fs.default.unlinkSync(curPath); | ||
} | ||
}); | ||
_fs.default.rmdirSync(path); | ||
} | ||
} | ||
const fileFilter = function (x) { | ||
return x !== ".DS_Store"; | ||
}; | ||
const assertTest = function (stdout, stderr, ipcMessage, opts, tmpDir) { | ||
const expectStderr = opts.stderr.trim(); | ||
stderr = stderr.trim(); | ||
try { | ||
if (opts.stderr) { | ||
if (opts.stderrContains) { | ||
expect(stderr).toContain(expectStderr); | ||
} else { | ||
expect(stderr).toBe(expectStderr); | ||
} | ||
} else if (stderr) { | ||
throw new Error("stderr:\n" + stderr); | ||
} | ||
} catch (e) { | ||
if (!process.env.OVERWRITE) throw e; | ||
console.log(`Updated test file: ${opts.stderrPath}`); | ||
outputFileSync(opts.stderrPath, stderr + "\n"); | ||
} | ||
const expectStdout = opts.stdout.trim(); | ||
stdout = stdout.trim(); | ||
stdout = stdout.replace(/\\/g, "/"); | ||
try { | ||
if (opts.stdout) { | ||
if (opts.stdoutContains) { | ||
expect(stdout).toContain(expectStdout); | ||
} else { | ||
expect(stdout).toBe(expectStdout); | ||
} | ||
} else if (stdout) { | ||
throw new Error("stdout:\n" + stdout); | ||
} | ||
} catch (e) { | ||
console.log(JSON.stringify(opts.stdout), JSON.stringify(stdout)); | ||
if (!process.env.OVERWRITE) throw e; | ||
console.log(`Updated test file: ${opts.stdoutPath}`); | ||
outputFileSync(opts.stdoutPath, stdout + "\n"); | ||
} | ||
if (opts.ipc) { | ||
expect(ipcMessage).toEqual(opts.ipcMessage); | ||
} | ||
if (opts.outFiles) { | ||
const actualFiles = readDir(tmpDir, fileFilter); | ||
Object.keys(actualFiles).forEach(function (filename) { | ||
try { | ||
if (filename !== ".babelrc" && filename !== ".babelignore" && !Object.prototype.hasOwnProperty.call(opts.inFiles, filename)) { | ||
const expected = opts.outFiles[filename]; | ||
const actual = actualFiles[filename]; | ||
expect(actual).toBe(expected || ""); | ||
} | ||
} catch (e) { | ||
if (!process.env.OVERWRITE) { | ||
e.message += "\n at " + filename; | ||
throw e; | ||
} | ||
const expectedLoc = _path.join(opts.testLoc, "out-files", filename); | ||
console.log(`Updated test file: ${expectedLoc}`); | ||
outputFileSync(expectedLoc, actualFiles[filename]); | ||
} | ||
}); | ||
Object.keys(opts.outFiles).forEach(function (filename) { | ||
expect(actualFiles).toHaveProperty([filename]); | ||
}); | ||
} | ||
}; | ||
function buildParallelProcessTests(name, tests) { | ||
return function (curr, total) { | ||
const sliceLength = Math.ceil(tests.length / total); | ||
const sliceStart = curr * sliceLength; | ||
const sliceEnd = sliceStart + sliceLength; | ||
const testsSlice = tests.slice(sliceStart, sliceEnd); | ||
describe(`${name} [${curr}/${total}]`, function () { | ||
it("dummy", () => {}); | ||
for (const test of testsSlice) { | ||
(test.skip ? it.skip : it)(test.suiteName + " " + test.testName, test.fn); | ||
} | ||
}); | ||
}; | ||
} | ||
function buildProcessTests(dir, beforeHook, afterHook) { | ||
const tests = []; | ||
_fs.default.readdirSync(dir).forEach(function (suiteName) { | ||
if (suiteName.startsWith(".") || suiteName === "package.json") return; | ||
const suiteLoc = _path.join(dir, suiteName); | ||
_fs.default.readdirSync(suiteLoc).forEach(function (testName) { | ||
if (testName.startsWith(".")) return; | ||
const testLoc = _path.join(suiteLoc, testName); | ||
let opts = { | ||
args: [] | ||
}; | ||
const optionsLoc = _path.join(testLoc, "options.json"); | ||
if (_fs.default.existsSync(optionsLoc)) { | ||
const taskOpts = JSON.parse((0, _fs.readFileSync)(optionsLoc, "utf8")); | ||
if (taskOpts.os) { | ||
let os = taskOpts.os; | ||
if (!Array.isArray(os) && typeof os !== "string") { | ||
throw new Error(`'os' should be either string or string array: ${taskOpts.os}`); | ||
} | ||
if (typeof os === "string") { | ||
os = [os]; | ||
} | ||
if (!os.includes(process.platform)) { | ||
return; | ||
} | ||
delete taskOpts.os; | ||
} | ||
opts = Object.assign({ | ||
args: [] | ||
}, taskOpts); | ||
} | ||
const executorLoc = _path.join(testLoc, "executor.js"); | ||
if (_fs.default.existsSync(executorLoc)) { | ||
opts.executor = executorLoc; | ||
} | ||
opts.stderrPath = _path.join(testLoc, "stderr.txt"); | ||
opts.stdoutPath = _path.join(testLoc, "stdout.txt"); | ||
for (const key of ["stdout", "stdin", "stderr"]) { | ||
const loc = _path.join(testLoc, key + ".txt"); | ||
if (_fs.default.existsSync(loc)) { | ||
opts[key] = (0, _helperFixtures.readFile)(loc); | ||
} else { | ||
opts[key] = opts[key] || ""; | ||
} | ||
} | ||
opts.testLoc = testLoc; | ||
opts.outFiles = readDir(_path.join(testLoc, "out-files"), fileFilter); | ||
opts.inFiles = readDir(_path.join(testLoc, "in-files"), fileFilter); | ||
const babelrcLoc = _path.join(testLoc, ".babelrc"); | ||
const babelIgnoreLoc = _path.join(testLoc, ".babelignore"); | ||
if (_fs.default.existsSync(babelrcLoc)) { | ||
opts.inFiles[".babelrc"] = (0, _helperFixtures.readFile)(babelrcLoc); | ||
} else if (!opts.noBabelrc) { | ||
opts.inFiles[".babelrc"] = "{}"; | ||
} | ||
if (_fs.default.existsSync(babelIgnoreLoc)) { | ||
opts.inFiles[".babelignore"] = (0, _helperFixtures.readFile)(babelIgnoreLoc); | ||
} | ||
const skip = opts.minNodeVersion && parseInt(process.versions.node, 10) < opts.minNodeVersion || opts.flaky && !process.env.BABEL_CLI_FLAKY_TESTS || opts.BABEL_8_BREAKING === false; | ||
if (opts.flaky) { | ||
testName += " (flaky)"; | ||
} | ||
const test = { | ||
suiteName, | ||
testName, | ||
skip, | ||
opts, | ||
fn: function (callback) { | ||
const tmpLoc = _path.join(tmpDir, "babel-process-test", (0, _crypto.createHash)("sha1").update(testLoc).digest("hex")); | ||
deleteDir(tmpLoc); | ||
(0, _makeDir.sync)(tmpLoc); | ||
const { | ||
inFiles | ||
} = opts; | ||
for (const filename of Object.keys(inFiles)) { | ||
outputFileSync(_path.join(tmpLoc, filename), inFiles[filename]); | ||
} | ||
try { | ||
beforeHook(test, tmpLoc); | ||
if (test.binLoc === undefined) { | ||
throw new Error("test.binLoc is undefined"); | ||
} | ||
let args = opts.executor && nodeGte8 ? ["--require", _path.join(dirname, "./exit-loader.cjs"), test.binLoc] : [test.binLoc]; | ||
args = args.concat(opts.args); | ||
const env = Object.assign({}, process.env, { | ||
FORCE_COLOR: "false" | ||
}, opts.env); | ||
const child = (0, _child_process.spawn)(process.execPath, args, { | ||
env, | ||
cwd: tmpLoc, | ||
stdio: opts.executor && nodeGte8 || opts.ipc ? ["pipe", "pipe", "pipe", "ipc"] : "pipe" | ||
}); | ||
let stderr = ""; | ||
let stdout = ""; | ||
let ipcMessage; | ||
child.on("close", function () { | ||
let err; | ||
try { | ||
const result = afterHook ? afterHook(test, tmpLoc, stdout, stderr) : { | ||
stdout, | ||
stderr | ||
}; | ||
assertTest(result.stdout, result.stderr, ipcMessage, opts, tmpLoc); | ||
} catch (e) { | ||
err = e; | ||
} finally { | ||
try { | ||
deleteDir(tmpLoc); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
if (err) { | ||
err.message = args.map(arg => `"${arg}"`).join(" ") + ": " + err.message; | ||
} | ||
callback(err); | ||
}); | ||
if (opts.ipc) { | ||
child.on("message", function (message) { | ||
ipcMessage = message; | ||
}); | ||
} | ||
if (opts.stdin) { | ||
child.stdin.write(opts.stdin); | ||
child.stdin.end(); | ||
} | ||
const captureOutput = proc => { | ||
proc.stderr.on("data", function (chunk) { | ||
stderr += chunk; | ||
}); | ||
proc.stdout.on("data", function (chunk) { | ||
stdout += chunk; | ||
}); | ||
}; | ||
if (opts.executor) { | ||
const executor = (0, _child_process.spawn)(process.execPath, [opts.executor], { | ||
cwd: tmpLoc | ||
}); | ||
child.stdout.pipe(executor.stdin); | ||
child.stderr.pipe(executor.stdin); | ||
executor.on("close", function () { | ||
if (nodeGte8) { | ||
child.send("exit"); | ||
} else { | ||
child.kill("SIGKILL"); | ||
} | ||
}); | ||
captureOutput(executor); | ||
} else { | ||
captureOutput(child); | ||
} | ||
} catch (e) { | ||
deleteDir(tmpLoc); | ||
throw e; | ||
} | ||
} | ||
}; | ||
tests.push(test); | ||
}); | ||
}); | ||
tests.sort(function (testA, testB) { | ||
const nameA = testA.suiteName + "/" + testA.testName; | ||
const nameB = testB.suiteName + "/" + testB.testName; | ||
return nameA.localeCompare(nameB); | ||
}); | ||
return tests; | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@babel/helper-transform-fixture-test-runner", | ||
"version": "7.22.19", | ||
"version": "7.23.9", | ||
"description": "Transform test runner for @babel/helper-fixtures module", | ||
@@ -18,10 +18,15 @@ "author": "The Babel Team (https://babel.dev/team)", | ||
"dependencies": { | ||
"@babel/code-frame": "^7.22.13", | ||
"@babel/core": "^7.22.19", | ||
"@babel/code-frame": "^7.23.5", | ||
"@babel/core": "^7.23.9", | ||
"@babel/helper-check-duplicate-nodes": "^7.22.5", | ||
"@babel/helper-fixtures": "^7.22.19", | ||
"@babel/helper-fixtures": "^7.23.4", | ||
"@jridgewell/trace-mapping": "^0.3.17", | ||
"fs-readdir-recursive": "^1.1.0", | ||
"jest-diff": "^29.6.4", | ||
"lru-cache": "^5.1.1" | ||
"lru-cache": "^5.1.1", | ||
"make-dir": "^2.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/fs-readdir-recursive": "^1.1.0" | ||
}, | ||
"engines": { | ||
@@ -28,0 +33,0 @@ "node": ">=6.9.0" |
Sorry, the diff of this file is not supported yet
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
106373
56.12%13
18.18%804
57.03%9
28.57%1
Infinity%15
66.67%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
Updated
Updated