electron/script/node-spec-runner.js
Charles Kerr 8e548dbf87
chore: use oxfmt and oxlint in 41-x-y (#51443)
* build: replace eslint with oxlint and add oxfmt

Replace ESLint and its plugin ecosystem with oxlint (oxc.rs).
Add oxfmt alongside oxlint for JS/TS formatting and import sorting.

- Consolidate root .eslintrc.json plus 13 nested configs into .oxlintrc.json
- script/lint.js spawns oxlint binary directly instead of ESLint Node API
- Per-process no-restricted-imports rules preserved as oxlintrc overrides
- mocha/no-exclusive-tests replaced by in-repo plugin (no-only-tests.mjs)
- docs ESLint pass replaced by inline node: protocol check in lint.js
- .oxfmtrc.json matching repo style (single quotes, semicolons, 2-space)
- yarn lint:fmt (oxfmt --check) chained into yarn lint
- yarn format (oxfmt --write) for local fixup
- lint-staged runs oxfmt --write on staged JS/TS files

This commit contains only rule/tooling infrastructure changes and is
intended to be cherry-picked to other maintenance branches, where
formatting and lint fixes can be applied separately.

Manual backport of electron/electron@e1af67c698 (#50691) and
electron/electron@3c7fd34f47 (#50692).

* chore: apply oxfmt formatting and oxlint fixes

One-time application of the new linting and formatting rules to the
41-x-y codebase:

- yarn format (oxfmt --write) over all JS/TS sources
- oxlint --fix to restore curly braces where oxfmt removed them
- Prefix unused parameters with _ (no-unused-vars)
- Add eslint-disable-next-line comments for intentional patterns:
  - prefer-promise-reject-errors in desktop-capturer.ts
  - no-throw-literal in preload.ts
  - no-only-tests/no-only-tests in spec-helpers.ts
- Restore bare require() in preload-sandbox.js test assertions
  (oxfmt incorrectly converted bare specifiers to node: protocol,
  defeating the equivalence test)

Pure formatting and lint suppression; no behavioral changes.
2026-05-02 15:22:30 -07:00

125 lines
3.4 KiB
JavaScript

const minimist = require('minimist');
const cp = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const utils = require('./lib/utils');
const DISABLED_TESTS = require('./node-disabled-tests.json');
const args = minimist(process.argv.slice(2), {
boolean: ['default', 'validateDisabled'],
string: ['jUnitDir']
});
const BASE = path.resolve(__dirname, '../..');
const ROOT_PACKAGE_JSON = path.resolve(BASE, 'package.json');
const NODE_DIR = path.resolve(BASE, 'third_party', 'electron_node');
const JUNIT_DIR = args.jUnitDir ? path.resolve(args.jUnitDir) : null;
const TAP_FILE_NAME = 'test.tap';
if (!require.main) {
throw new Error('Must call the node spec runner directly');
}
const defaultOptions = [
'tools/test.py',
'-p',
'tap',
'--logfile',
TAP_FILE_NAME,
'--mode=debug',
'default',
`--skip-tests=${DISABLED_TESTS.join(',')}`,
'--flaky-tests=dontcare',
'--measure-flakiness=9',
'--shell',
utils.getAbsoluteElectronExec(),
'-J'
];
// The root package.json is ESM, which breaks the test runner.
// Temporarily change it to CommonJS while running the tests, then
// change it back when done.
const resetPackageJson = ({ useESM }) => {
// This won't always exist in CI.
if (!fs.existsSync(ROOT_PACKAGE_JSON)) {
return;
}
const packageJson = JSON.parse(fs.readFileSync(ROOT_PACKAGE_JSON, 'utf-8'));
packageJson.type = useESM ? 'module' : 'commonjs';
fs.writeFileSync(ROOT_PACKAGE_JSON, JSON.stringify(packageJson, null, 2) + '\n');
};
const getCustomOptions = () => {
let customOptions = ['tools/test.py'];
// Add all custom arguments.
const extra = process.argv.slice(2);
if (extra) {
customOptions = customOptions.concat(extra);
}
// Necessary or Node.js will try to run from out/Release/node.
customOptions = customOptions.concat(['--shell', utils.getAbsoluteElectronExec()]);
return customOptions;
};
async function main() {
// Optionally validate that all disabled specs still exist.
if (args.validateDisabled) {
const missing = [];
for (const test of DISABLED_TESTS) {
const js = path.join(NODE_DIR, 'test', `${test}.js`);
const mjs = path.join(NODE_DIR, 'test', `${test}.mjs`);
if (!fs.existsSync(js) && !fs.existsSync(mjs)) {
missing.push(test);
}
}
if (missing.length > 0) {
console.error(`Found ${missing.length} missing disabled specs: \n${missing.join('\n')}`);
process.exit(1);
}
console.log(`All ${DISABLED_TESTS.length} disabled specs exist.`);
process.exit(0);
}
const options = args.default ? defaultOptions : getCustomOptions();
resetPackageJson({ useESM: false });
const testChild = cp.spawn('python3', options, {
env: {
...process.env,
ELECTRON_RUN_AS_NODE: 'true',
ELECTRON_EAGER_ASAR_HOOK_FOR_TESTING: 'true'
},
cwd: NODE_DIR,
stdio: 'inherit'
});
testChild.on('exit', (testCode) => {
resetPackageJson({ useESM: true });
if (JUNIT_DIR) {
fs.mkdirSync(JUNIT_DIR);
const converterStream = require('tap-xunit')();
fs.createReadStream(path.resolve(NODE_DIR, TAP_FILE_NAME))
.pipe(converterStream)
.pipe(fs.createWriteStream(path.resolve(JUNIT_DIR, 'nodejs.xml')))
.on('close', () => {
process.exit(testCode);
});
}
});
}
main().catch((err) => {
console.error('An unhandled error occurred in the node spec runner', err);
process.exit(1);
});