forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_version_validator.test.js
More file actions
118 lines (100 loc) · 4.42 KB
/
node_version_validator.test.js
File metadata and controls
118 lines (100 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var exec = require('child_process').exec;
var pkg = require('../../package.json');
var 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) {
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) {
var lowerPatchversion = requiredNodeVersionWithDiff(0, 0, -1);
testValidateNodeVersion(
done,
lowerPatchversion,
REQUIRED_NODE_JS_VERSION !== lowerPatchversion
);
});
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) {
var lowerMajorVersion = requiredNodeVersionWithDiff(-1, 0, 0);
testValidateNodeVersion(
done,
lowerMajorVersion,
REQUIRED_NODE_JS_VERSION !== lowerMajorVersion
);
});
it('should run the script WITH warning message if the minor version is higher', function (done) {
testValidateNodeVersion(done, requiredNodeVersionWithDiff(0, +1, 0), false, true);
});
it('should run the script WITH error if the minor version is lower', function (done) {
var lowerMinorVersion = requiredNodeVersionWithDiff(0, -1, 0);
testValidateNodeVersion(
done,
lowerMinorVersion,
REQUIRED_NODE_JS_VERSION !== lowerMinorVersion
);
});
});
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);
return `v${major}.${minor}.${patch}`;
}
function testValidateNodeVersion(done, versionToTest, expectError = false, expectWarning = 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 if (expectWarning) {
expect(error).toBeNull();
var speficicWarningMessage =
`You're using an untested version of Node.js. OpenSearch Dashboards has been tested to work with Node.js ` +
`${REQUIRED_NODE_JS_VERSION}. Your current version is ${versionToTest}.\n`;
expect(stderr).toStrictEqual(speficicWarningMessage);
} else {
expect(error).toBeNull();
expect(stderr).toHaveLength(0);
}
done();
});
}