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