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