Skip to content

Commit

Permalink
Prevent primitive linting limitations from being applied to unit test…
Browse files Browse the repository at this point in the history
…s found under `src/setup_node_env` (#3403)

Signed-off-by: Miki <[email protected]>
  • Loading branch information
AMoo-Miki authored Feb 9, 2023
1 parent d9dc91d commit 0b537f0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
26 changes: 13 additions & 13 deletions src/setup_node_env/node_version_validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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`;

Expand Down
4 changes: 2 additions & 2 deletions src/setup_node_env/root/force.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* under the License.
*/

var forceRoot = require('./force');
const forceRoot = require('./force');

describe('forceRoot', function () {
it('with flag', function () {
Expand All @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion src/setup_node_env/root/is_root.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 0b537f0

Please sign in to comment.