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