Skip to content

Commit 461910d

Browse files
authored
feat: automatically resolve non-absolute paths to files (cypress-io#189)
* feat: automatically try to resolve relative paths to files * only save coverage file if it has been updated
1 parent 0367d1b commit 461910d

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

.circleci/config.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ workflows:
130130
- run:
131131
name: Check code coverage 📈
132132
command: |
133-
node ../../scripts/check-coverage server.js
134-
node ../../scripts/check-coverage main.js
135-
node ../../scripts/check-coverage string-utils.js
133+
node ../../scripts/check-coverage fullstack/server/server.js
134+
node ../../scripts/check-coverage fullstack/main.js
135+
node ../../scripts/check-coverage fullstack/string-utils.js
136136
node ../../scripts/only-covered server.js main.js string-utils.js
137137
working_directory: examples/fullstack
138138

task.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-check
22
const istanbul = require('istanbul-lib-coverage')
3-
const { join, resolve } = require('path')
3+
const { join, resolve, isAbsolute } = require('path')
44
const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs')
55
const execa = require('execa')
66
const fs = require('fs')
@@ -37,6 +37,35 @@ function saveCoverage(coverage) {
3737
writeFileSync(nycFilename, JSON.stringify(coverage, null, 2))
3838
}
3939

40+
/**
41+
* Looks at all coverage objects in the given JSON coverage file
42+
* and if the file is relative, and exists, changes its path to
43+
* be absolute.
44+
*/
45+
function resolvePaths(nycFilename) {
46+
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))
47+
let changed
48+
Object.keys(nycCoverage).forEach(key => {
49+
const coverage = nycCoverage[key]
50+
if (coverage.path && !isAbsolute(coverage.path)) {
51+
if (existsSync(coverage.path)) {
52+
debug('resolving path %s', coverage.path)
53+
coverage.path = resolve(coverage.path)
54+
changed = true
55+
}
56+
}
57+
})
58+
59+
if (changed) {
60+
debug('saving updated file %s', nycFilename)
61+
writeFileSync(
62+
nycFilename,
63+
JSON.stringify(nycCoverage, null, 2) + '\n',
64+
'utf8'
65+
)
66+
}
67+
}
68+
4069
const tasks = {
4170
/**
4271
* Clears accumulated code coverage information.
@@ -97,6 +126,8 @@ const tasks = {
97126
return null
98127
}
99128

129+
resolvePaths(nycFilename)
130+
100131
if (customNycReportScript) {
101132
debug(
102133
'saving coverage report using script "%s" from package.json, command: %s',

0 commit comments

Comments
 (0)