@@ -34,12 +34,12 @@ var token = process.env.COVERALLS_TOKEN;
3434 * @returns {Object } key `Hash` contains MD5 digest string of file; `count` contains number of lines in source file
3535 */
3636function md5 ( path ) {
37- const hash = crypto . createHash ( 'md5' ) ; // Creating hash object
38- const buf = fs . readFileSync ( path , 'utf-8' ) ; // Read in file
39- const count = buf . split ( / \r \n | \r | \n / ) . length ; // Count the number of lines
40- hash . update ( buf , 'utf-8' ) ; // Update hash
37+ const hash = crypto . createHash ( 'md5' ) ; // Creating hash object
38+ const buf = fs . readFileSync ( path , 'utf-8' ) ; // Read in file
39+ const count = buf . split ( / \r \n | \r | \n / ) . length ; // Count the number of lines
40+ hash . update ( buf , 'utf-8' ) ; // Update hash
4141
42- return { hash : hash . digest ( 'hex' ) , count : count } ;
42+ return { hash : hash . digest ( 'hex' ) , count : count } ;
4343}
4444
4545
@@ -53,35 +53,37 @@ function md5(path) {
5353 * @todo Generalize path default
5454 */
5555async function formatCoverage ( classList , srcPath , sha ) {
56- var job = { } ;
57- var sourceFiles = [ ] ;
58- var digest ;
59- srcPath = typeof srcPath != "undefined" ? srcPath : process . env . REPO_PATH ; // default to home dir
60- // For each class, create file object containing array of lines covered and add to sourceFile array
61- await Promise . all ( classList . map ( async c => {
62- let file = { } ; // Initialize file object
63- let fullPath = c . $ . filename . startsWith ( srcPath ) ? c . $ . filename : path . join ( srcPath , c . $ . filename ) ;
64- digest = md5 ( fullPath ) ; // Create digest and line count for file
65- let lines = new Array ( digest . count ) . fill ( null ) ; // Initialize line array the size of source code file
66- c . lines [ 0 ] . line . forEach ( ln => {
67- let n = Number ( ln . $ . number ) ;
68- if ( n <= digest . count ) { lines [ n ] = Number ( ln . $ . hits ) }
69- } ) ;
70- // create source file object
71- file . name = c . $ . filename ;
72- file . source_digest = digest . hash ;
73- file . coverage = lines ; // file.coverage[0] == line 1
74- sourceFiles . push ( file ) ;
75- } ) ) ;
56+ var job = { } ;
57+ var sourceFiles = [ ] ;
58+ var digest ;
59+ srcPath = typeof srcPath != 'undefined' ? srcPath : process . env . REPO_PATH ; // default to home dir
60+ // For each class, create file object containing array of lines covered and add to sourceFile array
61+ await Promise . all ( classList . map ( async c => {
62+ let file = { } ; // Initialize file object
63+ let fullPath = c . $ . filename . startsWith ( srcPath ) ? c . $ . filename : path . join ( srcPath , c . $ . filename ) ;
64+ digest = md5 ( fullPath ) ; // Create digest and line count for file
65+ let lines = new Array ( digest . count ) . fill ( null ) ; // Initialize line array the size of source code file
66+ c . lines [ 0 ] . line . forEach ( ln => {
67+ let n = Number ( ln . $ . number ) ;
68+ if ( n <= digest . count ) {
69+ lines [ n ] = Number ( ln . $ . hits ) ;
70+ }
71+ } ) ;
72+ // create source file object
73+ file . name = c . $ . filename ;
74+ file . source_digest = digest . hash ;
75+ file . coverage = lines ; // file.coverage[0] == line 1
76+ sourceFiles . push ( file ) ;
77+ } ) ) ;
7678
77- job . repo_token = token ; // env secret token
78- job . service_name = `coverage/${ process . env . USERDOMAIN } ` ;
79- // The associated pull request ID of the build. Used for updating the status and/or commenting.
80- job . service_pull_request = '' ;
81- job . source_files = sourceFiles ;
82- job . commit_sha = sha ;
83- job . run_at = timestamp ; // "2013-02-18 00:52:48 -0800"
84- return job ;
79+ job . repo_token = token ; // env secret token
80+ job . service_name = `coverage/${ process . env . USERDOMAIN } ` ;
81+ // The associated pull request ID of the build. Used for updating the status and/or commenting.
82+ job . service_pull_request = '' ;
83+ job . source_files = sourceFiles ;
84+ job . commit_sha = sha ;
85+ job . run_at = timestamp ; // "2013-02-18 00:52:48 -0800"
86+ return job ;
8587}
8688
8789/**
@@ -94,37 +96,39 @@ async function formatCoverage(classList, srcPath, sha) {
9496 * @see {@link https://github.com/cobertura/cobertura/wiki|Cobertura Wiki }
9597 */
9698function coverage ( path , repo , sha , submodules ) {
97- return fs . promises . readFile ( path ) // Read in XML file
98- . then ( parser . parseStringPromise ) // Parse XML
99- . then ( result => {
100- // Extract root code path
101- const rootPath = ( result . coverage . sources [ 0 ] . source [ 0 ] || process . env . REPO_PATH ) . replace ( / [ \/ | \\ ] + $ / , '' )
102- timestamp = new Date ( result . coverage . $ . timestamp * 1000 ) ; // Convert UNIX timestamp to Date object
103- let classes = [ ] ; // Initialize classes array
99+ return fs . promises . readFile ( path ) // Read in XML file
100+ . then ( parser . parseStringPromise ) // Parse XML
101+ . then ( result => {
102+ // Extract root code path
103+ const rootPath = ( result . coverage . sources [ 0 ] . source [ 0 ] || process . env . REPO_PATH )
104+ . replace ( / [ \/ | \\ ] + $ / , '' ) ;
105+ timestamp = new Date ( result . coverage . $ . timestamp * 1000 ) ; // Convert UNIX timestamp to Date object
106+ let classes = [ ] ; // Initialize classes array
104107
105- const packages = result . coverage . packages [ 0 ] . package ;
106- packages . forEach ( pkg => { classes . push ( pkg . classes [ 0 ] . class ) } ) ; // Get all classes
107- classes = classes . reduce ( ( acc , val ) => acc . concat ( val ) , [ ] ) ; // Flatten
108+ const packages = result . coverage . packages [ 0 ] . package ;
109+ packages . forEach ( pkg => { classes . push ( pkg . classes [ 0 ] . class ) ; } ) ; // Get all classes
110+ classes = classes . reduce ( ( acc , val ) => acc . concat ( val ) , [ ] ) ; // Flatten
108111
109- // The submodules
110- const byModule = { 'main' : [ ] } ;
111- submodules . forEach ( ( x ) => { byModule [ x ] = [ ] ; } ) ; // initialize submodules
112+ // The submodules
113+ const byModule = { 'main' : [ ] } ;
114+ submodules . forEach ( ( x ) => { byModule [ x ] = [ ] ; } ) ; // initialize submodules
112115
113- // Sort into piles
114- byModule [ 'main' ] = classes . filter ( function ( e ) {
115- if ( e . $ . filename . search ( / ( t e s t s \\ | _ .* t e s t | d o c s \\ ) / i) !== - 1 ) { return false ; } // Filter out tests and docs
116- if ( ! Array . isArray ( e . lines [ 0 ] . line ) ) { return false ; } // Filter out files with no functional lines
117- for ( let submodule of submodules ) {
118- if ( e . $ . filename . startsWith ( submodule ) ) {
119- byModule [ submodule ] . push ( e ) ; return false ;
120- }
121- }
122- return true ;
116+ // Sort into piles
117+ byModule [ 'main' ] = classes . filter ( function ( e ) {
118+ if ( e . $ . filename . search ( / ( t e s t s \\ | _ .* t e s t | d o c s \\ ) / i) !== - 1 ) return false ; // Filter out tests and docs
119+ if ( ! Array . isArray ( e . lines [ 0 ] . line ) ) return false ; // Filter out files with no functional lines
120+ for ( let submodule of submodules ) {
121+ if ( e . $ . filename . startsWith ( submodule ) ) {
122+ byModule [ submodule ] . push ( e ) ;
123+ return false ;
124+ }
125+ }
126+ return true ;
127+ } ) ;
128+ // Select module
129+ let modules = byModule [ repo ] || byModule [ 'main' ] ;
130+ return formatCoverage ( modules , rootPath , sha ) ;
123131 } ) ;
124- // Select module
125- let modules = byModule [ repo ] || byModule [ 'main' ] ;
126- return formatCoverage ( modules , rootPath , sha ) ;
127- } ) ;
128132}
129133
130134
0 commit comments