From 3331519beb8579d7e4aec3ce383890bf293fd04e Mon Sep 17 00:00:00 2001 From: Anton Khodak Date: Thu, 8 Feb 2018 15:46:30 +0200 Subject: [PATCH 1/2] Add comparison for file contents --- cwltest/utils.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cwltest/utils.py b/cwltest/utils.py index 9808dc6..f761bff 100644 --- a/cwltest/utils.py +++ b/cwltest/utils.py @@ -35,6 +35,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: @@ -49,7 +60,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) From ccd6197a6b6fb21613c087ed4426b6bca90d2889 Mon Sep 17 00:00:00 2001 From: Anton Khodak Date: Thu, 8 Feb 2018 21:18:32 +0200 Subject: [PATCH 2/2] Add tests for comparison of file contents --- tests/test-data/cores.txt | 1 + tests/test_compare.py | 55 +++++++++++++++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 tests/test-data/cores.txt 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)