run-tests.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env python3
  2. # Runs a subsetting test suite. Compares the results of subsetting via harfbuzz
  3. # to subsetting via fonttools.
  4. from difflib import unified_diff
  5. import os
  6. import re
  7. import subprocess
  8. import sys
  9. import tempfile
  10. import shutil
  11. import io
  12. from subset_test_suite import SubsetTestSuite
  13. try:
  14. from fontTools.ttLib import TTFont
  15. except ImportError:
  16. TTFont = None
  17. ots_sanitize = shutil.which ("ots-sanitize")
  18. def subset_cmd (command):
  19. global hb_subset, process
  20. print (hb_subset + ' ' + " ".join(command))
  21. process.stdin.write ((';'.join (command) + '\n').encode ("utf-8"))
  22. process.stdin.flush ()
  23. return process.stdout.readline().decode ("utf-8").strip ()
  24. def cmd (command):
  25. p = subprocess.Popen (
  26. command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  27. universal_newlines=True)
  28. (stdoutdata, stderrdata) = p.communicate ()
  29. print (stderrdata, end="", file=sys.stderr)
  30. return stdoutdata, p.returncode
  31. def fail_test (test, cli_args, message):
  32. print ('ERROR: %s' % message)
  33. print ('Test State:')
  34. print (' test.font_path %s' % os.path.abspath (test.font_path))
  35. print (' test.profile_path %s' % os.path.abspath (test.profile_path))
  36. print (' test.unicodes %s' % test.unicodes ())
  37. expected_file = os.path.join (test_suite.get_output_directory (),
  38. test.get_font_name ())
  39. print (' expected_file %s' % os.path.abspath (expected_file))
  40. return 1
  41. def run_test (test, should_check_ots, preprocess):
  42. out_file = os.path.join (tempfile.mkdtemp (), test.get_font_name () + '-subset' + test.get_font_extension ())
  43. cli_args = ["--font-file=" + test.font_path,
  44. "--output-file=" + out_file,
  45. "--unicodes=%s" % test.unicodes (),
  46. "--drop-tables+=DSIG",
  47. "--drop-tables-=sbix"]
  48. if preprocess:
  49. cli_args.extend(["--preprocess-face",])
  50. cli_args.extend (test.get_profile_flags ())
  51. if test.get_instance_flags ():
  52. cli_args.extend (["--instance=%s" % ','.join(test.get_instance_flags ())])
  53. ret = subset_cmd (cli_args)
  54. if ret != "success":
  55. return fail_test (test, cli_args, "%s failed" % ' '.join (cli_args))
  56. expected_file = os.path.join (test_suite.get_output_directory (), test.get_font_name ())
  57. with open (expected_file, "rb") as fp:
  58. expected_contents = fp.read()
  59. with open (out_file, "rb") as fp:
  60. actual_contents = fp.read()
  61. if expected_contents == actual_contents:
  62. if should_check_ots:
  63. print ("Checking output with ots-sanitize.")
  64. if not check_ots (out_file):
  65. return fail_test (test, cli_args, 'ots for subsetted file fails.')
  66. return 0
  67. if TTFont is None:
  68. print ("fonttools is not present, skipping TTX diff.")
  69. return fail_test (test, cli_args, "hash for expected and actual does not match.")
  70. with io.StringIO () as fp:
  71. try:
  72. with TTFont (expected_file) as font:
  73. font.saveXML (fp)
  74. except Exception as e:
  75. print (e)
  76. return fail_test (test, cli_args, "ttx failed to parse the expected result")
  77. expected_ttx = fp.getvalue ()
  78. with io.StringIO () as fp:
  79. try:
  80. with TTFont (out_file) as font:
  81. font.saveXML (fp)
  82. except Exception as e:
  83. print (e)
  84. return fail_test (test, cli_args, "ttx failed to parse the actual result")
  85. actual_ttx = fp.getvalue ()
  86. if actual_ttx != expected_ttx:
  87. for line in unified_diff (expected_ttx.splitlines (1), actual_ttx.splitlines (1)):
  88. sys.stdout.write (line)
  89. sys.stdout.flush ()
  90. return fail_test (test, cli_args, 'ttx for expected and actual does not match.')
  91. return fail_test (test, cli_args, 'hash for expected and actual does not match, '
  92. 'but the ttx matches. Expected file needs to be updated?')
  93. def has_ots ():
  94. if not ots_sanitize:
  95. print ("OTS is not present, skipping all ots checks.")
  96. return False
  97. return True
  98. def check_ots (path):
  99. ots_report, returncode = cmd ([ots_sanitize, path])
  100. if returncode:
  101. print ("OTS Failure: %s" % ots_report)
  102. return False
  103. return True
  104. args = sys.argv[1:]
  105. if not args or sys.argv[1].find ('hb-subset') == -1 or not os.path.exists (sys.argv[1]):
  106. sys.exit ("First argument does not seem to point to usable hb-subset.")
  107. hb_subset, args = args[0], args[1:]
  108. if not len (args):
  109. sys.exit ("No tests supplied.")
  110. has_ots = has_ots()
  111. process = subprocess.Popen ([hb_subset, '--batch'],
  112. stdin=subprocess.PIPE,
  113. stdout=subprocess.PIPE,
  114. stderr=sys.stdout)
  115. fails = 0
  116. for path in args:
  117. with open (path, mode="r", encoding="utf-8") as f:
  118. print ("Running tests in " + path)
  119. test_suite = SubsetTestSuite (path, f.read ())
  120. for test in test_suite.tests ():
  121. # Tests are run with and without preprocessing, results should be the
  122. # same between them.
  123. fails += run_test (test, has_ots, False)
  124. fails += run_test (test, has_ots, True)
  125. if fails != 0:
  126. sys.exit ("%d test(s) failed." % fails)
  127. else:
  128. print ("All tests passed.")