From 43db1ea010e15ef9b601fc486d4e1a85d1003e4b Mon Sep 17 00:00:00 2001 From: Prateek Maurya <83070760+PrateekMaurya2@users.noreply.github.com> Date: Mon, 4 Mar 2024 13:20:25 +0530 Subject: [PATCH] Improving error handling In this version, I've added a handleNpmError function to handle errors that may occur during npm install and npm rebuild. If an error occurs, it prints an error message and sets the process exit code to 1. Additionally, the main function (runNPMPackageTests) is wrapped in a try-catch block to handle unexpected errors and print an error message. --- tools/test-npm-package.js | 121 +++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 59 deletions(-) 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))); +