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