Skip to content

feat: automatically resolve non-absolute paths to files #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ workflows:
- run:
name: Check code coverage 📈
command: |
node ../../scripts/check-coverage server.js
node ../../scripts/check-coverage main.js
node ../../scripts/check-coverage string-utils.js
node ../../scripts/check-coverage fullstack/server/server.js
node ../../scripts/check-coverage fullstack/main.js
node ../../scripts/check-coverage fullstack/string-utils.js
node ../../scripts/only-covered server.js main.js string-utils.js
working_directory: examples/fullstack

Expand Down
33 changes: 32 additions & 1 deletion task.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-check
const istanbul = require('istanbul-lib-coverage')
const { join, resolve } = require('path')
const { join, resolve, isAbsolute } = require('path')
const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs')
const execa = require('execa')
const fs = require('fs')
Expand Down Expand Up @@ -37,6 +37,35 @@ function saveCoverage(coverage) {
writeFileSync(nycFilename, JSON.stringify(coverage, null, 2))
}

/**
* Looks at all coverage objects in the given JSON coverage file
* and if the file is relative, and exists, changes its path to
* be absolute.
*/
function resolvePaths(nycFilename) {
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))
let changed
Object.keys(nycCoverage).forEach(key => {
const coverage = nycCoverage[key]
if (coverage.path && !isAbsolute(coverage.path)) {
if (existsSync(coverage.path)) {
debug('resolving path %s', coverage.path)
coverage.path = resolve(coverage.path)
changed = true
}
}
})

if (changed) {
debug('saving updated file %s', nycFilename)
writeFileSync(
nycFilename,
JSON.stringify(nycCoverage, null, 2) + '\n',
'utf8'
)
}
}

const tasks = {
/**
* Clears accumulated code coverage information.
Expand Down Expand Up @@ -97,6 +126,8 @@ const tasks = {
return null
}

resolvePaths(nycFilename)

if (customNycReportScript) {
debug(
'saving coverage report using script "%s" from package.json, command: %s',
Expand Down