blob: 03f03df6c39c02764dd6e65a4ae7b22d64cfd443 [file] [log] [blame]
Ebrahim Byagowi8d199072020-02-19 14:56:55 +03301#!/usr/bin/env python3
Ebrahim Byagowicab2c2c2018-03-29 12:48:47 +04302
Ebrahim Byagowiba810ce2020-04-05 22:51:58 +04303import sys, os, subprocess, hashlib
ebraminio5f061d22017-12-07 11:22:55 +03304
Behdad Esfahbodf3acb972021-08-10 11:05:40 -06005def shape_cmd(command):
6 global hb_shape, process
7 print (hb_shape + ' ' + " ".join(command))
8 process.stdin.write ((';'.join (command) + '\n').encode ("utf-8"))
Ebrahim Byagowic4b6bad2020-07-02 18:07:01 +04309 process.stdin.flush ()
10 return process.stdout.readline().decode ("utf-8").strip ()
ebraminio5f061d22017-12-07 11:22:55 +033011
Behdad Esfahbod504f9132018-01-09 23:15:54 +010012args = sys.argv[1:]
Behdad Esfahbodf9289312018-10-31 18:25:05 -070013
Khaled Hosnyf76ffa82022-03-24 06:23:22 +020014have_freetype = int(os.getenv ('HAVE_FREETYPE', 1))
15have_coretext = int(os.getenv ('HAVE_CORETEXT', 0))
16have_directwrite = int(os.getenv ('HAVE_DIRECTWRITE', 0))
17have_uniscribe = int(os.getenv ('HAVE_UNISCRIBE', 0))
Ebrahim Byagowi43e3ab02020-07-02 17:34:24 +043018
Behdad Esfahbodf9289312018-10-31 18:25:05 -070019if not args or args[0].find('hb-shape') == -1 or not os.path.exists (args[0]):
Ebrahim Byagowi7554f612020-05-28 22:51:29 +043020 sys.exit ("""First argument does not seem to point to usable hb-shape.""")
Behdad Esfahbod504f9132018-01-09 23:15:54 +010021hb_shape, args = args[0], args[1:]
ebraminio5f061d22017-12-07 11:22:55 +033022
Ebrahim Byagowic4b6bad2020-07-02 18:07:01 +043023process = subprocess.Popen ([hb_shape, '--batch'],
Behdad Esfahbod422debb2018-10-30 00:51:43 -070024 stdin=subprocess.PIPE,
25 stdout=subprocess.PIPE,
Ebrahim Byagowic4b6bad2020-07-02 18:07:01 +043026 stderr=sys.stdout)
Behdad Esfahbod422debb2018-10-30 00:51:43 -070027
Behdad Esfahbodea9512e2018-11-24 15:49:33 -050028passes = 0
Ebrahim Byagowi4e3cf912018-01-01 11:17:51 +033029fails = 0
Ebrahim Byagowi341851e2018-11-23 15:40:05 +033030skips = 0
Ebrahim Byagowi4e3cf912018-01-01 11:17:51 +033031
ebraminio5f061d22017-12-07 11:22:55 +033032if not len (args):
Behdad Esfahbod44c65ee2018-01-09 21:58:57 +010033 args = ['-']
ebraminio5f061d22017-12-07 11:22:55 +033034
Behdad Esfahbod44c65ee2018-01-09 21:58:57 +010035for filename in args:
Khaled Hosny1fd3a262021-08-01 19:38:39 +020036 if filename == '-':
37 print ("Running tests from standard input")
38 else:
39 print ("Running tests in " + filename)
ebraminio5f061d22017-12-07 11:22:55 +033040
Behdad Esfahbod44c65ee2018-01-09 21:58:57 +010041 if filename == '-':
42 f = sys.stdin
ebraminio5f061d22017-12-07 11:22:55 +033043 else:
Ebrahim Byagowi74fdd342020-03-14 20:03:14 +033044 f = open (filename, encoding='utf8')
ebraminio5f061d22017-12-07 11:22:55 +033045
46 for line in f:
Behdad Esfahbodee3a3e12018-11-24 15:37:01 -050047 comment = False
48 if line.startswith ("#"):
49 comment = True
50 line = line[1:]
51
52 if line.startswith (' '):
Khaled Hosny1fd3a262021-08-01 19:38:39 +020053 print ("#%s" % line)
Behdad Esfahbodee3a3e12018-11-24 15:37:01 -050054 continue
55
56 line = line.strip ()
57 if not line:
58 continue
59
Behdad Esfahbodf3acb972021-08-10 11:05:40 -060060 fontfile, options, unicodes, glyphs_expected = line.split (';')
Ebrahim Byagowi43e3ab02020-07-02 17:34:24 +043061 options = options.split ()
Ebrahim Byagowi341851e2018-11-23 15:40:05 +033062 if fontfile.startswith ('/') or fontfile.startswith ('"/'):
Ebrahim Byagowi03564fd2020-03-14 20:09:00 +033063 if os.name == 'nt': # Skip on Windows
Ebrahim Byagowi74fdd342020-03-14 20:03:14 +033064 continue
65
Behdad Esfahbodd8ea5522021-02-18 12:07:46 -070066 fontfile, expected_hash = (fontfile.split('@') + [''])[:2]
Ebrahim Byagowi341851e2018-11-23 15:40:05 +033067
68 try:
69 with open (fontfile, 'rb') as ff:
Behdad Esfahbodd8ea5522021-02-18 12:07:46 -070070 if expected_hash:
71 actual_hash = hashlib.sha1 (ff.read()).hexdigest ().strip ()
72 if actual_hash != expected_hash:
73 print ('different version of %s found; Expected hash %s, got %s; skipping.' %
74 (fontfile, expected_hash, actual_hash))
75 skips += 1
76 continue
77 except IOError:
Behdad Esfahbodea9512e2018-11-24 15:49:33 -050078 print ('%s not found, skip.' % fontfile)
79 skips += 1
Ebrahim Byagowi341851e2018-11-23 15:40:05 +033080 continue
81 else:
82 cwd = os.path.dirname(filename)
83 fontfile = os.path.normpath (os.path.join (cwd, fontfile))
ebraminio20e69c92017-12-07 12:24:12 +033084
Behdad Esfahbod0abce582018-10-04 16:23:42 +020085 extra_options = ["--shaper=ot"]
Behdad Esfahbod55468ca2018-10-04 12:13:55 +020086 if glyphs_expected != '*':
87 extra_options.append("--verify")
Behdad Esfahbod33145a42022-05-31 04:59:07 -060088 extra_options.append("--unsafe-to-concat")
Behdad Esfahbod55468ca2018-10-04 12:13:55 +020089
Behdad Esfahbodee3a3e12018-11-24 15:37:01 -050090 if comment:
Khaled Hosny1fd3a262021-08-01 19:38:39 +020091 print ('# %s "%s" --unicodes %s' % (hb_shape, fontfile, unicodes))
ebraminio20e69c92017-12-07 12:24:12 +033092 continue
ebraminio5f061d22017-12-07 11:22:55 +033093
Ebrahim Byagowi2013bab2020-07-06 11:57:45 +043094 if "--font-funcs=ft" in options and not have_freetype:
Ebrahim Byagowi43e3ab02020-07-02 17:34:24 +043095 skips += 1
96 continue
ebraminio5f061d22017-12-07 11:22:55 +033097
Khaled Hosnyf76ffa82022-03-24 06:23:22 +020098 if "--shaper=coretext" in options and not have_coretext:
99 skips += 1
100 continue
101
102 if "--shaper=directwrite" in options and not have_directwrite:
103 skips += 1
104 continue
105
106 if "--shaper=uniscribe" in options and not have_uniscribe:
107 skips += 1
108 continue
109
Ebrahim Byagowi2013bab2020-07-06 11:57:45 +0430110 if "--font-funcs=ot" in options or not have_freetype:
Behdad Esfahbodf3acb972021-08-10 11:05:40 -0600111 glyphs1 = shape_cmd ([fontfile, "--font-funcs=ot"] + extra_options + ["--unicodes", unicodes] + options)
Behdad Esfahbodea9512e2018-11-24 15:49:33 -0500112 else:
Behdad Esfahbodf3acb972021-08-10 11:05:40 -0600113 glyphs1 = shape_cmd ([fontfile, "--font-funcs=ft"] + extra_options + ["--unicodes", unicodes] + options)
114 glyphs2 = shape_cmd ([fontfile, "--font-funcs=ot"] + extra_options + ["--unicodes", unicodes] + options)
Ebrahim Byagowi43e3ab02020-07-02 17:34:24 +0430115
116 if glyphs1 != glyphs2 and glyphs_expected != '*':
117 print ("FT funcs: " + glyphs1, file=sys.stderr)
118 print ("OT funcs: " + glyphs2, file=sys.stderr)
119 fails += 1
120 else:
121 passes += 1
ebraminio5f061d22017-12-07 11:22:55 +0330122
Ebrahim Byagowic4b6bad2020-07-02 18:07:01 +0430123 if glyphs1.strip() != glyphs_expected and glyphs_expected != '*':
Behdad Esfahbodfc9e6ae2022-07-30 12:02:36 -0600124 print ("hb-shape", fontfile, "--unicodes", unicodes, file=sys.stderr)
Ebrahim Byagowic4b6bad2020-07-02 18:07:01 +0430125 print ("Actual: " + glyphs1, file=sys.stderr)
Ebrahim Byagowicd5580e2020-05-28 23:43:55 +0430126 print ("Expected: " + glyphs_expected, file=sys.stderr)
Behdad Esfahbodea9512e2018-11-24 15:49:33 -0500127 fails += 1
128 else:
129 passes += 1
ebraminio5f061d22017-12-07 11:22:55 +0330130
Khaled Hosny1fd3a262021-08-01 19:38:39 +0200131print ("%d tests passed; %d failed; %d skipped." % (passes, fails, skips), file=sys.stderr)
132if not (fails + passes):
133 print ("No tests ran.")
134elif not (fails + skips):
135 print ("All tests passed.")
Behdad Esfahbodea9512e2018-11-24 15:49:33 -0500136
137if fails:
138 sys.exit (1)
139elif passes:
140 sys.exit (0)
141else:
142 sys.exit (77)