diff --git a/cwltest/utils.py b/cwltest/utils.py index 66da97d..934017a 100644 --- a/cwltest/utils.py +++ b/cwltest/utils.py @@ -62,6 +62,17 @@ def compare_location(expected, actual): raise CompareFail.format(expected, actual, u"%s does not end with %s" % (actual[comp], expected[comp])) +def compare_contents(expected, actual): + # type: (Dict[str,Any], Dict[str,Any]) -> None + expected_contents = expected["contents"] + with open(actual["path"]) as f: + actual_contents = f.read() + if expected_contents != actual_contents: + raise CompareFail.format(expected, actual, + json.dumps(u"Output file contents do not match: actual '%s' is not equal to expected '%s'" + % (actual_contents, expected_contents))) + + def check_keys(keys, expected, actual): # type: (Set[str], Dict[str,Any], Dict[str,Any]) -> None for k in keys: @@ -76,7 +87,9 @@ def check_keys(keys, expected, actual): def compare_file(expected, actual): # type: (Dict[str,Any], Dict[str,Any]) -> None compare_location(expected, actual) - other_keys = set(expected.keys()) - {'path', 'location', 'listing'} + if 'contents' in expected: + compare_contents(expected, actual) + other_keys = set(expected.keys()) - {'path', 'location', 'listing', 'contents'} check_keys(other_keys, expected, actual) diff --git a/tests/test-data/cores.txt b/tests/test-data/cores.txt new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/tests/test-data/cores.txt @@ -0,0 +1 @@ +2 diff --git a/tests/test_compare.py b/tests/test_compare.py index 74ae644..ddaeeea 100644 --- a/tests/test_compare.py +++ b/tests/test_compare.py @@ -1,11 +1,20 @@ import unittest from cwltest import CompareFail -from cwltest.utils import compare_file +from cwltest.utils import compare_file, compare -class TestCompareFile(unittest.TestCase): +class TestCompare(unittest.TestCase): - def test_general(self): + def compare_success(self, expected, actual): + try: + compare(expected, actual) + except CompareFail: + self.fail("Comparison failed unexpectedly") + + +class TestCompareFile(TestCompare): + + def test_compare_file(self): expected = { "location": "cores.txt", "size": 2, @@ -21,8 +30,44 @@ def test_general(self): "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 + self.fail("File comparison failed unexpectedly") + + def test_compare_contents_success(self): + expected = { + "location": "cores.txt", + "size": 2, + "class": "File", + "contents": "2\n" + } + + actual = { + "basename": "cores.txt", + "checksum": "sha1$7448d8798a4380162d4b56f9b452e2f6f9e24e7a", + "class": "File", + "location": "file:///var/folders/8x/2df05_7j20j6r8y81w4qf43r0000gn/T/tmpG0EkrS/cores.txt", + "path": "tests/test-data/cores.txt", + "size": 2 + } + self.compare_success(expected, actual) + + def test_compare_contents_failure(self): + expected = { + "location": "cores.txt", + "size": 2, + "class": "File", + "contents": "2" + } + + actual = { + "basename": "cores.txt", + "checksum": "sha1$7448d8798a4380162d4b56f9b452e2f6f9e24e7a", + "class": "File", + "location": "file:///var/folders/8x/2df05_7j20j6r8y81w4qf43r0000gn/T/tmpG0EkrS/cores.txt", + "path": "tests/test-data/cores.txt", + "size": 2 + } + with self.assertRaises(CompareFail): + compare_file(expected, actual)