You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

86 lines
2.6 KiB

  1. """
  2. Checks that every .c and .h file in an implementation is present as a
  3. dependency of that scheme's Makefile.
  4. """
  5. import datetime
  6. import glob
  7. import os
  8. import pytest
  9. import helpers
  10. import pqclean
  11. @pytest.mark.parametrize(
  12. 'implementation,test_dir,impl_path, init, destr',
  13. [(impl,
  14. *helpers.isolate_test_files(impl.path(), 'test_makefile_deps_'))
  15. for impl in pqclean.Scheme.all_implementations()],
  16. ids=[str(impl) for impl in pqclean.Scheme.all_implementations()],
  17. )
  18. @helpers.filtered_test
  19. def test_makefile_dependencies(implementation, impl_path, test_dir,
  20. init, destr):
  21. init()
  22. # initial build - want to have *all* files in place at beginning
  23. helpers.make('clean', working_dir=impl_path)
  24. helpers.make(working_dir=impl_path)
  25. # test case for each candidate file
  26. cfiles = glob.glob(os.path.join(impl_path, '*.c'))
  27. hfiles = glob.glob(os.path.join(impl_path, '*.h'))
  28. for file in (cfiles + hfiles):
  29. check_makefile_dependencies(implementation, impl_path, file)
  30. destr()
  31. def touch(time, *files):
  32. for path in files:
  33. times = (time.timestamp(), time.timestamp())
  34. os.utime(path, times)
  35. def make_check(path, expect_error=False):
  36. makeflag = '-q' if os.name != 'nt' else '/Q'
  37. expected_returncode = 0
  38. if expect_error:
  39. expected_returncode = 1 if os.name != 'nt' else 255
  40. helpers.make(makeflag, working_dir=path,
  41. expected_returncode=expected_returncode)
  42. def check_makefile_dependencies(implementation, impl_path, file):
  43. cfiles = glob.glob(os.path.join(impl_path, '*.c'))
  44. hfiles = glob.glob(os.path.join(impl_path, '*.h'))
  45. ofiles = glob.glob(
  46. os.path.join(impl_path,
  47. '*.o' if os.name != 'nt' else '*.obj'))
  48. libfile = os.path.join(impl_path, implementation.libname())
  49. # modification time-based calculations is tricky on a sub-second basis
  50. # so we reset all the modification times to a known and "sensible" order
  51. now = datetime.datetime.now() - datetime.timedelta(seconds=10)
  52. ago15 = now - datetime.timedelta(minutes=15)
  53. ago10 = now - datetime.timedelta(minutes=10)
  54. ago5 = now - datetime.timedelta(minutes=5)
  55. touch(ago15, *cfiles, *hfiles)
  56. touch(ago10, *ofiles)
  57. touch(ago5, libfile)
  58. # Sanity check: the scheme is up to date
  59. make_check(impl_path)
  60. # touch the candidate .c / .h file
  61. touch(now, file)
  62. # check if it needs to be rebuilt using make -q
  63. make_check(impl_path, expect_error=True)
  64. if __name__ == '__main__':
  65. import sys
  66. pytest.main(sys.argv)