Skip to content

Commit

Permalink
Merge pull request #104 from common-workflow-language/use-schema-salad
Browse files Browse the repository at this point in the history
Use schema salad
  • Loading branch information
tetron authored Feb 20, 2020
2 parents 221e21c + ba52fe6 commit 85709d1
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 19 deletions.
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
include Makefile gittaggers.py
include cwltest/cwltest-schema.yml
include cwltest/tests/*
include cwltest/tests/test-data/*
include cwltest/tests/test-data/v1.0/*
global-exclude *~
global-exclude *.pyc

41 changes: 34 additions & 7 deletions cwltest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import ruamel.yaml as yaml
import ruamel.yaml.scanner as yamlscanner
import schema_salad.ref_resolver
import schema_salad.schema
import schema_salad.avro.schema
import pkg_resources # part of setuptools

import junit_xml
Expand Down Expand Up @@ -49,6 +51,7 @@ def prepare_test_command(
args, # type: List[str]
testargs, # type: Optional[List[str]]
test, # type: Dict[str, str]
cwd, # type: str
verbose=False, # type: bool
): # type: (...) -> List[str]
""" Turn the test into a command line. """
Expand Down Expand Up @@ -77,9 +80,18 @@ def prepare_test_command(
test_command.extend(["--outdir={}".format(outdir)])
if not verbose:
test_command.extend(["--quiet"])
test_command.extend([os.path.normcase(test["tool"])])
if test.get("job"):
test_command.append(os.path.normcase(test["job"]))

cwd = schema_salad.ref_resolver.file_uri(cwd)
toolpath = test["tool"]
if toolpath.startswith(cwd):
toolpath = toolpath[len(cwd)+1:]
test_command.extend([os.path.normcase(toolpath)])

jobpath = test.get("job")
if jobpath:
if jobpath.startswith(cwd):
jobpath = jobpath[len(cwd)+1:]
test_command.append(os.path.normcase(jobpath))
return test_command


Expand All @@ -106,8 +118,9 @@ def run_test(
suffix = "\n"
try:
process = None # type: Optional[subprocess.Popen[str]]
cwd = os.getcwd()
test_command = prepare_test_command(
args.tool, args.args, args.testargs, test, verbose
args.tool, args.args, args.testargs, test, cwd, verbose
)

if test.get("short_name"):
Expand All @@ -131,7 +144,7 @@ def run_test(

start_time = time.time()
stderr = subprocess.PIPE if not args.verbose else None
process = subprocess.Popen(test_command, stdout=subprocess.PIPE, stderr=stderr, universal_newlines=True)
process = subprocess.Popen(test_command, stdout=subprocess.PIPE, stderr=stderr, universal_newlines=True, cwd=cwd)
outstr, outerr = process.communicate(timeout=timeout)
return_code = process.poll()
duration = time.time() - start_time
Expand Down Expand Up @@ -342,8 +355,18 @@ def main(): # type: () -> int
arg_parser().print_help()
return 1

with open(args.test) as f:
tests = yaml.load(f, Loader=yaml.SafeLoader)
schema_resource = pkg_resources.resource_stream(__name__, "cwltest-schema.yml")
cache = {"https://w3id.org/cwl/cwltest/cwltest-schema.yml": schema_resource.read().decode("utf-8")}
(document_loader,
avsc_names,
schema_metadata,
metaschema_loader) = schema_salad.schema.load_schema("https://w3id.org/cwl/cwltest/cwltest-schema.yml", cache=cache)

if not isinstance(avsc_names, schema_salad.avro.schema.Names):
print(avsc_names)
return 1

tests, metadata = schema_salad.schema.load_and_validate(document_loader, avsc_names, args.test, True)

failures = 0
unsupported = 0
Expand Down Expand Up @@ -378,6 +401,10 @@ def main(): # type: () -> int
if any((tag in ts for tag in tags)):
tests.append(t)

for t in tests:
if t.get("label"):
t["short_name"] = t["label"]

if args.l:
for i, t in enumerate(tests):
if t.get("short_name"):
Expand Down
26 changes: 26 additions & 0 deletions cwltest/cwltest-schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
$base: "https://w3id.org/cwl/cwltest#"
$graph:
- name: TestCase
type: record
documentRoot: true
fields:
id:
type: ["null", int, string]
jsonldPredicate:
_type: "@id"
identity: true
label: string?
short_name: string?
doc: string?
tags: string[]?
tool:
type: string
jsonldPredicate:
_type: "@id"
job:
type: string?
jsonldPredicate:
_type: "@id"
should_fail: boolean?
output:
type: Any?
4 changes: 2 additions & 2 deletions cwltest/tests/mock_cwl_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def main(): # type: ()->int

args = parser.parse_args()

if args.processfile == UNSUPPORTED_FEATURE_TOOL:
if args.processfile.endswith(UNSUPPORTED_FEATURE_TOOL):
exit(UNSUPPORTED_FEATURE)
elif args.processfile == ERROR_TOOL:
elif args.processfile.endswith(ERROR_TOOL):
exit(1)

exit(0)
Expand Down
Empty file.
Empty file.
Empty file.
3 changes: 1 addition & 2 deletions cwltest/tests/test-data/short-names.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
output: {}
tool: return-0.cwl
doc: Test with a short name
short_name: opt-error
label: opt-error
tags: [ js, init_work_dir ]

Empty file.
Empty file.
Empty file.
20 changes: 14 additions & 6 deletions cwltest/tests/test_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@

from .util import run_with_mock_cwl_runner, get_data
import xml.etree.ElementTree as ET

import schema_salad.ref_resolver

class TestCategories(unittest.TestCase):
maxDiff = None

def test_unsupported_with_required_tests(self):
args = ["--test", get_data("tests/test-data/required-unsupported.yml")]
error_code, stdout, stderr = run_with_mock_cwl_runner(args)
args = ["--test", schema_salad.ref_resolver.file_uri(get_data("tests/test-data/required-unsupported.yml"))]
try:
cwd = os.getcwd()
os.chdir(get_data("tests/test-data/"))
error_code, stdout, stderr = run_with_mock_cwl_runner(args)
finally:
os.chdir(cwd)

self.assertEqual(error_code, 1)
print(stderr)
stderr = re.sub(r" '?--outdir=[^ ]*", "", stderr)
Expand Down Expand Up @@ -39,7 +47,7 @@ def test_unsupported_with_required_tests(self):
)

def test_unsupported_with_optional_tests(self):
args = ["--test", get_data("tests/test-data/optional-unsupported.yml")]
args = ["--test", schema_salad.ref_resolver.file_uri(get_data("tests/test-data/optional-unsupported.yml"))]
error_code, stdout, stderr = run_with_mock_cwl_runner(args)
self.assertEqual(error_code, 0)
self.assertEqual(
Expand All @@ -50,7 +58,7 @@ def test_unsupported_with_optional_tests(self):
)

def test_error_with_optional_tests(self):
args = ["--test", get_data("tests/test-data/optional-error.yml")]
args = ["--test", schema_salad.ref_resolver.file_uri(get_data("tests/test-data/optional-error.yml"))]
error_code, stdout, stderr = run_with_mock_cwl_runner(args)
self.assertEqual(error_code, 1)
self.assertIn("1 failures", stderr)
Expand All @@ -59,7 +67,7 @@ def test_category_in_junit_xml(self):
junit_xml_report = get_data("tests/test-data/junit-report.xml")
args = [
"--test",
get_data("tests/test-data/optional-error.yml"),
schema_salad.ref_resolver.file_uri(get_data("tests/test-data/optional-error.yml")),
"--junit-xml",
junit_xml_report,
]
Expand Down
1 change: 1 addition & 0 deletions cwltest/tests/test_prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_unix_relative_path(self):
"job": "v1.0/bwa-mem-job.json",
"tags": ["required"],
},
cwd=os.getcwd()
)
if os.name == "nt":
self.assertEqual(command[3], "v1.0\\bwa-mem-tool.cwl")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
except ImportError:
tagger = egg_info_cmd.egg_info

install_requires = ["schema-salad >= 2", "junit-xml >= 1.8"]
install_requires = ["schema-salad >= 5.0.20200220195218", "junit-xml >= 1.8"]

needs_pytest = {"pytest", "test", "ptr"}.intersection(sys.argv)
pytest_runner = ["pytest < 6", "pytest-runner < 5"] if needs_pytest else []
Expand Down

0 comments on commit 85709d1

Please sign in to comment.