From 51ff4337b988d1f1178f7e24c4715521779ea30c Mon Sep 17 00:00:00 2001 From: Miki Date: Thu, 9 Feb 2023 12:56:53 -0800 Subject: [PATCH] Prevent primitive linting limitations from being applied to unit tests found under `src/setup_node_env` (#3403) Signed-off-by: Miki Signed-off-by: Arpit Bandejiya --- .eslintrc.js | 2 +- CHANGELOG.md | 1 + .../node_version_validator.test.js | 26 +++++++++---------- src/setup_node_env/root/force.test.js | 4 +-- src/setup_node_env/root/is_root.test.js | 2 +- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 2381a3071913..90e789d38e58 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -459,7 +459,7 @@ module.exports = { * Files that run BEFORE node version check */ { - files: ['scripts/**/*.js', 'src/setup_node_env/**/*.js'], + files: ['scripts/**/*.js', 'src/setup_node_env/**/!(*.test).js'], rules: { 'import/no-commonjs': 'off', 'prefer-object-spread/prefer-object-spread': 'off', diff --git a/CHANGELOG.md b/CHANGELOG.md index d659c28afce2..f17b9cfa2af2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -142,6 +142,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Tests] Bumps `chromedriver` to v107 ([#3017](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3017)) - [Vis Builder] Adds field unit tests ([#3211](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3211)) - [BWC Tests] Add BWC tests for 2.6.0 ([#3356](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3356)) +- Prevent primitive linting limitations from being applied to unit tests found under `src/setup_node_env` ([#3403](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3403)) ## [2.x] diff --git a/src/setup_node_env/node_version_validator.test.js b/src/setup_node_env/node_version_validator.test.js index cb3639154c6c..92c596548207 100644 --- a/src/setup_node_env/node_version_validator.test.js +++ b/src/setup_node_env/node_version_validator.test.js @@ -28,10 +28,10 @@ * under the License. */ -var exec = require('child_process').exec; -var pkg = require('../../package.json'); +const exec = require('child_process').exec; +const pkg = require('../../package.json'); -var REQUIRED_NODE_JS_VERSION = 'v' + pkg.engines.node; +const REQUIRED_NODE_JS_VERSION = 'v' + pkg.engines.node; describe('NodeVersionValidator', function () { it('should run the script WITHOUT error when the version is the same', function (done) { @@ -43,7 +43,7 @@ describe('NodeVersionValidator', function () { }); it('should run the script WITH error if the patch version is lower', function (done) { - var lowerPatchversion = requiredNodeVersionWithDiff(0, 0, -1); + const lowerPatchversion = requiredNodeVersionWithDiff(0, 0, -1); testValidateNodeVersion( done, lowerPatchversion, @@ -56,7 +56,7 @@ describe('NodeVersionValidator', function () { }); it('should run the script WITH error if the major version is lower', function (done) { - var lowerMajorVersion = requiredNodeVersionWithDiff(-1, 0, 0); + const lowerMajorVersion = requiredNodeVersionWithDiff(-1, 0, 0); testValidateNodeVersion( done, lowerMajorVersion, @@ -69,7 +69,7 @@ describe('NodeVersionValidator', function () { }); it('should run the script WITH error if the minor version is lower', function (done) { - var lowerMinorVersion = requiredNodeVersionWithDiff(0, -1, 0); + const lowerMinorVersion = requiredNodeVersionWithDiff(0, -1, 0); testValidateNodeVersion( done, lowerMinorVersion, @@ -79,24 +79,24 @@ describe('NodeVersionValidator', function () { }); function requiredNodeVersionWithDiff(majorDiff, minorDiff, patchDiff) { - var matches = REQUIRED_NODE_JS_VERSION.match(/^v(\d+)\.(\d+)\.(\d+)/); - var major = Math.max(parseInt(matches[1], 10) + majorDiff, 0); - var minor = Math.max(parseInt(matches[2], 10) + minorDiff, 0); - var patch = Math.max(parseInt(matches[3], 10) + patchDiff, 0); + const matches = REQUIRED_NODE_JS_VERSION.match(/^v(\d+)\.(\d+)\.(\d+)/); + const major = Math.max(parseInt(matches[1], 10) + majorDiff, 0); + const minor = Math.max(parseInt(matches[2], 10) + minorDiff, 0); + const patch = Math.max(parseInt(matches[3], 10) + patchDiff, 0); return `v${major}.${minor}.${patch}`; } 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')"`; + const processVersionOverwrite = `Object.defineProperty(process, 'version', { value: '${versionToTest}', writable: true });`; + const 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 = + const 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`; diff --git a/src/setup_node_env/root/force.test.js b/src/setup_node_env/root/force.test.js index f48f3255e4ed..eca20f9547b2 100644 --- a/src/setup_node_env/root/force.test.js +++ b/src/setup_node_env/root/force.test.js @@ -28,7 +28,7 @@ * under the License. */ -var forceRoot = require('./force'); +const forceRoot = require('./force'); describe('forceRoot', function () { it('with flag', function () { @@ -40,7 +40,7 @@ describe('forceRoot', function () { }); test('remove argument', function () { - var args = ['--allow-root', 'foo']; + const args = ['--allow-root', 'foo']; forceRoot(args); expect(args.includes('--allow-root')).toBeFalsy(); }); diff --git a/src/setup_node_env/root/is_root.test.js b/src/setup_node_env/root/is_root.test.js index 81dfbe3616cb..e7f8e0670b77 100644 --- a/src/setup_node_env/root/is_root.test.js +++ b/src/setup_node_env/root/is_root.test.js @@ -28,7 +28,7 @@ * under the License. */ -var isRoot = require('./is_root'); +const isRoot = require('./is_root'); describe('isRoot', function () { test('0 is root', function () {