diff options
author | Fabio Berger <me@fabioberger.com> | 2018-08-14 04:40:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-14 04:40:46 +0800 |
commit | 7340338626ea11bccd44eef1da277196b78a6a7d (patch) | |
tree | 1a99424e80ece948af73995378a5737c8985dd0e /packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts | |
parent | 083319786fad31dfde16cb9e06e893bfeb23785d (diff) | |
parent | 9d3c287918389d07f884245bd1bc968955768b6f (diff) | |
download | dexon-sol-tools-7340338626ea11bccd44eef1da277196b78a6a7d.tar dexon-sol-tools-7340338626ea11bccd44eef1da277196b78a6a7d.tar.gz dexon-sol-tools-7340338626ea11bccd44eef1da277196b78a6a7d.tar.bz2 dexon-sol-tools-7340338626ea11bccd44eef1da277196b78a6a7d.tar.lz dexon-sol-tools-7340338626ea11bccd44eef1da277196b78a6a7d.tar.xz dexon-sol-tools-7340338626ea11bccd44eef1da277196b78a6a7d.tar.zst dexon-sol-tools-7340338626ea11bccd44eef1da277196b78a6a7d.zip |
Merge pull request #938 from 0xProject/sol-cov-fixes
Sol cov fixes
Diffstat (limited to 'packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts')
-rw-r--r-- | packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts | 58 |
1 files changed, 51 insertions, 7 deletions
diff --git a/packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts b/packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts index 53b77aed5..2706435cc 100644 --- a/packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts +++ b/packages/sol-cov/src/artifact_adapters/truffle_artifact_adapter.ts @@ -1,25 +1,40 @@ import { Compiler, CompilerOptions } from '@0xproject/sol-compiler'; -import * as rimraf from 'rimraf'; +import * as fs from 'fs'; +import * as glob from 'glob'; +import * as path from 'path'; import { ContractData } from '../types'; import { AbstractArtifactAdapter } from './abstract_artifact_adapter'; import { SolCompilerArtifactAdapter } from './sol_compiler_artifact_adapter'; +const DEFAULT_TRUFFLE_ARTIFACTS_DIR = './build/contracts'; + +interface TruffleConfig { + solc?: any; + contracts_build_directory?: string; +} + export class TruffleArtifactAdapter extends AbstractArtifactAdapter { private readonly _solcVersion: string; - private readonly _sourcesPath: string; - constructor(sourcesPath: string, solcVersion: string) { + private readonly _projectRoot: string; + constructor(projectRoot: string, solcVersion: string) { super(); this._solcVersion = solcVersion; - this._sourcesPath = sourcesPath; + this._projectRoot = projectRoot; } public async collectContractsDataAsync(): Promise<ContractData[]> { const artifactsDir = '.0x-artifacts'; + const contractsDir = path.join(this._projectRoot, 'contracts'); + const truffleConfig = this._getTruffleConfig(); + const solcConfig = truffleConfig.solc || {}; + const truffleArtifactsDirectory = truffleConfig.contracts_build_directory || DEFAULT_TRUFFLE_ARTIFACTS_DIR; + this._assertSolidityVersionIsCorrect(truffleArtifactsDirectory); const compilerOptions: CompilerOptions = { - contractsDir: this._sourcesPath, + contractsDir, artifactsDir, compilerSettings: { + ...solcConfig, outputSelection: { ['*']: { ['*']: ['abi', 'evm.bytecode.object', 'evm.deployedBytecode.object'], @@ -31,9 +46,38 @@ export class TruffleArtifactAdapter extends AbstractArtifactAdapter { }; const compiler = new Compiler(compilerOptions); await compiler.compileAsync(); - const solCompilerArtifactAdapter = new SolCompilerArtifactAdapter(artifactsDir, this._sourcesPath); + const solCompilerArtifactAdapter = new SolCompilerArtifactAdapter(artifactsDir, contractsDir); const contractsDataFrom0xArtifacts = await solCompilerArtifactAdapter.collectContractsDataAsync(); - rimraf.sync(artifactsDir); return contractsDataFrom0xArtifacts; } + private _getTruffleConfig(): TruffleConfig { + const truffleConfigFileShort = path.resolve(path.join(this._projectRoot, 'truffle.js')); + const truffleConfigFileLong = path.resolve(path.join(this._projectRoot, 'truffle-config.js')); + if (fs.existsSync(truffleConfigFileShort)) { + const truffleConfig = require(truffleConfigFileShort); + return truffleConfig; + } else if (fs.existsSync(truffleConfigFileLong)) { + const truffleConfig = require(truffleConfigFileLong); + return truffleConfig; + } else { + throw new Error( + `Neither ${truffleConfigFileShort} nor ${truffleConfigFileLong} exists. Make sure the project root is correct`, + ); + } + } + private _assertSolidityVersionIsCorrect(truffleArtifactsDirectory: string): void { + const artifactsGlob = `${truffleArtifactsDirectory}/**/*.json`; + const artifactFileNames = glob.sync(artifactsGlob, { absolute: true }); + for (const artifactFileName of artifactFileNames) { + const artifact = JSON.parse(fs.readFileSync(artifactFileName).toString()); + const compilerVersion = artifact.compiler.version; + if (!compilerVersion.startsWith(this._solcVersion)) { + throw new Error( + `${artifact.contractName} was compiled with solidity ${compilerVersion} but specified version is ${ + this._solcVersion + } making it impossible for sol-cov to process traces`, + ); + } + } + } } |