3 # Runs a subsetting test suite. Compares the results of subsetting via harfbuzz
4 # to subsetting via fonttools.
6 from difflib import unified_diff
15 from subset_test_suite import SubsetTestSuite
18 from fontTools.ttLib import TTFont
22 ots_sanitize = shutil.which ("ots-sanitize")
24 def 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 ()
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
39 def fail_test (test, cli_args, message):
40 print ('ERROR: %s' % message)
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))
50 def 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",
56 "--drop-tables-=sbix"]
58 cli_args.extend(["--preprocess-face",])
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 ret = subset_cmd (cli_args)
66 return fail_test (test, cli_args, "%s failed" % ' '.join (cli_args))
68 expected_file = os.path.join (test_suite.get_output_directory (), test.get_font_name ())
69 with open (expected_file, "rb") as fp:
70 expected_contents = fp.read()
71 with open (out_file, "rb") as fp:
72 actual_contents = fp.read()
74 if expected_contents == actual_contents:
76 print ("Checking output with ots-sanitize.")
77 if not check_ots (out_file):
78 return fail_test (test, cli_args, 'ots for subsetted file fails.')
82 print ("fonttools is not present, skipping TTX diff.")
83 return fail_test (test, cli_args, "hash for expected and actual does not match.")
85 with io.StringIO () as fp:
87 with TTFont (expected_file) as font:
89 except Exception as e:
91 return fail_test (test, cli_args, "ttx failed to parse the expected result")
92 expected_ttx = fp.getvalue ()
94 with io.StringIO () as fp:
96 with TTFont (out_file) as font:
98 except Exception as e:
100 return fail_test (test, cli_args, "ttx failed to parse the actual result")
101 actual_ttx = fp.getvalue ()
103 if actual_ttx != expected_ttx:
104 for line in unified_diff (expected_ttx.splitlines (1), actual_ttx.splitlines (1)):
105 sys.stdout.write (line)
107 return fail_test (test, cli_args, 'ttx for expected and actual does not match.')
109 return fail_test (test, cli_args, 'hash for expected and actual does not match, '
110 'but the ttx matches. Expected file needs to be updated?')
115 print ("OTS is not present, skipping all ots checks.")
119 def check_ots (path):
120 ots_report, returncode = cmd ([ots_sanitize, path])
122 print ("OTS Failure: %s" % ots_report)
127 if not args or sys.argv[1].find ('hb-subset') == -1 or not os.path.exists (sys.argv[1]):
128 sys.exit ("First argument does not seem to point to usable hb-subset.")
129 hb_subset, args = args[0], args[1:]
132 sys.exit ("No tests supplied.")
136 process = subprocess.Popen ([hb_subset, '--batch'],
137 stdin=subprocess.PIPE,
138 stdout=subprocess.PIPE,
143 with open (path, mode="r", encoding="utf-8") as f:
144 print ("Running tests in " + path)
145 test_suite = SubsetTestSuite (path, f.read ())
146 for test in test_suite.tests ():
147 # Tests are run with and without preprocessing, results should be the
149 fails += run_test (test, has_ots, False)
150 fails += run_test (test, has_ots, True)
153 sys.exit ("%d test(s) failed." % fails)
155 print ("All tests passed.")