Skip to content

feat: find missing files when using all:true option #208

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 7 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
search using default extension masks if include is not present
  • Loading branch information
bahmutov committed Apr 21, 2020
commit 2f7009066d72e93425f49ee40b54a7c84e6a0d99
3 changes: 2 additions & 1 deletion examples/all-files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"report": "../../node_modules/.bin/nyc report"
},
"nyc": {
"all": true
"all": true,
"include": "*.js"
},
"devDependencies": {
"@babel/core": "7.9.0"
Expand Down
50 changes: 42 additions & 8 deletions task-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,24 +235,35 @@ function tryFindingLocalFiles(nycFilename) {
}

/**
* If the website or unit tests did not load ALL files we need to
* include, then we should include the missing files ourselves
* before generating the report.
*
* @see https://github.com/cypress-io/code-coverage/issues/207
* Tries to find source files to be included in the final coverage report
* using NYC options: extension list, include and exclude.
*/
function includeAllFiles(nycFilename, nycOptions) {
function findSourceFiles(nycOptions) {
debug('include all files options: %o', {
all: nycOptions.all,
include: nycOptions.include,
exclude: nycOptions.exclude
exclude: nycOptions.exclude,
extension: nycOptions.extension
})

if (!Array.isArray(nycOptions.extension)) {
console.error(
'Expected NYC "extension" option to be a list of file extensions'
)
console.error(nycOptions)
return []
}

let patterns = []
if (Array.isArray(nycOptions.include)) {
patterns = patterns.concat(nycOptions.include)
} else if (typeof nycOptions.include === 'string') {
patterns.push(nycOptions.include)
} else {
debug('using default list of extensions')
nycOptions.extension.forEach(extension => {
patterns.push('**/*' + extension)
})
}

if (Array.isArray(nycOptions.exclude)) {
Expand All @@ -268,7 +279,30 @@ function includeAllFiles(nycFilename, nycOptions) {
debug('searching files to include using patterns %o', patterns)

const allFiles = globby.sync(patterns, { absolute: true })
debug('found these files %o', allFiles)
return allFiles
}
/**
* If the website or unit tests did not load ALL files we need to
* include, then we should include the missing files ourselves
* before generating the report.
*
* @see https://github.com/cypress-io/code-coverage/issues/207
*/
function includeAllFiles(nycFilename, nycOptions) {
if (!nycOptions.all) {
debug('NYC "all" option is not set, skipping including all files')
return
}

const allFiles = findSourceFiles(nycOptions)
if (debug.enabled) {
debug('found %d file(s)', allFiles.length)
console.error(allFiles.join('\n'))
}
if (!allFiles.length) {
debug('no files found, hoping for the best')
return
}

const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))
const coverageKeys = Object.keys(nycCoverage)
Expand Down