Skip to content

Commit

Permalink
Introduce coveragePreferIstanbul param to executeTestPlan
Browse files Browse the repository at this point in the history
By default node v8 will be preferred to collect coverage. Padding coveragePreferIstanbul is meant to be used if multiple coverage sources are used and some of these sources report using istanbul format. This is because both formats are incompatible. See istanbuljs/v8-to-istanbul#144
  • Loading branch information
Damien Maillard committed May 4, 2021
1 parent 61a8fa6 commit 6785289
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 106 deletions.
3 changes: 3 additions & 0 deletions src/executeTestPlan.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export const executeTestPlan = async ({
coverageConfig = jsenvCoverageConfig,
coverageIncludeMissing = true,
coverageAndExecutionAllowed = false,
coverageForceIstanbul = false,

coverageTextLog = true,
coverageJsonFile = Boolean(process.env.CI),
coverageJsonFileLog = true,
Expand Down Expand Up @@ -167,6 +169,7 @@ export const executeTestPlan = async ({
coverage,
coverageConfig,
coverageIncludeMissing,
coverageForceIstanbul,

...rest,
})
Expand Down
12 changes: 3 additions & 9 deletions src/internal/browser-launcher/executeHtmlFile.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { extname } from "path"
import { resolveUrl, assertFilePresence } from "@jsenv/util"
import { normalizeIstanbulCoverage } from "@jsenv/core/src/internal/executing/coverage/normalizeIstanbulCoverage.js"
import { composeIstanbulCoverages } from "@jsenv/core/src/internal/executing/coverage/composeIstanbulCoverages.js"

import { evalSource } from "../runtime/createNodeRuntime/evalSource.js"
import { escapeRegexpSpecialCharacters } from "../escapeRegexpSpecialCharacters.js"
import { projectDirectoryUrl } from "@jsenv/core/jsenv.config.js"

export const executeHtmlFile = async (
fileRelativeUrl,
Expand Down Expand Up @@ -82,14 +80,14 @@ export const executeHtmlFile = async (
status: "errored",
error: evalException(exceptionSource, { projectDirectoryUrl, compileServerOrigin }),
namespace: fileExecutionResultMap,
readCoverage: () => generateCoverageForPage(fileExecutionResultMap),
coverageMap: generateCoverageForPage(fileExecutionResultMap),
}
}

return {
status: "completed",
namespace: fileExecutionResultMap,
readCoverage: () => generateCoverageForPage(fileExecutionResultMap),
coverageMap: generateCoverageForPage(fileExecutionResultMap),
}
}

Expand All @@ -98,11 +96,7 @@ const generateCoverageForPage = (fileExecutionResultMap) => {
Object.keys(fileExecutionResultMap).forEach((fileRelativeUrl) => {
const istanbulCoverage = fileExecutionResultMap[fileRelativeUrl].coverageMap
if (istanbulCoverage) {
const istanbulCoverageNormalized = normalizeIstanbulCoverage(
istanbulCoverage,
projectDirectoryUrl,
)
istanbulCoverages.push(istanbulCoverageNormalized)
istanbulCoverages.push(istanbulCoverage)
}
})
const istanbulCoverage = composeIstanbulCoverages(...istanbulCoverages)
Expand Down
13 changes: 2 additions & 11 deletions src/internal/executing/coverage/istanbulCoverageFromV8Coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from "@jsenv/util"
import { require } from "@jsenv/core/src/internal/require.js"
import { composeIstanbulCoverages } from "./composeIstanbulCoverages.js"
import { normalizeIstanbulCoverage } from "./normalizeIstanbulCoverage.js"

const { mergeProcessCovs } = require("@c88/v8-coverage")

Expand Down Expand Up @@ -38,7 +37,6 @@ export const istanbulCoverageFromV8Coverage = async ({

const coverageReport = mergeCoverageReports(coverageReportsFiltered)
const istanbulCoverage = await convertV8CoverageToIstanbul(coverageReport, {
projectDirectoryUrl,
sourceMapCache,
})
return istanbulCoverage
Expand Down Expand Up @@ -91,10 +89,7 @@ const mergeCoverageReports = (coverageReports) => {
return coverageReport
}

const convertV8CoverageToIstanbul = async (
coverageReport,
{ projectDirectoryUrl, sourceMapCache },
) => {
const convertV8CoverageToIstanbul = async (coverageReport, { sourceMapCache }) => {
const istanbulCoverages = await Promise.all(
coverageReport.result.map(async (fileV8Coverage) => {
const sources = sourcesFromSourceMapCache(fileV8Coverage.url, sourceMapCache)
Expand All @@ -110,11 +105,7 @@ const convertV8CoverageToIstanbul = async (

converter.applyCoverage(fileV8Coverage.functions)
const istanbulCoverage = converter.toIstanbul()
const istanbulCoverageNormalized = normalizeIstanbulCoverage(
istanbulCoverage,
projectDirectoryUrl,
)
return istanbulCoverageNormalized
return istanbulCoverage
}),
)

Expand Down
36 changes: 22 additions & 14 deletions src/internal/executing/coverage/reportToCoverageMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ import { normalizeIstanbulCoverage } from "./normalizeIstanbulCoverage.js"
export const reportToCoverageMap = async (
report,
{
logger,
cancellationToken,
projectDirectoryUrl,
babelPluginMap,
coverageConfig,
coverageIncludeMissing,
},
) => {
const coverageMapForReport = executionReportToCoverageMap(report)
const istanbulCoverageFromExecutionRaw = executionReportToCoverageMap(report, { logger })
const istanbulCoverageFromExecution = normalizeIstanbulCoverage(
istanbulCoverageFromExecutionRaw,
projectDirectoryUrl,
)

if (!coverageIncludeMissing) {
return coverageMapForReport
return istanbulCoverageFromExecution
}

const relativeFileUrlToCoverArray = await listRelativeFileUrlToCover({
Expand All @@ -27,27 +32,27 @@ export const reportToCoverageMap = async (

const relativeFileUrlMissingCoverageArray = relativeFileUrlToCoverArray.filter(
(relativeFileUrlToCover) =>
Object.keys(coverageMapForReport).every((key) => {
Object.keys(istanbulCoverageFromExecution).every((key) => {
return key !== `./${relativeFileUrlToCover}`
}),
)

const coverageMapForMissedFiles = {}
const istanbulCoverageFromMissedFiles = {}
await Promise.all(
relativeFileUrlMissingCoverageArray.map(async (relativeFileUrlMissingCoverage) => {
const emptyCoverage = await relativeUrlToEmptyCoverage(relativeFileUrlMissingCoverage, {
cancellationToken,
projectDirectoryUrl,
babelPluginMap,
})
coverageMapForMissedFiles[relativeFileUrlMissingCoverage] = emptyCoverage
istanbulCoverageFromMissedFiles[relativeFileUrlMissingCoverage] = emptyCoverage
return emptyCoverage
}),
)

return {
...coverageMapForReport, // already normalized
...normalizeIstanbulCoverage(coverageMapForMissedFiles, projectDirectoryUrl),
...istanbulCoverageFromExecution, // already normalized
...normalizeIstanbulCoverage(istanbulCoverageFromMissedFiles, projectDirectoryUrl),
}
}

Expand All @@ -70,15 +75,15 @@ const listRelativeFileUrlToCover = async ({
return matchingFileResultArray.map(({ relativeUrl }) => relativeUrl)
}

const executionReportToCoverageMap = (report) => {
const coverageMapArray = []
const executionReportToCoverageMap = (report, { logger }) => {
const istanbulCoverages = []

Object.keys(report).forEach((file) => {
const executionResultForFile = report[file]
Object.keys(executionResultForFile).forEach((executionName) => {
const executionResultForFileOnRuntime = executionResultForFile[executionName]

const { coverageMap } = executionResultForFileOnRuntime
const { status, coverageMap } = executionResultForFileOnRuntime
if (!coverageMap) {
// several reasons not to have coverageMap here:
// 1. the file we executed did not import an instrumented file.
Expand All @@ -97,15 +102,18 @@ const executionReportToCoverageMap = (report) => {
// in any scenario we are fine because
// coverDescription will generate empty coverage for files
// that were suppose to be coverage but were not.

if (status === "completed") {
logger.warn(`No execution.coverageMap from execution named "${executionName}" of ${file}`)
}
return
}

coverageMapArray.push(coverageMap)
istanbulCoverages.push(coverageMap)
})
})

debugger
const executionCoverageMap = composeIstanbulCoverages(...coverageMapArray)
const istanbulCoverage = composeIstanbulCoverages(...istanbulCoverages)

return executionCoverageMap
return istanbulCoverage
}
3 changes: 3 additions & 0 deletions src/internal/executing/executeConcurrently.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const executeConcurrently = async (
coverage,
coverageConfig,
coverageIncludeMissing,
coverageForceIstanbul,

...rest
},
Expand Down Expand Up @@ -160,6 +161,7 @@ export const executeConcurrently = async (
fileRelativeUrl,
collectCoverage,
coverageConfig,
coverageForceIstanbul,

...rest,
})
Expand Down Expand Up @@ -239,6 +241,7 @@ export const executeConcurrently = async (
...(coverage
? {
coverageMap: await reportToCoverageMap(report, {
logger,
cancellationToken,
projectDirectoryUrl,
babelPluginMap,
Expand Down
3 changes: 2 additions & 1 deletion src/internal/executing/executePlan.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export const executePlan = async (
logSummary,
measureGlobalDuration,

// coverage parameters
coverage,
coverageConfig,
coverageIncludeMissing,
coverageForceIstanbul,

...rest
} = {},
Expand Down Expand Up @@ -109,6 +109,7 @@ export const executePlan = async (
coverage,
coverageConfig,
coverageIncludeMissing,
coverageForceIstanbul,

...rest,
})
Expand Down
26 changes: 13 additions & 13 deletions src/internal/executing/launchAndExecute.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const launchAndExecute = async ({
inheritCoverage = false,
collectCoverage = false,
coverageConfig,
coverageForceIstanbul,
...rest
} = {}) => {
const logger = createLogger({ logLevel: executionLogLevel })
Expand Down Expand Up @@ -156,6 +157,7 @@ export const launchAndExecute = async ({
runtimeStoppedCallback,
collectCoverage,
coverageConfig,
coverageForceIstanbul,

...rest,
})
Expand Down Expand Up @@ -236,6 +238,7 @@ const computeExecutionResult = async ({
runtimeDisconnectCallback,

collectCoverage,
coverageForceIstanbul,

...rest
}) => {
Expand All @@ -248,6 +251,7 @@ const computeExecutionResult = async ({
cancellationToken,
logger,
collectCoverage,
coverageForceIstanbul,
...rest,
})
runtimeStartedCallback({ name: value.name, version: value.version })
Expand Down Expand Up @@ -315,6 +319,7 @@ const computeExecutionResult = async ({
registerErrorCallback,
registerConsoleCallback,
disconnected,
finalizeExecutionResult = (executionResult) => executionResult,
} = await launchOperation

const runtime = `${runtimeName}/${runtimeVersion}`
Expand Down Expand Up @@ -359,7 +364,7 @@ const computeExecutionResult = async ({
timing = TIMING_AFTER_EXECUTION

if (raceResult.winner === disconnected) {
return createDisconnectedExecutionResult({})
return createDisconnectedExecutionResult()
}

if (stopAfterExecute) {
Expand All @@ -381,17 +386,11 @@ const computeExecutionResult = async ({
["runtime"]: runtime,
}),
)
return {
...createErroredExecutionResult(executionResult.error),
...(collectCoverage ? { coverageMap: await executionResult.readCoverage() } : {}),
}
return finalizeExecutionResult(createErroredExecutionResult(executionResult))
}

logger.debug(`${fileRelativeUrl} ${runtime}: execution completed.`)
return {
...createCompletedExecutionResult(executionResult.namespace),
...(collectCoverage ? { coverageMap: await executionResult.readCoverage() } : {}),
}
return finalizeExecutionResult(createCompletedExecutionResult(executionResult))
},
})

Expand All @@ -412,17 +411,18 @@ const createDisconnectedExecutionResult = () => {
}
}

const createErroredExecutionResult = (error) => {
const createErroredExecutionResult = (executionResult) => {
return {
...executionResult,
status: "errored",
error,
}
}

const createCompletedExecutionResult = (namespace) => {
const createCompletedExecutionResult = (executionResult) => {
return {
...executionResult,
status: "completed",
namespace: normalizeNamespace(namespace),
namespace: normalizeNamespace(executionResult.namespace),
}
}

Expand Down
Loading

0 comments on commit 6785289

Please sign in to comment.