run-tests.py 3.8 KB

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