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