diff --git a/src/setup_node_env/node_version_validator.js b/src/setup_node_env/node_version_validator.js index 4218eeb9e1ea..504d0970ecf6 100644 --- a/src/setup_node_env/node_version_validator.js +++ b/src/setup_node_env/node_version_validator.js @@ -35,16 +35,18 @@ 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 currentVersionMajorMinorPatch = currentVersion.match(/^v(\d+)\.(\d+)\.(\d+)/); +var requiredVersionMajorMinorPatch = requiredVersion.match(/^v(\d+)\.(\d+)\.(\d+)/); +var isVersionValid = + currentVersionMajorMinorPatch[1] === requiredVersionMajorMinorPatch[1] && + currentVersionMajorMinorPatch[2] === requiredVersionMajorMinorPatch[2] && + parseInt(currentVersionMajorMinorPatch[3], 10) >= parseInt(requiredVersionMajorMinorPatch[3], 10); // 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 + - '.'; + `OpenSearch Dashboards was built with ${requiredVersion} and does not support the current Node.js version ${currentVersion}. ` + + `Please use Node.js ${requiredVersion} or a higher patch version.`; // Actions to apply when validation fails: error report + exit. console.error(errorMessage); diff --git a/src/setup_node_env/node_version_validator.test.js b/src/setup_node_env/node_version_validator.test.js index fc22dce39949..4e95f7219d5a 100644 --- a/src/setup_node_env/node_version_validator.test.js +++ b/src/setup_node_env/node_version_validator.test.js @@ -32,38 +32,64 @@ var exec = require('child_process').exec; var pkg = require('../../package.json'); var REQUIRED_NODE_JS_VERSION = 'v' + pkg.engines.node; -var INVALID_NODE_JS_VERSION = 'v0.10.0'; describe('NodeVersionValidator', function () { - it('should run the script WITH error', function (done) { - var processVersionOverwrite = - "Object.defineProperty(process, 'version', { value: '" + - INVALID_NODE_JS_VERSION + - "', writable: true });"; - var command = - 'node -e "' + processVersionOverwrite + "require('./node_version_validator.js')\""; - - exec(command, { cwd: __dirname }, function (error, stdout, stderr) { - expect(error.code).toBe(1); - expect(stderr).toBeDefined(); - expect(stderr).not.toHaveLength(0); - done(); - }); + it('should run the script WITHOUT error when the version is the same', function (done) { + testValidateNodeVersion(done, REQUIRED_NODE_JS_VERSION); + }); + + it('should run the script WITHOUT error when only the patch version is higher', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, 0, +1)); + }); + + it('should run the script WITH error if the patch version is lower', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, 0, -1), true); + }); + + it('should run the script WITH error if the major version is higher', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(+1, 0, 0), true); + }); + + it('should run the script WITH error if the major version is lower', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(-1, 0, 0), true); + }); + + it('should run the script WITH error if the minor version is higher', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, +1, 0), true); + }); + + it('should run the script WITH error if the minor version is lower', function (done) { + testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, -1, 0), true); }); +}); + +function requiredNodeVersionWithDiff(majorDiff, minorDiff, patchDiff) { + var matches = REQUIRED_NODE_JS_VERSION.match(/^v(\d+)\.(\d+)\.(\d+)/); + var major = parseInt(matches[1]) + majorDiff; + var minor = parseInt(matches[2]) + minorDiff; + var patch = parseInt(matches[3]) + patchDiff; - it('should run the script WITHOUT error', function (done) { - var processVersionOverwrite = - "Object.defineProperty(process, 'version', { value: '" + - REQUIRED_NODE_JS_VERSION + - "', writable: true });"; - var command = - 'node -e "' + processVersionOverwrite + "require('./node_version_validator.js')\""; + return `v${major}.${minor}.${patch}`; +} - exec(command, { cwd: __dirname }, function (error, stdout, stderr) { +function testValidateNodeVersion(done, versionToTest, expectError = false) { + var processVersionOverwrite = `Object.defineProperty(process, 'version', { value: '${versionToTest}', writable: true });`; + var command = `node -e "${processVersionOverwrite}require('./node_version_validator.js')"`; + + exec(command, { cwd: __dirname }, function (error, _stdout, stderr) { + expect(stderr).toBeDefined(); + if (expectError) { + expect(error.code).toBe(1); + + var speficicErrorMessage = + `OpenSearch Dashboards was built with ${REQUIRED_NODE_JS_VERSION} and does not support the current Node.js version ${versionToTest}. ` + + `Please use Node.js ${REQUIRED_NODE_JS_VERSION} or a higher patch version.\n`; + + expect(stderr).toStrictEqual(speficicErrorMessage); + } else { expect(error).toBeNull(); - expect(stderr).toBeDefined(); expect(stderr).toHaveLength(0); - done(); - }); + } + done(); }); -}); +}