From 8a120b3be715db65ea73426b6302587e0375c966 Mon Sep 17 00:00:00 2001 From: Thom Wiggers Date: Fri, 23 Aug 2019 15:15:45 +0200 Subject: [PATCH] Render proper diffs for duplicate_consistency test (#228) Use Python's built-in ``diflib`` to compute diffs between the two versions. --- test/test_duplicate_consistency.py | 31 ++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/test/test_duplicate_consistency.py b/test/test_duplicate_consistency.py index 224189ca..6226b3fe 100644 --- a/test/test_duplicate_consistency.py +++ b/test/test_duplicate_consistency.py @@ -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)