Imported Upstream version 3.4.0
[platform/upstream/harfbuzz.git] / test / subset / run-tests.py
1 #!/usr/bin/env python3
2
3 # Runs a subsetting test suite. Compares the results of subsetting via harfbuzz
4 # to subsetting via fonttools.
5
6 from difflib import unified_diff
7 import os
8 import re
9 import subprocess
10 import sys
11 import tempfile
12 import shutil
13 import io
14
15 from subset_test_suite import SubsetTestSuite
16
17 try:
18         from fontTools.ttLib import TTFont
19 except ImportError:
20     TTFont = None
21
22 ots_sanitize = shutil.which ("ots-sanitize")
23
24 def subset_cmd (command):
25         global hb_subset, process
26         print (hb_subset + ' ' + " ".join(command))
27         process.stdin.write ((';'.join (command) + '\n').encode ("utf-8"))
28         process.stdin.flush ()
29         return process.stdout.readline().decode ("utf-8").strip ()
30
31 def cmd (command):
32         p = subprocess.Popen (
33                 command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
34                 universal_newlines=True)
35         (stdoutdata, stderrdata) = p.communicate ()
36         print (stderrdata, end="", file=sys.stderr)
37         return stdoutdata, p.returncode
38
39 def fail_test (test, cli_args, message):
40         print ('ERROR: %s' % message)
41         print ('Test State:')
42         print ('  test.font_path    %s' % os.path.abspath (test.font_path))
43         print ('  test.profile_path %s' % os.path.abspath (test.profile_path))
44         print ('  test.unicodes     %s' % test.unicodes ())
45         expected_file = os.path.join (test_suite.get_output_directory (),
46                                       test.get_font_name ())
47         print ('  expected_file     %s' % os.path.abspath (expected_file))
48         return 1
49
50 def run_test (test, should_check_ots):
51         out_file = os.path.join (tempfile.mkdtemp (), test.get_font_name () + '-subset' + test.get_font_extension ())
52         cli_args = ["--font-file=" + test.font_path,
53                     "--output-file=" + out_file,
54                     "--unicodes=%s" % test.unicodes (),
55                     "--drop-tables+=DSIG",
56                     "--drop-tables-=sbix"]
57         cli_args.extend (test.get_profile_flags ())
58         ret = subset_cmd (cli_args)
59
60         if ret != "success":
61                 return fail_test (test, cli_args, "%s failed" % ' '.join (cli_args))
62
63         expected_file = os.path.join (test_suite.get_output_directory (), test.get_font_name ())
64         with open (expected_file, "rb") as fp:
65                 expected_contents = fp.read()
66         with open (out_file, "rb") as fp:
67                 actual_contents = fp.read()
68
69         if expected_contents == actual_contents:
70                 if should_check_ots:
71                         print ("Checking output with ots-sanitize.")
72                         if not check_ots (out_file):
73                                 return fail_test (test, cli_args, 'ots for subsetted file fails.')
74                 return 0
75
76         if TTFont is None:
77                 print ("fonttools is not present, skipping TTX diff.")
78                 return fail_test (test, cli_args, "hash for expected and actual does not match.")
79
80         with io.StringIO () as fp:
81                 try:
82                         with TTFont (expected_file) as font:
83                                 font.saveXML (fp)
84                 except Exception as e:
85                         print (e)
86                         return fail_test (test, cli_args, "ttx failed to parse the expected result")
87                 expected_ttx = fp.getvalue ()
88
89         with io.StringIO () as fp:
90                 try:
91                         with TTFont (out_file) as font:
92                                 font.saveXML (fp)
93                 except Exception as e:
94                         print (e)
95                         return fail_test (test, cli_args, "ttx failed to parse the actual result")
96                 actual_ttx = fp.getvalue ()
97
98         if actual_ttx != expected_ttx:
99                 for line in unified_diff (expected_ttx.splitlines (1), actual_ttx.splitlines (1)):
100                         sys.stdout.write (line)
101                 sys.stdout.flush ()
102                 return fail_test (test, cli_args, 'ttx for expected and actual does not match.')
103
104         return fail_test (test, cli_args, 'hash for expected and actual does not match, '
105                                           'but the ttx matches. Expected file needs to be updated?')
106
107
108 def has_ots ():
109         if not ots_sanitize:
110                 print ("OTS is not present, skipping all ots checks.")
111                 return False
112         return True
113
114 def check_ots (path):
115         ots_report, returncode = cmd ([ots_sanitize, path])
116         if returncode:
117                 print ("OTS Failure: %s" % ots_report)
118                 return False
119         return True
120
121 args = sys.argv[1:]
122 if not args or sys.argv[1].find ('hb-subset') == -1 or not os.path.exists (sys.argv[1]):
123         sys.exit ("First argument does not seem to point to usable hb-subset.")
124 hb_subset, args = args[0], args[1:]
125
126 if not len (args):
127         sys.exit ("No tests supplied.")
128
129 has_ots = has_ots()
130
131 process = subprocess.Popen ([hb_subset, '--batch'],
132                             stdin=subprocess.PIPE,
133                             stdout=subprocess.PIPE,
134                             stderr=sys.stdout)
135
136 fails = 0
137 for path in args:
138         with open (path, mode="r", encoding="utf-8") as f:
139                 print ("Running tests in " + path)
140                 test_suite = SubsetTestSuite (path, f.read ())
141                 for test in test_suite.tests ():
142                         fails += run_test (test, has_ots)
143
144 if fails != 0:
145         sys.exit ("%d test(s) failed." % fails)
146 else:
147         print ("All tests passed.")