2022-12-07 00:40:01 +03:00
#!/usr/bin/env python3
2003-02-11 02:08:28 +03:00
import sys
import time
import os
2004-10-03 02:55:49 +04:00
sys . path . insert ( 0 , " python " )
2003-02-11 02:08:28 +03:00
import libxml2
#
# the testsuite description
#
2003-02-13 18:52:58 +03:00
DIR = " xinclude-test-suite "
CONF = " testdescr.xml "
2003-02-11 02:08:28 +03:00
LOG = " check-xinclude-test-suite.log "
log = open ( LOG , " w " )
2003-02-13 18:52:58 +03:00
os . chdir ( DIR )
2003-02-11 21:03:05 +03:00
test_nr = 0
test_succeed = 0
test_failed = 0
test_error = 0
2003-02-11 02:08:28 +03:00
#
# Error and warning handlers
#
error_nr = 0
error_msg = ' '
2003-02-11 21:03:05 +03:00
2003-02-11 02:08:28 +03:00
def errorHandler ( ctx , str ) :
global error_nr
global error_msg
2022-03-29 16:55:51 +03:00
if str . find ( " error: " ) > = 0 :
error_nr = error_nr + 1
2003-02-11 02:08:28 +03:00
if len ( error_msg ) < 300 :
if len ( error_msg ) == 0 or error_msg [ - 1 ] == ' \n ' :
2022-03-29 16:55:51 +03:00
error_msg = error_msg + " >> " + str
else :
error_msg = error_msg + str
2003-02-11 02:08:28 +03:00
libxml2 . registerErrorHandler ( errorHandler , None )
def testXInclude ( filename , id ) :
global error_nr
global error_msg
global log
error_nr = 0
error_msg = ' '
2022-03-29 16:55:51 +03:00
print ( " testXInclude( %s , %s ) " % ( filename , id ) )
2003-02-11 02:08:28 +03:00
return 1
def runTest ( test , basedir ) :
global test_nr
global test_failed
global test_error
global test_succeed
global error_msg
global log
2003-02-11 21:03:05 +03:00
fatal_error = 0
2003-02-11 02:08:28 +03:00
uri = test . prop ( ' href ' )
id = test . prop ( ' id ' )
2003-02-13 18:52:58 +03:00
type = test . prop ( ' type ' )
2022-03-29 16:55:51 +03:00
if uri is None :
print ( " Test without ID: " , uri )
return - 1
if id is None :
print ( " Test without URI: " , id )
return - 1
if type is None :
print ( " Test without URI: " , id )
return - 1
2003-02-11 02:08:28 +03:00
if basedir != None :
2022-03-29 16:55:51 +03:00
URI = basedir + " / " + uri
2003-02-11 02:08:28 +03:00
else :
URI = uri
if os . access ( URI , os . R_OK ) == 0 :
2022-03-29 16:55:51 +03:00
print ( " Test %s missing: base %s uri %s " % ( URI , basedir , uri ) )
return - 1
2003-02-11 02:08:28 +03:00
2003-02-11 21:03:05 +03:00
expected = None
2004-09-06 13:54:35 +04:00
outputfile = None
diff = None
2003-02-13 18:52:58 +03:00
if type != ' error ' :
2022-03-29 16:55:51 +03:00
output = test . xpathEval ( ' string(output) ' )
if output == ' No output file. ' :
output = None
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 ( )
outputfile = output
except :
print ( " Result for %s unreadable: %s " % ( id , output ) )
2003-02-11 21:03:05 +03:00
try :
2022-03-29 16:55:51 +03:00
# print("testing %s" % (URI))
doc = libxml2 . parseFile ( URI )
2003-02-11 21:03:05 +03:00
except :
doc = None
if doc != None :
res = doc . xincludeProcess ( )
2022-03-29 16:55:51 +03:00
if res > = 0 and expected != None :
result = doc . serialize ( )
if result != expected :
print ( " Result for %s differs " % ( id ) )
open ( " xinclude.res " , " w " ) . write ( result )
diff = os . popen ( " diff %s xinclude.res " % outputfile ) . read ( )
doc . freeDoc ( )
2003-02-11 21:03:05 +03:00
else :
2022-03-29 16:55:51 +03:00
print ( " Failed to parse %s " % ( URI ) )
res = - 1
2003-02-11 21:03:05 +03:00
2003-02-11 02:08:28 +03:00
test_nr = test_nr + 1
2003-02-13 18:52:58 +03:00
if type == ' success ' :
2022-03-29 16:55:51 +03:00
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 ) )
2003-02-13 18:52:58 +03:00
elif type == ' error ' :
2022-03-29 16:55:51 +03:00
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
2003-02-13 18:52:58 +03:00
elif type == ' optional ' :
2022-03-29 16:55:51 +03:00
if res > 0 :
test_succeed = test_succeed + 1
else :
print ( " Test %s : failed optional test " % ( id ) )
2003-02-11 02:08:28 +03:00
# Log the ontext
if res != 1 :
2022-03-29 16:55:51 +03:00
log . write ( " Test ID %s \n " % ( id ) )
log . write ( " File: %s \n " % ( URI ) )
content = test . content . strip ( )
while content [ - 1 ] == ' \n ' :
content = content [ 0 : - 1 ]
log . write ( " %s : %s \n \n " % ( type , content ) )
if error_msg != ' ' :
log . write ( " ---- \n %s ---- \n " % ( error_msg ) )
error_msg = ' '
log . write ( " \n " )
2004-09-06 13:54:35 +04:00
if diff != None :
log . write ( " diff from test %s : \n " % ( id ) )
2022-03-29 16:55:51 +03:00
log . write ( " ----------- \n %s \n ----------- \n " % ( diff ) ) ;
2003-02-11 02:08:28 +03:00
return 0
2022-03-29 16:55:51 +03:00
2003-02-11 02:08:28 +03:00
def runTestCases ( case ) :
creator = case . prop ( ' creator ' )
if creator != None :
2022-03-29 16:55:51 +03:00
print ( " => " , creator )
2003-02-11 02:08:28 +03:00
base = case . getBase ( None )
basedir = case . prop ( ' basedir ' )
if basedir != None :
2022-03-29 16:55:51 +03:00
base = libxml2 . buildURI ( basedir , base )
2003-02-11 02:08:28 +03:00
test = case . children
while test != None :
if test . name == ' testcase ' :
2022-03-29 16:55:51 +03:00
runTest ( test , base )
if test . name == ' testcases ' :
runTestCases ( test )
2003-02-11 02:08:28 +03:00
test = test . next
2022-03-29 16:55:51 +03:00
2003-02-11 02:08:28 +03:00
conf = libxml2 . parseFile ( CONF )
2022-03-29 16:55:51 +03:00
if conf is None :
print ( " Unable to load %s " % CONF )
2003-02-11 02:08:28 +03:00
sys . exit ( 1 )
testsuite = conf . getRootElement ( )
if testsuite . name != ' testsuite ' :
2022-03-29 16:55:51 +03:00
print ( " Expecting TESTSUITE root element: aborting " )
2003-02-11 02:08:28 +03:00
sys . exit ( 1 )
profile = testsuite . prop ( ' PROFILE ' )
if profile != None :
2022-03-29 16:55:51 +03:00
print ( profile )
2003-02-11 02:08:28 +03:00
start = time . time ( )
case = testsuite . children
while case != None :
if case . name == ' testcases ' :
2022-03-29 16:55:51 +03:00
old_test_nr = test_nr
old_test_succeed = test_succeed
old_test_failed = test_failed
old_test_error = test_error
2003-02-11 02:08:28 +03:00
runTestCases ( case )
2022-03-29 16:55:51 +03:00
print ( " Ran %d tests: %d succeeded, %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 ) )
2003-02-11 02:08:28 +03:00
case = case . next
conf . freeDoc ( )
log . close ( )
2022-03-29 16:55:51 +03:00
print ( " Ran %d tests: %d succeeded, %d failed and %d generated an error in %.2f s. " % (
test_nr , test_succeed , test_failed , test_error , time . time ( ) - start ) )