Skip to content

Improving error handling #51953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 62 additions & 59 deletions tools/test-npm-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,73 +37,75 @@ function spawnCopyDeepSync(source, destination) {
}

function runNPMPackageTests({ srcDir, install, rebuild, testArgs, logfile }) {
// Make sure we don't conflict with concurrent test runs
const srcHash = createHash('md5').update(srcDir).digest('hex');
tmpDir.path = `${tmpDir.path}.npm.${srcHash}`;
tmpDir.refresh();

const npmCache = path.join(tmpDir.path, 'npm-cache');
const npmPrefix = path.join(tmpDir.path, 'npm-prefix');
const npmTmp = path.join(tmpDir.path, 'npm-tmp');
const npmUserconfig = path.join(tmpDir.path, 'npm-userconfig');
const pkgDir = path.join(tmpDir.path, 'pkg');

spawnCopyDeepSync(srcDir, pkgDir);

const npmOptions = {
cwd: pkgDir,
env: Object.assign({}, process.env, {
'npm_config_cache': npmCache,
'npm_config_prefix': npmPrefix,
'npm_config_tmp': npmTmp,
'npm_config_userconfig': npmUserconfig,
}),
stdio: 'inherit',
};
try {
// Make sure we don't conflict with concurrent test runs
const srcHash = createHash('md5').update(srcDir).digest('hex');
tmpDir.path = `${tmpDir.path}.npm.${srcHash}`;
tmpDir.refresh();

if (common.isWindows) {
npmOptions.env.home = tmpDir.path;
npmOptions.env.Path = `${nodePath};${process.env.Path}`;
} else {
npmOptions.env.HOME = tmpDir.path;
npmOptions.env.PATH = `${nodePath}:${process.env.PATH}`;
}
const npmCache = path.join(tmpDir.path, 'npm-cache');
const npmPrefix = path.join(tmpDir.path, 'npm-prefix');
const npmTmp = path.join(tmpDir.path, 'npm-tmp');
const npmUserconfig = path.join(tmpDir.path, 'npm-userconfig');
const pkgDir = path.join(tmpDir.path, 'pkg');

spawnCopyDeepSync(srcDir, pkgDir);

const npmOptions = {
cwd: pkgDir,
env: Object.assign({}, process.env, {
'npm_config_cache': npmCache,
'npm_config_prefix': npmPrefix,
'npm_config_tmp': npmTmp,
'npm_config_userconfig': npmUserconfig,
}),
stdio: 'inherit',
};

if (common.isWindows) {
npmOptions.env.home = tmpDir.path;
npmOptions.env.Path = `${nodePath};${process.env.Path}`;
} else {
npmOptions.env.HOME = tmpDir.path;
npmOptions.env.PATH = `${nodePath}:${process.env.PATH}`;
}

if (rebuild) {
spawnSync(process.execPath, [
npmBin,
'rebuild',
], npmOptions);
}
if (rebuild) {
handleNpmError(spawnSync(process.execPath, [npmBin, 'rebuild'], npmOptions));
}

if (install) {
spawnSync(process.execPath, [
npmBin,
'install',
'--ignore-scripts',
'--no-save',
], npmOptions);
}
if (install) {
handleNpmError(spawnSync(process.execPath, [npmBin, 'install', '--ignore-scripts', '--no-save'], npmOptions));
}

const testChild = spawn(process.execPath, [npmBin, '--silent', 'run', ...testArgs], Object.assign({}, npmOptions, { stdio: 'pipe' }));

const testChild = spawn(process.execPath, [
npmBin,
'--silent',
'run',
...testArgs,
], Object.assign({}, npmOptions, { stdio: 'pipe' }));
testChild.stdout.pipe(process.stdout);
testChild.stderr.pipe(process.stderr);

testChild.stdout.pipe(process.stdout);
testChild.stderr.pipe(process.stderr);
if (logfile) {
const logStream = createWriteStream(logfile);
testChild.stdout.pipe(logStream);
}

if (logfile) {
const logStream = createWriteStream(logfile);
testChild.stdout.pipe(logStream);
testChild.on('exit', () => {
tmpDir.refresh();
rmdirSync(tmpDir.path);
});
} catch (error) {
console.error(`An unexpected error occurred: ${error.message}`);
process.exitCode = 1;
}
}

testChild.on('exit', () => {
tmpDir.refresh();
rmdirSync(tmpDir.path);
});
function handleNpmError(result) {
if (result.error) {
console.error(`Error executing npm command: ${result.error.message}`);
process.exitCode = 1;
} else if (result.status !== 0) {
console.error(`npm command failed with status code ${result.status}`);
process.exitCode = result.status;
}
}

function parseArgs(args) {
Expand Down Expand Up @@ -137,3 +139,4 @@ function parseArgs(args) {
}

runNPMPackageTests(parseArgs(process.argv.slice(2)));