diff --git a/tools/test-npm-package.js b/tools/test-npm-package.js index 392d9feaa1938e..ecaf17ad3cf627 100755 --- a/tools/test-npm-package.js +++ b/tools/test-npm-package.js @@ -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) { @@ -137,3 +139,4 @@ function parseArgs(args) { } runNPMPackageTests(parseArgs(process.argv.slice(2))); +