1
1
"use strict" ;
2
2
3
+ var fs = require ( "fs" ) ;
3
4
var Promise = require ( "bluebird" ) ;
4
5
var lcov_parse = require ( "lcov-parse" ) ;
5
6
var _ = require ( "lodash" ) ;
6
7
var vile = require ( "vile" ) ;
8
+ var fs_stat = Promise . promisify ( fs . stat ) ;
9
+
10
+ var log = vile . logger . create ( "coverage" ) ;
7
11
8
12
var DEFAULT_COV_DIR = "coverage" ;
9
13
10
14
var total_cov = function total_cov ( lines ) {
11
- return ! lines ? 0 : lines . length <= 0 ? 100 : _ . reduce ( lines , function ( count , item ) {
15
+ return _ . isEmpty ( lines ) ? 0 : _ . reduce ( lines , function ( count , item ) {
12
16
return item . hit > 0 ? count + 1 : count ;
13
17
} , 0 ) / lines . length * 100 ;
14
18
} ;
@@ -35,29 +39,42 @@ var possible_lcov_file = function possible_lcov_file(target) {
35
39
var parse_lcov_file_into_issues = function parse_lcov_file_into_issues ( lcov_path ) {
36
40
return new Promise ( function ( resolve , reject ) {
37
41
lcov_parse ( lcov_path , function ( err , lcov ) {
38
- if ( err ) reject ( err ) ; else resolve ( lcov_into_issues ( lcov ) ) ;
42
+ if ( err ) log . warn ( err ) ;
43
+ resolve ( lcov_into_issues ( lcov ) ) ;
39
44
} ) ;
40
45
} ) ;
41
46
} ;
42
47
48
+ var warn_and_resolve = function warn_and_resolve ( lcov_path ) {
49
+ log . warn ( "can't find path: \"" + lcov_path + "\"" ) ;
50
+ return Promise . resolve ( [ ] ) ;
51
+ } ;
52
+
43
53
var detect_lcov_into_issues = function detect_lcov_into_issues ( ) {
44
- return vile . promise_each ( DEFAULT_COV_DIR , function ( target , is_dir ) {
54
+ var dirpath = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : DEFAULT_COV_DIR ;
55
+ return fs . existsSync ( dirpath ) ? vile . promise_each ( dirpath , function ( target , is_dir ) {
45
56
return is_dir || possible_lcov_file ( target ) ;
46
57
} , function ( file ) {
47
- return parse_lcov_file_into_issue ( file ) ;
48
- } , { read_data : false } ) ;
58
+ return parse_lcov_file_into_issues ( file ) ;
59
+ } , { read_data : false } ) : warn_and_resolve ( dirpath ) ;
60
+ } ;
61
+
62
+ var lcov_paths_into_issues = function lcov_paths_into_issues ( lcov_path ) {
63
+ return fs . existsSync ( lcov_path ) ? fs_stat ( lcov_path ) . then ( function ( stats ) {
64
+ return stats . isDirectory ( ) ? detect_lcov_into_issues ( lcov_path ) : parse_lcov_file_into_issues ( lcov_path ) ;
65
+ } ) : warn_and_resolve ( lcov_path ) ;
49
66
} ;
50
67
51
68
var report_cov = function report_cov ( plugin_config ) {
52
- var lcov_paths = _ . get ( plugin_config , "config.path " ) ;
69
+ var lcov_paths = _ . get ( plugin_config , "config.paths " ) ;
53
70
54
- if ( _ . isEmpty ( lcov_paths ) ) {
55
- return detect_lcov_into_issues ( ) ;
56
- } else {
57
- return Promise . map ( _ . concat ( [ ] , lcov_paths ) , function ( lcov_path ) {
58
- return detect_lcov_into_issues ( lcov_path ) ;
59
- } ) ;
60
- }
71
+ return ( _ . isEmpty ( lcov_paths ) ? detect_lcov_into_issues ( ) : Promise . map ( _ . concat ( [ ] , lcov_paths ) , lcov_paths_into_issues ) ) . then ( function ( issues ) {
72
+ var flat_issues = _ . flatten ( issues ) ;
73
+ if ( _ . isEmpty ( flat_issues ) ) {
74
+ log . warn ( "no coverage data was found" ) ;
75
+ }
76
+ return flat_issues ;
77
+ } ) ;
61
78
} ;
62
79
63
80
module . exports = {
0 commit comments