Skip to content

Add javascript code snippet validation #662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 47 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
01c79e7
Add validation for javascript expressions.
ThomasHickman Feb 15, 2018
5182ab6
Merge branch 'master' into js-linting
ThomasHickman Feb 15, 2018
f9bcbd0
Fix unfixed merge confict
ThomasHickman Feb 15, 2018
8543182
Rename jshint files add add to MANIFEST.in
ThomasHickman Feb 15, 2018
a320e10
Fix issues found in testing
ThomasHickman Feb 15, 2018
df6fe7d
Fix items in failing tests and add disable-js-validation and js-hint-…
ThomasHickman Mar 2, 2018
1eac465
Merge branch 'master' into js-linting
ThomasHickman Mar 2, 2018
c4a6f16
draft2tool -> command_line_tool in validate_js
ThomasHickman Mar 2, 2018
d4b5168
Move get_data from tests to cwltool
ThomasHickman Mar 5, 2018
ddf4291
Fix issues found in testing
ThomasHickman Mar 5, 2018
b847c81
Merge branch 'master' into js-linting
ThomasHickman Mar 5, 2018
4f76771
Revert "Move get_data from tests to cwltool" excluding utils.py
ThomasHickman Mar 5, 2018
397711d
Add get_test_data
ThomasHickman Mar 5, 2018
4f126db
Add folder checking and exception to get_data
ThomasHickman Mar 5, 2018
b774648
Corrected incorrect logic in get_data
ThomasHickman Mar 5, 2018
149933f
Add jshint wrapper file existrance checking
ThomasHickman Mar 6, 2018
1da4c43
Create cwlNodeEngineWithContext for jshint
ThomasHickman Mar 9, 2018
f56192c
Merge branch 'master' into js-linting
ThomasHickman Mar 9, 2018
4062fb9
remove unneeded import
mr-c Mar 10, 2018
915f961
include new javascript in dist
mr-c Mar 10, 2018
5452b8a
Merge branch 'master' into js-linting
mr-c Mar 10, 2018
89d1cdb
a little more unicode…
mr-c Mar 10, 2018
55641d0
a bit more unicode
mr-c Mar 10, 2018
9bd96ba
make unicode fix py2.7 compat
mr-c Mar 10, 2018
202a536
Update validate_js.py
mr-c Mar 10, 2018
16dc781
fix reporting of jshint expressionlib warnings
ThomasHickman Mar 10, 2018
dd9336f
propogate globals from each line in expression_lib
ThomasHickman Mar 10, 2018
b372ac4
better open
mr-c Mar 10, 2018
aff6564
Add copy import
ThomasHickman Mar 10, 2018
583dc99
globals -> js_globals
ThomasHickman Mar 10, 2018
0b514c8
Remove should_include_jshint_message
ThomasHickman Mar 10, 2018
af15fc0
tweak windows testing
mr-c Mar 10, 2018
0783014
mock too
mr-c Mar 10, 2018
daac50c
turn off appveyor pytest caching
mr-c Mar 10, 2018
2d54920
add nodejs to appveyor
mr-c Mar 10, 2018
530b7d0
use a fixed nodejs version for appveyor
mr-c Mar 10, 2018
01fe931
Try fix exec_js with context in windows
ThomasHickman Mar 12, 2018
a8b1266
Merge branch 'js-linting' of https://github.com/wtsi-hgi/cwltool into…
ThomasHickman Mar 12, 2018
b243a10
Use resource_stream for validate_js
ThomasHickman Mar 12, 2018
7926be4
Revert get_data -> get_test_data
ThomasHickman Mar 12, 2018
7552315
move validate_js_expressions to Process.__init__
ThomasHickman Mar 12, 2018
5e08b88
Remove unused imports
ThomasHickman Mar 12, 2018
50d9d8f
Correct Process type
ThomasHickman Mar 12, 2018
b28dfd5
Further correct type errors
ThomasHickman Mar 12, 2018
c295a04
Merge branch 'master' into js-linting
ThomasHickman Mar 15, 2018
17fa7fc
Merge branch 'master' into js-linting
tetron Mar 22, 2018
1786a7f
Merge branch 'master' into js-linting
tetron Mar 22, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ include cwltool/schemas/v1.1.0-dev1/salad/schema_salad/metaschema/*.yml
include cwltool/schemas/v1.1.0-dev1/salad/schema_salad/metaschema/*.md
include cwltool/cwlNodeEngine.js
include cwltool/cwlNodeEngineJSConsole.js
include cwltool/cwlNodeEngineWithContext.js
include cwltool/extensions.yml
include cwltool/jshint/jshint_wrapper.js
include cwltool/jshint/jshint.js
global-exclude *~
global-exclude *.pyc
12 changes: 7 additions & 5 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
version: .{build}-{branch}

environment:
nodejs_version: "8"

SYSTEMROOT: "C:\\WINDOWS"

matrix:
Expand All @@ -22,18 +24,18 @@ environment:


install:
# Get the latest stable version of Node.js or io.js
- ps: Install-Product node $env:nodejs_version
- "SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%"
- "python --version"
- "python -c \"import struct; print(struct.calcsize('P') * 8)\""

build_script:
- "%CMD_IN_ENV% python -m pip install -U setuptools"
- "%CMD_IN_ENV% python -m pip install -U setuptools pip"
- "%CMD_IN_ENV% pip install pytest mock"
- "%CMD_IN_ENV% pip install ."


test_script:

- "%CMD_IN_ENV% python setup.py test"
- "%CMD_IN_ENV% py.test --verbose -p no:cacheprovider"

branches:
only:
Expand Down
5 changes: 5 additions & 0 deletions cwltool/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ def arg_parser(): # type: () -> argparse.ArgumentParser
"timestamps to the errors, warnings, and "
"notifications.")
parser.add_argument("--js-console", action="store_true", help="Enable javascript console output")
parser.add_argument("--disable-js-validation", action="store_true", help="Disable javascript validation.")
parser.add_argument("--js-hint-options-file",
type=Text,
help="File of options to pass to jshint."
"This includes the added option \"includewarnings\". ")
dockergroup = parser.add_mutually_exclusive_group()
dockergroup.add_argument("--user-space-docker-cmd", metavar="CMD",
help="(Linux/OS X only) Specify a user space docker "
Expand Down
37 changes: 37 additions & 0 deletions cwltool/cwlNodeEngineWithContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";
process.stdin.setEncoding("utf8");
var incoming = "";
var firstInput = true;
var context = {};

process.stdin.on("data", function(chunk) {
incoming += chunk;
var i = incoming.indexOf("\n");
while (i > -1) {
try{
var input = incoming.substr(0, i);
incoming = incoming.substr(i+1);
var fn = JSON.parse(input);
if(firstInput){
context = require("vm").runInNewContext(fn, {});
}
else{
process.stdout.write(JSON.stringify(require("vm").runInNewContext(fn, context)) + "\n");
}
}
catch(e){
console.error(e);
}
if(firstInput){
firstInput = false;
}
else{
/*strings to indicate the process has finished*/
console.log("r1cepzbhUTxtykz5XTC4");
console.error("r1cepzbhUTxtykz5XTC4");
}

