88Tests for font-subset
99'''
1010
11+ import argparse
1112import filecmp
1213import os
1314import subprocess
1415import sys
1516from zipfile import ZipFile
1617
17- # Dictionary to map the platform name to the output directory
18- # of the font artifacts.
19- PLATFORM_2_PATH = {
20- 'darwin' : 'darwin-x64' ,
21- 'linux' : 'linux-x64' ,
22- 'linux2' : 'linux-x64' ,
23- 'cygwin' : 'windows-x64' ,
24- 'win' : 'windows-x64' ,
25- 'win32' : 'windows-x64' ,
26- }
27-
2818SCRIPT_DIR = os .path .dirname (os .path .abspath (__file__ ))
2919SRC_DIR = os .path .normpath (os .path .join (SCRIPT_DIR , '..' , '..' , '..' ))
3020MATERIAL_TTF = os .path .join (SCRIPT_DIR , 'fixtures' , 'MaterialIcons-Regular.ttf' )
3121VARIABLE_MATERIAL_TTF = os .path .join (SCRIPT_DIR , 'fixtures' , 'MaterialSymbols-Variable.ttf' )
32- IS_WINDOWS = sys .platform .startswith (('cygwin' , 'win' ))
33- EXE = '.exe' if IS_WINDOWS else ''
34- BAT = '.bat' if IS_WINDOWS else ''
35- FONT_SUBSET = os .path .join (SRC_DIR , 'out' , 'host_debug' , 'font-subset' + EXE )
36- FONT_SUBSET_ZIP = os .path .join (
37- SRC_DIR , 'out' , 'host_debug' , 'zip_archives' , PLATFORM_2_PATH .get (sys .platform , '' ),
38- 'font-subset.zip'
39- )
40- if not os .path .isfile (FONT_SUBSET ):
41- FONT_SUBSET = os .path .join (SRC_DIR , 'out' , 'host_debug_unopt' , 'font-subset' + EXE )
42- FONT_SUBSET_ZIP = os .path .join (
43- SRC_DIR , 'out' , 'host_debug_unopt' , 'zip_archives' , PLATFORM_2_PATH .get (sys .platform , '' ),
44- 'font-subset.zip'
45- )
46- if not os .path .isfile (FONT_SUBSET ):
47- raise Exception (
48- 'Could not locate font-subset%s in host_debug or host_debug_unopt - build before running this script.'
49- % EXE
50- )
5122
5223COMPARE_TESTS = (
5324 (True , '1.ttf' , MATERIAL_TTF , [r'57347' ]),
9162 ]),
9263)
9364
94- FAIL_TESTS = [
95- ([FONT_SUBSET , 'output.ttf' , 'does-not-exist.ttf' ], [
96- '1' ,
97- ]), # non-existent input font
98- ([FONT_SUBSET , 'output.ttf' , MATERIAL_TTF ], [
99- '0xFFFFFFFF' ,
100- ]), # Value too big.
101- ([FONT_SUBSET , 'output.ttf' , MATERIAL_TTF ], [
102- '-1' ,
103- ]), # invalid value
104- ([FONT_SUBSET , 'output.ttf' , MATERIAL_TTF ], [
105- 'foo' ,
106- ]), # no valid values
107- ([FONT_SUBSET , 'output.ttf' , MATERIAL_TTF ], [
108- '0xE003' ,
109- '0x12' ,
110- '0xE004' ,
111- ]), # codepoint not in font
112- ([FONT_SUBSET , 'non-existent-dir/output.ttf' , MATERIAL_TTF ], [
113- '0xE003' ,
114- ]), # dir doesn't exist
115- ([FONT_SUBSET , 'output.ttf' , MATERIAL_TTF ], [
116- ' ' ,
117- ]), # empty input
118- ([FONT_SUBSET , 'output.ttf' , MATERIAL_TTF ], []), # empty input
119- ([FONT_SUBSET , 'output.ttf' , MATERIAL_TTF ], ['' ]), # empty input
120- # repeat tests with variable input font
121- ([FONT_SUBSET , 'output.ttf' , VARIABLE_MATERIAL_TTF ], [
122- '0xFFFFFFFF' ,
123- ]), # Value too big.
124- ([FONT_SUBSET , 'output.ttf' , VARIABLE_MATERIAL_TTF ], [
125- '-1' ,
126- ]), # invalid value
127- ([FONT_SUBSET , 'output.ttf' , VARIABLE_MATERIAL_TTF ], [
128- 'foo' ,
129- ]), # no valid values
130- ([FONT_SUBSET , 'output.ttf' , VARIABLE_MATERIAL_TTF ], [
131- '0xE003' ,
132- '0x12' ,
133- '0xE004' ,
134- ]), # codepoint not in font
135- ([FONT_SUBSET , 'non-existent-dir/output.ttf' , VARIABLE_MATERIAL_TTF ], [
136- '0xE003' ,
137- ]), # dir doesn't exist
138- ([FONT_SUBSET , 'output.ttf' , VARIABLE_MATERIAL_TTF ], [
139- ' ' ,
140- ]), # empty input
141- ([FONT_SUBSET , 'output.ttf' , VARIABLE_MATERIAL_TTF ], []), # empty input
142- ([FONT_SUBSET , 'output.ttf' , VARIABLE_MATERIAL_TTF ], ['' ]), # empty input
143- ]
144-
145-
146- def RunCmd (cmd , codepoints , fail = False ):
65+
66+ def fail_tests (font_subset ):
67+ return [
68+ ([font_subset , 'output.ttf' , 'does-not-exist.ttf' ], [
69+ '1' ,
70+ ]), # non-existent input font
71+ ([font_subset , 'output.ttf' , MATERIAL_TTF ], [
72+ '0xFFFFFFFF' ,
73+ ]), # Value too big.
74+ ([font_subset , 'output.ttf' , MATERIAL_TTF ], [
75+ '-1' ,
76+ ]), # invalid value
77+ ([font_subset , 'output.ttf' , MATERIAL_TTF ], [
78+ 'foo' ,
79+ ]), # no valid values
80+ ([font_subset , 'output.ttf' , MATERIAL_TTF ], [
81+ '0xE003' ,
82+ '0x12' ,
83+ '0xE004' ,
84+ ]), # codepoint not in font
85+ ([font_subset , 'non-existent-dir/output.ttf' , MATERIAL_TTF ], [
86+ '0xE003' ,
87+ ]), # dir doesn't exist
88+ ([font_subset , 'output.ttf' , MATERIAL_TTF ], [
89+ ' ' ,
90+ ]), # empty input
91+ ([font_subset , 'output.ttf' , MATERIAL_TTF ], []), # empty input
92+ ([font_subset , 'output.ttf' , MATERIAL_TTF ], ['' ]), # empty input
93+ # repeat tests with variable input font
94+ ([font_subset , 'output.ttf' , VARIABLE_MATERIAL_TTF ], [
95+ '0xFFFFFFFF' ,
96+ ]), # Value too big.
97+ ([font_subset , 'output.ttf' , VARIABLE_MATERIAL_TTF ], [
98+ '-1' ,
99+ ]), # invalid value
100+ ([font_subset , 'output.ttf' , VARIABLE_MATERIAL_TTF ], [
101+ 'foo' ,
102+ ]), # no valid values
103+ ([font_subset , 'output.ttf' , VARIABLE_MATERIAL_TTF ], [
104+ '0xE003' ,
105+ '0x12' ,
106+ '0xE004' ,
107+ ]), # codepoint not in font
108+ ([font_subset , 'non-existent-dir/output.ttf' , VARIABLE_MATERIAL_TTF ], [
109+ '0xE003' ,
110+ ]), # dir doesn't exist
111+ ([font_subset , 'output.ttf' , VARIABLE_MATERIAL_TTF ], [
112+ ' ' ,
113+ ]), # empty input
114+ ([font_subset , 'output.ttf' , VARIABLE_MATERIAL_TTF ], []), # empty input
115+ ([font_subset , 'output.ttf' , VARIABLE_MATERIAL_TTF ], ['' ]), # empty input
116+ ]
117+
118+
119+ def run_cmd (cmd , codepoints , fail = False ):
147120 print ('Running command:' )
148121 print (' %s' % ' ' .join (cmd ))
149122 print ('STDIN: "%s"' % ' ' .join (codepoints ))
@@ -169,34 +142,66 @@ def RunCmd(cmd, codepoints, fail=False):
169142 return p .returncode
170143
171144
172- def TestZip ( ):
173- with ZipFile (FONT_SUBSET_ZIP , 'r' ) as zip :
145+ def test_zip ( font_subset_zip , exe ):
146+ with ZipFile (font_subset_zip , 'r' ) as zip :
174147 files = zip .namelist ()
175- if 'font-subset%s' % EXE not in files :
176- print ('expected %s to contain font-subset%s' % (files , EXE ))
148+ if 'font-subset%s' % exe not in files :
149+ print ('expected %s to contain font-subset%s' % (files , exe ))
177150 return 1
178151 return 0
179152
180153
154+ # Maps the platform name to the output directory of the font artifacts.
155+ def platform_to_path (os , cpu ):
156+ d = {
157+ 'darwin' : 'darwin-' ,
158+ 'linux' : 'linux-' ,
159+ 'linux2' : 'linux-' ,
160+ 'cygwin' : 'windows-' ,
161+ 'win' : 'windows-' ,
162+ 'win32' : 'windows-' ,
163+ }
164+ return d [os ] + cpu
165+
166+
181167def main ():
182- print ('Using font subset binary at %s (%s)' % (FONT_SUBSET , FONT_SUBSET_ZIP ))
168+ parser = argparse .ArgumentParser (description = 'Runs font-subset tests.' )
169+ parser .add_argument ('--variant' , type = str , required = True )
170+ parser .add_argument ('--target-cpu' , type = str , default = 'x64' )
171+ args = parser .parse_args ()
172+ variant = args .variant
173+
174+ is_windows = sys .platform .startswith (('cygwin' , 'win' ))
175+ exe = '.exe' if is_windows else ''
176+ font_subset = os .path .join (SRC_DIR , 'out' , variant , 'font-subset' + exe )
177+ font_subset_zip = os .path .join (
178+ SRC_DIR , 'out' , variant , 'zip_archives' , platform_to_path (sys .platform , args .target_cpu ),
179+ 'font-subset.zip'
180+ )
181+ if not os .path .isfile (font_subset ):
182+ raise Exception (
183+ 'Could not locate font-subset%s in host_debug or host_debug_unopt - build before running this script.'
184+ % exe
185+ )
186+
187+ print ('Using font subset binary at %s (%s)' % (font_subset , font_subset_zip ))
183188 failures = 0
184189
185- failures += TestZip ( )
190+ failures += test_zip ( font_subset_zip , exe )
186191
187192 for should_pass , golden_font , input_font , codepoints in COMPARE_TESTS :
188193 gen_ttf = os .path .join (SCRIPT_DIR , 'gen' , golden_font )
189194 golden_ttf = os .path .join (SCRIPT_DIR , 'fixtures' , golden_font )
190- cmd = [FONT_SUBSET , gen_ttf , input_font ]
191- RunCmd (cmd , codepoints )
195+ cmd = [font_subset , gen_ttf , input_font ]
196+ run_cmd (cmd , codepoints )
192197 cmp = filecmp .cmp (gen_ttf , golden_ttf , shallow = False )
193198 if (should_pass and not cmp ) or (not should_pass and cmp ):
194199 print ('Test case %s failed.' % cmd )
195200 failures += 1
196201
197202 with open (os .devnull , 'w' ) as devnull :
198- for cmd , codepoints in FAIL_TESTS :
199- if RunCmd (cmd , codepoints , fail = True ) == 0 :
203+ for cmd , codepoints in fail_tests ( font_subset ) :
204+ if run_cmd (cmd , codepoints , fail = True ) == 0 :
200205 failures += 1
201206
202207 if failures > 0 :
0 commit comments