|
1 | 1 | // @ts-check
|
2 | 2 | const istanbul = require('istanbul-lib-coverage')
|
3 |
| -const { join, resolve, isAbsolute } = require('path') |
| 3 | +const { join, resolve } = require('path') |
4 | 4 | const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs')
|
5 | 5 | const execa = require('execa')
|
6 | 6 | const path = require('path')
|
7 |
| -const { fixSourcePathes, showNycInfo } = require('./utils') |
| 7 | +const { |
| 8 | + fixSourcePathes, |
| 9 | + showNycInfo, |
| 10 | + resolveRelativePaths, |
| 11 | + checkAllPathsNotFound, |
| 12 | + tryFindingLocalFiles |
| 13 | +} = require('./utils') |
8 | 14 | const NYC = require('nyc')
|
9 | 15 |
|
10 | 16 | const debug = require('debug')('code-coverage')
|
@@ -37,166 +43,6 @@ function saveCoverage(coverage) {
|
37 | 43 | writeFileSync(nycFilename, JSON.stringify(coverage, null, 2))
|
38 | 44 | }
|
39 | 45 |
|
40 |
| -/** |
41 |
| - * @param {string[]} filepaths |
42 |
| - * @returns {string | undefined} common prefix that corresponds to current folder |
43 |
| - */ |
44 |
| -function findCommonRoot(filepaths) { |
45 |
| - if (!filepaths.length) { |
46 |
| - debug('cannot find common root without any files') |
47 |
| - return |
48 |
| - } |
49 |
| - |
50 |
| - // assuming / as file separator |
51 |
| - const splitParts = filepaths.map(name => name.split('/')) |
52 |
| - const lengths = splitParts.map(arr => arr.length) |
53 |
| - const shortestLength = Math.min.apply(null, lengths) |
54 |
| - debug('shorted file path has %d parts', shortestLength) |
55 |
| - |
56 |
| - const cwd = process.cwd() |
57 |
| - let commonPrefix = [] |
58 |
| - let foundCurrentFolder |
59 |
| - |
60 |
| - for (let k = 0; k < shortestLength; k += 1) { |
61 |
| - const part = splitParts[0][k] |
62 |
| - const prefix = commonPrefix.concat(part).join('/') |
63 |
| - debug('testing prefix %o', prefix) |
64 |
| - const allFilesStart = filepaths.every(name => name.startsWith(prefix)) |
65 |
| - if (!allFilesStart) { |
66 |
| - debug('stopped at non-common prefix %s', prefix) |
67 |
| - break |
68 |
| - } |
69 |
| - |
70 |
| - commonPrefix.push(part) |
71 |
| - |
72 |
| - const removedPrefixNames = filepaths.map(filepath => |
73 |
| - filepath.slice(prefix.length) |
74 |
| - ) |
75 |
| - debug('removedPrefix %o', removedPrefixNames) |
76 |
| - const foundAllPaths = removedPrefixNames.every(filepath => |
77 |
| - existsSync(path.join(cwd, filepath)) |
78 |
| - ) |
79 |
| - debug('all files found at %s? %o', prefix, foundAllPaths) |
80 |
| - if (foundAllPaths) { |
81 |
| - debug('found prefix that matches current folder: %s', prefix) |
82 |
| - foundCurrentFolder = prefix |
83 |
| - break |
84 |
| - } |
85 |
| - } |
86 |
| - |
87 |
| - return foundCurrentFolder |
88 |
| -} |
89 |
| - |
90 |
| -function tryFindingLocalFiles(nycFilename) { |
91 |
| - const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8')) |
92 |
| - const coverageKeys = Object.keys(nycCoverage) |
93 |
| - const filenames = coverageKeys.map(key => nycCoverage[key].path) |
94 |
| - const commonFolder = findCommonRoot(filenames) |
95 |
| - if (!commonFolder) { |
96 |
| - debug('could not find common folder %s', commonFolder) |
97 |
| - return |
98 |
| - } |
99 |
| - const cwd = process.cwd() |
100 |
| - debug( |
101 |
| - 'found common folder %s that matches current working directory %s', |
102 |
| - commonFolder, |
103 |
| - cwd |
104 |
| - ) |
105 |
| - const length = commonFolder.length |
106 |
| - let changed |
107 |
| - |
108 |
| - coverageKeys.forEach(key => { |
109 |
| - const from = nycCoverage[key].path |
110 |
| - if (from.startsWith(commonFolder)) { |
111 |
| - const to = path.join(cwd, from.slice(length)) |
112 |
| - nycCoverage[key].path = to |
113 |
| - debug('replaced %s -> %s', from, to) |
114 |
| - changed = true |
115 |
| - } |
116 |
| - }) |
117 |
| - |
118 |
| - if (changed) { |
119 |
| - debug('saving updated file %s', nycFilename) |
120 |
| - writeFileSync( |
121 |
| - nycFilename, |
122 |
| - JSON.stringify(nycCoverage, null, 2) + '\n', |
123 |
| - 'utf8' |
124 |
| - ) |
125 |
| - } |
126 |
| -} |
127 |
| - |
128 |
| -function checkAllPathsNotFound(nycFilename) { |
129 |
| - const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8')) |
130 |
| - |
131 |
| - const coverageKeys = Object.keys(nycCoverage) |
132 |
| - if (!coverageKeys.length) { |
133 |
| - console.error('⚠️ file %s has no coverage information', nycFilename) |
134 |
| - return |
135 |
| - } |
136 |
| - |
137 |
| - const allFilesAreMissing = coverageKeys.every((key, k) => { |
138 |
| - const coverage = nycCoverage[key] |
139 |
| - return !existsSync(coverage.path) |
140 |
| - }) |
141 |
| - |
142 |
| - debug( |
143 |
| - 'in file %s all files are not found? %o', |
144 |
| - nycFilename, |
145 |
| - allFilesAreMissing |
146 |
| - ) |
147 |
| - return allFilesAreMissing |
148 |
| -} |
149 |
| - |
150 |
| -/** |
151 |
| - * Looks at all coverage objects in the given JSON coverage file |
152 |
| - * and if the file is relative, and exists, changes its path to |
153 |
| - * be absolute. |
154 |
| - */ |
155 |
| -function resolveRelativePaths(nycFilename) { |
156 |
| - const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8')) |
157 |
| - |
158 |
| - const coverageKeys = Object.keys(nycCoverage) |
159 |
| - if (!coverageKeys.length) { |
160 |
| - console.error('⚠️ file %s has no coverage information', nycFilename) |
161 |
| - return |
162 |
| - } |
163 |
| - debug('NYC file %s has %d key(s)', nycFilename, coverageKeys.length) |
164 |
| - |
165 |
| - let changed |
166 |
| - |
167 |
| - coverageKeys.forEach((key, k) => { |
168 |
| - const coverage = nycCoverage[key] |
169 |
| - |
170 |
| - if (!coverage.path) { |
171 |
| - debug('key %s does not have path', key) |
172 |
| - return |
173 |
| - } |
174 |
| - |
175 |
| - if (!isAbsolute(coverage.path)) { |
176 |
| - if (existsSync(coverage.path)) { |
177 |
| - debug('resolving path %s', coverage.path) |
178 |
| - coverage.path = resolve(coverage.path) |
179 |
| - changed = true |
180 |
| - } |
181 |
| - return |
182 |
| - } |
183 |
| - |
184 |
| - // path is absolute, let's check if it exists |
185 |
| - if (!existsSync(coverage.path)) { |
186 |
| - debug('⚠️ cannot find file %s with hash %s', coverage.path, coverage.hash) |
187 |
| - } |
188 |
| - }) |
189 |
| - |
190 |
| - if (changed) { |
191 |
| - debug('saving updated file %s', nycFilename) |
192 |
| - writeFileSync( |
193 |
| - nycFilename, |
194 |
| - JSON.stringify(nycCoverage, null, 2) + '\n', |
195 |
| - 'utf8' |
196 |
| - ) |
197 |
| - } |
198 |
| -} |
199 |
| - |
200 | 46 | const tasks = {
|
201 | 47 | /**
|
202 | 48 | * Clears accumulated code coverage information.
|
|
0 commit comments