[tests] Use TTFont of fonttools to avoid temp files

This commit is contained in:
Ebrahim Byagowi 2020-07-27 09:03:35 +04:30
parent 08fef15818
commit 322426f8c8
1 changed files with 20 additions and 36 deletions

View File

@ -10,10 +10,12 @@ import subprocess
import sys import sys
import tempfile import tempfile
import shutil import shutil
import io
from subset_test_suite import SubsetTestSuite from subset_test_suite import SubsetTestSuite
try: try:
from fontTools import ttx from fontTools.ttLib import TTFont
except ImportError: except ImportError:
print ("fonttools is not present, skipping test.") print ("fonttools is not present, skipping test.")
sys.exit (77) sys.exit (77)
@ -28,10 +30,6 @@ def cmd (command):
print (stderrdata, end="", file=sys.stderr) print (stderrdata, end="", file=sys.stderr)
return stdoutdata, p.returncode return stdoutdata, p.returncode
def read_binary (file_path):
with open (file_path, 'rb') as f:
return f.read ()
def fail_test (test, cli_args, message): def fail_test (test, cli_args, message):
print ('ERROR: %s' % message) print ('ERROR: %s' % message)
print ('Test State:') print ('Test State:')
@ -50,7 +48,7 @@ def run_test (test, should_check_ots):
"--output-file=" + out_file, "--output-file=" + out_file,
"--unicodes=%s" % test.unicodes (), "--unicodes=%s" % test.unicodes (),
"--drop-tables+=DSIG,GPOS,GSUB,GDEF", "--drop-tables+=DSIG,GPOS,GSUB,GDEF",
"--drop-tables-=sbix"] "--drop-tables-=sbix"]
cli_args.extend (test.get_profile_flags ()) cli_args.extend (test.get_profile_flags ())
print (' '.join (cli_args)) print (' '.join (cli_args))
_, return_code = cmd (cli_args) _, return_code = cmd (cli_args)
@ -58,36 +56,26 @@ def run_test (test, should_check_ots):
if return_code: if return_code:
return fail_test (test, cli_args, "%s returned %d" % (' '.join (cli_args), return_code)) return fail_test (test, cli_args, "%s returned %d" % (' '.join (cli_args), return_code))
expected_ttx = tempfile.mktemp () expected_ttx = io.StringIO ()
return_code = run_ttx (os.path.join (test_suite.get_output_directory (), try:
test.get_font_name ()), with TTFont (os.path.join (test_suite.get_output_directory (), test.get_font_name ())) as font:
expected_ttx) font.saveXML (expected_ttx)
if return_code: except Exception as e:
if os.path.exists (expected_ttx): os.remove (expected_ttx) print (e)
return fail_test (test, cli_args, "ttx (expected) returned %d" % (return_code)) return fail_test (test, cli_args, "ttx (expected) returned %d" % (return_code))
actual_ttx = tempfile.mktemp () actual_ttx = io.StringIO ()
return_code = run_ttx (out_file, actual_ttx) try:
if return_code: with TTFont (out_file) as font:
if os.path.exists (expected_ttx): os.remove (expected_ttx) font.saveXML (actual_ttx)
if os.path.exists (actual_ttx): os.remove (actual_ttx) except Exception as e:
print (e)
return fail_test (test, cli_args, "ttx (actual) returned %d" % (return_code)) return fail_test (test, cli_args, "ttx (actual) returned %d" % (return_code))
with open (expected_ttx, encoding='utf-8') as f: expected_ttx_text = strip_check_sum (expected_ttx.getvalue ())
expected_ttx_text = f.read () expected_ttx.close ()
with open (actual_ttx, encoding='utf-8') as f: actual_ttx_text = strip_check_sum (actual_ttx.getvalue ())
actual_ttx_text = f.read () actual_ttx.close ()
# cleanup
try:
os.remove (expected_ttx)
os.remove (actual_ttx)
except:
pass
print ("stripping checksums.")
expected_ttx_text = strip_check_sum (expected_ttx_text)
actual_ttx_text = strip_check_sum (actual_ttx_text)
if not actual_ttx_text == expected_ttx_text: if not actual_ttx_text == expected_ttx_text:
for line in unified_diff (expected_ttx_text.splitlines (1), actual_ttx_text.splitlines (1)): for line in unified_diff (expected_ttx_text.splitlines (1), actual_ttx_text.splitlines (1)):
@ -102,10 +90,6 @@ def run_test (test, should_check_ots):
return 0 return 0
def run_ttx (font_path, ttx_output_path):
print ("fonttools ttx %s" % font_path)
ttx.main (args=['-q', '-o', ttx_output_path, font_path])
def strip_check_sum (ttx_string): def strip_check_sum (ttx_string):
return re.sub ('checkSumAdjustment value=["]0x([0-9a-fA-F])+["]', return re.sub ('checkSumAdjustment value=["]0x([0-9a-fA-F])+["]',
'checkSumAdjustment value="0x00000000"', 'checkSumAdjustment value="0x00000000"',