From 7a41a5249fcb8457cb8beb823ce9a1362cdbfa98 Mon Sep 17 00:00:00 2001 From: perissology Date: Thu, 14 Jun 2018 11:17:04 -0700 Subject: Collect coverage for provided sources When solidity generates source maps during contract compilation, the contracts are referred to by an id, which corresponds to an array index. We may not want to cover all sources that were included in a compilation, but because we use array indexes (vs. the id that is provided by solidity compiler) to map the contract to the sourceMap, the provided sourceCodes array must include the code at the correct index. This can result in empty slots in the sourceCodes array. This commit allows the coverage to only be collected for the contracts with provided sourceCode. --- packages/sol-cov/src/collect_coverage_entries.ts | 2 +- packages/sol-cov/src/coverage_subprovider.ts | 6 ++++++ packages/sol-cov/src/source_maps.ts | 5 ++++- 3 files changed, 11 insertions(+), 2 deletions(-) (limited to 'packages') diff --git a/packages/sol-cov/src/collect_coverage_entries.ts b/packages/sol-cov/src/collect_coverage_entries.ts index b145f044e..3fc85008c 100644 --- a/packages/sol-cov/src/collect_coverage_entries.ts +++ b/packages/sol-cov/src/collect_coverage_entries.ts @@ -10,7 +10,7 @@ const coverageEntriesBySourceHash: { [sourceHash: string]: CoverageEntriesDescri export const collectCoverageEntries = (contractSource: string) => { const sourceHash = ethUtil.sha3(contractSource).toString('hex'); - if (_.isUndefined(coverageEntriesBySourceHash[sourceHash])) { + if (_.isUndefined(coverageEntriesBySourceHash[sourceHash]) && !_.isUndefined(contractSource)) { const ast = parser.parse(contractSource, { range: true }); const locationByOffset = getLocationByOffset(contractSource); const visitor = new ASTVisitor(locationByOffset); diff --git a/packages/sol-cov/src/coverage_subprovider.ts b/packages/sol-cov/src/coverage_subprovider.ts index 0fa7f873e..2f0bcbb93 100644 --- a/packages/sol-cov/src/coverage_subprovider.ts +++ b/packages/sol-cov/src/coverage_subprovider.ts @@ -66,6 +66,12 @@ export const coverageHandler: SingleFileSubtraceHandler = ( ): Coverage => { const absoluteFileName = contractData.sources[fileIndex]; const coverageEntriesDescription = collectCoverageEntries(contractData.sourceCodes[fileIndex]); + + // if the source wasn't provided for the fileIndex, we can't cover the file + if (_.isUndefined(coverageEntriesDescription)) { + return {}; + } + let sourceRanges = _.map(subtrace, structLog => pcToSourceRange[structLog.pc]); sourceRanges = _.compact(sourceRanges); // Some PC's don't map to a source range and we just ignore them. // By default lodash does a shallow object comparasion. We JSON.stringify them and compare as strings. diff --git a/packages/sol-cov/src/source_maps.ts b/packages/sol-cov/src/source_maps.ts index f9503e16c..c95c7d2b0 100644 --- a/packages/sol-cov/src/source_maps.ts +++ b/packages/sol-cov/src/source_maps.ts @@ -12,6 +12,9 @@ export interface SourceLocation { } export function getLocationByOffset(str: string): LocationByOffset { + if (_.isUndefined(str)) { + return {}; + } const locationByOffset: LocationByOffset = { 0: { line: 1, column: 0 } }; let currentOffset = 0; for (const char of str.split('')) { @@ -56,7 +59,7 @@ export function parseSourceMap( length, fileIndex, }; - if (parsedEntry.fileIndex !== -1) { + if (parsedEntry.fileIndex !== -1 && !_.isUndefined(locationByOffsetByFileIndex[parsedEntry.fileIndex])) { const sourceRange = { location: { start: locationByOffsetByFileIndex[parsedEntry.fileIndex][parsedEntry.offset], -- cgit v1.2.3 From c5ea985a7003fef8430555e8477a130262407137 Mon Sep 17 00:00:00 2001 From: perissology Date: Thu, 21 Jun 2018 06:24:50 -0700 Subject: only call getLocationByOffset if source if defined --- packages/sol-cov/src/source_maps.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'packages') diff --git a/packages/sol-cov/src/source_maps.ts b/packages/sol-cov/src/source_maps.ts index c95c7d2b0..39b83ea0d 100644 --- a/packages/sol-cov/src/source_maps.ts +++ b/packages/sol-cov/src/source_maps.ts @@ -12,9 +12,6 @@ export interface SourceLocation { } export function getLocationByOffset(str: string): LocationByOffset { - if (_.isUndefined(str)) { - return {}; - } const locationByOffset: LocationByOffset = { 0: { line: 1, column: 0 } }; let currentOffset = 0; for (const char of str.split('')) { @@ -39,7 +36,7 @@ export function parseSourceMap( ): { [programCounter: number]: SourceRange } { const bytecode = Uint8Array.from(Buffer.from(bytecodeHex, 'hex')); const pcToInstructionIndex: { [programCounter: number]: number } = getPcToInstructionIndexMapping(bytecode); - const locationByOffsetByFileIndex = _.map(sourceCodes, getLocationByOffset); + const locationByOffsetByFileIndex = _.map(sourceCodes, (s) => _.isUndefined(s) ? {} : getLocationByOffset(s)); const entries = srcMap.split(';'); let lastParsedEntry: SourceLocation = {} as any; const instructionIndexToSourceRange: { [instructionIndex: number]: SourceRange } = {}; -- cgit v1.2.3 From ade8e95d2e1c46bb1a3f8212a6eac5f396bc3e94 Mon Sep 17 00:00:00 2001 From: perissology Date: Thu, 21 Jun 2018 09:12:30 -0700 Subject: fix linter issues --- packages/sol-cov/src/source_maps.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages') diff --git a/packages/sol-cov/src/source_maps.ts b/packages/sol-cov/src/source_maps.ts index 39b83ea0d..90b21dda1 100644 --- a/packages/sol-cov/src/source_maps.ts +++ b/packages/sol-cov/src/source_maps.ts @@ -36,7 +36,7 @@ export function parseSourceMap( ): { [programCounter: number]: SourceRange } { const bytecode = Uint8Array.from(Buffer.from(bytecodeHex, 'hex')); const pcToInstructionIndex: { [programCounter: number]: number } = getPcToInstructionIndexMapping(bytecode); - const locationByOffsetByFileIndex = _.map(sourceCodes, (s) => _.isUndefined(s) ? {} : getLocationByOffset(s)); + const locationByOffsetByFileIndex = _.map(sourceCodes, s => (_.isUndefined(s) ? {} : getLocationByOffset(s))); const entries = srcMap.split(';'); let lastParsedEntry: SourceLocation = {} as any; const instructionIndexToSourceRange: { [instructionIndex: number]: SourceRange } = {}; -- cgit v1.2.3