i = incoming.indexOf("\n");
}
});
process.stdin.on("end", process.exit);
24,568 changes: 24,568 additions & 0 deletions cwltool/jshint/jshint.js

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions cwltool/jshint/jshint_wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";
// set a global object, in order for jshint to work
var global = this;

function validateJS(input) {
var jshintGlobalsObj = {};
input.globals.forEach(function (global) {
jshintGlobalsObj[global] = true;
})
var includewarnings;

if (input.options.includewarnings !== undefined) {
includewarnings = input.options.includewarnings;
delete input.options.includewarnings;
}

JSHINT(
input.code,
input.options,
jshintGlobalsObj
)

var jshintData = JSHINT.data();
if (jshintData.errors !== undefined) {
if (includewarnings !== undefined) {
jshintData.errors = jshintData.errors.filter(function (error) {
return includewarnings.indexOf(error.code) !== -1 || error.code[0] == "E";
})
}

jshintData.errors.forEach(function (error) {
if (error.code == "W104" || error.code == "W119") {
if (error.code == "W104"){
var jslint_suffix = " (use 'esversion: 6') or Mozilla JS extensions (use moz)."
}
else{
var jslint_suffix = " (use 'esversion: 6')"
}

error.reason = error.reason.slice(0, -jslint_suffix.length - 1) +
". CWL only supports ES5.1";
}
})
}

return jshintData;
}
2 changes: 1 addition & 1 deletion cwltool/load_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def validate_document(document_loader, # type: Loader
fetcher_constructor=None, # type: FetcherConstructorType
skip_schemas=None, # type: bool
overrides=None, # type: List[Dict]
metadata=None, # type: Optional[Dict]
metadata=None # type: Optional[Dict]
):
# type: (...) -> Tuple[Loader, Names, Union[Dict[Text, Any], List[Dict[Text, Any]]], Dict[Text, Any], Text]
"""Validate a CWL document."""
Expand Down
14 changes: 14 additions & 0 deletions cwltool/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from schema_salad.sourceline import SourceLine
from six.moves import urllib

from .validate_js import validate_js_expressions
from .utils import cmp_like_py2
from .builder import Builder
from .errors import UnsupportedRequirement, WorkflowException
Expand Down Expand Up @@ -548,6 +549,19 @@ def __init__(self, toolpath_object, **kwargs):
(Text(e), self.tool["id"],
json.dumps(self.outputs_record_schema, indent=4)))

if toolpath_object.get("class") is not None and not kwargs.get("disable_js_validation", False):
if kwargs.get("js_hint_options_file") is not None:
try:
with open(kwargs["js_hint_options_file"]) as options_file:
validate_js_options = json.load(options_file)
except (OSError, ValueError) as e:
_logger.error("Failed to read options file %s" % kwargs["js_hint_options_file"])
raise e
else:
validate_js_options = None

validate_js_expressions(cast(CommentedMap, toolpath_object), self.doc_schema.names[toolpath_object["class"]], validate_js_options)

def _init_job(self, joborder, **kwargs):
# type: (Dict[Text, Text], **Any) -> Builder
"""
Expand Down
Loading