Skip to content

Commit

Permalink
Merge pull request #52 from common-workflow-language/check-contents
Browse files Browse the repository at this point in the history
Add comparison for file contents
  • Loading branch information
mr-c authored Feb 9, 2018
2 parents 4da150b + ccd6197 commit f3a4276
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
15 changes: 14 additions & 1 deletion cwltest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)


Expand Down
1 change: 1 addition & 0 deletions tests/test-data/cores.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
55 changes: 50 additions & 5 deletions tests/test_compare.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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")
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)

0 comments on commit f3a4276

Please sign in to comment.