apicompatanamdw/compatanalysercmd/headeranalyser/tsrc/hatest.py
changeset 0 638b9c697799
equal deleted inserted replaced
-1:000000000000 0:638b9c697799
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:
       
    15 #
       
    16 
       
    17 import sys
       
    18 import os
       
    19 import re
       
    20 import time
       
    21 import platform
       
    22 from xml.dom import minidom
       
    23 
       
    24 TC_DIR = os.getcwd() + os.sep + "TC" + os.sep
       
    25 SCRIPTS_DIR = os.getcwd() + os.sep + "scripts" + os.sep
       
    26 RESULTS_DIR = os.getcwd() + os.sep + "results" + os.sep
       
    27 usage = "-h /h -? /?".split()
       
    28 debug = 0
       
    29 
       
    30 def UpdateTimeTaken(tm):
       
    31     global time_taken, total_time_taken
       
    32     time_taken = tm
       
    33     total_time_taken += tm
       
    34 
       
    35 def Usage():
       
    36     print "Usage               : "+ sys.argv[0] + " <outputfile> | -build [<testcasefile>]"
       
    37     print "Execute test case(s): " + sys.argv[0] + " <outputfile> [<testcasefile>]"
       
    38     print "Build test case     : " + sys.argv[0] + " -build <testcase name>"
       
    39     sys.exit()
       
    40 
       
    41 def HABuild():
       
    42     if len(sys.argv) != 3:
       
    43         Usage()
       
    44     cmd = "python " + SCRIPTS_DIR + "hatestbuild.py " + sys.argv[2].upper()
       
    45     os.system(cmd)
       
    46     sys.exit()
       
    47 
       
    48 def OpenResults():
       
    49     result = RESULTS_DIR + sys.argv[1]
       
    50     if os.path.exists(result):
       
    51         cmd = "start " + result
       
    52         os.system(cmd)
       
    53     return
       
    54     
       
    55 def RunAll():
       
    56     global time_taken;
       
    57     cases = os.listdir(TC_DIR)
       
    58     p = re.compile('^HATC')
       
    59     for tc in cases:
       
    60         tc = tc.strip().upper()
       
    61         if p.match(tc):
       
    62             print "\n" + tc
       
    63             cmd = "python " + SCRIPTS_DIR + "hatestrun.py " + tc
       
    64             stime = time.clock()
       
    65             os.system(cmd)
       
    66             etime = time.clock()
       
    67             UpdateTimeTaken(etime-stime)
       
    68             CompareResult(tc)
       
    69     WriteXml()
       
    70     if os.name != "posix":
       
    71         OpenResults()
       
    72     return
       
    73     
       
    74 def RunTC():
       
    75     global time_taken;
       
    76     file = open( sys.argv[2] )
       
    77     cases = file.readlines()
       
    78     file.close()
       
    79     for tc in cases:
       
    80         tc = tc.strip().upper()
       
    81         if tc:
       
    82             print "\n" + tc
       
    83             cmd = "python " + SCRIPTS_DIR + "hatestrun.py " + tc
       
    84             stime = time.clock()
       
    85             os.system(cmd)
       
    86             etime = time.clock()
       
    87             UpdateTimeTaken(etime-stime)
       
    88             CompareResult(tc)
       
    89     WriteXml()
       
    90     if os.name != "posix":
       
    91         OpenResults()
       
    92     return
       
    93 
       
    94 
       
    95 def CheckIssues(issue1, issueArr):
       
    96     issuefound = 0
       
    97 
       
    98     if debug == 1:
       
    99         print >>debfile, "-------------------------------\n"
       
   100         if issue1.getElementsByTagName('cause')[0].firstChild != None:
       
   101             print >>debfile, "ISSUE: "+ issue1.getElementsByTagName('cause')[0].firstChild.data +" --> "
       
   102         else:
       
   103             print >>debfile, "ISSUE:  --> "
       
   104         
       
   105     for issue2 in issueArr:
       
   106         if  issue1.getElementsByTagName('typestring')[0].firstChild.data == issue2.getElementsByTagName('typestring')[0].firstChild.data and \
       
   107             (( issue1.getElementsByTagName('cause')[0].firstChild == None and issue2.getElementsByTagName('cause')[0].firstChild == None ) or \
       
   108             (issue1.getElementsByTagName('cause')[0].firstChild != None and \
       
   109             issue1.getElementsByTagName('cause')[0].firstChild.data == issue2.getElementsByTagName('cause')[0].firstChild.data )) and \
       
   110             issue1.getElementsByTagName('identitydescription')[0].firstChild.data == issue2.getElementsByTagName('identitydescription')[0].firstChild.data and \
       
   111             issue1.getElementsByTagName('severity')[0].getElementsByTagName('typestring')[0].firstChild.data == issue2.getElementsByTagName('severity')[0].getElementsByTagName('typestring')[0].firstChild.data and \
       
   112             issue1.getElementsByTagName('severity')[0].getElementsByTagName('typeid')[0].firstChild.data == issue2.getElementsByTagName('severity')[0].getElementsByTagName('typeid')[0].firstChild.data and \
       
   113             issue1.getElementsByTagName('scseverity')[0].getElementsByTagName('typestring')[0].firstChild.data == issue2.getElementsByTagName('scseverity')[0].getElementsByTagName('typestring')[0].firstChild.data and \
       
   114             issue1.getElementsByTagName('scseverity')[0].getElementsByTagName('typeid')[0].firstChild.data == issue2.getElementsByTagName('scseverity')[0].getElementsByTagName('typeid')[0].firstChild.data and \
       
   115             issue1.getElementsByTagName('linenumber')[0].firstChild.data == issue2.getElementsByTagName('linenumber')[0].firstChild.data :
       
   116                if debug == 1:
       
   117                    print >>debfile, "Found.\n"
       
   118                issuefound = 1
       
   119                break				
       
   120 
       
   121     if issuefound == 0:        
       
   122         if debug == 1:
       
   123             print >>debfile, "NOT FOUND.\n"
       
   124 
       
   125     return issuefound 
       
   126 
       
   127 def WriteXml():
       
   128     global totalCount, passedCount, timeval, outfile, errfile, debug, debfile, time_taken, total_time_taken
       
   129     timeval = time.strftime("%a %b %d, %Y at %H:%M:%S", time.localtime())
       
   130     
       
   131     if totalCount > 0:
       
   132         passedPercent = "%.2f" % (passedCount/totalCount)*100
       
   133     else:
       
   134         passedPercent = "0"
       
   135 
       
   136     failedcount = totalCount - passedCount
       
   137     ohdr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
       
   138     ohdr = ohdr + "<?xml-stylesheet type=\"text/xsl\" href=\"hatestresults.xsl\"?>\n"
       
   139     ohdr = ohdr+"<testsuite errors=\"0\" failures=\""+str(failedcount)+"\" hostname=\""+platform.node()+"\" name=\"HeaderAnalyser\" tests=\""+str(totalCount)+"\" time=\""+str(round(total_time_taken,3))+"\" timestamp=\""+str(timeval)+"\">\n"
       
   140     ohdr = ohdr+"<properties>\n</properties>\n"
       
   141     ohdr = ohdr+ostr+"</testsuite>\n"
       
   142     print >>outfile, ohdr
       
   143     outfile.close()
       
   144     
       
   145     if passedCount == totalCount:
       
   146         print >>errfile, "No differencies.\n"
       
   147     
       
   148     errfile.close()
       
   149     if debug == 1:
       
   150         debfile.close()
       
   151 
       
   152 def CompareResult(tc):              
       
   153     global ostr, totalCount, passedCount, debug, debfile, errfile
       
   154     if debug == 1:
       
   155         print >>debfile, "\n-----------------------\n"
       
   156         print >>debfile, "Testcase "+tc+"\n"
       
   157         print >>debfile, "-----------------------\n"
       
   158     
       
   159     # Read expected results (xml report):
       
   160     expfilename = "TC"+os.sep+tc+os.sep+"results"+os.sep+tc+"_expected_results.xml"
       
   161     if debug == 1:
       
   162         print >>debfile, "Reading expected results from file: "+expfilename+"\n"
       
   163         
       
   164     expected = minidom.parse(expfilename)
       
   165                             
       
   166     # Read actual results (xml report):
       
   167     actfilename = "TC"+os.sep+tc+os.sep+"results"+os.sep+tc+"_results.xml"
       
   168     if not os.path.exists(actfilename):
       
   169         print "os.path.exists(actfilename)"
       
   170         failed = 1
       
   171         print >>errfile, "--------------------------------------------\n"
       
   172         print >>errfile, "TESTCASE: "+tc+"\n\n"
       
   173         print >>errfile, "Difference: Actual results file missing:\n"
       
   174         print >>errfile, "Filename: "+actfilename+"\n"
       
   175         print >>errfile, "--------------------------------------------\n"
       
   176         ostr = ostr+"  <testcase classname=\"HeaderAnalyser\" name=\""+tc+"\" time=\""+str(round(time_taken,3))+"\"\">\n"
       
   177         ostr = ostr+"    <failure message=\"Failed\" type=\"Failed\">Failed</failure>\n"
       
   178         ostr = ostr+"    <expresults>"+os.pardir+os.sep+expfilename+"</expresults>\n"
       
   179         ostr = ostr+"    <actresults>"+os.pardir+os.sep+actfilename+"</actresults>\n"
       
   180         ostr = ostr+"  </testcase>\n"
       
   181         totalCount += 1
       
   182         return
       
   183 
       
   184     if debug == 1 :
       
   185         print >>debfile, "Reading actual results from file: "+actfilename+"\n"
       
   186 
       
   187     actual = minidom.parse(actfilename)               
       
   188     ostr = ostr + "  <testcase classname=\"HeaderAnalyser\" name=\""+tc+"\" time=\""+str(round(time_taken,3))+"\">\n"
       
   189        
       
   190 
       
   191     totalCount += 1
       
   192     
       
   193     failed = 0 
       
   194     headerfound = 0 
       
   195 
       
   196     for expHeader in expected.getElementsByTagName('headerfile'):   
       
   197         headerfound = 0 
       
   198         expHeaderFilename = expHeader.getElementsByTagName('filename')[0].firstChild.data
       
   199         expHeaderCompareFilename = expHeader.getElementsByTagName('comparefilename')[0].firstChild.data
       
   200             
       
   201         # Strip off the paths from the filenames:
       
   202         
       
   203         tmpIndex = expHeaderFilename.lower().rindex('\\') # index where the header name begins
       
   204         expFile = expHeaderFilename[ tmpIndex+1 : len(expHeaderFilename) ].lower()
       
   205        
       
   206        
       
   207         tmpIndex = expHeaderCompareFilename.lower().rindex('\\') # index of last backslash.
       
   208         expCompareFile = expHeaderCompareFilename[ tmpIndex+1 : len(expHeaderCompareFilename) ].lower()
       
   209 
       
   210         if debug == 1:
       
   211             print >>debfile, "********************************\n"
       
   212             print >>debfile, "EXP FILE: "+expFile+" -->\n"
       
   213 
       
   214         for actHeader in actual.getElementsByTagName('headerfile'):
       
   215             actHeaderFilename = actHeader.getElementsByTagName('filename')[0].firstChild.data
       
   216             actHeaderCompareFilename = actHeader.getElementsByTagName('comparefilename')[0].firstChild.data
       
   217             
       
   218             # Strip off the paths from the filenames:
       
   219             
       
   220             tmpIndex = actHeaderFilename.lower().rindex(os.sep)  # index of last backslash.
       
   221             actFile = actHeaderFilename[ tmpIndex+1 : len(actHeaderFilename) ].lower()
       
   222             
       
   223             tmpIndex = actHeaderCompareFilename.lower().rindex(os.sep)  # index of last backslash.
       
   224             actCompareFile = actHeaderCompareFilename[ tmpIndex+1 : len(actHeaderCompareFilename) ].lower()
       
   225 
       
   226             if expFile == actFile and expCompareFile == actCompareFile:   
       
   227                 if debug == 1:
       
   228                     print >>debfile, "Found.\n"
       
   229                     
       
   230                 for iss1 in expHeader.getElementsByTagName('issue'):
       
   231                     if CheckIssues(iss1, actHeader.getElementsByTagName('issue')) == 0:
       
   232                         failed = 1
       
   233                         print >>errfile, "--------------------------------------------\n"
       
   234                         print >>errfile, "TESTCASE: "+tc+"\n\n"
       
   235                         print >>errfile, "Difference: Issue missing from actual results:\n"
       
   236                         print >>errfile, "Filename: "+expFile+"\n"
       
   237                         if iss1.getElementsByTagName('cause')[0].firstChild != None:
       
   238                             print >>errfile, "Issue: "+iss1.getElementsByTagName('cause')[0].firstChild.data+"\n"
       
   239                         else:
       
   240                             print >>errfile, "Issue: \n"
       
   241                         print >>errfile, "Description: "+iss1.getElementsByTagName('identitydescription')[0].firstChild.data +" "+ iss1.getElementsByTagName('typestring')[0].firstChild.data+" \n"                     
       
   242                         print >>errfile, "--------------------------------------------\n"
       
   243 
       
   244                 headerfound = 1
       
   245                 break
       
   246             
       
   247 
       
   248         if headerfound == 0:
       
   249             if debug == 1:
       
   250                 print >>debfile, "NOT FOUND.\n"
       
   251             failed = 1
       
   252             print >>errfile, "--------------------------------------------\n"
       
   253             print >>errfile, "TESTCASE: "+tc+"\n\n"
       
   254             print >>errfile, "Difference: Header results missing from actual results:\n"
       
   255             print >>errfile, "Filename: "+expFile+"\n"
       
   256             if iss1.getElementsByTagName('cause')[0].firstChild != None:
       
   257                 print >>errfile, "Issue: "+iss1.getElementsByTagName('cause')[0].firstChild.data+"\n"
       
   258             else:
       
   259                 print >>errfile, "Issue: \n"
       
   260             print >>errfile, "Description: "+iss1.getElementsByTagName('identitydescription')[0].firstChild.data +" "+ iss1.getElementsByTagName('typestring')[0].firstChild.data+" \n"                     
       
   261             print >>errfile, "--------------------------------------------\n"
       
   262 
       
   263     
       
   264     for actHeader in actual.getElementsByTagName('headerfile'):
       
   265         actHeaderFilename = actHeader.getElementsByTagName('filename')[0].firstChild.data
       
   266         actHeaderCompareFilename = actHeader.getElementsByTagName('comparefilename')[0].firstChild.data
       
   267         
       
   268         # Strip off the paths from the filenames:
       
   269         
       
   270         tmpIndex = actHeaderFilename.lower().rindex(os.sep)  # index of last backslash.
       
   271         actFile = actHeaderFilename[ tmpIndex+1 : len(actHeaderFilename) ].lower()
       
   272         
       
   273         tmpIndex = actHeaderCompareFilename.lower().rindex(os.sep)  # index of last backslash.
       
   274         actCompareFile = actHeaderCompareFilename[ tmpIndex+1 : len(actHeaderCompareFilename) ].lower()
       
   275         
       
   276         if debug == 1:
       
   277             print >>debfile, "********************************\n"
       
   278             print >>debfile, "ACT FILE: "+actFile+" -->\n"
       
   279 
       
   280         headerfound = 0 
       
   281         for expHeader in expected.getElementsByTagName('headerfile'):
       
   282            expHeaderFilename = expHeader.getElementsByTagName('filename')[0].firstChild.data
       
   283            expHeaderCompareFilename = expHeader.getElementsByTagName('comparefilename')[0].firstChild.data
       
   284             
       
   285            # Strip off the paths from the filenames:
       
   286            tmpIndex = expHeaderFilename.lower().rindex('\\') # index where the header name begins
       
   287            expFile = expHeaderFilename[ tmpIndex+1 : len(expHeaderFilename) ].lower()
       
   288        
       
   289            tmpIndex = expHeaderCompareFilename.lower().rindex('\\') # index of last backslash.
       
   290            expCompareFile = expHeaderCompareFilename[ tmpIndex+1 : len(expHeaderCompareFilename) ].lower()
       
   291             
       
   292            if actFile == expFile and actCompareFile == expCompareFile:   
       
   293                if debug == 1:
       
   294                    print >>debfile, "Found.\n"
       
   295                    
       
   296                for iss2 in actHeader.getElementsByTagName('issue'):
       
   297                    if CheckIssues(iss2, expHeader.getElementsByTagName('issue')) == 0:
       
   298                        failed = 1
       
   299                        print >>errfile, "--------------------------------------------\n"
       
   300                        print >>errfile, "TESTCASE: "+tc+"\n\n"
       
   301                        print >>errfile, "Difference: Issue missing from expected results:\n"
       
   302                        print >>errfile, "Filename: "+actFile+"\n"
       
   303                        if iss1.getElementsByTagName('cause')[0].firstChild != None:
       
   304                            print >>errfile, "Issue: "+iss1.getElementsByTagName('cause')[0].firstChild.data+"\n"
       
   305                        else:
       
   306                            print >>errfile, "Issue: \n"
       
   307                        print >>errfile, "Description: "+iss1.getElementsByTagName('identitydescription')[0].firstChild.data +" "+ iss1.getElementsByTagName('typestring')[0].firstChild.data+" \n"
       
   308                        print >>errfile, "--------------------------------------------\n"
       
   309                        
       
   310                headerfound = 1
       
   311                break
       
   312             
       
   313         if headerfound == 0:
       
   314             if debug == 1:
       
   315                 print >>debfile, "NOT FOUND.\n"
       
   316             
       
   317             failed = 1
       
   318             print >>errfile, "--------------------------------------------\n"
       
   319             print >>errfile, "TESTCASE: "+tc+"\n\n"
       
   320             print >>errfile, "Difference: Header results missing from expected results:\n"
       
   321             print >>errfile, "Filename: "+actFile+"\n"
       
   322             if iss1.getElementsByTagName('cause')[0].firstChild != None:
       
   323                 print >>errfile, "Issue: "+iss1.getElementsByTagName('cause')[0].firstChild.data+"\n"
       
   324             else:
       
   325                 print >>errfile, "Issue: \n"
       
   326             print >>errfile, "Description: "+iss1.getElementsByTagName('identitydescription')[0].firstChild.data +" "+ iss1.getElementsByTagName('typestring')[0].firstChild.data+" \n"
       
   327             print >>errfile, "--------------------------------------------\n"
       
   328         
       
   329     if failed == 0:
       
   330         passedCount += 1
       
   331         ostr = ostr+"    <expresults>"+os.pardir+os.sep+expfilename+"</expresults>\n"
       
   332         ostr = ostr+"    <actresults>"+os.pardir+os.sep+actfilename+"</actresults>\n"
       
   333         ostr = ostr+"  </testcase>\n"
       
   334     else:
       
   335         ostr = ostr+"    <failure message=\"Failed\" type=\"Failed\">Failed</failure>\n"
       
   336         ostr = ostr+"    <expresults>"+os.pardir+os.sep+expfilename+"</expresults>\n"
       
   337         ostr = ostr+"    <actresults>"+os.pardir+os.sep+actfilename+"</actresults>\n"
       
   338         ostr = ostr+"  </testcase>\n"
       
   339 
       
   340 
       
   341 if len(sys.argv) == 1:
       
   342     Usage()
       
   343 if sys.argv[1] in usage:
       
   344     Usage()
       
   345 
       
   346 if sys.argv[1] == "-build":
       
   347     HABuild()
       
   348     
       
   349 ostr = ""
       
   350 if debug == 1:
       
   351     debfile = open("log/hatestdebug.txt","w") # Debug information is printed to this file.
       
   352 
       
   353 passedCount = 0 
       
   354 totalCount = 0 
       
   355 time_taken = 0
       
   356 total_time_taken = 0
       
   357 
       
   358 outfile = open("results/"+ sys.argv[1],"w") # Test execution results are printed here.
       
   359 errfile = open("log/hatestdiff.txt","w")  # Differences of not passed test cases are printed here.
       
   360 
       
   361 if len(sys.argv) == 2:
       
   362     RunAll()
       
   363 else:
       
   364     RunTC()
       
   365