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):
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"]
57 cli_args.extend (test.get_profile_flags ())
58 ret = subset_cmd (cli_args)
61 return fail_test (test, cli_args, "%s failed" % ' '.join (cli_args))
63 expected_file = os.path.join (test_suite.get_output_directory (), test.get_font_name ())
64 with open (expected_file, "rb") as fp:
65 expected_contents = fp.read()
66 with open (out_file, "rb") as fp:
67 actual_contents = fp.read()
69 if expected_contents == actual_contents:
71 print ("Checking output with ots-sanitize.")
72 if not check_ots (out_file):
73 return fail_test (test, cli_args, 'ots for subsetted file fails.')
77 print ("fonttools is not present, skipping TTX diff.")
78 return fail_test (test, cli_args, "hash for expected and actual does not match.")
80 with io.StringIO () as fp:
82 with TTFont (expected_file) as font:
84 except Exception as e:
86 return fail_test (test, cli_args, "ttx failed to parse the expected result")
87 expected_ttx = fp.getvalue ()
89 with io.StringIO () as fp:
91 with TTFont (out_file) as font:
93 except Exception as e:
95 return fail_test (test, cli_args, "ttx failed to parse the actual result")
96 actual_ttx = fp.getvalue ()
98 if actual_ttx != expected_ttx:
99 for line in unified_diff (expected_ttx.splitlines (1), actual_ttx.splitlines (1)):
100 sys.stdout.write (line)
102 return fail_test (test, cli_args, 'ttx for expected and actual does not match.')
104 return fail_test (test, cli_args, 'hash for expected and actual does not match, '
105 'but the ttx matches. Expected file needs to be updated?')
110 print ("OTS is not present, skipping all ots checks.")
114 def check_ots (path):
115 ots_report, returncode = cmd ([ots_sanitize, path])
117 print ("OTS Failure: %s" % ots_report)
122 if not args or sys.argv[1].find ('hb-subset') == -1 or not os.path.exists (sys.argv[1]):
123 sys.exit ("First argument does not seem to point to usable hb-subset.")
124 hb_subset, args = args[0], args[1:]
127 sys.exit ("No tests supplied.")
131 process = subprocess.Popen ([hb_subset, '--batch'],
132 stdin=subprocess.PIPE,
133 stdout=subprocess.PIPE,
138 with open (path, mode="r", encoding="utf-8") as f:
139 print ("Running tests in " + path)
140 test_suite = SubsetTestSuite (path, f.read ())
141 for test in test_suite.tests ():
142 fails += run_test (test, has_ots)
145 sys.exit ("%d test(s) failed." % fails)
147 print ("All tests passed.")