Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

80 řádky
2.8 KiB

  1. """
  2. Checks that files duplicated across schemes/implementations are consistent.
  3. """
  4. import difflib
  5. import os
  6. import sys
  7. import yaml
  8. import helpers
  9. import pqclean
  10. sys.tracebacklimit = 0
  11. def pytest_generate_tests(metafunc):
  12. ids = []
  13. argvalues = []
  14. for scheme in pqclean.Scheme.all_schemes():
  15. for implementation in scheme.implementations:
  16. if os.path.isfile(
  17. os.path.join(
  18. 'duplicate_consistency',
  19. '{}_{}.yml'.format(scheme.name, implementation.name))):
  20. metafile = os.path.join(
  21. 'duplicate_consistency',
  22. '{}_{}.yml'.format(scheme.name, implementation.name))
  23. with open(metafile, encoding='utf-8') as f:
  24. metadata = yaml.safe_load(f.read())
  25. for group in metadata['consistency_checks']:
  26. source = pqclean.Implementation.by_name(
  27. group['source']['scheme'],
  28. group['source']['implementation'])
  29. argvalues.append(
  30. (implementation, source, group['files']))
  31. ids.append(
  32. "{metafile}: {scheme.name} {implementation.name}"
  33. .format(scheme=scheme,
  34. implementation=implementation,
  35. metafile=metafile))
  36. metafunc.parametrize(('implementation', 'source', 'files'),
  37. argvalues,
  38. ids=ids)
  39. def file_get_contents(filename):
  40. with open(filename) as file:
  41. return file.read()
  42. @helpers.filtered_test
  43. def test_duplicate_consistency(implementation, source, files):
  44. """Test sets of files to be identical modulo namespacing"""
  45. messages = []
  46. for file in files:
  47. target_path = os.path.join(source.path(), file)
  48. this_path = os.path.join(implementation.path(), file)
  49. target_src = file_get_contents(target_path)
  50. this_src = file_get_contents(this_path)
  51. this_transformed_src = this_src.replace(
  52. implementation.namespace_prefix(), '')
  53. target_transformed_src = target_src.replace(
  54. source.namespace_prefix(), '')
  55. if not this_transformed_src == target_transformed_src:
  56. diff = difflib.unified_diff(
  57. this_transformed_src.splitlines(keepends=True),
  58. target_transformed_src.splitlines(keepends=True),
  59. fromfile=this_path,
  60. tofile=target_path)
  61. messages.append("{} differed:\n{}".format(file, ''.join(diff)))
  62. if messages:
  63. raise AssertionError("Files differed:\n{}".format('\n'.join(messages)))
  64. if __name__ == '__main__':
  65. import pytest
  66. pytest.main(sys.argv)