• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3from __future__ import print_function, division, absolute_import
4
5import sys, os, subprocess, hashlib, tempfile, shutil
6
7def cmd(command):
8	global process
9	process.stdin.write ((' '.join (command) + '\n').encode ("utf-8"))
10	process.stdin.flush ()
11	return process.stdout.readline().decode ("utf-8").strip ()
12
13args = sys.argv[1:]
14
15reference = False
16if len (args) and args[0] == "--reference":
17	reference = True
18	args = args[1:]
19
20if not args or args[0].find('hb-shape') == -1 or not os.path.exists (args[0]):
21	print ("""First argument does not seem to point to usable hb-shape.""")
22	sys.exit (1)
23hb_shape, args = args[0], args[1:]
24
25process = subprocess.Popen ([hb_shape, '--batch'],
26			    stdin=subprocess.PIPE,
27			    stdout=subprocess.PIPE,
28			    stderr=sys.stdout)
29
30passes = 0
31fails = 0
32skips = 0
33
34if not len (args):
35	args = ['-']
36
37for filename in args:
38	if not reference:
39		if filename == '-':
40			print ("Running tests from standard input")
41		else:
42			print ("Running tests in " + filename)
43
44	if filename == '-':
45		f = sys.stdin
46	else:
47		f = open (filename)
48
49	for line in f:
50		comment = False
51		if line.startswith ("#"):
52			comment = True
53			line = line[1:]
54
55			if line.startswith (' '):
56				if not reference:
57					print ("#%s" % line)
58				continue
59
60		line = line.strip ()
61		if not line:
62			continue
63
64		fontfile, options, unicodes, glyphs_expected = line.split (":")
65		if fontfile.startswith ('/') or fontfile.startswith ('"/'):
66			fontfile, expected_hash = fontfile.split('@')
67
68			try:
69				with open (fontfile, 'rb') as ff:
70					actual_hash = hashlib.sha1 (ff.read()).hexdigest ().strip ()
71					if actual_hash != expected_hash:
72						print ('different version of %s found; Expected hash %s, got %s; skipping.' %
73							   (fontfile, expected_hash, actual_hash))
74						skips += 1
75						continue
76			except:
77				print ('%s not found, skip.' % fontfile)
78				skips += 1
79				continue
80		else:
81			cwd = os.path.dirname(filename)
82			fontfile = os.path.normpath (os.path.join (cwd, fontfile))
83
84		extra_options = ["--shaper=ot"]
85		if glyphs_expected != '*':
86			extra_options.append("--verify")
87
88		if comment:
89			if not reference:
90				print ('# %s "%s" --unicodes %s' % (hb_shape, fontfile, unicodes))
91			continue
92
93		if not reference:
94			print ('%s "%s" %s %s --unicodes %s' %
95					 (hb_shape, fontfile, ' '.join(extra_options), options, unicodes))
96
97		# hack to support fonts with space on run-tests.py, after several other tries...
98		if ' ' in fontfile:
99			new_fontfile = os.path.join (tempfile.gettempdir (), 'tmpfile')
100			shutil.copyfile(fontfile, new_fontfile)
101			fontfile = new_fontfile
102
103		glyphs1 = cmd ([hb_shape, "--font-funcs=ft",
104			fontfile] + extra_options + ["--unicodes",
105			unicodes] + (options.split (' ') if options else []))
106
107		glyphs2 = cmd ([hb_shape, "--font-funcs=ot",
108			fontfile] + extra_options + ["--unicodes",
109			unicodes] + (options.split (' ') if options else []))
110
111		if glyphs1 != glyphs2 and glyphs_expected != '*':
112			print ("FT funcs: " + glyphs1) # file=sys.stderr
113			print ("OT funcs: " + glyphs2) # file=sys.stderr
114			fails += 1
115		else:
116			passes += 1
117
118		if reference:
119			print (":".join ([fontfile, options, unicodes, glyphs1]))
120			continue
121
122		if glyphs1.strip() != glyphs_expected and glyphs_expected != '*':
123			print ("Actual:   " + glyphs1) # file=sys.stderr
124			print ("Expected: " + glyphs_expected) # file=sys.stderr
125			fails += 1
126		else:
127			passes += 1
128
129if not reference:
130	print ("%d tests passed; %d failed; %d skipped." % (passes, fails, skips)) # file=sys.stderr
131	if not (fails + passes):
132		print ("No tests ran.")
133	elif not (fails + skips):
134		print ("All tests passed.")
135
136if fails:
137	sys.exit (1)
138elif passes:
139	sys.exit (0)
140else:
141	sys.exit (77)
142