Skip to content

Commit

Permalink
Merge pull request #36 from kapilkd13/testsuite
Browse files Browse the repository at this point in the history
Adding basic Testsuite to cwltest
  • Loading branch information
Anton Khodak authored Feb 8, 2018
2 parents d3f7cce + 05ef85b commit 4da150b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 27 deletions.
64 changes: 37 additions & 27 deletions cwltest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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=".")
Expand All @@ -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('--')

Expand All @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[mypy]

[mypy-ruamel.*]
ignore_errors = True
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
Empty file added tests/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions tests/test_argparse.py
Original file line number Diff line number Diff line change
@@ -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()

28 changes: 28 additions & 0 deletions tests/test_compare.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 4da150b

Please sign in to comment.