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