(root)/
harfbuzz-8.3.0/
test/
subset/
run-tests.py
       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  
       6  from difflib import unified_diff
       7  import os
       8  import re
       9  import subprocess
      10  import sys
      11  import tempfile
      12  import shutil
      13  import io
      14  
      15  from subset_test_suite import SubsetTestSuite
      16  
      17  try:
      18  	from fontTools.ttLib import TTFont
      19  except ImportError:
      20      TTFont = None
      21  
      22  ots_sanitize = shutil.which ("ots-sanitize")
      23  
      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 ()
      30  
      31  def 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  
      39  def 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  
      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"]
      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  	ret = subset_cmd (cli_args)
      64  
      65  	if ret != "success":
      66  		return fail_test (test, cli_args, "%s failed" % ' '.join (cli_args))
      67  
      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()
      73  
      74  	if expected_contents == actual_contents:
      75  		if should_check_ots:
      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.')
      79  		return 0
      80  
      81  	if TTFont is None:
      82  		print ("fonttools is not present, skipping TTX diff.")
      83  		return fail_test (test, cli_args, "hash for expected and actual does not match.")
      84  
      85  	with io.StringIO () as fp:
      86  		try:
      87  			with TTFont (expected_file) as font:
      88  				font.saveXML (fp)
      89  		except Exception as e:
      90  			print (e)
      91  			return fail_test (test, cli_args, "ttx failed to parse the expected result")
      92  		expected_ttx = fp.getvalue ()
      93  
      94  	with io.StringIO () as fp:
      95  		try:
      96  			with TTFont (out_file) as font:
      97  				font.saveXML (fp)
      98  		except Exception as e:
      99  			print (e)
     100  			return fail_test (test, cli_args, "ttx failed to parse the actual result")
     101  		actual_ttx = fp.getvalue ()
     102  
     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)
     106  		sys.stdout.flush ()
     107  		return fail_test (test, cli_args, 'ttx for expected and actual does not match.')
     108  
     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?')
     111  
     112  
     113  def has_ots ():
     114  	if not ots_sanitize:
     115  		print ("OTS is not present, skipping all ots checks.")
     116  		return False
     117  	return True
     118  
     119  def check_ots (path):
     120  	ots_report, returncode = cmd ([ots_sanitize, path])
     121  	if returncode:
     122  		print ("OTS Failure: %s" % ots_report)
     123  		return False
     124  	return True
     125  
     126  args = sys.argv[1:]
     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:]
     130  
     131  if not len (args):
     132  	sys.exit ("No tests supplied.")
     133  
     134  has_ots = has_ots()
     135  
     136  process = subprocess.Popen ([hb_subset, '--batch'],
     137                              stdin=subprocess.PIPE,
     138                              stdout=subprocess.PIPE,
     139                              stderr=sys.stdout)
     140  
     141  fails = 0
     142  for path in args:
     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
     148  			# same between them.
     149  			fails += run_test (test, has_ots, False)
     150  			fails += run_test (test, has_ots, True)
     151  
     152  if fails != 0:
     153  	sys.exit ("%d test(s) failed." % fails)
     154  else:
     155  	print ("All tests passed.")