diff --git a/cwltest/__init__.py b/cwltest/__init__.py index 9eda222..9a71357 100755 --- a/cwltest/__init__.py +++ b/cwltest/__init__.py @@ -21,7 +21,7 @@ from concurrent.futures import ThreadPoolExecutor from six.moves import range from six.moves import zip -from typing import Any, Dict, List +from typing import Any, Dict, List, Text from cwltest.utils import compare, CompareFail, TestResult @@ -40,6 +40,34 @@ templock = threading.Lock() +def prepare_test_command(args, i, tests): + # type: (argparse.Namespace, int, List[Dict[str, str]]) -> List[str] + t = tests[i] + test_command = [args.tool] + test_command.extend(args.args) + + # Add additional arguments given in test case + if args.testargs is not None: + for testarg in args.testargs: + (test_case_name, prefix) = testarg.split('==') + if test_case_name in t: + test_command.extend([prefix, t[test_case_name]]) + + # Add prefixes if running on MacOSX so that boot2docker writes to /Users + with templock: + if 'darwin' in sys.platform and args.tool == 'cwltool': + outdir = tempfile.mkdtemp(prefix=os.path.abspath(os.path.curdir)) + test_command.extend(["--tmp-outdir-prefix={}".format(outdir), "--tmpdir-prefix={}".format(outdir)]) + else: + outdir = tempfile.mkdtemp() + test_command.extend(["--outdir={}".format(outdir), + "--quiet", + t["tool"]]) + if t.get("job"): + test_command.append(t["job"]) + return test_command + + def run_test(args, i, tests, timeout): # type: (argparse.Namespace, int, List[Dict[str, str]], int) -> TestResult @@ -56,28 +84,7 @@ def run_test(args, i, tests, timeout): else: suffix = "\n" try: - test_command = [args.tool] - test_command.extend(args.args) - - # Add additional arguments given in test case - if args.testargs is not None: - for testarg in args.testargs: - (test_case_name, prefix) = testarg.split('==') - if test_case_name in t: - test_command.extend([prefix, t[test_case_name]]) - - # Add prefixes if running on MacOSX so that boot2docker writes to /Users - with templock: - if 'darwin' in sys.platform and args.tool == 'cwltool': - outdir = tempfile.mkdtemp(prefix=os.path.abspath(os.path.curdir)) - test_command.extend(["--tmp-outdir-prefix={}".format(outdir), "--tmpdir-prefix={}".format(outdir)]) - else: - outdir = tempfile.mkdtemp() - test_command.extend(["--outdir={}".format(outdir), - "--quiet", - t["tool"]]) - if t.get("job"): - test_command.append(t["job"]) + test_command = prepare_test_command(args, i, tests) sys.stderr.write("%sTest [%i/%i] %s\n" % (prefix, i + 1, len(tests), suffix)) sys.stderr.flush() @@ -142,8 +149,7 @@ def run_test(args, i, tests, timeout): return TestResult((1 if fail_message else 0), outstr, outerr, duration, args.classname, fail_message) -def main(): # type: () -> int - +def arg_parser(): # type: () -> argparse.ArgumentParser parser = argparse.ArgumentParser(description='Compliance tests for cwltool') parser.add_argument("--test", type=str, help="YAML file describing test cases", required=True) parser.add_argument("--basedir", type=str, help="Basedir to use for tests", default=".") @@ -164,8 +170,12 @@ def main(): # type: () -> int parser.add_argument("--timeout", type=int, default=DEFAULT_TIMEOUT, help="Time of execution in seconds after " "which the test will be skipped." "Defaults to 900 sec (15 minutes)") + return parser + + +def main(): # type: () -> int - args = parser.parse_args() + args = arg_parser().parse_args(sys.argv[1:]) if '--' in args.args: args.args.remove('--') @@ -174,7 +184,7 @@ def main(): # type: () -> int args.testargs = [testarg for testarg in args.testargs if testarg.count('==') == 1] if not args.test: - parser.print_help() + arg_parser().print_help() return 1 with open(args.test) as f: diff --git a/mypy.ini b/mypy.ini index 7985b20..225366b 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,2 +1,4 @@ +[mypy] + [mypy-ruamel.*] ignore_errors = True diff --git a/setup.py b/setup.py index 2b16faf..9127eb0 100755 --- a/setup.py +++ b/setup.py @@ -39,6 +39,7 @@ license='Apache 2.0', packages=["cwltest"], install_requires=install_requires, + test_suite='tests', tests_require=[], entry_points={ 'console_scripts': ["cwltest=cwltest:main"] diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_argparse.py b/tests/test_argparse.py new file mode 100644 index 0000000..44c3c88 --- /dev/null +++ b/tests/test_argparse.py @@ -0,0 +1,18 @@ +import unittest +from cwltest import arg_parser + +class TestArgparse(unittest.TestCase): + def setUp(self): + self.parser = arg_parser() + + def test_arg(self): + parsed = self.parser.parse_args(['--test', 'test_name','-n','52','--tool','cwltool','-j','4']) + self.assertEqual(parsed.test, 'test_name') + self.assertEqual(parsed.n, '52') + self.assertEqual(parsed.tool, 'cwltool') + self.assertEqual(parsed.j, 4) + + +if __name__ == '__main__': + unittest.main() + diff --git a/tests/test_compare.py b/tests/test_compare.py new file mode 100644 index 0000000..74ae644 --- /dev/null +++ b/tests/test_compare.py @@ -0,0 +1,28 @@ +import unittest +from cwltest import CompareFail +from cwltest.utils import compare_file + + +class TestCompareFile(unittest.TestCase): + + def test_general(self): + expected = { + "location": "cores.txt", + "size": 2, + "class": "File", + "checksum": "sha1$7448d8798a4380162d4b56f9b452e2f6f9e24e7a" + } + + actual = { + "basename": "cores.txt", + "checksum": "sha1$7448d8798a4380162d4b56f9b452e2f6f9e24e7a", + "class": "File", + "location": "file:///var/folders/8x/2df05_7j20j6r8y81w4qf43r0000gn/T/tmpG0EkrS/cores.txt", + "path": "/var/folders/8x/2df05_7j20j6r8y81w4qf43r0000gn/T/tmpG0EkrS/cores.txt", + "size": 2 + } + + try: + compare_file(expected, actual) + except CompareFail: + self.fail("File comparison failed unexpectedly") \ No newline at end of file