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