diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-05-15 02:01:18 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-05-23 06:20:34 +0800 |
commit | 974575b695108dd70f4b165f6789f71c3647c2b1 (patch) | |
tree | 3d589c157c51c02066583a08bdec879aa7a18735 /packages/sol-cov/src/artifact_adapters | |
parent | 60b1fdd367101047d433d4b9c1c47925925296a2 (diff) | |
download | dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.tar dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.tar.gz dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.tar.bz2 dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.tar.lz dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.tar.xz dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.tar.zst dexon-sol-tools-974575b695108dd70f4b165f6789f71c3647c2b1.zip |
Make sol-cov work with truffle and other artifact adapters
Diffstat (limited to 'packages/sol-cov/src/artifact_adapters')
-rw-r--r-- | packages/sol-cov/src/artifact_adapters/0x.ts | 41 | ||||
-rw-r--r-- | packages/sol-cov/src/artifact_adapters/abstract.ts | 5 | ||||
-rw-r--r-- | packages/sol-cov/src/artifact_adapters/truffle.ts | 43 |
3 files changed, 89 insertions, 0 deletions
diff --git a/packages/sol-cov/src/artifact_adapters/0x.ts b/packages/sol-cov/src/artifact_adapters/0x.ts new file mode 100644 index 000000000..87d23b0aa --- /dev/null +++ b/packages/sol-cov/src/artifact_adapters/0x.ts @@ -0,0 +1,41 @@ +import * as fs from 'fs'; +import * as glob from 'glob'; +import * as _ from 'lodash'; +import * as path from 'path'; + +import { ContractData } from '../types'; + +import { AbstractArtifactAdapter } from './abstract'; + +export class ZeroExArtifactAdapter extends AbstractArtifactAdapter { + private _artifactsPath: string; + private _sourcesPath: string; + constructor(artifactsPath: string, sourcesPath: string) { + super(); + this._artifactsPath = artifactsPath; + this._sourcesPath = sourcesPath; + } + public async collectContractsDataAsync(): Promise<ContractData[]> { + const artifactsGlob = `${this._artifactsPath}/**/*.json`; + const artifactFileNames = glob.sync(artifactsGlob, { absolute: true }); + const contractsData: ContractData[] = []; + for (const artifactFileName of artifactFileNames) { + const artifact = JSON.parse(fs.readFileSync(artifactFileName).toString()); + let sources = _.keys(artifact.sources); + sources = _.map(sources, relativeFilePath => path.resolve(this._sourcesPath, relativeFilePath)); + const contractName = artifact.contractName; + // We don't compute coverage for dependencies + const sourceCodes = _.map(sources, (source: string) => fs.readFileSync(source).toString()); + const contractData = { + sourceCodes, + sources, + bytecode: artifact.compilerOutput.evm.bytecode.object, + sourceMap: artifact.compilerOutput.evm.bytecode.sourceMap, + runtimeBytecode: artifact.compilerOutput.evm.deployedBytecode.object, + sourceMapRuntime: artifact.compilerOutput.evm.deployedBytecode.sourceMap, + }; + contractsData.push(contractData); + } + return contractsData; + } +} diff --git a/packages/sol-cov/src/artifact_adapters/abstract.ts b/packages/sol-cov/src/artifact_adapters/abstract.ts new file mode 100644 index 000000000..fcc6562ad --- /dev/null +++ b/packages/sol-cov/src/artifact_adapters/abstract.ts @@ -0,0 +1,5 @@ +import { ContractData } from '../types'; + +export abstract class AbstractArtifactAdapter { + public abstract async collectContractsDataAsync(): Promise<ContractData[]>; +} diff --git a/packages/sol-cov/src/artifact_adapters/truffle.ts b/packages/sol-cov/src/artifact_adapters/truffle.ts new file mode 100644 index 000000000..e891bb464 --- /dev/null +++ b/packages/sol-cov/src/artifact_adapters/truffle.ts @@ -0,0 +1,43 @@ +import { Compiler, CompilerOptions } from '@0xproject/sol-compiler'; +import * as fs from 'fs'; +import * as glob from 'glob'; +import * as _ from 'lodash'; +import * as path from 'path'; +import * as rimraf from 'rimraf'; + +import { ContractData } from '../types'; + +import { ZeroExArtifactAdapter } from './0x'; +import { AbstractArtifactAdapter } from './abstract'; + +export class TruffleArtifactAdapter extends AbstractArtifactAdapter { + private _solcVersion: string; + private _sourcesPath: string; + constructor(sourcesPath: string, solcVersion: string) { + super(); + this._solcVersion = solcVersion; + this._sourcesPath = sourcesPath; + } + public async collectContractsDataAsync(): Promise<ContractData[]> { + const artifactsDir = '0x-artifacts'; + const compilerOptions: CompilerOptions = { + contractsDir: this._sourcesPath, + artifactsDir, + compilerSettings: { + outputSelection: { + ['*']: { + ['*']: ['abi', 'evm.bytecode.object', 'evm.deployedBytecode.object'], + }, + }, + }, + contracts: '*', + solcVersion: this._solcVersion, + }; + const compiler = new Compiler(compilerOptions); + await compiler.compileAsync(); + const zeroExArtifactAdapter = new ZeroExArtifactAdapter(artifactsDir, this._sourcesPath); + const contractsDataFrom0xArtifacts = await zeroExArtifactAdapter.collectContractsDataAsync(); + rimraf.sync(artifactsDir); + return contractsDataFrom0xArtifacts; + } +} |