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