github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/bddtests/steps/coverage.py (about) 1 import cStringIO 2 import os 3 import glob 4 import errno 5 from collections import OrderedDict 6 7 import bdd_test_util 8 9 10 def testCoverage(): 11 #First save the coverage files 12 saveCoverageFiles("coverage","scenario_1", ["bddtests_vp0_1","bddtests_vp1_1","bddtests_vp2_1","bddtests_vp3_1",], "cov") 13 14 # Now collect the filenames for coverage files. 15 files = glob.glob(os.path.join('coverage','*.cov')) 16 17 #Create the aggregate coverage file 18 coverageContents = createCoverageFile(files) 19 20 #Ouput the aggregate coverage file 21 with open('coverage.total', 'w') as outfile: 22 outfile.write(coverageContents) 23 outfile.close() 24 25 def createCoverageAggregate(): 26 # Now collect the filenames for coverage files. 27 files = glob.glob(os.path.join('coverage','*.cov')) 28 29 #Create the aggregate coverage file 30 coverageContents = createCoverageFile(files) 31 32 #Ouput the aggregate coverage file 33 with open('coverage-behave.cov', 'w') as outfile: 34 outfile.write(coverageContents) 35 outfile.close() 36 37 38 def saveCoverageFiles(folderName, rootName, containerNames, extension): 39 'Will save the converage files to folderName' 40 # Make the directory 41 try: 42 os.makedirs(folderName) 43 except OSError as exception: 44 if exception.errno != errno.EEXIST: 45 raise 46 for containerName in containerNames: 47 srcPath = "{0}:/opt/gopath/src/github.com/hyperledger/fabric/coverage.cov".format(containerName) 48 print("sourcepath = {0}".format(srcPath)) 49 destPath = os.path.join(folderName, "{0}-{1}.{2}".format(rootName, containerName, extension)) 50 output, error, returncode = \ 51 bdd_test_util.cli_call(["docker", "cp", srcPath, destPath], expect_success=False) 52 53 def testCreateSystemCoverageFile(folderName, rootName, containerNames, extension): 54 'Will create a single aggregate coverage file fromsave the converage files to folderName' 55 files = glob.glob(os.path.join('coverage','*.cov')) 56 for containerName in containerNames: 57 srcPath = "{0}:/opt/gopath/src/github.com/hyperledger/fabric/peer/coverage.cov".format(containerName) 58 destPath = os.path.join(folderName, "{0}-{1}.{2}".format(rootName, containerName, extension)) 59 output, error, returncode = \ 60 bdd_test_util.cli_call(["docker", "cp", srcPath, destPath], expect_success=False) 61 62 63 def createCoverageFile(filenames): 64 """Creates an aggregated coverage file""" 65 output = cStringIO.StringIO() 66 output.write('mode: count\n') 67 linesMap = {} 68 #with open('coverage.total', 'w') as outfile: 69 for fname in filenames: 70 with open(fname) as infile: 71 firstLine = True 72 for line in infile: 73 if firstLine: 74 firstLine = False 75 continue 76 else: 77 # Split the line based upon white space 78 lineParts = line.split() 79 if lineParts[0] in linesMap: 80 # Found, keep the greater 81 newCount = long(lineParts[2]) 82 oldCount = long(linesMap[lineParts[0]].split()[2]) 83 if newCount > oldCount: 84 linesMap[lineParts[0]] = line 85 else: 86 linesMap[lineParts[0]] = line 87 # Now sort the output 88 od = OrderedDict(sorted(linesMap.items(), key=lambda i: i[1])) 89 90 for (key, line) in od.items(): 91 output.write(line) 92 contents = output.getvalue() 93 output.close() 94 return contents