check-xinclude-test-suite.py 5.21 KB
Newer Older
1 2 3 4 5
#!/usr/bin/python
import sys
import time
import os
import string
6
sys.path.insert(0, "python")
7 8 9 10 11
import libxml2

#
# the testsuite description
#
12 13
DIR="xinclude-test-suite"
CONF="testdescr.xml"
14 15 16 17
LOG="check-xinclude-test-suite.log"

log = open(LOG, "w")

18 19
os.chdir(DIR)

20 21 22 23
test_nr = 0
test_succeed = 0
test_failed = 0
test_error = 0
24 25 26 27 28
#
# Error and warning handlers
#
error_nr = 0
error_msg = ''
29

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
def errorHandler(ctx, str):
    global error_nr
    global error_msg

    if string.find(str, "error:") >= 0:
	error_nr = error_nr + 1
    if len(error_msg) < 300:
        if len(error_msg) == 0 or error_msg[-1] == '\n':
	    error_msg = error_msg + "   >>" + str
	else:
	    error_msg = error_msg + str

libxml2.registerErrorHandler(errorHandler, None)

def testXInclude(filename, id):
    global error_nr
    global error_msg
    global log

    error_nr = 0
    error_msg = ''

    print "testXInclude(%s, %s)" % (filename, id)
    return 1

def runTest(test, basedir):
    global test_nr
    global test_failed
    global test_error
    global test_succeed
    global error_msg
    global log

63
    fatal_error = 0
64 65
    uri = test.prop('href')
    id = test.prop('id')
66
    type = test.prop('type')
67 68 69 70 71 72
    if uri == None:
        print "Test without ID:", uri
	return -1
    if id == None:
        print "Test without URI:", id
	return -1
73 74 75
    if type == None:
        print "Test without URI:", id
	return -1
76 77 78 79 80 81 82 83
    if basedir != None:
	URI = basedir + "/" + uri
    else:
        URI = uri
    if os.access(URI, os.R_OK) == 0:
        print "Test %s missing: base %s uri %s" % (URI, basedir, uri)
	return -1

84
    expected = None
85 86
    outputfile = None
    diff = None
87 88 89
    if type != 'error':
	output = test.xpathEval('string(output)')
	if output == 'No output file.':
90
	    output = None
91 92 93 94 95 96 97 98 99 100 101 102
	if output == '':
	    output = None
	if output != None:
	    if basedir != None:
		output = basedir + "/" + output
	    if os.access(output, os.R_OK) == 0:
		print "Result for %s missing: %s" % (id, output)
		output = None
	    else:
		try:
		    f = open(output)
		    expected = f.read()
103
		    outputfile = output
104 105
		except:
		    print "Result for %s unreadable: %s" % (id, output)
106 107 108 109 110 111 112 113

    try:
        # print "testing %s" % (URI)
	doc = libxml2.parseFile(URI)
    except:
        doc = None
    if doc != None:
        res = doc.xincludeProcess()
114
	if res >= 0 and expected != None:
115 116 117
	    result = doc.serialize()
	    if result != expected:
	        print "Result for %s differs" % (id)
118 119
		open("xinclude.res", "w").write(result)
		diff = os.popen("diff %s xinclude.res" % outputfile).read()
120 121 122 123 124 125 126

	doc.freeDoc()
    else:
        print "Failed to parse %s" % (URI)
	res = -1

    
127 128

    test_nr = test_nr + 1
129
    if type == 'success':
130 131 132 133 134 135 136 137
	if res > 0:
	    test_succeed = test_succeed + 1
	elif res == 0:
	    test_failed = test_failed + 1
	    print "Test %s: no substitution done ???" % (id)
	elif res < 0:
	    test_error = test_error + 1
	    print "Test %s: failed valid XInclude processing" % (id)
138
    elif type == 'error':
139 140 141 142 143 144 145 146
	if res > 0:
	    test_error = test_error + 1
	    print "Test %s: failed to detect invalid XInclude processing" % (id)
	elif res == 0:
	    test_failed = test_failed + 1
	    print "Test %s: Invalid but no substitution done" % (id)
	elif res < 0:
	    test_succeed = test_succeed + 1
147 148 149 150 151
    elif type == 'optional':
	if res > 0:
	    test_succeed = test_succeed + 1
	else:
	    print "Test %s: failed optional test" % (id)
152 153 154

    # Log the ontext
    if res != 1:
155
	log.write("Test ID %s\n" % (id))
156 157 158 159
	log.write("   File: %s\n" % (URI))
	content = string.strip(test.content)
	while content[-1] == '\n':
	    content = content[0:-1]
160
	log.write("   %s:%s\n\n" % (type, content))
161 162 163 164
	if error_msg != '':
	    log.write("   ----\n%s   ----\n" % (error_msg))
	    error_msg = ''
	log.write("\n")
165 166 167
    if diff != None:
        log.write("diff from test %s:\n" %(id))
	log.write("   -----------\n%s\n   -----------\n" % (diff));
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

    return 0
	    

def runTestCases(case):
    creator = case.prop('creator')
    if creator != None:
	print "=>", creator
    base = case.getBase(None)
    basedir = case.prop('basedir')
    if basedir != None:
	base = libxml2.buildURI(basedir, base)
    test = case.children
    while test != None:
        if test.name == 'testcase':
	    runTest(test, base)
	if test.name == 'testcases':
	    runTestCases(test)
        test = test.next
        
conf = libxml2.parseFile(CONF)
if conf == None:
    print "Unable to load %s" % CONF
    sys.exit(1)

testsuite = conf.getRootElement()
if testsuite.name != 'testsuite':
    print "Expecting TESTSUITE root element: aborting"
    sys.exit(1)

profile = testsuite.prop('PROFILE')
if profile != None:
    print profile

start = time.time()

case = testsuite.children
while case != None:
    if case.name == 'testcases':
	old_test_nr = test_nr
	old_test_succeed = test_succeed
	old_test_failed = test_failed
	old_test_error = test_error
        runTestCases(case)
	print "   Ran %d tests: %d suceeded, %d failed and %d generated an error" % (
	       test_nr - old_test_nr, test_succeed - old_test_succeed,
	       test_failed - old_test_failed, test_error - old_test_error)
    case = case.next

conf.freeDoc()
log.close()

print "Ran %d tests: %d suceeded, %d failed and %d generated an error in %.2f s." % (
      test_nr, test_succeed, test_failed, test_error, time.time() - start)