diff --git a/src/setup_node_env/node_version_validator.js b/src/setup_node_env/node_version_validator.js index b646d786dcfe..5d61a71979d0 100644 --- a/src/setup_node_env/node_version_validator.js +++ b/src/setup_node_env/node_version_validator.js @@ -37,15 +37,16 @@ var pkg = require('../../package.json'); var currentVersion = (process && process.version) || null; var rawRequiredVersion = (pkg && pkg.engines && pkg.engines.node) || null; var requiredVersion = rawRequiredVersion ? 'v' + rawRequiredVersion : rawRequiredVersion; -var isVersionValid = !!currentVersion && !!requiredVersion && currentVersion === requiredVersion; +var requiredVersionMajorMinor = requiredVersion.match(/^v(\d+\.\d+)/)[1] +var isVersionValid = requiredVersionMajorMinor === currentVersion.match(/^v(\d+\.\d+)/)[1] // Validates current the NodeJS version compatibility when OpenSearch Dashboards starts. if (!isVersionValid) { var errorMessage = 'OpenSearch Dashboards does not support the current Node.js version ' + currentVersion + - '. Please use Node.js ' + - requiredVersion + + '. Please use Node.js ~v' + + requiredVersionMajorMinor + '.'; // Actions to apply when validation fails: error report + exit. diff --git a/src/setup_node_env/node_version_validator.test.js b/src/setup_node_env/node_version_validator.test.js index 6266a7432ffd..c8b2c62dc371 100644 --- a/src/setup_node_env/node_version_validator.test.js +++ b/src/setup_node_env/node_version_validator.test.js @@ -68,4 +68,25 @@ describe('NodeVersionValidator', function () { done(); }); }); + + it('should run the script WITHOUT error', function (done) { + var matches = REQUIRED_NODE_JS_VERSION.match(/^v(\d+)\.(\d+)\.(\d+)/) + let major = matches[1] + let minor = matches[2] + let patch = parseInt(matches[3]) + 1 // change patch version to be higher than required + + var processVersionOverwrite = + "Object.defineProperty(process, 'version', { value: '" + + "v" + major + "." + minor + "." + patch + + "', writable: true });"; + var command = + 'node -e "' + processVersionOverwrite + "require('./node_version_validator.js')\""; + + exec(command, { cwd: __dirname }, function (error, stdout, stderr) { + expect(error).toBeNull(); + expect(stderr).toBeDefined(); + expect(stderr).toHaveLength(0); + done(); + }); + }); });