Skip to content

Commit 79f5d89

Browse files
committed
sandboxjs.py: checks for valid version of node/nodejs
- raises runtime exception if install node version is below a threshold version. - Fixes #254
1 parent 7c4ad54 commit 79f5d89

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

cwltool/sandboxjs.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ class JavascriptException(Exception):
2323
have_node_slim = False
2424

2525

26+
def check_js_threshold_version(working_alias):
27+
# type: (str) -> bool
28+
""" parse node version: 'v4.2.6\n' -> ['4', '2', '6'],
29+
https://github.com/nodejs/node/blob/master/CHANGELOG.md#nodejs-changelog """
30+
31+
v1, v2, v3 = [int(v) for v in subprocess.check_output(
32+
[working_alias, "-v"]).decode('ascii').strip().strip('v').split('.')]
33+
34+
if v1 == 0:
35+
if v2 == 10 and v3 <= 25 or v2 < 10:
36+
return False
37+
return True
38+
2639
def new_js_proc():
2740
# type: () -> subprocess.Popen
2841

@@ -31,9 +44,11 @@ def new_js_proc():
3144

3245
nodejs = None
3346
trynodes = ("nodejs", "node")
47+
working_alias = None
3448
for n in trynodes:
3549
try:
3650
if subprocess.check_output([n, "--eval", "process.stdout.write('t')"]) != "t":
51+
working_alias = n
3752
continue
3853
nodejs = subprocess.Popen([n, "--eval", nodecode],
3954
stdin=subprocess.PIPE,
@@ -48,6 +63,14 @@ def new_js_proc():
4863
else:
4964
raise
5065

66+
""" check nodejs version, if it is below certain threshold version,
67+
raise Runtime Exception. Such a test won't be required for docker nodejs"""
68+
if nodejs is not None:
69+
if check_js_threshold_version(working_alias) is False:
70+
raise JavascriptException(u"cwltool requires updated version of Node.js engine. "
71+
"Try updating: https://docs.npmjs.com/getting-started/installing-node")
72+
73+
5174
if nodejs is None:
5275
try:
5376
nodeimg = "node:slim"

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
'extensions.yml']},
4747
include_package_data=True,
4848
install_requires=[
49+
'mock >= 2.0.0',
4950
'setuptools',
5051
'requests >= 1.0',
5152
'ruamel.yaml >= 0.12.4',
@@ -54,8 +55,6 @@
5455
'schema-salad >= 2.4.20170308171942, < 3',
5556
'typing >= 3.5.2, < 3.6',
5657
'six >= 1.10.0',
57-
'mock >= 2.0.0',
58-
5958
],
6059
setup_requires=[] + pytest_runner,
6160
test_suite='tests',

tests/test_js_sandbox.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import unittest
2+
from mock import Mock
3+
4+
# we should modify the subprocess imported from cwltool.sandboxjs
5+
from cwltool.sandboxjs import check_js_threshold_version, subprocess
6+
7+
class Javascript_Sanity_Checks(unittest.TestCase):
8+
9+
def test_node_version(self):
10+
subprocess.check_output = Mock(return_value=b'v0.8.26\n')
11+
self.assertEquals(check_js_threshold_version('node'), False)
12+
13+
subprocess.check_output = Mock(return_value=b'v0.10.25\n')
14+
self.assertEquals(check_js_threshold_version('node'), False)
15+
16+
subprocess.check_output = Mock(return_value=b'v0.10.26\n')
17+
self.assertEquals(check_js_threshold_version('node'), True)
18+
19+
subprocess.check_output = Mock(return_value=b'v4.4.2\n')
20+
self.assertEquals(check_js_threshold_version('node'), True)
21+
22+
subprocess.check_output = Mock(return_value=b'v7.7.3\n')
23+
self.assertEquals(check_js_threshold_version('node'), True)
24+
25+
def test_is_javascript_installed(self):
26+
pass

0 commit comments

Comments
 (0)