From c55827262496982c795748ee913d2f38e76755cc Mon Sep 17 00:00:00 2001 From: kapilkd13 Date: Fri, 25 Aug 2017 11:43:23 +0530 Subject: [PATCH 1/4] refactoring functions for easy testing --- cwltest/__init__.py | 60 +++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/cwltest/__init__.py b/cwltest/__init__.py index fd56cda..95a1451 100755 --- a/cwltest/__init__.py +++ b/cwltest/__init__.py @@ -163,6 +163,33 @@ def compare(expected, actual): # type: (Any, Any) -> None templock = threading.Lock() +def prepare_test_command(args, i, tests): + 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): # type: (argparse.Namespace, int, List[Dict[str, str]]) -> TestResult global templock @@ -177,28 +204,7 @@ def run_test(args, i, tests): # type: (argparse.Namespace, int, List[Dict[str, 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() @@ -250,8 +256,7 @@ def run_test(args, i, tests): # type: (argparse.Namespace, int, List[Dict[str, 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=".") @@ -269,8 +274,11 @@ def main(): # type: () -> int "(defaults to one).") parser.add_argument("--verbose", action="store_true", help="More verbose output during test run.") parser.add_argument("--classname", type=str, default="", help="Specify classname for the Test Suite.") + 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('--') @@ -279,7 +287,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: From ae862afc424974d26328d569a523d7145b34e2fb Mon Sep 17 00:00:00 2001 From: kapilkd13 Date: Fri, 25 Aug 2017 15:36:44 +0530 Subject: [PATCH 2/4] adding test suite to the cwltest module --- tests/test_argparse.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/test_argparse.py 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() + From 3644bc780b5a9adecefa19ad2b8ed088d2687ba6 Mon Sep 17 00:00:00 2001 From: kapilkd13 Date: Fri, 25 Aug 2017 23:45:17 +0530 Subject: [PATCH 3/4] enabling setup.py test --- setup.py | 1 + tests/__init__.py | 0 2 files changed, 1 insertion(+) create mode 100644 tests/__init__.py diff --git a/setup.py b/setup.py index 81cab1b..eef44a2 100755 --- a/setup.py +++ b/setup.py @@ -40,6 +40,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 From 05ef85b1e805ffb2ecd3a202b047e29b9f424a1a Mon Sep 17 00:00:00 2001 From: Anton Khodak Date: Thu, 8 Feb 2018 20:42:38 +0200 Subject: [PATCH 4/4] Add first unit test --- cwltest/__init__.py | 8 ++++++-- mypy.ini | 2 ++ tests/test_compare.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/test_compare.py diff --git a/cwltest/__init__.py b/cwltest/__init__.py index eb0aae9..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 @@ -39,7 +39,9 @@ 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) @@ -82,7 +84,7 @@ def run_test(args, i, tests, timeout): else: suffix = "\n" try: - test_command=prepare_test_command(args, i, tests) + 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() @@ -146,6 +148,7 @@ def run_test(args, i, tests, timeout): return TestResult((1 if fail_message else 0), outstr, outerr, duration, args.classname, fail_message) + 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) @@ -169,6 +172,7 @@ def arg_parser(): # type: () -> argparse.ArgumentParser "Defaults to 900 sec (15 minutes)") return parser + def main(): # type: () -> int args = arg_parser().parse_args(sys.argv[1:]) 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/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