run-tests.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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,BASE",
  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. if test.iup_optimize:
  54. cli_args.extend (["--optimize",])
  55. ret = subset_cmd (cli_args)
  56. if ret != "success":
  57. return fail_test (test, cli_args, "%s failed" % ' '.join (cli_args))
  58. expected_file = os.path.join (test_suite.get_output_directory (), test.get_font_name ())
  59. with open (expected_file, "rb") as fp:
  60. expected_contents = fp.read()
  61. with open (out_file, "rb") as fp:
  62. actual_contents = fp.read()
  63. if expected_contents == actual_contents:
  64. if should_check_ots:
  65. print ("Checking output with ots-sanitize.")
  66. if not check_ots (out_file):
  67. return fail_test (test, cli_args, 'ots for subsetted file fails.')
  68. return 0
  69. if TTFont is None:
  70. print ("fonttools is not present, skipping TTX diff.")
  71. return fail_test (test, cli_args, "hash for expected and actual does not match.")
  72. with io.StringIO () as fp:
  73. try:
  74. with TTFont (expected_file) as font:
  75. font.saveXML (fp)
  76. except Exception as e:
  77. print (e)
  78. return fail_test (test, cli_args, "ttx failed to parse the expected result")
  79. expected_ttx = fp.getvalue ()
  80. with io.StringIO () as fp:
  81. try:
  82. with TTFont (out_file) as font:
  83. font.saveXML (fp)
  84. except Exception as e:
  85. print (e)
  86. return fail_test (test, cli_args, "ttx failed to parse the actual result")
  87. actual_ttx = fp.getvalue ()
  88. if actual_ttx != expected_ttx:
  89. for line in unified_diff (expected_ttx.splitlines (1), actual_ttx.splitlines (1)):
  90. sys.stdout.write (line)
  91. sys.stdout.flush ()
  92. return fail_test (test, cli_args, 'ttx for expected and actual does not match.')
  93. return fail_test (test, cli_args, 'hash for expected and actual does not match, '
  94. 'but the ttx matches. Expected file needs to be updated?')
  95. def has_ots ():
  96. if not ots_sanitize:
  97. print ("OTS is not present, skipping all ots checks.")
  98. return False
  99. return True
  100. def check_ots (path):
  101. ots_report, returncode = cmd ([ots_sanitize, path])
  102. if returncode:
  103. print ("OTS Failure: %s" % ots_report)
  104. return False
  105. return True
  106. args = sys.argv[1:]
  107. if not args or sys.argv[1].find ('hb-subset') == -1 or not os.path.exists (sys.argv[1]):
  108. sys.exit ("First argument does not seem to point to usable hb-subset.")
  109. hb_subset, args = args[0], args[1:]
  110. if not len (args):
  111. sys.exit ("No tests supplied.")
  112. has_ots = has_ots()
  113. env = os.environ.copy()
  114. env['LC_ALL'] = 'C'
  115. process = subprocess.Popen ([hb_subset, '--batch'],
  116. stdin=subprocess.PIPE,
  117. stdout=subprocess.PIPE,
  118. stderr=sys.stdout,
  119. env=env)
  120. fails = 0
  121. for path in args:
  122. with open (path, mode="r", encoding="utf-8") as f:
  123. print ("Running tests in " + path)
  124. test_suite = SubsetTestSuite (path, f.read ())
  125. for test in test_suite.tests ():
  126. # Tests are run with and without preprocessing, results should be the
  127. # same between them.
  128. fails += run_test (test, has_ots, False)
  129. fails += run_test (test, has_ots, True)
  130. if fails != 0:
  131. sys.exit ("%d test(s) failed." % fails)
  132. else:
  133. print ("All tests passed.")