source: trunk/third/libxml2/check-xinclude-test-suite.py @ 20735

Revision 20735, 4.9 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r20734, which included commits to RCS files with non-trunk default branches.
  • Property svn:executable set to *
Line 
1#!/usr/bin/python
2import sys
3import time
4import os
5import string
6sys.path.append("python")
7import libxml2
8
9#
10# the testsuite description
11#
12DIR="xinclude-test-suite"
13CONF="testdescr.xml"
14LOG="check-xinclude-test-suite.log"
15
16log = open(LOG, "w")
17
18os.chdir(DIR)
19
20test_nr = 0
21test_succeed = 0
22test_failed = 0
23test_error = 0
24#
25# Error and warning handlers
26#
27error_nr = 0
28error_msg = ''
29
30def 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
42libxml2.registerErrorHandler(errorHandler, None)
43
44def 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
55def 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
63    fatal_error = 0
64    uri = test.prop('href')
65    id = test.prop('id')
66    type = test.prop('type')
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
73    if type == None:
74        print "Test without URI:", id
75        return -1
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
84    expected = None
85    if type != 'error':
86        output = test.xpathEval('string(output)')
87        if output == 'No output file.':
88            output = None
89        if output == '':
90            output = None
91        if output != None:
92            if basedir != None:
93                output = basedir + "/" + output
94            if os.access(output, os.R_OK) == 0:
95                print "Result for %s missing: %s" % (id, output)
96                output = None
97            else:
98                try:
99                    f = open(output)
100                    expected = f.read()
101                except:
102                    print "Result for %s unreadable: %s" % (id, output)
103
104    try:
105        # print "testing %s" % (URI)
106        doc = libxml2.parseFile(URI)
107    except:
108        doc = None
109    if doc != None:
110        res = doc.xincludeProcess()
111        if res >= 0 and expected != None:
112            result = doc.serialize()
113            if result != expected:
114                print "Result for %s differs" % (id)
115
116        doc.freeDoc()
117    else:
118        print "Failed to parse %s" % (URI)
119        res = -1
120
121   
122
123    test_nr = test_nr + 1
124    if type == 'success':
125        if res > 0:
126            test_succeed = test_succeed + 1
127        elif res == 0:
128            test_failed = test_failed + 1
129            print "Test %s: no substitution done ???" % (id)
130        elif res < 0:
131            test_error = test_error + 1
132            print "Test %s: failed valid XInclude processing" % (id)
133    elif type == 'error':
134        if res > 0:
135            test_error = test_error + 1
136            print "Test %s: failed to detect invalid XInclude processing" % (id)
137        elif res == 0:
138            test_failed = test_failed + 1
139            print "Test %s: Invalid but no substitution done" % (id)
140        elif res < 0:
141            test_succeed = test_succeed + 1
142    elif type == 'optional':
143        if res > 0:
144            test_succeed = test_succeed + 1
145        else:
146            print "Test %s: failed optional test" % (id)
147
148    # Log the ontext
149    if res != 1:
150        log.write("Test ID %s\n" % (id))
151        log.write("   File: %s\n" % (URI))
152        content = string.strip(test.content)
153        while content[-1] == '\n':
154            content = content[0:-1]
155        log.write("   %s:%s\n\n" % (type, content))
156        if error_msg != '':
157            log.write("   ----\n%s   ----\n" % (error_msg))
158            error_msg = ''
159        log.write("\n")
160
161    return 0
162           
163
164def runTestCases(case):
165    creator = case.prop('creator')
166    if creator != None:
167        print "=>", creator
168    base = case.getBase(None)
169    basedir = case.prop('basedir')
170    if basedir != None:
171        base = libxml2.buildURI(basedir, base)
172    test = case.children
173    while test != None:
174        if test.name == 'testcase':
175            runTest(test, base)
176        if test.name == 'testcases':
177            runTestCases(test)
178        test = test.next
179       
180conf = libxml2.parseFile(CONF)
181if conf == None:
182    print "Unable to load %s" % CONF
183    sys.exit(1)
184
185testsuite = conf.getRootElement()
186if testsuite.name != 'testsuite':
187    print "Expecting TESTSUITE root element: aborting"
188    sys.exit(1)
189
190profile = testsuite.prop('PROFILE')
191if profile != None:
192    print profile
193
194start = time.time()
195
196case = testsuite.children
197while case != None:
198    if case.name == 'testcases':
199        old_test_nr = test_nr
200        old_test_succeed = test_succeed
201        old_test_failed = test_failed
202        old_test_error = test_error
203        runTestCases(case)
204        print "   Ran %d tests: %d suceeded, %d failed and %d generated an error" % (
205               test_nr - old_test_nr, test_succeed - old_test_succeed,
206               test_failed - old_test_failed, test_error - old_test_error)
207    case = case.next
208
209conf.freeDoc()
210log.close()
211
212print "Ran %d tests: %d suceeded, %d failed and %d generated an error in %.2f s." % (
213      test_nr, test_succeed, test_failed, test_error, time.time() - start)
Note: See TracBrowser for help on using the repository browser.