[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 tempfile
import shutil
import io
from subset_test_suite import SubsetTestSuite
try:
from fontTools import ttx
from fontTools.ttLib import TTFont
except ImportError:
print ("fonttools is not present, skipping test.")
sys.exit (77)
@ -28,10 +30,6 @@ def cmd (command):
print (stderrdata, end="", file=sys.stderr)
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):
print ('ERROR: %s' % message)
print ('Test State:')
@ -58,36 +56,26 @@ def run_test (test, should_check_ots):
if return_code:
return fail_test (test, cli_args, "%s returned %d" % (' '.join (cli_args), return_code))
expected_ttx = tempfile.mktemp ()
return_code = run_ttx (os.path.join (test_suite.get_output_directory (),
test.get_font_name ()),
expected_ttx)
if return_code:
if os.path.exists (expected_ttx): os.remove (expected_ttx)
expected_ttx = io.StringIO ()
try:
with TTFont (os.path.join (test_suite.get_output_directory (), test.get_font_name ())) as font:
font.saveXML (expected_ttx)
except Exception as e:
print (e)
return fail_test (test, cli_args, "ttx (expected) returned %d" % (return_code))
actual_ttx = tempfile.mktemp ()
return_code = run_ttx (out_file, actual_ttx)
if return_code:
if os.path.exists (expected_ttx): os.remove (expected_ttx)
if os.path.exists (actual_ttx): os.remove (actual_ttx)
actual_ttx = io.StringIO ()
try:
with TTFont (out_file) as font:
font.saveXML (actual_ttx)
except Exception as e:
print (e)
return fail_test (test, cli_args, "ttx (actual) returned %d" % (return_code))
with open (expected_ttx, encoding='utf-8') as f:
expected_ttx_text = f.read ()
with open (actual_ttx, encoding='utf-8') as f:
actual_ttx_text = f.read ()
# 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)
expected_ttx_text = strip_check_sum (expected_ttx.getvalue ())
expected_ttx.close ()
actual_ttx_text = strip_check_sum (actual_ttx.getvalue ())
actual_ttx.close ()
if not actual_ttx_text == expected_ttx_text:
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
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):
return re.sub ('checkSumAdjustment value=["]0x([0-9a-fA-F])+["]',
'checkSumAdjustment value="0x00000000"',