Skip to content

Commit 98539f8

Browse files
committed
move utility functions
1 parent 7923697 commit 98539f8

File tree

2 files changed

+177
-163
lines changed

2 files changed

+177
-163
lines changed

task.js

Lines changed: 8 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
// @ts-check
22
const istanbul = require('istanbul-lib-coverage')
3-
const { join, resolve, isAbsolute } = require('path')
3+
const { join, resolve } = require('path')
44
const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs')
55
const execa = require('execa')
66
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')
814
const NYC = require('nyc')
915

1016
const debug = require('debug')('code-coverage')
@@ -37,166 +43,6 @@ function saveCoverage(coverage) {
3743
writeFileSync(nycFilename, JSON.stringify(coverage, null, 2))
3844
}
3945

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-
20046
const tasks = {
20147
/**
20248
* Clears accumulated code coverage information.

utils.js

Lines changed: 169 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1+
// @ts-check
12
/// <reference types="cypress" />
3+
const { readFileSync, writeFileSync, existsSync } = require('fs')
4+
const { isAbsolute, resolve, join } = require('path')
5+
const debug = require('debug')('code-coverage')
6+
7+
function checkAllPathsNotFound(nycFilename) {
8+
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))
9+
10+
const coverageKeys = Object.keys(nycCoverage)
11+
if (!coverageKeys.length) {
12+
console.error('⚠️ file %s has no coverage information', nycFilename)
13+
return
14+
}
15+
16+
const allFilesAreMissing = coverageKeys.every((key, k) => {
17+
const coverage = nycCoverage[key]
18+
return !existsSync(coverage.path)
19+
})
20+
21+
debug(
22+
'in file %s all files are not found? %o',
23+
nycFilename,
24+
allFilesAreMissing
25+
)
26+
return allFilesAreMissing
27+
}
228

329
/**
430
* remove coverage for the spec files themselves,
531
* only keep "external" application source file coverage
632
*/
733
const filterSpecsFromCoverage = (totalCoverage, config = Cypress.config) => {
834
const integrationFolder = config('integrationFolder')
35+
// @ts-ignore
936
const testFilePattern = config('testFiles')
1037

1138
// test files chould be:
@@ -85,8 +112,149 @@ function showNycInfo(nycFilename) {
85112
})
86113
}
87114

115+
/**
116+
* Looks at all coverage objects in the given JSON coverage file
117+
* and if the file is relative, and exists, changes its path to
118+
* be absolute.
119+
*/
120+
function resolveRelativePaths(nycFilename) {
121+
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))
122+
123+
const coverageKeys = Object.keys(nycCoverage)
124+
if (!coverageKeys.length) {
125+
console.error('⚠️ file %s has no coverage information', nycFilename)
126+
return
127+
}
128+
debug('NYC file %s has %d key(s)', nycFilename, coverageKeys.length)
129+
130+
let changed
131+
132+
coverageKeys.forEach((key, k) => {
133+
const coverage = nycCoverage[key]
134+
135+
if (!coverage.path) {
136+
debug('key %s does not have path', key)
137+
return
138+
}
139+
140+
if (!isAbsolute(coverage.path)) {
141+
if (existsSync(coverage.path)) {
142+
debug('resolving path %s', coverage.path)
143+
coverage.path = resolve(coverage.path)
144+
changed = true
145+
}
146+
return
147+
}
148+
149+
// path is absolute, let's check if it exists
150+
if (!existsSync(coverage.path)) {
151+
debug('⚠️ cannot find file %s with hash %s', coverage.path, coverage.hash)
152+
}
153+
})
154+
155+
if (changed) {
156+
debug('saving updated file %s', nycFilename)
157+
writeFileSync(
158+
nycFilename,
159+
JSON.stringify(nycCoverage, null, 2) + '\n',
160+
'utf8'
161+
)
162+
}
163+
}
164+
165+
/**
166+
* @param {string[]} filepaths
167+
* @returns {string | undefined} common prefix that corresponds to current folder
168+
*/
169+
function findCommonRoot(filepaths) {
170+
if (!filepaths.length) {
171+
debug('cannot find common root without any files')
172+
return
173+
}
174+
175+
// assuming / as file separator
176+
const splitParts = filepaths.map(name => name.split('/'))
177+
const lengths = splitParts.map(arr => arr.length)
178+
const shortestLength = Math.min.apply(null, lengths)
179+
debug('shorted file path has %d parts', shortestLength)
180+
181+
const cwd = process.cwd()
182+
let commonPrefix = []
183+
let foundCurrentFolder
184+
185+
for (let k = 0; k < shortestLength; k += 1) {
186+
const part = splitParts[0][k]
187+
const prefix = commonPrefix.concat(part).join('/')
188+
debug('testing prefix %o', prefix)
189+
const allFilesStart = filepaths.every(name => name.startsWith(prefix))
190+
if (!allFilesStart) {
191+
debug('stopped at non-common prefix %s', prefix)
192+
break
193+
}
194+
195+
commonPrefix.push(part)
196+
197+
const removedPrefixNames = filepaths.map(filepath =>
198+
filepath.slice(prefix.length)
199+
)
200+
debug('removedPrefix %o', removedPrefixNames)
201+
const foundAllPaths = removedPrefixNames.every(filepath =>
202+
existsSync(join(cwd, filepath))
203+
)
204+
debug('all files found at %s? %o', prefix, foundAllPaths)
205+
if (foundAllPaths) {
206+
debug('found prefix that matches current folder: %s', prefix)
207+
foundCurrentFolder = prefix
208+
break
209+
}
210+
}
211+
212+
return foundCurrentFolder
213+
}
214+
215+
function tryFindingLocalFiles(nycFilename) {
216+
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))
217+
const coverageKeys = Object.keys(nycCoverage)
218+
const filenames = coverageKeys.map(key => nycCoverage[key].path)
219+
const commonFolder = findCommonRoot(filenames)
220+
if (!commonFolder) {
221+
debug('could not find common folder %s', commonFolder)
222+
return
223+
}
224+
const cwd = process.cwd()
225+
debug(
226+
'found common folder %s that matches current working directory %s',
227+
commonFolder,
228+
cwd
229+
)
230+
const length = commonFolder.length
231+
let changed
232+
233+
coverageKeys.forEach(key => {
234+
const from = nycCoverage[key].path
235+
if (from.startsWith(commonFolder)) {
236+
const to = join(cwd, from.slice(length))
237+
nycCoverage[key].path = to
238+
debug('replaced %s -> %s', from, to)
239+
changed = true
240+
}
241+
})
242+
243+
if (changed) {
244+
debug('saving updated file %s', nycFilename)
245+
writeFileSync(
246+
nycFilename,
247+
JSON.stringify(nycCoverage, null, 2) + '\n',
248+
'utf8'
249+
)
250+
}
251+
}
252+
88253
module.exports = {
89254
fixSourcePathes,
90255
filterSpecsFromCoverage,
91-
showNycInfo
256+
showNycInfo,
257+
resolveRelativePaths,
258+
checkAllPathsNotFound,
259+
tryFindingLocalFiles
92260
}

0 commit comments

Comments
 (0)