Skip to content

feat: find local files better #197

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 6 commits into from
Apr 17, 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
Next Next commit
move utility functions
  • Loading branch information
bahmutov committed Apr 17, 2020
commit 98539f8a17810bb4127f938e4392b4cd30f9e0fc
170 changes: 8 additions & 162 deletions task.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
// @ts-check
const istanbul = require('istanbul-lib-coverage')
const { join, resolve, isAbsolute } = require('path')
const { join, resolve } = require('path')
const { existsSync, mkdirSync, readFileSync, writeFileSync } = require('fs')
const execa = require('execa')
const path = require('path')
const { fixSourcePathes, showNycInfo } = require('./utils')
const {
fixSourcePathes,
showNycInfo,
resolveRelativePaths,
checkAllPathsNotFound,
tryFindingLocalFiles
} = require('./utils')
const NYC = require('nyc')

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

/**
* @param {string[]} filepaths
* @returns {string | undefined} common prefix that corresponds to current folder
*/
function findCommonRoot(filepaths) {
if (!filepaths.length) {
debug('cannot find common root without any files')
return
}

// assuming / as file separator
const splitParts = filepaths.map(name => name.split('/'))
const lengths = splitParts.map(arr => arr.length)
const shortestLength = Math.min.apply(null, lengths)
debug('shorted file path has %d parts', shortestLength)

const cwd = process.cwd()
let commonPrefix = []
let foundCurrentFolder

for (let k = 0; k < shortestLength; k += 1) {
const part = splitParts[0][k]
const prefix = commonPrefix.concat(part).join('/')
debug('testing prefix %o', prefix)
const allFilesStart = filepaths.every(name => name.startsWith(prefix))
if (!allFilesStart) {
debug('stopped at non-common prefix %s', prefix)
break
}

commonPrefix.push(part)

const removedPrefixNames = filepaths.map(filepath =>
filepath.slice(prefix.length)
)
debug('removedPrefix %o', removedPrefixNames)
const foundAllPaths = removedPrefixNames.every(filepath =>
existsSync(path.join(cwd, filepath))
)
debug('all files found at %s? %o', prefix, foundAllPaths)
if (foundAllPaths) {
debug('found prefix that matches current folder: %s', prefix)
foundCurrentFolder = prefix
break
}
}

return foundCurrentFolder
}

function tryFindingLocalFiles(nycFilename) {
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))
const coverageKeys = Object.keys(nycCoverage)
const filenames = coverageKeys.map(key => nycCoverage[key].path)
const commonFolder = findCommonRoot(filenames)
if (!commonFolder) {
debug('could not find common folder %s', commonFolder)
return
}
const cwd = process.cwd()
debug(
'found common folder %s that matches current working directory %s',
commonFolder,
cwd
)
const length = commonFolder.length
let changed

coverageKeys.forEach(key => {
const from = nycCoverage[key].path
if (from.startsWith(commonFolder)) {
const to = path.join(cwd, from.slice(length))
nycCoverage[key].path = to
debug('replaced %s -> %s', from, to)
changed = true
}
})

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

function checkAllPathsNotFound(nycFilename) {
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))

const coverageKeys = Object.keys(nycCoverage)
if (!coverageKeys.length) {
console.error('⚠️ file %s has no coverage information', nycFilename)
return
}

const allFilesAreMissing = coverageKeys.every((key, k) => {
const coverage = nycCoverage[key]
return !existsSync(coverage.path)
})

debug(
'in file %s all files are not found? %o',
nycFilename,
allFilesAreMissing
)
return allFilesAreMissing
}

