Allow for multiple reference text files

This commit is contained in:
Nyall Dawson 2021-02-05 09:24:39 +10:00
parent 0db5fabd05
commit d4f2b842ba
2 changed files with 19 additions and 3 deletions

View File

@ -368,8 +368,17 @@ class AlgorithmsTest(object):
else: else:
self.assertEqual(strhash, expected_result['hash']) self.assertEqual(strhash, expected_result['hash'])
elif 'file' == expected_result['type']: elif 'file' == expected_result['type']:
expected_filepath = self.filepath_from_param(expected_result)
result_filepath = results[id] result_filepath = results[id]
if isinstance(expected_result.get('name'), list):
# test to see if any match expected
for path in expected_result['name']:
expected_filepath = self.filepath_from_param({'name': path})
if self.checkFilesEqual(expected_filepath, result_filepath):
break
else:
expected_filepath = self.filepath_from_param({'name': expected_result['name'][0]})
else:
expected_filepath = self.filepath_from_param(expected_result)
self.assertFilesEqual(expected_filepath, result_filepath) self.assertFilesEqual(expected_filepath, result_filepath)
elif 'directory' == expected_result['type']: elif 'directory' == expected_result['type']:

View File

@ -196,7 +196,7 @@ class TestCase(_TestCase):
return True return True
def assertFilesEqual(self, filepath_expected, filepath_result): def checkFilesEqual(self, filepath_expected, filepath_result, use_asserts=False):
with open(filepath_expected, 'r') as file_expected: with open(filepath_expected, 'r') as file_expected:
with open(filepath_result, 'r') as file_result: with open(filepath_result, 'r') as file_result:
diff = difflib.unified_diff( diff = difflib.unified_diff(
@ -206,7 +206,14 @@ class TestCase(_TestCase):
tofile='result', tofile='result',
) )
diff = list(diff) diff = list(diff)
self.assertEqual(0, len(diff), ''.join(diff)) eq = not len(diff)
if use_asserts:
self.assertEqual(0, len(diff), ''.join(diff))
else:
return eq
def assertFilesEqual(self, filepath_expected, filepath_result):
self.checkFilesEqual(filepath_expected, filepath_result, use_asserts=True)
def assertDirectoriesEqual(self, dirpath_expected, dirpath_result): def assertDirectoriesEqual(self, dirpath_expected, dirpath_result):
""" Checks whether both directories have the same content (recursively) and raises an assertion error if not. """ """ Checks whether both directories have the same content (recursively) and raises an assertion error if not. """