harfbuzz/test/subset/run-tests.py

156 lines
4.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2018-01-27 01:57:42 +01:00
2019-05-28 11:18:39 +02:00
# Runs a subsetting test suite. Compares the results of subsetting via harfbuzz
2018-01-27 01:57:42 +01:00
# to subsetting via fonttools.
from difflib import unified_diff
2018-01-27 01:57:42 +01:00
import os
import re
2018-01-27 01:57:42 +01:00
import subprocess
import sys
import tempfile
import shutil
import io
2018-01-27 01:57:42 +01:00
from subset_test_suite import SubsetTestSuite
try:
from fontTools.ttLib import TTFont
except ImportError:
TTFont = None
2018-01-27 01:57:42 +01:00
ots_sanitize = shutil.which ("ots-sanitize")
def subset_cmd (command):
global hb_subset, process
print (hb_subset + ' ' + " ".join(command))
process.stdin.write ((';'.join (command) + '\n').encode ("utf-8"))
process.stdin.flush ()
return process.stdout.readline().decode ("utf-8").strip ()
def cmd (command):
2018-01-27 01:57:42 +01:00
p = subprocess.Popen (
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
(stdoutdata, stderrdata) = p.communicate ()
print (stderrdata, end="", file=sys.stderr)
return stdoutdata, p.returncode
2018-01-27 01:57:42 +01:00
2019-05-28 11:18:39 +02:00
def fail_test (test, cli_args, message):
print ('ERROR: %s' % message)
print ('Test State:')
print (' test.font_path %s' % os.path.abspath (test.font_path))
print (' test.profile_path %s' % os.path.abspath (test.profile_path))
print (' test.unicodes %s' % test.unicodes ())
2020-07-27 06:49:29 +02:00
expected_file = os.path.join (test_suite.get_output_directory (),
test.get_font_name ())
print (' expected_file %s' % os.path.abspath (expected_file))
return 1
2018-01-27 01:57:42 +01:00
def run_test (test, should_check_ots, preprocess):
2019-05-28 11:18:39 +02:00
out_file = os.path.join (tempfile.mkdtemp (), test.get_font_name () + '-subset' + test.get_font_extension ())
cli_args = ["--font-file=" + test.font_path,
"--output-file=" + out_file,
"--unicodes=%s" % test.unicodes (),
"--drop-tables+=DSIG",
2020-07-27 06:49:29 +02:00
"--drop-tables-=sbix"]
if preprocess:
cli_args.extend(["--preprocess-face",])
cli_args.extend (test.get_profile_flags ())
if test.get_instance_flags ():
cli_args.extend (["--instance=%s" % ','.join(test.get_instance_flags ())])
ret = subset_cmd (cli_args)
2018-01-27 01:57:42 +01:00
if ret != "success":
return fail_test (test, cli_args, "%s failed" % ' '.join (cli_args))
expected_file = os.path.join (test_suite.get_output_directory (), test.get_font_name ())
with open (expected_file, "rb") as fp:
expected_contents = fp.read()
with open (out_file, "rb") as fp:
actual_contents = fp.read()
if expected_contents == actual_contents:
if should_check_ots:
print ("Checking output with ots-sanitize.")
if not check_ots (out_file):
return fail_test (test, cli_args, 'ots for subsetted file fails.')
return 0
if TTFont is None:
print ("fonttools is not present, skipping TTX diff.")
return fail_test (test, cli_args, "hash for expected and actual does not match.")
with io.StringIO () as fp:
try:
with TTFont (expected_file) as font:
font.saveXML (fp)
except Exception as e:
print (e)
return fail_test (test, cli_args, "ttx failed to parse the expected result")
expected_ttx = fp.getvalue ()
with io.StringIO () as fp:
try:
with TTFont (out_file) as font:
font.saveXML (fp)
except Exception as e:
print (e)
return fail_test (test, cli_args, "ttx failed to parse the actual result")
actual_ttx = fp.getvalue ()
if actual_ttx != expected_ttx:
for line in unified_diff (expected_ttx.splitlines (1), actual_ttx.splitlines (1)):
sys.stdout.write (line)
sys.stdout.flush ()
2019-05-28 11:18:39 +02:00
return fail_test (test, cli_args, 'ttx for expected and actual does not match.')
2018-01-27 01:57:42 +01:00
return fail_test (test, cli_args, 'hash for expected and actual does not match, '
'but the ttx matches. Expected file needs to be updated?')
2018-01-27 01:57:42 +01:00
def has_ots ():
if not ots_sanitize:
2020-07-27 06:49:29 +02:00
print ("OTS is not present, skipping all ots checks.")
return False
return True
def check_ots (path):
ots_report, returncode = cmd ([ots_sanitize, path])
if returncode:
print ("OTS Failure: %s" % ots_report)
return False
return True
2018-01-27 01:57:42 +01:00
args = sys.argv[1:]
2019-05-28 11:18:39 +02:00
if not args or sys.argv[1].find ('hb-subset') == -1 or not os.path.exists (sys.argv[1]):
2020-05-28 20:21:29 +02:00
sys.exit ("First argument does not seem to point to usable hb-subset.")
2018-01-27 01:57:42 +01:00
hb_subset, args = args[0], args[1:]
if not len (args):
2020-05-28 20:21:29 +02:00
sys.exit ("No tests supplied.")
2018-01-27 01:57:42 +01:00
has_ots = has_ots()
process = subprocess.Popen ([hb_subset, '--batch'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=sys.stdout)
2018-01-27 01:57:42 +01:00
fails = 0
for path in args:
2020-05-28 21:41:19 +02:00
with open (path, mode="r", encoding="utf-8") as f:
2018-01-27 01:57:42 +01:00
print ("Running tests in " + path)
2019-05-28 11:18:39 +02:00
test_suite = SubsetTestSuite (path, f.read ())
for test in test_suite.tests ():
# Tests are run with and without preprocessing, results should be the
# same between them.
fails += run_test (test, has_ots, False)
fails += run_test (test, has_ots, True)
2018-01-27 01:57:42 +01:00
if fails != 0:
2020-05-28 20:21:29 +02:00
sys.exit ("%d test(s) failed." % fails)
2018-01-27 01:57:42 +01:00
else:
print ("All tests passed.")