harfbuzz/test/shaping/run-tests.py

142 lines
3.6 KiB
Python
Raw Normal View History

2017-12-07 07:52:55 +00:00
#!/usr/bin/env python
from __future__ import print_function, division, absolute_import
2018-11-23 12:10:05 +00:00
import sys, os, subprocess, hashlib, tempfile, shutil
2017-12-07 07:52:55 +00:00
def cmd(command):
global process
2018-10-30 08:08:34 +00:00
process.stdin.write ((' '.join (command) + '\n').encode ("utf-8"))
process.stdin.flush ()
return process.stdout.readline().decode ("utf-8").strip ()
2017-12-07 07:52:55 +00:00
args = sys.argv[1:]
2018-11-01 01:25:05 +00:00
reference = False
if len (args) and args[0] == "--reference":
reference = True
args = args[1:]
if not args or args[0].find('hb-shape') == -1 or not os.path.exists (args[0]):
print ("""First argument does not seem to point to usable hb-shape.""")
sys.exit (1)
hb_shape, args = args[0], args[1:]
2017-12-07 07:52:55 +00:00
process = subprocess.Popen ([hb_shape, '--batch'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=sys.stdout)
2018-11-24 20:49:33 +00:00
passes = 0
fails = 0
2018-11-23 12:10:05 +00:00
skips = 0
2017-12-07 07:52:55 +00:00
if not len (args):
args = ['-']
2017-12-07 07:52:55 +00:00
for filename in args:
2017-12-07 07:52:55 +00:00
if not reference:
if filename == '-':
2017-12-07 07:52:55 +00:00
print ("Running tests from standard input")
else:
print ("Running tests in " + filename)
if filename == '-':
f = sys.stdin
2017-12-07 07:52:55 +00:00
else:
f = open (filename)
2017-12-07 07:52:55 +00:00
for line in f:
comment = False
if line.startswith ("#"):
comment = True
line = line[1:]
if line.startswith (' '):
if not reference:
print ("#%s" % line)
continue
line = line.strip ()
if not line:
continue
fontfile, options, unicodes, glyphs_expected = line.split (":")
2018-11-23 12:10:05 +00:00
if fontfile.startswith ('/') or fontfile.startswith ('"/'):
fontfile, expected_hash = fontfile.split('@')
try:
with open (fontfile, 'rb') as ff:
actual_hash = hashlib.sha1 (ff.read()).hexdigest ().strip ()
if actual_hash != expected_hash:
2018-11-24 20:49:33 +00:00
print ('different version of %s found; Expected hash %s, got %s; skipping.' %
2018-11-23 12:10:05 +00:00
(fontfile, expected_hash, actual_hash))
2018-11-24 20:49:33 +00:00
skips += 1
2018-11-23 12:10:05 +00:00
continue
except:
2018-11-24 20:49:33 +00:00
print ('%s not found, skip.' % fontfile)
skips += 1
2018-11-23 12:10:05 +00:00
continue
else:
cwd = os.path.dirname(filename)
fontfile = os.path.normpath (os.path.join (cwd, fontfile))
extra_options = ["--shaper=ot"]
if glyphs_expected != '*':
extra_options.append("--verify")
if comment:
if not reference:
2018-11-25 03:01:06 +00:00
print ('# %s "%s" --unicodes %s' % (hb_shape, fontfile, unicodes))
2017-12-07 07:52:55 +00:00
continue
if not reference:
2018-11-25 03:01:06 +00:00
print ('%s "%s" %s %s --unicodes %s' %
(hb_shape, fontfile, ' '.join(extra_options), options, unicodes))
2017-12-07 07:52:55 +00:00
2018-11-23 12:10:05 +00:00
# hack to support fonts with space on run-tests.py, after several other tries...
if ' ' in fontfile:
new_fontfile = os.path.join (tempfile.gettempdir (), 'tmpfile')
shutil.copyfile(fontfile, new_fontfile)
fontfile = new_fontfile
glyphs1 = cmd ([hb_shape, "--font-funcs=ft",
fontfile] + extra_options + ["--unicodes",
unicodes] + (options.split (' ') if options else []))
2017-12-07 07:52:55 +00:00
glyphs2 = cmd ([hb_shape, "--font-funcs=ot",
fontfile] + extra_options + ["--unicodes",
unicodes] + (options.split (' ') if options else []))
2017-12-07 07:52:55 +00:00
2018-10-04 14:36:42 +00:00
if glyphs1 != glyphs2 and glyphs_expected != '*':
print ("FT funcs: " + glyphs1) # file=sys.stderr
print ("OT funcs: " + glyphs2) # file=sys.stderr
2018-11-24 20:49:33 +00:00
fails += 1
else:
passes += 1
2017-12-07 07:52:55 +00:00
if reference:
print (":".join ([fontfile, options, unicodes, glyphs1]))
continue
if glyphs1.strip() != glyphs_expected and glyphs_expected != '*':
print ("Actual: " + glyphs1) # file=sys.stderr
print ("Expected: " + glyphs_expected) # file=sys.stderr
2018-11-24 20:49:33 +00:00
fails += 1
else:
passes += 1
2017-12-07 07:52:55 +00:00
2018-11-24 20:49:33 +00:00
if not reference:
print ("%d tests passed; %d failed; %d skipped." % (passes, fails, skips)) # file=sys.stderr
if not (fails + passes):
print ("No tests ran.")
elif not (fails + skips):
2017-12-07 07:52:55 +00:00
print ("All tests passed.")
2018-11-24 20:49:33 +00:00
if fails:
sys.exit (1)
elif passes:
sys.exit (0)
else:
sys.exit (77)