Skip to content

Commit

Permalink
Use template literals in node version validator
Browse files Browse the repository at this point in the history
  • Loading branch information
hashworks committed Apr 14, 2022
1 parent 3e94742 commit d2b7bd4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 33 deletions.
9 changes: 2 additions & 7 deletions src/setup_node_env/node_version_validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,8 @@ var isVersionValid = requiredVersionMajorMinor === currentVersion.match(/^v(\d+\
// Validates current the NodeJS version compatibility when OpenSearch Dashboards starts.
if (!isVersionValid) {
var errorMessage =
'OpenSearch Dashboards was built with ' +
requiredVersion +
' and does not support the current Node.js version ' +
currentVersion +
'. Please use Node.js ~v' +
requiredVersionMajorMinor +
'.';
`OpenSearch Dashboards was built with ${requiredVersion} and does not support the current Node.js version ${currentVersion}. ` +
`Please use Node.js ~v'${requiredVersionMajorMinor}' or higher.`;

// Actions to apply when validation fails: error report + exit.
console.error(errorMessage);
Expand Down
35 changes: 9 additions & 26 deletions src/setup_node_env/node_version_validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@ 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')\"";
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) {
exec(command, { cwd: __dirname }, function (error, _stdout, stderr) {
expect(error.code).toBe(1);
expect(stderr).toBeDefined();
expect(stderr).not.toHaveLength(0);
Expand All @@ -54,14 +50,10 @@ describe('NodeVersionValidator', function () {
});

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')\"";
var processVersionOverwrite = `Object.defineProperty(process, 'version', { value: '${REQUIRED_NODE_JS_VERSION}', writable: true });`;
var command = `node -e "${processVersionOverwrite}require('./node_version_validator.js')"`;

exec(command, { cwd: __dirname }, function (error, stdout, stderr) {
exec(command, { cwd: __dirname }, function (error, _stdout, stderr) {
expect(error).toBeNull();
expect(stderr).toBeDefined();
expect(stderr).toHaveLength(0);
Expand All @@ -75,19 +67,10 @@ describe('NodeVersionValidator', function () {
var minor = matches[2];
var 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')\"";
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) {
exec(command, { cwd: __dirname }, function (error, _stdout, stderr) {
expect(error).toBeNull();
expect(stderr).toBeDefined();
expect(stderr).toHaveLength(0);
Expand Down

0 comments on commit d2b7bd4

Please sign in to comment.