check-xinclude-test-suite.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/usr/bin/python
  2. import sys
  3. import time
  4. import os
  5. import string
  6. sys.path.insert(0, "python")
  7. import libxml2
  8. #
  9. # the testsuite description
  10. #
  11. DIR="xinclude-test-suite"
  12. CONF="testdescr.xml"
  13. LOG="check-xinclude-test-suite.log"
  14. log = open(LOG, "w")
  15. os.chdir(DIR)
  16. test_nr = 0
  17. test_succeed = 0
  18. test_failed = 0
  19. test_error = 0
  20. #
  21. # Error and warning handlers
  22. #
  23. error_nr = 0
  24. error_msg = ''
  25. def errorHandler(ctx, str):
  26. global error_nr
  27. global error_msg
  28. if string.find(str, "error:") >= 0:
  29. error_nr = error_nr + 1
  30. if len(error_msg) < 300:
  31. if len(error_msg) == 0 or error_msg[-1] == '\n':
  32. error_msg = error_msg + " >>" + str
  33. else:
  34. error_msg = error_msg + str
  35. libxml2.registerErrorHandler(errorHandler, None)
  36. def testXInclude(filename, id):
  37. global error_nr
  38. global error_msg
  39. global log
  40. error_nr = 0
  41. error_msg = ''
  42. print "testXInclude(%s, %s)" % (filename, id)
  43. return 1
  44. def runTest(test, basedir):
  45. global test_nr
  46. global test_failed
  47. global test_error
  48. global test_succeed
  49. global error_msg
  50. global log
  51. fatal_error = 0
  52. uri = test.prop('href')
  53. id = test.prop('id')
  54. type = test.prop('type')
  55. if uri == None:
  56. print "Test without ID:", uri
  57. return -1
  58. if id == None:
  59. print "Test without URI:", id
  60. return -1
  61. if type == None:
  62. print "Test without URI:", id
  63. return -1
  64. if basedir != None:
  65. URI = basedir + "/" + uri
  66. else:
  67. URI = uri
  68. if os.access(URI, os.R_OK) == 0:
  69. print "Test %s missing: base %s uri %s" % (URI, basedir, uri)
  70. return -1
  71. expected = None
  72. outputfile = None
  73. diff = None
  74. if type != 'error':
  75. output = test.xpathEval('string(output)')
  76. if output == 'No output file.':
  77. output = None
  78. if output == '':
  79. output = None
  80. if output != None:
  81. if basedir != None:
  82. output = basedir + "/" + output
  83. if os.access(output, os.R_OK) == 0:
  84. print "Result for %s missing: %s" % (id, output)
  85. output = None
  86. else:
  87. try:
  88. f = open(output)
  89. expected = f.read()
  90. outputfile = output
  91. except:
  92. print "Result for %s unreadable: %s" % (id, output)
  93. try:
  94. # print "testing %s" % (URI)
  95. doc = libxml2.parseFile(URI)
  96. except:
  97. doc = None
  98. if doc != None:
  99. res = doc.xincludeProcess()
  100. if res >= 0 and expected != None:
  101. result = doc.serialize()
  102. if result != expected:
  103. print "Result for %s differs" % (id)
  104. open("xinclude.res", "w").write(result)
  105. diff = os.popen("diff %s xinclude.res" % outputfile).read()
  106. doc.freeDoc()
  107. else:
  108. print "Failed to parse %s" % (URI)
  109. res = -1
  110. test_nr = test_nr + 1
  111. if type == 'success':
  112. if res > 0:
  113. test_succeed = test_succeed + 1
  114. elif res == 0:
  115. test_failed = test_failed + 1
  116. print "Test %s: no substitution done ???" % (id)
  117. elif res < 0:
  118. test_error = test_error + 1
  119. print "Test %s: failed valid XInclude processing" % (id)
  120. elif type == 'error':
  121. if res > 0:
  122. test_error = test_error + 1
  123. print "Test %s: failed to detect invalid XInclude processing" % (id)
  124. elif res == 0:
  125. test_failed = test_failed + 1
  126. print "Test %s: Invalid but no substitution done" % (id)
  127. elif res < 0:
  128. test_succeed = test_succeed + 1
  129. elif type == 'optional':
  130. if res > 0:
  131. test_succeed = test_succeed + 1
  132. else:
  133. print "Test %s: failed optional test" % (id)
  134. # Log the ontext
  135. if res != 1:
  136. log.write("Test ID %s\n" % (id))
  137. log.write(" File: %s\n" % (URI))
  138. content = string.strip(test.content)
  139. while content[-1] == '\n':
  140. content = content[0:-1]
  141. log.write(" %s:%s\n\n" % (type, content))
  142. if error_msg != '':
  143. log.write(" ----\n%s ----\n" % (error_msg))
  144. error_msg = ''
  145. log.write("\n")
  146. if diff != None:
  147. log.write("diff from test %s:\n" %(id))
  148. log.write(" -----------\n%s\n -----------\n" % (diff));
  149. return 0
  150. def runTestCases(case):
  151. creator = case.prop('creator')
  152. if creator != None:
  153. print "=>", creator
  154. base = case.getBase(None)
  155. basedir = case.prop('basedir')
  156. if basedir != None:
  157. base = libxml2.buildURI(basedir, base)
  158. test = case.children
  159. while test != None:
  160. if test.name == 'testcase':
  161. runTest(test, base)
  162. if test.name == 'testcases':
  163. runTestCases(test)
  164. test = test.next
  165. conf = libxml2.parseFile(CONF)
  166. if conf == None:
  167. print "Unable to load %s" % CONF
  168. sys.exit(1)
  169. testsuite = conf.getRootElement()
  170. if testsuite.name != 'testsuite':
  171. print "Expecting TESTSUITE root element: aborting"
  172. sys.exit(1)
  173. profile = testsuite.prop('PROFILE')
  174. if profile != None:
  175. print profile
  176. start = time.time()
  177. case = testsuite.children
  178. while case != None:
  179. if case.name == 'testcases':
  180. old_test_nr = test_nr
  181. old_test_succeed = test_succeed
  182. old_test_failed = test_failed
  183. old_test_error = test_error
  184. runTestCases(case)
  185. print " Ran %d tests: %d suceeded, %d failed and %d generated an error" % (
  186. test_nr - old_test_nr, test_succeed - old_test_succeed,
  187. test_failed - old_test_failed, test_error - old_test_error)
  188. case = case.next
  189. conf.freeDoc()
  190. log.close()
  191. print "Ran %d tests: %d suceeded, %d failed and %d generated an error in %.2f s." % (
  192. test_nr, test_succeed, test_failed, test_error, time.time() - start)