/**
* 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 resolveRelativePaths(nycFilename) {
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))

const coverageKeys = Object.keys(nycCoverage)
if (!coverageKeys.length) {
console.error('⚠️ file %s has no coverage information', nycFilename)
return
}
debug('NYC file %s has %d key(s)', nycFilename, coverageKeys.length)

let changed

coverageKeys.forEach((key, k) => {
const coverage = nycCoverage[key]

if (!coverage.path) {
debug('key %s does not have path', key)
return
}

if (!isAbsolute(coverage.path)) {
if (existsSync(coverage.path)) {
debug('resolving path %s', coverage.path)
coverage.path = resolve(coverage.path)
changed = true
}
return
}

// path is absolute, let's check if it exists
if (!existsSync(coverage.path)) {
debug('⚠️ cannot find file %s with hash %s', coverage.path, coverage.hash)
}
})

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
170 changes: 169 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
// @ts-check
/// <reference types="cypress" />
const { readFileSync, writeFileSync, existsSync } = require('fs')
const { isAbsolute, resolve, join } = require('path')
const debug = require('debug')('code-coverage')

function checkAllPathsNotFound(nycFilename) {
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))

const coverageKeys = Object.keys(nycCoverage)
if (!coverageKeys.length) {
console.error('⚠️ file %s has no coverage information', nycFilename)
return
}

const allFilesAreMissing = coverageKeys.every((key, k) => {
const coverage = nycCoverage[key]
return !existsSync(coverage.path)
})

debug(
'in file %s all files are not found? %o',
nycFilename,
allFilesAreMissing
)
return allFilesAreMissing
}

/**
* remove coverage for the spec files themselves,
* only keep "external" application source file coverage
*/
const filterSpecsFromCoverage = (totalCoverage, config = Cypress.config) => {
const integrationFolder = config('integrationFolder')
// @ts-ignore
const testFilePattern = config('testFiles')

// test files chould be:
Expand Down Expand Up @@ -85,8 +112,149 @@ function showNycInfo(nycFilename) {
})
}

/**
* 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 resolveRelativePaths(nycFilename) {
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))

const coverageKeys = Object.keys(nycCoverage)
if (!coverageKeys.length) {
console.error('⚠️ file %s has no coverage information', nycFilename)
return
}
debug('NYC file %s has %d key(s)', nycFilename, coverageKeys.length)

let changed

coverageKeys.forEach((key, k) => {
const coverage = nycCoverage[key]

if (!coverage.path) {
debug('key %s does not have path', key)
return
}

if (!isAbsolute(coverage.path)) {
if (existsSync(coverage.path)) {
debug('resolving path %s', coverage.path)
coverage.path = resolve(coverage.path)
changed = true
}
return
}

// path is absolute, let's check if it exists
if (!existsSync(coverage.path)) {
debug('⚠️ cannot find file %s with hash %s', coverage.path, coverage.hash)
}
})

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

/**
* @param {string[]} filepaths
* @returns {string | undefined} common prefix that corresponds to current folder
*/
function findCommonRoot(filepaths) {
if (!filepaths.length) {
debug('cannot find common root without any files')
return
}

// assuming / as file separator
const splitParts = filepaths.map(name => name.split('/'))
const lengths = splitParts.map(arr => arr.length)
const shortestLength = Math.min.apply(null, lengths)
debug('shorted file path has %d parts', shortestLength)

const cwd = process.cwd()
let commonPrefix = []
let foundCurrentFolder

for (let k = 0; k < shortestLength; k += 1) {
const part = splitParts[0][k]
const prefix = commonPrefix.concat(part).join('/')
debug('testing prefix %o', prefix)
const allFilesStart = filepaths.every(name => name.startsWith(prefix))
if (!allFilesStart) {
debug('stopped at non-common prefix %s', prefix)
break
}

commonPrefix.push(part)

const removedPrefixNames = filepaths.map(filepath =>
filepath.slice(prefix.length)
)
debug('removedPrefix %o', removedPrefixNames)
const foundAllPaths = removedPrefixNames.every(filepath =>
existsSync(join(cwd, filepath))
)
debug('all files found at %s? %o', prefix, foundAllPaths)
if (foundAllPaths) {
debug('found prefix that matches current folder: %s', prefix)
foundCurrentFolder = prefix
break
}
}

return foundCurrentFolder
}

function tryFindingLocalFiles(nycFilename) {
const nycCoverage = JSON.parse(readFileSync(nycFilename, 'utf8'))
const coverageKeys = Object.keys(nycCoverage)
const filenames = coverageKeys.map(key => nycCoverage[key].path)
const commonFolder = findCommonRoot(filenames)
if (!commonFolder) {
debug('could not find common folder %s', commonFolder)
return
}
const cwd = process.cwd()
debug(
'found common folder %s that matches current working directory %s',
commonFolder,
cwd
)
const length = commonFolder.length
let changed

coverageKeys.forEach(key => {
const from = nycCoverage[key].path
if (from.startsWith(commonFolder)) {
const to = join(cwd, from.slice(length))
nycCoverage[key].path = to
debug('replaced %s -> %s', from, to)
changed = true
}
})

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

module.exports = {
fixSourcePathes,
filterSpecsFromCoverage,
showNycInfo
showNycInfo,
resolveRelativePaths,
checkAllPathsNotFound,
tryFindingLocalFiles
}