1
1
mirror of https://github.com/henrydcase/pqc.git synced 2024-11-22 07:35:38 +00:00

Render proper diffs for duplicate_consistency test (#228)

Use Python's built-in ``diflib`` to compute diffs between the two versions.
This commit is contained in:
Thom Wiggers 2019-08-23 15:15:45 +02:00 committed by GitHub
parent 9a5caaa95b
commit 8a120b3be7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,9 @@
Checks that files duplicated across schemes/implementations are consistent.
"""
import difflib
import os
import sys
import yaml
@ -31,8 +33,9 @@ def pytest_generate_tests(metafunc):
for file in group['files']:
argvalues.append((implementation, source, file))
ids.append(
"{scheme.name}-{source.scheme.name}: {file}"
"{scheme.name} {implementation.name} {source.scheme.name}: {file}"
.format(scheme=scheme, source=source,
implementation=implementation,
file=file))
metafunc.parametrize(('implementation', 'source', 'file'),
argvalues,
@ -47,17 +50,25 @@ def file_get_contents(filename):
@helpers.skip_windows()
@helpers.filtered_test
def test_duplicate_consistency(implementation, source, file):
transformed_src = helpers.run_subprocess(
['sed', '-e', 's/{}/{}/g'.format(source.namespace_prefix(),
implementation.namespace_prefix()), os.path.join(source.path(), file)]
)
this_src = file_get_contents(os.path.join(implementation.path(), file))
print(os.path.join(implementation.path(), file))
print(this_src)
assert(transformed_src == this_src)
target_path = os.path.join(source.path(), file)
this_path = os.path.join(implementation.path(), file)
target_src = file_get_contents(target_path)
this_src = file_get_contents(this_path)
this_transformed_src = this_src.replace(
implementation.namespace_prefix(), '')
target_transformed_src = target_src.replace(source.namespace_prefix(), '')
if not this_transformed_src == target_transformed_src:
diff = difflib.unified_diff(
this_transformed_src.splitlines(keepends=True),
target_transformed_src.splitlines(keepends=True),
fromfile=this_path,
tofile=target_path)
raise AssertionError(
"Files differed:\n"
+ ''.join(diff))
if __name__ == '__main__':
import pytest
import sys
pytest.main(sys.argv)