Merge pull request #421 from sc-forks/buidler-plugin

Add Buidler plugin / Finalize API
pull/456/head v0.7.0-beta.3
cgewecke 5 years ago committed by GitHub
commit 212c88fc4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 39
      .circleci/config.yml
  2. 52
      README.md
  3. 4
      api.js
  4. 18
      buidler.config.js
  5. 0
      dist/buidler.plugin.js
  6. 460
      dist/plugin-assets/plugin.utils.js
  7. 367
      docs/api.md
  8. 2
      index.js
  9. 110
      lib/api.js
  10. 6
      lib/instrumenter.js
  11. 6
      lib/ui.js
  12. 10
      lib/validator.js
  13. 28
      package.json
  14. 0
      plugins/bin.js
  15. 142
      plugins/buidler.plugin.js
  16. 85
      plugins/resources/buidler.ui.js
  17. 108
      plugins/resources/buidler.utils.js
  18. 273
      plugins/resources/plugin.utils.js
  19. 0
      plugins/resources/truffle.library.js
  20. 0
      plugins/resources/truffle.library.js.map
  21. 0
      plugins/resources/truffle.ui.js
  22. 217
      plugins/resources/truffle.utils.js
  23. 24
      plugins/truffle.plugin.js
  24. 67
      scripts/run-buidler.sh
  25. 7
      scripts/run-metacoin.sh
  26. 39
      scripts/run-moloch.sh
  27. 0
      test/integration/generic/assets/SimpleError.sol
  28. 0
      test/integration/generic/assets/asset.js
  29. 0
      test/integration/generic/contracts/Migrations.sol
  30. 0
      test/integration/generic/migrations/1_initial.js
  31. 0
      test/integration/generic/test/.marker
  32. 8
      test/integration/projects/bad-solcoverjs/buidler.config.js
  33. 1
      test/integration/projects/ganache-solcoverjs/.gitignore
  34. 5
      test/integration/projects/ganache-solcoverjs/.solcover.js
  35. 8
      test/integration/projects/ganache-solcoverjs/buidler.config.js
  36. 17
      test/integration/projects/ganache-solcoverjs/contracts/ContractA.sol
  37. 17
      test/integration/projects/ganache-solcoverjs/contracts/ContractB.sol
  38. 17
      test/integration/projects/ganache-solcoverjs/contracts/ContractC.sol
  39. 23
      test/integration/projects/ganache-solcoverjs/contracts/Migrations.sol
  40. 15
      test/integration/projects/ganache-solcoverjs/test/contracta.js
  41. 15
      test/integration/projects/ganache-solcoverjs/test/contractb.js
  42. 20
      test/integration/projects/ganache-solcoverjs/test/contractc.js
  43. 7
      test/integration/projects/ganache-solcoverjs/truffle-config.js
  44. 8
      test/integration/projects/import-paths/buidler.config.js
  45. 5
      test/integration/projects/import-paths/contracts/OnlyUsesImports.sol
  46. 10
      test/integration/projects/import-paths/node_modules/package/AnotherImport.sol
  47. 1
      test/integration/projects/import-paths/node_modules/package/package.json
  48. 7
      test/integration/projects/libraries/buidler.config.js
  49. 20
      test/integration/projects/libraries/test/libraries.js
  50. 1
      test/integration/projects/multiple-migrations/buidler.config.js
  51. 4
      test/integration/projects/multiple-suites/.solcover.js
  52. 8
      test/integration/projects/multiple-suites/buidler.config.js
  53. 17
      test/integration/projects/multiple-suites/contracts/ContractA.sol
  54. 17
      test/integration/projects/multiple-suites/contracts/ContractB.sol
  55. 17
      test/integration/projects/multiple-suites/contracts/ContractC.sol
  56. 23
      test/integration/projects/multiple-suites/contracts/Migrations.sol
  57. 15
      test/integration/projects/multiple-suites/test/contracta.js
  58. 15
      test/integration/projects/multiple-suites/test/contractb.js
  59. 20
      test/integration/projects/multiple-suites/test/contractc.js
  60. 7
      test/integration/projects/multiple-suites/truffle-config.js
  61. 7
      test/integration/projects/no-sources/buidler.config.js
  62. 8
      test/integration/projects/skipping/buidler.config.js
  63. 1
      test/integration/projects/test-files/.solcover.js
  64. 7
      test/integration/projects/test-files/buidler.config.js
  65. 2
      test/integration/projects/test-files/test/globby_b.js
  66. 2
      test/integration/projects/test-files/test/globby_c.js
  67. 2
      test/integration/projects/test-files/test/specific_a.js
  68. 4
      test/integration/projects/tests-folder/.solcover.js
  69. 8
      test/integration/projects/tests-folder/buidler.config.js
  70. 17
      test/integration/projects/tests-folder/contracts/ContractA.sol
  71. 17
      test/integration/projects/tests-folder/contracts/ContractB.sol
  72. 17
      test/integration/projects/tests-folder/contracts/ContractC.sol
  73. 23
      test/integration/projects/tests-folder/contracts/Migrations.sol
  74. 15
      test/integration/projects/tests-folder/test/contracta.js
  75. 15
      test/integration/projects/tests-folder/test/folder/contractb.js
  76. 20
      test/integration/projects/tests-folder/test/folder/contractc.js
  77. 7
      test/integration/projects/tests-folder/truffle-config.js
  78. 4
      test/sources/js/block-gas-limit.js
  79. 11
      test/sources/js/empty.js
  80. 16
      test/sources/js/inheritance.js
  81. 20
      test/sources/js/only-call.js
  82. 6
      test/sources/js/pureview.js
  83. 2
      test/sources/js/simple.js
  84. 17
      test/sources/js/sol-parse-fail.js
  85. 13
      test/sources/js/testrpc-options.js
  86. 19
      test/sources/js/totallyPure.js
  87. 4
      test/sources/js/truffle-crash.js
  88. 22
      test/sources/js/truffle-test-fail.js
  89. 2
      test/sources/js/wallet.js
  90. 2
      test/sources/solidity/contracts/app/Proxy.sol
  91. 101
      test/units/api.js
  92. 180
      test/units/buidler/errors.js
  93. 146
      test/units/buidler/flags.js
  94. 277
      test/units/buidler/standard.js
  95. 7
      test/units/truffle/errors.js
  96. 20
      test/units/truffle/flags.js
  97. 113
      test/units/truffle/standard.js
  98. 30
      test/units/validator.js
  99. 139
      test/util/integration.js
  100. 4
      test/util/util.js
  101. Some files were not shown because too many files have changed in this diff Show More

@ -1,6 +1,7 @@
version: 2.0
# Necessary for running in machine mode, which is necessary to execute the
# Zeppelin and MetaCoin E2E scripts
# Necessary for running in machine mode,
# which is necessary to execute the E2E scripts
step_install_nvm: &step_install_nvm
run:
name: "Install nvm for machine"
@ -12,6 +13,7 @@ step_install_nvm: &step_install_nvm
nvm alias default v8.15.0
echo 'export NVM_DIR="/opt/circleci/.nvm"' >> $BASH_ENV
echo "[ -s \"$NVM_DIR/nvm.sh\" ] && . \"$NVM_DIR/nvm.sh\"" >> $BASH_ENV
jobs:
unit-test:
machine: true
@ -73,6 +75,24 @@ jobs:
name: MetaCoin E2E
command: |
./scripts/run-metacoin.sh
e2e-buidler:
machine: true
steps:
- checkout
- <<: *step_install_nvm
- run:
name: Buidler E2E
command: |
./scripts/run-buidler.sh
e2e-moloch:
machine: true
steps:
- checkout
- <<: *step_install_nvm
- run:
name: Moloch E2E
command: |
./scripts/run-moloch.sh
workflows:
version: 2
build:
@ -80,16 +100,5 @@ workflows:
- unit-test
- e2e-zeppelin
- e2e-metacoin
# TODO: re-enable.
# At the moment we're using forks so this is pointless
#nightly:
# triggers:
# - schedule:
# cron: "0 1 * * *" # 1am UTC
# filters:
# branches:
# only:
# - master
# jobs:
#- e2e-zeppelin
#- e2e-colony
- e2e-buidler
- e2e-moloch

@ -4,6 +4,8 @@
![npm (tag)](https://img.shields.io/npm/v/solidity-coverage/beta)
[![CircleCI](https://circleci.com/gh/sc-forks/solidity-coverage.svg?style=svg)][20]
[![codecov](https://codecov.io/gh/sc-forks/solidity-coverage/branch/beta/graph/badge.svg)][21]
[![buidler](https://buidler.dev/buidler-plugin-badge.svg?1)][26]
## Code coverage for Solidity testing
![coverage example][22]
@ -17,9 +19,9 @@
$ npm install --save-dev solidity-coverage@beta
```
## Truffle V5
### Truffle V5
**Add** this package to your plugins array in `truffle-config.js`
**Add** this package to your plugins array in `truffle-config.js` ([Truffle docs][27])
```javascript
module.exports = {
networks: {...},
@ -31,22 +33,36 @@ module.exports = {
truffle run coverage [command-options]
```
### Buidler
**Add** the plugin in `buidler.config.js` ([Buidler docs][26])
```javascript
usePlugin('solidity-coverage')
module.exports = {
networks: {...},
}
```
**Run**
```
npx buidler coverage [command-options]
```
## Usage notes:
+ Coverage runs tests a little more slowly.
+ Coverage launches its own in-process ganache server.
+ You can set [ganache options][1] using the `providerOptions` key in your `.solcover.js` [config][15].
+ Coverage [distorts gas consumption][13]. Tests that check exact gas consumption should be [skipped][24].
+ :warning: Contracts are compiled **without optimization**. Please report unexpected compilation faults to [issue 417][25]
+ :warning: Contracts are compiled **without optimization**. Please report unexpected compilation faults to [issue 417][25]
## Command Options
| Option <img width=200/> | Example <img width=750/>| Description <img width=1000/> |
|--------------|------------------------------------|--------------------------------|
| file | `--file="test/registry/*.js"` | Filename or glob describing a subset of JS tests to run. (Globs must be enclosed by quotes.)|
| file | `--file="test/registry/*.js"` | (Truffle) Filename or glob describing a subset of JS tests to run. (Globs must be enclosed by quotes.)|
| testfiles | `--testfiles test/file.js` | (Buidler) JS test file(s) to run.|
| solcoverjs | `--solcoverjs ./../.solcover.js` | Relative path from working directory to config. Useful for monorepo packages that share settings. (Path must be "./" prefixed) |
| network | `--network development` | Use network settings defined in the Truffle config |
| network | `--network development` | Use network settings defined in the Truffle or Buidler config |
| temp[<sup>*</sup>][14] | `--temp build` | :warning: **Caution** :warning: Path to a *disposable* folder to store compilation artifacts in. Useful when your test setup scripts include hard-coded paths to a build directory. [More...][14] |
| version | | Version info |
| help | | Usage notes |
[<sup>*</sup> Advanced use][14]
@ -63,21 +79,32 @@ module.exports = {
```
| Option <img width=200/>| Type <img width=200/> | Default <img width=700/> | Description <img width=1000/> |
| Option <img width=200/>| Type <img width=200/> | Default <img width=1300/> | Description <img width=800/> |
| ------ | ---- | ------- | ----------- |
| silent | *Boolean* | false | Suppress logging output |
| client | *Object* | `require("ganache-core")` | Useful if you need a specific ganache version. |
| providerOptions | *Object* | `{ }` | [ganache-core options][1] |
| skipFiles | *Array* | `['Migrations.sol']` | Array of contracts or folders (with paths expressed relative to the `contracts` directory) that should be skipped when doing instrumentation. |
| istanbulReporter | *Array* | `['html', 'lcov', 'text']` | [Istanbul coverage reporters][2] |
| istanbulFolder | *String* | `./coverage` | Folder location for Istanbul coverage reports. |
| istanbulReporter | *Array* | `['html', 'lcov', 'text', 'json']` | [Istanbul coverage reporters][2] |
| mocha | *Object* | `{ }` | [Mocha options][3] to merge into existing mocha config. `grep` and `invert` are useful for skipping certain tests under coverage using tags in the test descriptions.|
| onServerReady[<sup>*</sup>][14] | *Function* | | Hook run *after* server is launched, *before* the tests execute. Useful if you need to use the Oraclize bridge or have setup scripts which rely on the server's availability. [More...][23] |
| onCompileComplete[<sup>*</sup>][14] | *Function* | | Hook run *after* compilation completes, *before* tests are run. Useful if you have secondary compilation steps or need to modify built artifacts. [More...][23]|
| onTestsComplete[<sup>*</sup>][14] | *Function* | | Hook run *after* the tests complete, *before* Istanbul reports are generated.|
| onIstanbulComplete[<sup>*</sup>][14] | *Function* | | Hook run *after* the Istanbul reports are generated, *before* the ganache server is shut down. Useful if you need to clean resources up.|
| onTestsComplete[<sup>*</sup>][14] | *Function* | | Hook run *after* the tests complete, *before* Istanbul reports are generated. [More...][23]|
| onIstanbulComplete[<sup>*</sup>][14] | *Function* | | Hook run *after* the Istanbul reports are generated, *before* the ganache server is shut down. Useful if you need to clean resources up. [More...][23]|
[<sup>*</sup> Advanced use][14]
## API
Solidity-coverage's core methods and many utilities are available as an API.
```javascript
const CoverageAPI = require('solidity-coverage/api');
```
[Documentation available here][28].
## FAQ
Common problems & questions:
@ -152,3 +179,6 @@ $ yarn
[23]: https://github.com/sc-forks/solidity-coverage/blob/beta/docs/advanced.md#workflow-hooks
[24]: https://github.com/sc-forks/solidity-coverage/blob/beta/docs/advanced.md#skipping-tests
[25]: https://github.com/sc-forks/solidity-coverage/issues/417
[26]: https://buidler.dev/
[27]: https://www.trufflesuite.com/docs
[28]: https://github.com/sc-forks/solidity-coverage/blob/beta/docs/api.md

@ -0,0 +1,4 @@
// For require('solidity-coverage/api');
const api = require('./lib/api');
module.exports = api;

@ -1,18 +0,0 @@
// Mute compiler warnings - this will need to be addressed properly in
// the Buidler plugin by overloading TASK_COMPILE_COMPILE.
const originalLog = console.log;
console.warn = () => {};
console.log = val => val === '\n' ? null : originalLog(val);
module.exports = {
solc: {
version: "0.5.8"
},
paths: {
artifacts: "./test/artifacts",
cache: "./test/cache",
test: "./test/units",
sources: "./test/sources/contracts",
}
}

@ -1,460 +0,0 @@
/**
* A collection of utilities for common tasks plugins will need in the course
* of composing a workflow using the solidity-coverage API
*
* TODO: Sweep back through here and make all `config.truffle_variable` plugin
* platform neutral...
*/
const PluginUI = require('./truffle.ui');
const path = require('path');
const fs = require('fs-extra');
const dir = require('node-dir');
const globby = require('globby');
const shell = require('shelljs');
const globalModules = require('global-modules');
const TruffleProvider = require('@truffle/provider');
// ===
// UI
// ===
/**
* Displays a list of skipped contracts
* @param {TruffleConfig} config
* @return {Object[]} skipped array of objects generated by `assembleTargets` method
*/
function reportSkipped(config, skipped=[]){
let started = false;
const ui = new PluginUI(config.logger.log);
for (let item of skipped){
if (!started) {
ui.report('instr-skip', []);
started = true;
}
ui.report('instr-skipped', [item.relativePath]);
}
}
// ========
// File I/O
// ========
/**
* Loads source
* @param {String} _path absolute path
* @return {String} source file
*/
function loadSource(_path){
return fs.readFileSync(_path).toString();
}
/**
* Save a set of instrumented files to a temporary directory.
* @param {Object[]} targets array of targets generated by `assembleTargets`
* @param {[type]} originalDir absolute path to parent directory of original source
* @param {[type]} tempDir absolute path to temp parent directory
*/
function save(targets, originalDir, tempDir){
let _path;
for (target of targets) {
_path = target.canonicalPath.replace(originalDir, tempDir);
fs.outputFileSync(_path, target.source);
}
}
/**
* Relativizes an absolute file path, given an absolute parent path
* @param {String} pathToFile
* @param {String} pathToParent
* @return {String} relative path
*/
function toRelativePath(pathToFile, pathToParent){
return pathToFile.replace(`${pathToParent}${path.sep}`, '');
}
/**
* Returns a pair of canonically named temporary directory paths for contracts
* and artifacts. Instrumented assets can be written & compiled to these.
* Then the unit tests can be run, consuming them as sources.
* @param {TruffleConfig} config
* @return {Object} temp paths
*/
function getTempLocations(config){
const cwd = config.working_directory;
const contractsDirName = '.coverage_contracts';
const artifactsDirName = config.temp || '.coverage_artifacts';
return {
tempContractsDir: path.join(cwd, contractsDirName),
tempArtifactsDir: path.join(cwd, artifactsDirName)
}
}
/**
* Checks for existence of contract sources, and sweeps away debris
* left over from an uncontrolled crash.
*/
function checkContext(config, tempContractsDir, tempArtifactsDir){
const ui = new PluginUI(config.logger.log);
if (!shell.test('-e', config.contracts_directory)){
const msg = ui.generate('sources-fail', [config.contracts_directory])
throw new Error(msg);
}
if (shell.test('-e', tempContractsDir)){
shell.rm('-Rf', tempContractsDir);
}
if (shell.test('-e', tempArtifactsDir)){
shell.rm('-Rf', tempArtifactsDir);
}
}
// =============================
// Instrumentation Set Assembly
// =============================
function assembleFiles(config, skipFiles=[]){
let targets;
let skipFolders;
let skipped = [];
const {
tempContractsDir,
tempArtifactsDir
} = getTempLocations(config);
checkContext(config, tempContractsDir, tempArtifactsDir);
shell.mkdir(tempContractsDir);
shell.mkdir(tempArtifactsDir);
targets = shell.ls(`${config.contracts_directory}/**/*.sol`);
skipFiles = assembleSkipped(config, targets, skipFiles);
return assembleTargets(config, targets, skipFiles)
}
function assembleTargets(config, targets=[], skipFiles=[]){
const skipped = [];
const filtered = [];
const cd = config.contracts_directory;
for (let target of targets){
if (skipFiles.includes(target)){
skipped.push({
canonicalPath: target,
relativePath: toRelativePath(target, cd),
source: loadSource(target)
})
} else {
filtered.push({
canonicalPath: target,
relativePath: toRelativePath(target, cd),
source: loadSource(target)
})
}
}
return {
skipped: skipped,
targets: filtered
}
}
/**
* Parses the skipFiles option (which also accepts folders)
*/
function assembleSkipped(config, targets, skipFiles=[]){
const cd = config.contracts_directory;
// Make paths absolute
skipFiles = skipFiles.map(contract => `${cd}/${contract}`);
skipFiles.push(`${cd}/Migrations.sol`);
// Enumerate files in skipped folders
const skipFolders = skipFiles.filter(item => path.extname(item) !== '.sol')
for (let folder of skipFolders){
for (let target of targets ) {
if (target.indexOf(folder) === 0)
skipFiles.push(target);
}
};
return skipFiles;
}
// ========
// Truffle
// ========
/**
* Returns a list of test files to pass to mocha.
* @param {Object} config truffleConfig
* @return {String[]} list of files to pass to mocha
*/
function getTestFilePaths(config){
let target;
let ui = new PluginUI(config.logger.log);
// Handle --file <path|glob> cli option (subset of tests)
(typeof config.file === 'string')
? target = globby.sync([config.file])
: target = dir.files(config.test_directory, { sync: true }) || [];
// Filter native solidity tests and warn that they're skipped
const solregex = /.*\.(sol)$/;
const hasSols = target.filter(f => f.match(solregex) != null);
if (hasSols.length > 0) ui.report('sol-tests', [hasSols.length]);
// Return list of test files
const testregex = /.*\.(js|ts|es|es6|jsx)$/;
return target.filter(f => f.match(testregex) != null);
}
/**
* Configures the network. Runs before the server is launched.
* User can request a network from truffle-config with "--network <name>".
* There are overlapiing options in solcoverjs (like port and providerOptions.network_id).
* Where there are mismatches user is warned & the truffle network settings are preferred.
*
* Also generates a default config & sets the default gas high / gas price low.
*
* @param {TruffleConfig} config
* @param {SolidityCoverage} api
*/
function setNetwork(config, api){
const ui = new PluginUI(config.logger.log);
// --network <network-name>
if (config.network){
const network = config.networks[config.network];
// Check network:
if (!network){
throw new Error(ui.generate('no-network', [config.network]));
}
// Check network id
if (!isNaN(parseInt(network.network_id))){
// Warn: non-matching provider options id and network id
if (api.providerOptions.network_id &&
api.providerOptions.network_id !== parseInt(network.network_id)){
ui.report('id-clash', [ parseInt(network.network_id) ]);
}
// Prefer network defined id.
api.providerOptions.network_id = parseInt(network.network_id);
} else {
network.network_id = "*";
}
// Check port: use solcoverjs || default if undefined
if (!network.port) {
ui.report('no-port', [api.port]);
network.port = api.port;
}
// Warn: port conflicts
if (api.port !== api.defaultPort && api.port !== network.port){
ui.report('port-clash', [ network.port ])
}
// Prefer network port if defined;
api.port = network.port;
network.gas = api.gasLimit;
network.gasPrice = api.gasPrice;
setOuterConfigKeys(config, api, network.network_id);
return;
}
// Default Network Configuration
config.network = 'soliditycoverage';
setOuterConfigKeys(config, api, "*");
config.networks[config.network] = {
network_id: "*",
port: api.port,
host: api.host,
gas: api.gasLimit,
gasPrice: api.gasPrice
}
}
/**
* Sets the default `from` account field in the truffle network that will be used.
* This needs to be done after accounts are fetched from the launched client.
* @param {TruffleConfig} config
* @param {Array} accounts
*/
function setNetworkFrom(config, accounts){
if (!config.networks[config.network].from){
config.networks[config.network].from = accounts[0];
}
}
// Truffle complains that these outer keys *are not* set when running plugin fn directly.
// But throws saying they *cannot* be manually set when running as truffle command.
function setOuterConfigKeys(config, api, id){
try {
config.network_id = id;
config.port = api.port;
config.host = api.host;
config.provider = TruffleProvider.create(config);
} catch (err){}
}
/**
* Tries to load truffle module library and reports source. User can force use of
* a non-local version using cli flags (see option). It's necessary to maintain
* a fail-safe lib because feature was only introduced in 5.0.30. Load order is:
*
* 1. local node_modules
* 2. global node_modules
* 3. fail-safe (truffle lib v 5.0.31 at ./plugin-assets/truffle.library)
*
* @param {Object} truffleConfig config
* @return {Module}
*/
function loadTruffleLibrary(config){
const ui = new PluginUI(config.logger.log);
// Local
try {
if (config.useGlobalTruffle || config.usePluginTruffle) throw null;
const lib = require("truffle");
ui.report('lib-local');
return lib;
} catch(err) {};
// Global
try {
if (config.usePluginTruffle) throw null;
const globalTruffle = path.join(globalModules, 'truffle');
const lib = require(globalTruffle);
ui.report('lib-global');
return lib;
} catch(err) {};
// Plugin Copy @ v 5.0.31
try {
if (config.forceLibFailure) throw null; // For err unit testing
ui.report('lib-warn');
return require("./truffle.library")
} catch(err) {
throw new Error(ui.generate('lib-fail', [err]));
};
}
function loadSolcoverJS(config){
let solcoverjs;
let coverageConfig;
let ui = new PluginUI(config.logger.log);
// Handle --solcoverjs flag
(config.solcoverjs)
? solcoverjs = path.join(config.working_directory, config.solcoverjs)
: solcoverjs = path.join(config.working_directory, '.solcover.js');
// Catch solcoverjs syntax errors
if (shell.test('-e', solcoverjs)){
try {
coverageConfig = require(solcoverjs);
} catch(error){
error.message = ui.generate('solcoverjs-fail') + error.message;
throw new Error(error)
}
// Config is optional
} else {
coverageConfig = {};
}
// Truffle writes to coverage config
coverageConfig.log = config.logger.log;
coverageConfig.cwd = config.working_directory;
coverageConfig.originalContractsDir = config.contracts_directory;
// Solidity-Coverage writes to Truffle config
config.mocha = config.mocha || {};
if (coverageConfig.mocha && typeof coverageConfig.mocha === 'object'){
config.mocha = Object.assign(
config.mocha,
coverageConfig.mocha
);
}
return coverageConfig;
}
// ==========================
// Finishing / Cleanup
// ==========================
/**
* Silently removes temporary folders and calls api.finish to shut server down
* @param {TruffleConfig} config
* @param {SolidityCoverage} api
* @return {Promise}
*/
async function finish(config, api){
const {
tempContractsDir,
tempArtifactsDir
} = getTempLocations(config);
shell.config.silent = true;
shell.rm('-Rf', tempContractsDir);
shell.rm('-Rf', tempArtifactsDir);
if (api) await api.finish();
}
module.exports = {
assembleFiles: assembleFiles,
assembleSkipped: assembleSkipped,
assembleTargets: assembleTargets,
checkContext: checkContext,
finish: finish,
getTempLocations: getTempLocations,
getTestFilePaths: getTestFilePaths,
loadSource: loadSource,
loadSolcoverJS: loadSolcoverJS,
loadTruffleLibrary: loadTruffleLibrary,
reportSkipped: reportSkipped,
save: save,
setNetwork: setNetwork,
setNetworkFrom: setNetworkFrom,
setOuterConfigKeys: setOuterConfigKeys,
checkContext: checkContext,
toRelativePath: toRelativePath
}

@ -0,0 +1,367 @@
# Solidity-Coverage API
Solidity-coverage tracks which lines are hit as your tests run by instrumenting the contracts with special solidity statements and detecting their execution in a coverage-enabled EVM.
As such, the API spans the full set of tasks typically required to run a solidity test suite. The
table below shows how its core methods relate to the stages of a test run:
| Test Stage <img width=200/> | API Method <img width=200/> | Description <img width=800/> |
|---------------|--------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| compilation | `instrument` | A **pre-compilation** step: Rewrites contracts and generates an instrumentation data map. |
| client launch | `ganache` | A **substitute** step: Launches a ganache client with coverage collection enabled in its VM. As the client runs it will mark line/branch hits on the instrumentation data map. |
| test | `report` | A **post-test** step: Generates a coverage report from the data collected by the VM after tests complete. |
| exit | `finish` | A **substitute** step: Shuts client down |
[3]: https://github.com/gotwarlost/istanbul
**Additional Resources:**
+ the library includes [file system utilities](#Utils) for managing the
disposable set of contracts/artifacts which coverage must use in lieu of the 'real' (uninstrumented)
contracts.
+ there are two complete [coverage tool/plugin implementations][5] (for Buidler and Truffle)
which can be used as sources if you're building something similar.
[5]: https://github.com/sc-forks/solidity-coverage/tree/beta/plugins
# Table of Contents
- [API Methods](#api)
* [constructor](#constructor)
* [instrument](#instrument)
* [ganache](#ganache)
* [report](#report)
* [finish](#finish)
* [getInstrumentationData](#getinstrumentationdata)
* [setInstrumentationData](#setinstrumentationdata)
- [Utils Methods](#utils)
* [loadSolcoverJS](#loadsolcoverjs)
* [assembleFiles](#assemblefiles)
* [getTempLocations](#gettemplocations)
* [setupTempFolders](#setuptempfolders)
* [save](#save)
* [finish](#finish-1)
# API
**Example**
```javascript
const CoverageAPI = require("solidity-coverage/api");
const api = new CoverageAPI(options);
```
## constructor
Creates a coverage API instance. Configurable.
**Parameters**
- `options` **Object** : API options
| Option <img width=200/>| Type <img width=200/> | Default <img width=1300/> | Description <img width=800/> |
| ------ | ---- | ------- | ----------- |
| port | *Number* | 8555 | Port to launch client on |
| silent | *Boolean* | false | Suppress logging output |
| client | *Object* | `require("ganache-core")` | JS Ethereum client |
| providerOptions | *Object* | `{ }` | [ganache-core options][1] |
| skipFiles | *Array* | `[]` | Array of contracts or folders (with paths expressed relative to the `contracts` directory) that should be skipped when doing instrumentation. |
| istanbulFolder | *String* | `./coverage` | Folder location for Istanbul coverage reports. |
| istanbulReporter | *Array* | `['html', 'lcov', 'text', 'json']` | [Istanbul coverage reporters][2] |
[1]: https://github.com/trufflesuite/ganache-core#options
[2]: https://istanbul.js.org/docs/advanced/alternative-reporters/
--------------
## instrument
Instruments a set of sources to prepare them for compilation.
:warning: **Important:** Instrumented sources must be compiled with **solc optimization OFF** :warning:
**Parameters**
- `contracts` **Object[]**: Array of solidity sources and their paths
Returns **Object[]** in the same format as the `contracts` param, but with sources instrumented.
**Example**
```javascript
const contracts = [{
source: "contract Simple { uint x = 5; }",
canonicalPath: "/Users/user/project/contracts/Simple.sol",
relativePath: "Simple.sol" // Optional, used for pretty printing.
},...]
const instrumented = api.instrument(contracts)
```
--------------
## ganache
Enables coverage data collection on an in-process ganache server. By default, this method launches
the server, begins listening on the port specified in the [config](#constructor) (or 8555 if unspecified), and
returns a url string. When `autoLaunchServer` is false, method returns `ganache.server` so you can control
the `server.listen` invocation yourself.
**Parameters**
- `client` **Object**: (*Optional*) ganache module
- `autoLaunchServer` **Boolean**: (*Optional*)
Returns **Promise** Address of server to connect to, or initialized, unlaunched server
**Example**
```javascript
const client = require('ganache-cli');
const api = new CoverageAPI( { client: client } );
const address = await api.ganache();
> http://127.0.0.1:8555
// Alternatively...
const server = await api.ganache(client, false);
await pify(server.listen()(8545));
```
--------------
## report
Generates coverage report using IstanbulJS
**Parameters**
- `istanbulFolder` **String**: (*Optional*) path to folder Istanbul will deposit coverage reports in.
Returns **Promise**
**Example**
```javascript
await api.report('./coverage_4A3cd2b'); // Default folder name is 'coverage'
```
-------------
## finish
Shuts down coverage-enabled ganache server instance
Returns **Promise**
**Example**
```javascript
const client = require('ganache-cli');
await api.ganache(client); // Server listening...
await api.finish(); // Server shut down.
```
-------------
## getInstrumentationData
Returns a copy of the hit map created during instrumentation. Useful if you'd like to delegate
coverage collection to multiple processes.
Returns **Object** instrumentation data;
**Example**
```javascript
const contracts = api.instrument(contracts);
const data = api.getInstrumentationData();
save(data); // Pseudo-code
```
-------------
## setInstrumentationData
Sets the hit map object generated during instrumentation. Useful if you'd like
to collect or convert data to coverage for an instrumentation which was generated
in a different process.
**Example**
```javascript
const data = load(data); // Pseudo-code
api.setIntrumentationData(data);
// Client will collect data for the loaded map
const address = await api.ganache(client);
// Or to `report` instrumentation data which was collected in a different process.
const data = load(data); // Pseudo-code
api.setInstrumentationData(data);
api.report();
```
----------------------------------------------------------------------------------------------------
# Utils
```javascript
const utils = require('solidity-coverage/utils');
```
Many of the utils methods take a `config` object param which
defines the absolute paths to your project root and contracts directory.
**Example**
```javascript
const config = {
workingDir: process.cwd(),
contractsDir: path.join(process.cwd(), 'contracts'),
}
```
-------------
## loadSolcoverJS
Loads `.solcoverjs`. Users may specify [options][7] in a `.solcover.js` config file which your
application needs to consume.
**Parameters**
- `config` **Object**: [See *config* above](#Utils)
Returns **Object** Normalized coverage config
**Example**
```javascript
const solcoverJS = utils.loadSolcoverJS(config);
const api = new CoverageAPI(solcoverJS);
```
[7]: https://github.com/sc-forks/solidity-coverage/tree/beta#config-options
-------------
## assembleFiles
Loads contracts from the filesystem in a format that can be passed directly to the
[api.instrument](#instrument) method. Filters by an optional `skipFiles` parameter.
**Parameters**
- `config` **Object**: [See *config* above](#Utils)
- `skipFiles` **String[]**: (*Optional*) Array of files or folders to skip
[See API *constructor*](#constructor)
Returns **Object** with `targets` and `skipped` keys. These are Object arrays of contract sources
and paths.
**Example**
```javascript
const {
targets,
skipped
} = utils.assembleFiles(config, ['Migrations.sol'])
const instrumented = api.instrument(targets);
```
--------------
## getTempLocations
Returns a pair of canonically named temporary directory paths for contracts
and artifacts. Instrumented assets can be compiled from and written to these so the unit tests can
use them as sources.
**Parameters**
- `config` **Object**: [See *config* above](#Utils)
Returns **Object** with two absolute paths to disposable folders, `tempContractsDir`, `tempArtifactsDir`.
These directories are named `.coverage_contracts` and `.coverage_artifacts`.
**Example**
```javascript
const {
tempContractsDir,
tempArtifactsDir
} = utils.getTempLocations(config)
utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir)
// Later, you can call `utils.finish` to delete these...
utils.finish(config, api)
```
----------
## setupTempFolders
Creates temporary directories to store instrumented contracts and their compilation artifacts in.
**Parameters**
- `config` **Object**: [See *config* above](#Utils)
- `tempContractsDir` **String**: absolute path to temporary contracts directory
- `tempArtifactsDir` **String**: absolute path to temporary artifacts directory
**Example**
```javascript
const {
tempContractsDir,
tempArtifactsDir
} = utils.getTempLocations(config)
utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir);
```
-------------
## save
Writes an array of instrumented sources in the object format returned by
[api.instrument](#instrument) to a temporary directory.
**Parameters**
- `contracts` **Object[]**: array of contracts & paths generated by [api.instrument](#instrument)
- `originalDir` **String**: absolute path to original contracts directory
- `tempDir` **String**: absolute path to temp contracts directory (the destination of the save)
**Example**
```javascript
const {
tempContractsDir,
tempArtifactsDir
} = utils.getTempLocations(config)
utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir);
const instrumented = api.instrument(targets);
utils.save(instrumented, config.contractsDir, tempContractsDir);
```
-------------
## finish
Deletes temporary folders and shuts the ganache server down. Is tolerant - if folders or ganache
server don't exist it will return silently.
**Parameters**
- `config` **Object**: [See *config* above](#Utils)
- `api` **Object**: (*Optional*) coverage api instance whose own `finish` method will be called
Returns **Promise**
**Example**
```javascript
await utils.finish(config, api);
```

@ -1,2 +0,0 @@
// stub: this is package.json's "main"
// truffle require.resolve's this in order to check plugin is installed

@ -3,9 +3,9 @@ const pify = require('pify');
const fs = require('fs');
const path = require('path');
const istanbul = require('istanbul');
const util = require('util');
const assert = require('assert');
const detect = require('detect-port');
const _ = require('lodash/lang');
const ConfigValidator = require('./validator');
const Instrumenter = require('./instrumenter');
@ -17,7 +17,7 @@ const AppUI = require('./ui').AppUI;
* Coverage Runner
*/
class API {
constructor(config) {
constructor(config={}) {
this.coverage = new Coverage();
this.instrumenter = new Instrumenter();
this.validator = new ConfigValidator()
@ -30,7 +30,6 @@ class API {
this.testsErrored = false;
this.cwd = config.cwd || process.cwd();
this.originalContractsDir = config.originalContractsDir
this.defaultHook = () => {};
this.onServerReady = config.onServerReady || this.defaultHook;
@ -39,13 +38,13 @@ class API {
this.onIstanbulComplete = config.onIstanbulComplete || this.defaultHook;
this.server = null;
this.provider = null;
this.defaultPort = 8555;
this.client = config.client;
this.defaultNetworkName = 'soliditycoverage';
this.port = config.port || this.defaultPort;
this.host = config.host || "127.0.0.1";
this.providerOptions = config.providerOptions || {};
this.autoLaunchServer = config.autoLaunchServer === false ? false : true;
this.skipFiles = config.skipFiles || [];
@ -55,32 +54,23 @@ class API {
this.gasLimitString = "0xfffffffffff"; // block gas limit for ganache (higher than "gas sent")
this.gasPrice = 0x01;
this.istanbulReporter = config.istanbulReporter || ['html', 'lcov', 'text'];
this.istanbulFolder = config.istanbulFolder || false;
this.istanbulReporter = config.istanbulReporter || ['html', 'lcov', 'text', 'json'];
this.setLoggingLevel(config.silent);
this.ui = new AppUI(this.log);
}
/**
* Instruments a set of sources to prepare them for running under coverage
* @param {Object[]} targets (see below)
* @return {Object[]} (see below)
* @example:
*
* targets:
* [{
* canonicalPath: <absolute-path>
* relativePath: <relative-path>
* source: <source-file>
*
* },...]
*
* outputs:
* [{
* canonicalPath: <path>
* source: <instrumented-source-file>
* }...]
* @example of input/output array:
* [{
* source: (required) <solidity-source>,
* canonicalPath: (required) <absolute path to source file>
* relativePath: (optional) <rel path to source file for logging>
* }]
*/
instrument(targets=[]) {
let currentFile; // Keep track of filename in case we crash...
@ -96,7 +86,7 @@ class API {
this.ui.report('instr-start');
}
this.ui.report('instr-item', [target.relativePath]);
this.ui.report('instr-item', [currentFile]);
const instrumented = this.instrumenter.instrument(
target.source,
@ -121,39 +111,62 @@ class API {
}
/**
* Launches an in-process ethereum client server, hooking the DataCollector to its VM.
* @param {Object} client ganache client
* @return {String} address of server to connect to
* Returns a copy of the hit map created during instrumentation.
* Useful if you'd like to delegate coverage collection to multiple processes.
* @return {Object} instrumentationData
*/
getInstrumentationData(){
return _.cloneDeep(this.instrumenter.instrumentationData)
}
/**
* Sets the hit map object generated during instrumentation. Useful if you'd like
* to collect data for a pre-existing instrumentation.
* @param {Object} data
*/
async ganache(client){
let retry = false;
let address = `http://${this.host}:${this.port}`;
setInstrumentationData(data={}){
this.instrumenter.instrumentationData = _.cloneDeep(data);
}
/**
* Enables coverage collection on in-process ethereum client server, hooking the DataCollector
* to its VM. By default, method will return a url after server has begun listening on the port
* specified in the config. When `autoLaunchServer` is false, method returns`ganache.server` so
* the consumer can control the 'server.listen' invocation themselves.
* @param {Object} client ganache client
* @param {Boolean} autoLaunchServer boolean
* @return {<Promise> (String | Server) } address of server to connect to, or initialized, unlaunched server.
*/
async ganache(client, autoLaunchServer){
// Check for port-in-use
if (await detect(this.port) !== this.port){
throw new Error(this.ui.generate('server-fail', [this.port]))
}
if(!this.client) this.client = client; // Prefer client from options
this.collector = new DataCollector(this.instrumenter.instrumentationData);
this.providerOptions.gasLimit = this.gasLimitString;
this.providerOptions.allowUnlimitedContractSize = true;
// Launch server and attach to vm step of supplied client
// Attach to vm step of supplied client
try {
if (this.config.forceBackupServer) throw new Error()
await this.attachToVM()
await this.attachToVM(client)
}
// Fallback to ganache-core-sc (eq: ganache-core 2.7.0)
// Fallback to ganache-cli)
catch(err) {
this.ui.report('vm-fail', []);
this.client = require('ganache-core-sc');
await this.attachToVM();
const _ganache = require('ganache-cli');
this.ui.report('vm-fail', [_ganache.version]);
await this.attachToVM(_ganache);
}
if (autoLaunchServer === false || this.autoLaunchServer === false){
return this.server;
}
await pify(this.server.listen)(this.port);
const address = `http://${this.host}:${this.port}`;
this.ui.report('server', [address]);
return address;
}
@ -161,16 +174,15 @@ class API {
/**
* Generate coverage / write coverage report / run istanbul
*/
async report() {
async report(_folder) {
const folder = _folder || this.istanbulFolder;
const collector = new istanbul.Collector();
const reporter = new istanbul.Reporter();
const reporter = new istanbul.Reporter(false, folder);
return new Promise((resolve, reject) => {
try {
this.coverage.generate(
this.instrumenter.instrumentationData,
this.originalContractsDir
);
this.coverage.generate(this.instrumenter.instrumentationData);
const mapping = this.makeKeysRelative(this.coverage.data, this.cwd);
this.saveCoverage(mapping);
@ -181,7 +193,8 @@ class API {
// Pify doesn't like this one...
reporter.write(collector, true, (err) => {
if (err) throw err;
if (err) return reject(err);
this.ui.report('istanbul');
resolve();
});
@ -208,10 +221,12 @@ class API {
// ========
// Provider
// ========
async attachToVM(){
async attachToVM(client){
const self = this;
this.server = this.client.server(this.providerOptions);
// Fallback to client from options
if(!client) client = this.client;
this.server = client.server(this.providerOptions);
this.assertHasBlockchain(this.server.provider);
await this.vmIsResolved(this.server.provider);
@ -228,9 +243,6 @@ class API {
vm.on('step', self.collector.step.bind(self.collector));
return vm;
}
// NB: EADDRINUSE errors are uncatch-able?
pify(this.server.listen)(this.port);
}
assertHasBlockchain(provider){

@ -70,7 +70,11 @@ class Instrumenter {
// Walk the AST, recording injection points
ast = SolidityParser.parse(contract.instrumented, {range: true});
contract.contractName = ast.children.filter(node => this._isRootNode(node))[0].name;
const root = ast.children.filter(node => this._isRootNode(node));
// Handle contracts which only contain import statements
contract.contractName = (root.length) ? root[0].name : null;
parse[ast.type](contract, ast);
// We have to iterate through these points in descending order

@ -56,9 +56,9 @@ class AppUI extends UI {
const w = ":warning:";
const kinds = {
'vm-fail': `${w} ${c.red('There was a problem attaching to the ganache-core VM.')} `+
`${c.red('Check the provider option syntax in solidity-coverage docs.')}\n`+
`${w} ${c.red('Using ganache-core-sc (eq. core v2.7.0) instead.')}\n`,
'vm-fail': `${w} ${c.red('There was a problem attaching to the ganache VM.')}\n` +
`${w} ${c.red('For help, see the "client" & "providerOptions" syntax in solidity-coverage docs.')}\n`+
`${w} ${c.red(`Using ganache-cli (v${args[0]}) instead.`)}\n`,
'instr-start': `\n${c.bold('Instrumenting for coverage...')}` +

@ -2,11 +2,6 @@ const Validator = require('jsonschema').Validator;
const AppUI = require('./ui').AppUI;
const util = require('util')
function isFunction(input){
}
Validator.prototype.customFormats.isFunction = function(input) {
return typeof input === "function"
};
@ -20,14 +15,15 @@ const configSchema = {
cwd: {type: "string"},
host: {type: "string"},
originalContractsDir: {type: "string"},
port: {type: "number"},
providerOptions: {type: "object"},
silent: {type: "boolean"},
autoLaunchServer: {type: "boolean"},
istanbulFolder: {type: "string"},
// Hooks:
onServerReady: {type: "function", format: "isFunction"},
onCompileComplete: {type: "function", format: "isFunction"},
onTestComplete: {type: "function", format: "isFunction"},
onIstanbulComplete: {type: "function", format: "isFunction"},

@ -2,19 +2,18 @@
"name": "solidity-coverage",
"version": "0.7.0-beta.2",
"description": "",
"main": "index.js",
"buidler": "dist/buidler.coverage.js",
"main": "plugins/buidler.plugin.js",
"bin": {
"solidity-coverage": "./dist/bin.js"
"solidity-coverage": "./plugins/bin.js"
},
"directories": {
"test": "test"
},
"scripts": {
"nyc": "SILENT=true nyc --exclude '**/sc_temp/**' --exclude '**/test/**'",
"test": "npm run nyc -- mocha test/units/* --timeout 100000 --no-warnings --exit",
"test:ci": "SILENT=true nyc --reporter=lcov --exclude '**/sc_temp/**' --exclude '**/test/**/' -- mocha test/units/* --timeout 100000 --no-warnings --exit",
"test:debug": "mocha test/units/* --timeout 100000 --no-warnings --exit"
"test": "SILENT=true node --max-old-space-size=4096 ./node_modules/.bin/nyc -- mocha test/units/* --timeout 100000 --no-warnings --exit",
"test:ci": "SILENT=true node --max-old-space-size=4096 ./node_modules/.bin/nyc --reporter=lcov --exclude '**/sc_temp/**' --exclude '**/test/**/' -- mocha test/units/* --timeout 100000 --no-warnings --exit",
"test:debug": "node --max-old-space-size=4096 ./node_modules/.bin/mocha test/units/* --timeout 100000 --no-warnings --exit"
},
"homepage": "https://github.com/sc-forks/solidity-coverage",
"repository": {
@ -27,27 +26,30 @@
"@truffle/provider": "^0.1.17",
"chalk": "^2.4.2",
"death": "^1.1.0",
"fs-extra": "^8.1.0",
"detect-port": "^1.3.0",
"ganache-core-sc": "2.7.0-sc.0",
"fs-extra": "^8.1.0",
"ganache-cli": "6.7.0",
"ghost-testrpc": "^0.0.2",
"global-modules": "^2.0.0",
"globby": "^10.0.1",
"istanbul": "^0.4.5",
"jsonschema": "^1.2.4",
"node-dir": "^0.1.17",
"lodash": "^4.17.15",
"node-emoji": "^1.10.0",
"pify": "^4.0.1",
"recursive-readdir": "^2.2.2",
"shelljs": "^0.8.3",
"solidity-parser-antlr": "^0.4.7",
"solidity-parser-antlr": "0.4.7",
"web3": "1.2.1",
"web3-utils": "^1.0.0"
},
"devDependencies": {
"@nomiclabs/buidler": "^1.0.0-beta.8",
"@nomiclabs/buidler-truffle5": "^1.0.0-beta.8",
"@nomiclabs/buidler-web3": "^1.0.0-beta.8",
"@nomiclabs/buidler": "^1.0.1",
"@nomiclabs/buidler-truffle5": "^1.0.1",
"@nomiclabs/buidler-web3": "^1.0.1",
"@truffle/contract": "^4.0.36",
"decache": "^4.5.1",
"ganache-core-sc": "^2.7.0-sc.0",
"mocha": "5.2.0",
"nyc": "^14.1.1",
"solc": "^0.5.10",

@ -0,0 +1,142 @@
const API = require('./../lib/api');
const utils = require('./resources/plugin.utils');
const buidlerUtils = require('./resources/buidler.utils');
const PluginUI = require('./resources/buidler.ui');
const pkg = require('./../package.json');
const death = require('death');
const path = require('path');
const Web3 = require('web3');
const ganache = require('ganache-cli');
const { task, types } = require("@nomiclabs/buidler/config");
const { ensurePluginLoadedWithUsePlugin } = require("@nomiclabs/buidler/plugins");
const {
TASK_TEST,
TASK_COMPILE,
} = require("@nomiclabs/buidler/builtin-tasks/task-names");
ensurePluginLoadedWithUsePlugin();
function plugin() {
// UI for the task flags...
const ui = new PluginUI();
task("coverage", "Generates a code coverage report for tests")
.addOptionalParam("testfiles", ui.flags.file, null, types.string)
.addOptionalParam("solcoverjs", ui.flags.solcoverjs, null, types.string)
.addOptionalParam('temp', ui.flags.temp, null, types.string)
.setAction(async function(args, env){
let error;
let ui;
let api;
let config;
try {
death(buidlerUtils.finish.bind(null, config, api)); // Catch interrupt signals
config = buidlerUtils.normalizeConfig(env.config, args);
ui = new PluginUI(config.logger.log);
api = new API(utils.loadSolcoverJS(config));
// ==============
// Server launch
// ==============
const network = buidlerUtils.setupNetwork(env, api, ui);
const client = api.client || ganache;
const address = await api.ganache(client);
const web3 = new Web3(address);
const accounts = await web3.eth.getAccounts();
const nodeInfo = await web3.eth.getNodeInfo();
const ganacheVersion = nodeInfo.split('/')[1];
// Set default account
network.from = accounts[0];
// Version Info
ui.report('versions', [
ganacheVersion,
pkg.version
]);
ui.report('network', [
env.network.name,
api.port
]);
// Run post-launch server hook;
await api.onServerReady(config);
// ================
// Instrumentation
// ================
const skipFiles = api.skipFiles || [];
let {
targets,
skipped
} = utils.assembleFiles(config, skipFiles);
targets = api.instrument(targets);
utils.reportSkipped(config, skipped);
// ==============
// Compilation
// ==============
config.temp = args.temp;
const {
tempArtifactsDir,
tempContractsDir
} = utils.getTempLocations(config);
utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir)
utils.save(targets, config.paths.sources, tempContractsDir);
utils.save(skipped, config.paths.sources, tempContractsDir);
config.paths.sources = tempContractsDir;
config.paths.artifacts = tempArtifactsDir;
config.paths.cache = buidlerUtils.tempCacheDir(config);
config.solc.optimizer.enabled = false;
await env.run(TASK_COMPILE);
await api.onCompileComplete(config);
// ======
// Tests
// ======
const testfiles = args.testfiles ? [args.testfiles] : [];
try {
await env.run(TASK_TEST, {testFiles: testfiles})
} catch (e) {
error = e;
}
await api.onTestsComplete(config);
// ========
// Istanbul
// ========
await api.report();
await api.onIstanbulComplete(config);
} catch(e) {
error = e;
}
await buidlerUtils.finish(config, api);
if (error !== undefined ) throw error;
if (process.exitCode > 0) throw new Error(ui.generate('tests-fail', [process.exitCode]));
})
}
module.exports = plugin;

@ -0,0 +1,85 @@
const UI = require('./../../lib/ui').UI;
/**
* Buidler Plugin logging
*/
class PluginUI extends UI {
constructor(log){
super(log);
this.flags = {
file: `Path (or glob) defining a subset of tests to run`,
solcoverjs: `Relative path from working directory to config. ` +
`Useful for monorepo packages that share settings.`,
temp: `Path to a disposable folder to store compilation artifacts in. ` +
`Useful when your test setup scripts include hard-coded paths to ` +
`a build directory.`,
}
}
/**
* Writes a formatted message via log
* @param {String} kind message selector
* @param {String[]} args info to inject into template
*/
report(kind, args=[]){
const c = this.chalk;
const ct = c.bold.green('>');
const ds = c.bold.yellow('>');
const w = ":warning:";
const kinds = {
'instr-skip': `\n${c.bold('Coverage skipped for:')}` +
`\n${c.bold('=====================')}\n`,
'instr-skipped': `${ds} ${c.grey(args[0])}`,
'versions': `${ct} ${c.bold('ganache-core')}: ${args[0]}\n` +
`${ct} ${c.bold('solidity-coverage')}: v${args[1]}`,
'network': `\n${c.bold('Network Info')}` +
`\n${c.bold('============')}\n` +
`${ct} ${c.bold('port')}: ${args[1]}\n` +
`${ct} ${c.bold('network')}: ${args[0]}\n`,
'port-clash': `${w} ${c.red("The 'port' values in your Buidler url ")}` +
`${c.red("and .solcover.js are different. Using Buidler's: ")} ${c.bold(args[0])}.\n`,
}
this._write(kinds[kind]);
}
/**
* Returns a formatted message. Useful for error message.
* @param {String} kind message selector
* @param {String[]} args info to inject into template
* @return {String} message
*/
generate(kind, args=[]){
const c = this.chalk;
const x = ":x:";
const kinds = {
'sources-fail': `${c.red('Cannot locate expected contract sources folder: ')} ${args[0]}`,
'solcoverjs-fail': `${c.red('Could not load .solcover.js config file. ')}` +
`${c.red('This can happen if it has a syntax error or ')}` +
`${c.red('the path you specified for it is wrong.')}`,
'tests-fail': `${x} ${c.bold(args[0])} ${c.red('test(s) failed under coverage.')}`,
}
return this._format(kinds[kind])
}
}
module.exports = PluginUI;

@ -0,0 +1,108 @@
const shell = require('shelljs');
const globby = require('globby');
const pluginUtils = require("./plugin.utils");
const path = require('path');
const util = require('util');
const { createProvider } = require("@nomiclabs/buidler/internal/core/providers/construction");
// =============================
// Buidler Plugin Utils
// =============================
/**
* Normalizes buidler paths / logging for use by the plugin utilities and
* attaches them to the config
* @param {BuidlerConfig} config
* @return {BuidlerConfig} updated config
*/
function normalizeConfig(config, args={}){
config.workingDir = config.paths.root;
config.contractsDir = config.paths.sources;
config.testDir = config.paths.tests;
config.artifactsDir = config.paths.artifacts;
config.logger = config.logger ? config.logger : {log: null};
config.solcoverjs = args.solcoverjs
return config;
}
function setupNetwork(env, api, ui){
let networkConfig = {};
let networkName = (env.buidlerArguments.network !== 'buidlerevm')
? env.buidlerArguments.network
: api.defaultNetworkName;
if (networkName !== api.defaultNetworkName){
networkConfig = env.config.networks[networkName];
const configPort = networkConfig.url.split(':')[2];
// Warn: port conflicts
if (api.port !== api.defaultPort && api.port !== configPort){
ui.report('port-clash', [ configPort ])
}
// Prefer network port
api.port = parseInt(configPort);
}
networkConfig.url = `http://${api.host}:${api.port}`;
networkConfig.gas = api.gasLimit;
networkConfig.gasPrice = api.gasPrice;
const provider = createProvider(networkName, networkConfig);
env.config.networks[networkName] = networkConfig;
env.config.defaultNetwork = networkName;
env.network = {
name: networkName,
config: networkConfig,
provider: provider,
}
env.ethereum = provider;
// Return a reference so we can set the from account
return env.network;
}
/**
* Generates a path to a temporary compilation cache directory
* @param {BuidlerConfig} config
* @return {String} .../.coverage_cache
*/
function tempCacheDir(config){
return path.join(config.paths.root, '.coverage_cache');
}
/**
* Silently removes temporary folders and calls api.finish to shut server down
* @param {BuidlerConfig} config
* @param {SolidityCoverage} api
* @return {Promise}
*/
async function finish(config, api){
const {
tempContractsDir,
tempArtifactsDir
} = pluginUtils.getTempLocations(config);
shell.config.silent = true;
shell.rm('-Rf', tempContractsDir);
shell.rm('-Rf', tempArtifactsDir);
shell.rm('-Rf', path.join(config.paths.root, '.coverage_cache'));
shell.config.silent = false;
if (api) await api.finish();
}
module.exports = {
normalizeConfig: normalizeConfig,
finish: finish,
tempCacheDir: tempCacheDir,
setupNetwork: setupNetwork
}

@ -0,0 +1,273 @@
/**
* A collection of utilities for common tasks plugins will need in the course
* of composing a workflow using the solidity-coverage API
*/
const PluginUI = require('./truffle.ui');
const path = require('path');
const fs = require('fs-extra');
const shell = require('shelljs');
const util = require('util')
// ===
// UI
// ===
/**
* Displays a list of skipped contracts
* @param {TruffleConfig} config
* @return {Object[]} skipped array of objects generated by `assembleTargets` method
*/
function reportSkipped(config, skipped=[]){
let started = false;
const ui = new PluginUI(config.logger.log);
for (let item of skipped){
if (!started) {
ui.report('instr-skip', []);
started = true;
}
ui.report('instr-skipped', [item.relativePath]);
}
}
// ========
// File I/O
// ========
/**
* Loads source
* @param {String} _path absolute path
* @return {String} source file
*/
function loadSource(_path){
return fs.readFileSync(_path).toString();
}
/**
* Sets up temporary folders for instrumented contracts and their compilation artifacts
* @param {PlatformConfig} config
* @param {String} tempContractsDir
* @param {String} tempArtifactsDir
*/
function setupTempFolders(config, tempContractsDir, tempArtifactsDir){
checkContext(config, tempContractsDir, tempArtifactsDir);
shell.mkdir(tempContractsDir);
shell.mkdir(tempArtifactsDir);
}
/**
* Save a set of instrumented files to a temporary directory.
* @param {Object[]} targets array of targets generated by `assembleTargets`
* @param {[type]} originalDir absolute path to original contracts directory
* @param {[type]} tempDir absolute path to temp contracts directory
*/
function save(targets, originalDir, tempDir){
let _path;
for (target of targets) {
_path = target.canonicalPath.replace(originalDir, tempDir);
fs.outputFileSync(_path, target.source);
}
}
/**
* Relativizes an absolute file path, given an absolute parent path
* @param {String} pathToFile
* @param {String} pathToParent
* @return {String} relative path
*/
function toRelativePath(pathToFile, pathToParent){
return pathToFile.replace(`${pathToParent}${path.sep}`, '');
}
/**
* Returns a pair of canonically named temporary directory paths for contracts
* and artifacts. Instrumented assets can be written & compiled to these.
* Then the unit tests can be run, consuming them as sources.
* @param {TruffleConfig} config
* @return {Object} temp paths
*/
function getTempLocations(config){
const cwd = config.workingDir;
const contractsDirName = '.coverage_contracts';
const artifactsDirName = config.temp || '.coverage_artifacts';
return {
tempContractsDir: path.join(cwd, contractsDirName),
tempArtifactsDir: path.join(cwd, artifactsDirName)
}
}
/**
* Checks for existence of contract sources, and sweeps away debris
* left over from an uncontrolled crash.
*/
function checkContext(config, tempContractsDir, tempArtifactsDir){
const ui = new PluginUI(config.logger.log);
if (!shell.test('-e', config.contractsDir)){
const msg = ui.generate('sources-fail', [config.contractsDir])
throw new Error(msg);
}
if (shell.test('-e', tempContractsDir)){
shell.rm('-Rf', tempContractsDir);
}
if (shell.test('-e', tempArtifactsDir)){
shell.rm('-Rf', tempArtifactsDir);
}
}
// =============================
// Instrumentation Set Assembly
// =============================
function assembleFiles(config, skipFiles=[]){
let targets;
let skipFolders;
let skipped = [];
targets = shell.ls(`${config.contractsDir}/**/*.sol`);
skipFiles = assembleSkipped(config, targets, skipFiles);
return assembleTargets(config, targets, skipFiles)
}
function assembleTargets(config, targets=[], skipFiles=[]){
const skipped = [];
const filtered = [];
const cd = config.contractsDir;
for (let target of targets){
if (skipFiles.includes(target)){
skipped.push({
canonicalPath: target,
relativePath: toRelativePath(target, cd),
source: loadSource(target)
})
} else {
filtered.push({
canonicalPath: target,
relativePath: toRelativePath(target, cd),
source: loadSource(target)
})
}
}
return {
skipped: skipped,
targets: filtered
}
}
/**
* Parses the skipFiles option (which also accepts folders)
*/
function assembleSkipped(config, targets, skipFiles=[]){
// Make paths absolute
skipFiles = skipFiles.map(contract => `${config.contractsDir}/${contract}`);
// Enumerate files in skipped folders
const skipFolders = skipFiles.filter(item => path.extname(item) !== '.sol')
for (let folder of skipFolders){
for (let target of targets ) {
if (target.indexOf(folder) === 0)
skipFiles.push(target);
}
};
return skipFiles;
}
function loadSolcoverJS(config={}){
let solcoverjs;
let coverageConfig;
let log = config.logger ? config.logger.log : console.log;
let ui = new PluginUI(log);
// Handle --solcoverjs flag
(config.solcoverjs)
? solcoverjs = path.join(config.workingDir, config.solcoverjs)
: solcoverjs = path.join(config.workingDir, '.solcover.js');
// Catch solcoverjs syntax errors
if (shell.test('-e', solcoverjs)){
try {
coverageConfig = require(solcoverjs);
} catch(error){
error.message = ui.generate('solcoverjs-fail') + error.message;
throw new Error(error)
}
// Config is optional
} else {
coverageConfig = {};
}
// Truffle writes to coverage config
coverageConfig.log = log;
coverageConfig.cwd = config.workingDir;
coverageConfig.originalContractsDir = config.contractsDir;
// Solidity-Coverage writes to Truffle config
config.mocha = config.mocha || {};
if (coverageConfig.mocha && typeof coverageConfig.mocha === 'object'){
config.mocha = Object.assign(
config.mocha,
coverageConfig.mocha
);
}
return coverageConfig;
}
// ==========================
// Finishing / Cleanup
// ==========================
/**
* Silently removes temporary folders and calls api.finish to shut server down
* @param {TruffleConfig} config
* @param {SolidityCoverage} api
* @return {Promise}
*/
async function finish(config, api){
const {
tempContractsDir,
tempArtifactsDir
} = getTempLocations(config);
shell.config.silent = true;
shell.rm('-Rf', tempContractsDir);
shell.rm('-Rf', tempArtifactsDir);
shell.config.silent = false;
if (api) await api.finish();
}
module.exports = {
assembleFiles: assembleFiles,
assembleSkipped: assembleSkipped,
assembleTargets: assembleTargets,
checkContext: checkContext,
finish: finish,
getTempLocations: getTempLocations,
loadSource: loadSource,
loadSolcoverJS: loadSolcoverJS,
reportSkipped: reportSkipped,
save: save,
toRelativePath: toRelativePath,
setupTempFolders: setupTempFolders
}

@ -0,0 +1,217 @@
const PluginUI = require('./truffle.ui');
const globalModules = require('global-modules');
const TruffleProvider = require('@truffle/provider');
const recursive = require('recursive-readdir');
const globby = require('globby');
const path = require('path');
// =============================
// Truffle Specific Plugin Utils
// ==============================
/**
* Returns a list of test files to pass to mocha.
* @param {Object} config truffleConfig
* @return {String[]} list of files to pass to mocha
*/
async function getTestFilePaths(config){
let target;
let ui = new PluginUI(config.logger.log);
// Handle --file <path|glob> cli option (subset of tests)
(typeof config.file === 'string')
? target = globby.sync([config.file])
: target = await recursive(config.testDir);
// Filter native solidity tests and warn that they're skipped
const solregex = /.*\.(sol)$/;
const hasSols = target.filter(f => f.match(solregex) != null);
if (hasSols.length > 0) ui.report('sol-tests', [hasSols.length]);
// Return list of test files
const testregex = /.*\.(js|ts|es|es6|jsx)$/;
return target.filter(f => f.match(testregex) != null);
}
/**
* Configures the network. Runs before the server is launched.
* User can request a network from truffle-config with "--network <name>".
* There are overlapiing options in solcoverjs (like port and providerOptions.network_id).
* Where there are mismatches user is warned & the truffle network settings are preferred.
*
* Also generates a default config & sets the default gas high / gas price low.
*
* @param {TruffleConfig} config
* @param {SolidityCoverage} api
*/
function setNetwork(config, api){
const ui = new PluginUI(config.logger.log);
// --network <network-name>
if (config.network){
const network = config.networks[config.network];
// Check network:
if (!network){
throw new Error(ui.generate('no-network', [config.network]));
}
// Check network id
if (!isNaN(parseInt(network.network_id))){
// Warn: non-matching provider options id and network id
if (api.providerOptions.network_id &&
api.providerOptions.network_id !== parseInt(network.network_id)){
ui.report('id-clash', [ parseInt(network.network_id) ]);
}
// Prefer network defined id.
api.providerOptions.network_id = parseInt(network.network_id);
} else {
network.network_id = "*";
}
// Check port: use solcoverjs || default if undefined
if (!network.port) {
ui.report('no-port', [api.port]);
network.port = api.port;
}
// Warn: port conflicts
if (api.port !== api.defaultPort && api.port !== network.port){
ui.report('port-clash', [ network.port ])
}
// Prefer network port if defined;
api.port = network.port;
network.gas = api.gasLimit;
network.gasPrice = api.gasPrice;
setOuterConfigKeys(config, api, network.network_id);
return;
}
// Default Network Configuration
config.network = 'soliditycoverage';
setOuterConfigKeys(config, api, "*");
config.networks[config.network] = {
network_id: "*",
port: api.port,
host: api.host,
gas: api.gasLimit,
gasPrice: api.gasPrice
}
}
/**
* Sets the default `from` account field in the truffle network that will be used.
* This needs to be done after accounts are fetched from the launched client.
* @param {TruffleConfig} config
* @param {Array} accounts
*/
function setNetworkFrom(config, accounts){
if (!config.networks[config.network].from){
config.networks[config.network].from = accounts[0];
}
}
// Truffle complains that these outer keys *are not* set when running plugin fn directly.
// But throws saying they *cannot* be manually set when running as truffle command.
function setOuterConfigKeys(config, api, id){
try {
config.network_id = id;
config.port = api.port;
config.host = api.host;
config.provider = TruffleProvider.create(config);
} catch (err){}
}
/**
* Tries to load truffle module library and reports source. User can force use of
* a non-local version using cli flags (see option). It's necessary to maintain
* a fail-safe lib because feature was only introduced in 5.0.30. Load order is:
*
* 1. local node_modules
* 2. global node_modules
* 3. fail-safe (truffle lib v 5.0.31 at ./plugin-assets/truffle.library)
*
* @param {Object} truffleConfig config
* @return {Module}
*/
function loadLibrary(config){
const ui = new PluginUI(config.logger.log);
// Local
try {
if (config.useGlobalTruffle || config.usePluginTruffle) throw null;
const lib = require("truffle");
ui.report('lib-local');
return lib;
} catch(err) {};
// Global
try {
if (config.usePluginTruffle) throw null;
const globalTruffle = path.join(globalModules, 'truffle');
const lib = require(globalTruffle);
ui.report('lib-global');
return lib;
} catch(err) {};
// Plugin Copy @ v 5.0.31
try {
if (config.forceLibFailure) throw null; // For err unit testing
ui.report('lib-warn');
return require("./truffle.library")
} catch(err) {
throw new Error(ui.generate('lib-fail', [err]));
};
}
/**
* Maps truffle specific keys for the paths to things like sources to the generic
* keys required by the plugin utils
* @return {Object} truffle-config.js
*/
function normalizeConfig(config){
config.workingDir = config.working_directory;
config.contractsDir = config.contracts_directory;
config.testDir = config.test_directory;
config.artifactsDir = config.build_directory;
// eth-gas-reporter freezes the in-process client because it uses sync calls
if (typeof config.mocha === "object" && config.mocha.reporter === 'eth-gas-reporter'){
config.mocha.reporter = 'spec';
delete config.mocha.reporterOptions;
}
// Truffle V4 style solc settings are honored over V5 settings. Apparently it's common
// for both to be present in the same config (as an error).
if (typeof config.solc === "object" ){
config.solc.optimizer = { enabled: false };
}
return config;
}
module.exports = {
getTestFilePaths: getTestFilePaths,
setNetwork: setNetwork,
setNetworkFrom: setNetworkFrom,
loadLibrary: loadLibrary,
normalizeConfig: normalizeConfig,
}

@ -1,6 +1,7 @@
const API = require('./../lib/api');
const utils = require('./plugin-assets/plugin.utils');
const PluginUI = require('./plugin-assets/truffle.ui');
const utils = require('./resources/plugin.utils');
const truffleUtils = require('./resources/truffle.utils');
const PluginUI = require('./resources/truffle.ui');
const pkg = require('./../package.json');
const death = require('death');
@ -23,24 +24,27 @@ async function plugin(config){
try {
death(utils.finish.bind(null, config, api)); // Catch interrupt signals
config = truffleUtils.normalizeConfig(config);
ui = new PluginUI(config.logger.log);
if(config.help) return ui.report('help'); // Exit if --help
truffle = utils.loadTruffleLibrary(config);
truffle = truffleUtils.loadLibrary(config);
api = new API(utils.loadSolcoverJS(config));
utils.setNetwork(config, api);
truffleUtils.setNetwork(config, api);
// Server launch
const address = await api.ganache(truffle.ganache);
const client = api.client || truffle.ganache;
const address = await api.ganache(client);
const web3 = new Web3(address);
const accounts = await web3.eth.getAccounts();
const nodeInfo = await web3.eth.getNodeInfo();
const ganacheVersion = nodeInfo.split('/')[1];
utils.setNetworkFrom(config, accounts);
truffleUtils.setNetworkFrom(config, accounts);
// Version Info
ui.report('versions', [
@ -62,10 +66,13 @@ async function plugin(config){
await api.onServerReady(config);
// Instrument
const skipFiles = api.skipFiles || [];
skipFiles.push('Migrations.sol');
let {
targets,
skipped
} = utils.assembleFiles(config, api.skipFiles);
} = utils.assembleFiles(config, skipFiles);
targets = api.instrument(targets);
utils.reportSkipped(config, skipped);
@ -76,6 +83,7 @@ async function plugin(config){
tempContractsDir
} = utils.getTempLocations(config);
utils.setupTempFolders(config, tempContractsDir, tempArtifactsDir)
utils.save(targets, config.contracts_directory, tempContractsDir);
utils.save(skipped, config.contracts_directory, tempContractsDir);
@ -88,7 +96,7 @@ async function plugin(config){
);
config.all = true;
config.test_files = utils.getTestFilePaths(config);
config.test_files = await truffleUtils.getTestFilePaths(config);
config.compilers.solc.settings.optimizer.enabled = false;
// Compile Instrumented Contracts

@ -0,0 +1,67 @@
#!/usr/bin/env bash
#
# E2E CI: installs PR candidate on sc-forks/buidler-e2e (a simple example,
# similar to Metacoin) and runs coverage
#
set -o errexit
# Get rid of any caches
sudo rm -rf node_modules
echo "NVM CURRENT >>>>>" && nvm current
# Use PR env variables (for forks) or fallback on local if PR not available
SED_REGEX="s/git@github.com:/https:\/\/github.com\//"
if [[ -v CIRCLE_PR_REPONAME ]]; then
PR_PATH="https://github.com/$CIRCLE_PR_USERNAME/$CIRCLE_PR_REPONAME#$CIRCLE_SHA1"
else
PR_PATH=$(echo "$CIRCLE_REPOSITORY_URL#$CIRCLE_SHA1" | sudo sed "$SED_REGEX")
fi
echo "PR_PATH >>>>> $PR_PATH"
echo ""
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo "Simple buidler/buidler-trufflev5 "
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo ""
# Install buidler e2e test
git clone https://github.com/sc-forks/buidler-e2e.git
cd buidler-e2e
npm install
# Install and run solidity-coverage @ PR
npm install --save-dev $PR_PATH
cat package.json
npx buidler coverage
# Test that coverage/ was generated
if [ ! -d "coverage" ]; then
echo "ERROR: no coverage folder was created for buidler-trufflev5."
exit 1
fi
echo ""
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo "Simple buidler/buidler-ethers "
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo ""
cd ..
git clone https://github.com/sc-forks/example-buidler-ethers.git
cd example-buidler-ethers
npm install
# Install and run solidity-coverage @ PR
npm install --save-dev $PR_PATH
cat package.json
npx buidler coverage
# Test that coverage/ was generated
if [ ! -d "coverage" ]; then
echo "ERROR: no coverage folder was created for buidler-ethers."
exit 1
fi

@ -39,3 +39,10 @@ cat truffle-config.js
npm init --yes
yarn add $PR_PATH --dev
npx truffle run coverage
# Test that coverage/ was generated
if [ ! -d "coverage" ]; then
echo "ERROR: no coverage folder was created."
exit 1
fi

@ -0,0 +1,39 @@
#!/usr/bin/env bash
#
# E2E CI: installs PR candidate on sc-forks/buidler-e2e (a simple example,
# similar to Metacoin) and runs coverage
#
set -o errexit
# Get rid of any caches
sudo rm -rf node_modules
echo "NVM CURRENT >>>>>" && nvm current
# Use PR env variables (for forks) or fallback on local if PR not available
SED_REGEX="s/git@github.com:/https:\/\/github.com\//"
if [[ -v CIRCLE_PR_REPONAME ]]; then
PR_PATH="https://github.com/$CIRCLE_PR_USERNAME/$CIRCLE_PR_REPONAME#$CIRCLE_SHA1"
else
PR_PATH=$(echo "$CIRCLE_REPOSITORY_URL#$CIRCLE_SHA1" | sudo sed "$SED_REGEX")
fi
echo "PR_PATH >>>>> $PR_PATH"
# Install buidler e2e test
git clone https://github.com/sc-forks/moloch.git
cd moloch
npm install
npm uninstall --save-dev solidity-coverage
# Install and run solidity-coverage @ PR
# Should run on network 'localhost'
npm install --save-dev $PR_PATH
npm run coverage
# Test that coverage/ was generated
if [ ! -d "coverage" ]; then
echo "ERROR: no coverage folder was created."
exit 1
fi

@ -0,0 +1,8 @@
const { loadPluginFile } = require("@nomiclabs/buidler/plugins-testing");
loadPluginFile(__dirname + "/../plugins/buidler.plugin");
usePlugin("@nomiclabs/buidler-truffle5");
module.exports={
defaultNetwork: "buidlerevm",
logger: process.env.SILENT ? { log: () => {} } : console,
};

@ -0,0 +1,5 @@
module.exports = {
client: require('ganache-cli'),
silent: process.env.SILENT ? true : false,
istanbulReporter: ['json-summary', 'text'],
}

@ -0,0 +1,8 @@
const { loadPluginFile } = require("@nomiclabs/buidler/plugins-testing");
loadPluginFile(__dirname + "/../plugins/buidler.plugin");
usePlugin("@nomiclabs/buidler-truffle5");
module.exports={
defaultNetwork: "buidlerevm",
logger: process.env.SILENT ? { log: () => {} } : console,
};

@ -0,0 +1,17 @@
pragma solidity ^0.5.0;
contract ContractA {
uint x;
constructor() public {
}
function sendFn() public {
x = 5;
}
function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}

@ -0,0 +1,17 @@
pragma solidity ^0.5.0;
contract ContractB {
uint x;
constructor() public {
}
function sendFn() public {
x = 5;
}
function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}

@ -0,0 +1,17 @@
pragma solidity ^0.5.0;
contract ContractC {
uint x;
constructor() public {
}
function sendFn() public {
x = 5;
}
function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}

@ -0,0 +1,23 @@
pragma solidity >=0.4.21 <0.6.0;
contract Migrations {
address public owner;
uint public last_completed_migration;
constructor() public {
owner = msg.sender;
}
modifier restricted() {
if (msg.sender == owner) _;
}
function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}
function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}

@ -0,0 +1,15 @@
const ContractA = artifacts.require("ContractA");
contract("contracta", function(accounts) {
let instance;
before(async () => instance = await ContractA.new())
it('sends [ @skipForCoverage ]', async function(){
await instance.sendFn();
});
it('calls [ @skipForCoverage ]', async function(){
await instance.callFn();
})
});

@ -0,0 +1,15 @@
const ContractB = artifacts.require("ContractB");
contract("contractB [ @skipForCoverage ]", function(accounts) {
let instance;
before(async () => instance = await ContractB.new())
it('sends', async function(){
await instance.sendFn();
});
it('calls', async function(){
await instance.callFn();
})
});

@ -0,0 +1,20 @@
const ContractC = artifacts.require("ContractC");
contract("contractc", function(accounts) {
let instance;
before(async () => instance = await ContractC.new())
it('sends', async function(){
await instance.sendFn();
});
it('calls', async function(){
await instance.callFn();
})
it('sends', async function(){
await instance.sendFn();
});
});

@ -0,0 +1,7 @@
module.exports = {
networks: {},
mocha: {},
compilers: {
solc: {}
}
}

@ -0,0 +1,8 @@
const { loadPluginFile } = require("@nomiclabs/buidler/plugins-testing");
loadPluginFile(__dirname + "/../plugins/buidler.plugin");
usePlugin("@nomiclabs/buidler-truffle5");
module.exports={
defaultNetwork: "buidlerevm",
logger: process.env.SILENT ? { log: () => {} } : console,
};

@ -0,0 +1,5 @@
pragma solidity >=0.4.21 <0.6.0;
import "package/AnotherImport.sol";
interface Void {}

@ -0,0 +1,10 @@
pragma solidity >=0.4.21 <0.6.0;
contract AnotherImport {
uint x;
constructor() public {}
function isNodeModulesMethod() public {
x = 5;
}
}

@ -0,0 +1,7 @@
const { loadPluginFile } = require("@nomiclabs/buidler/plugins-testing");
loadPluginFile(__dirname + "/../plugins/buidler.plugin");
usePlugin("@nomiclabs/buidler-truffle5");
module.exports={
logger: process.env.SILENT ? { log: () => {} } : console,
};

@ -2,60 +2,60 @@ const UsesPure = artifacts.require('UsesPure');
contract('UsesPure', accounts => {
it('calls imported, inherited pure/view functions within its own function', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
await instance.usesThem();
});
it('calls a library method', async() => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.usesLibrary();
assert.equal(value.toNumber(), 1);
});
it('calls an imported, inherited pure function', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.isPure(4, 5);
assert.equal(value.toNumber(), 20);
});
it('calls an imported, inherited view function', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.isView();
assert.equal(value.toNumber(), 5);
});
it('overrides an imported, inherited abstract pure function', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.bePure(4, 5);
assert.equal(value.toNumber(), 9);
});
it('overrides an imported, inherited abstract view function', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.beView();
assert.equal(value.toNumber(), 99);
});
it('calls a pure method implemented in an inherited class', async() => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.inheritedPure(4, 5);
assert.equal(value.toNumber(), 9);
});
it('calls a view method implemented in an inherited class', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.inheritedView();
assert.equal(value.toNumber(), 5);
});
it('calls a view method whose modifiers span lines', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.multiline(5, 7)
assert.equal(value.toNumber(), 99);
});
it('calls a method who signature is defined by an interface', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
await instance.cry();
});

@ -0,0 +1,4 @@
module.exports = {
"silent": false,
"istanbulReporter": [ "json-summary", "text"]
}

@ -0,0 +1,8 @@
const { loadPluginFile } = require("@nomiclabs/buidler/plugins-testing");
loadPluginFile(__dirname + "/../plugins/buidler.plugin");
usePlugin("@nomiclabs/buidler-truffle5");
module.exports={
defaultNetwork: "buidlerevm",
logger: process.env.SILENT ? { log: () => {} } : console,
};

@ -0,0 +1,17 @@
pragma solidity ^0.5.0;
contract ContractA {
uint x;
constructor() public {
}
function sendFn() public {
x = 5;
}
function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}

@ -0,0 +1,17 @@
pragma solidity ^0.5.0;
contract ContractB {
uint x;
constructor() public {
}
function sendFn() public {
x = 5;
}
function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}

@ -0,0 +1,17 @@
pragma solidity ^0.5.0;
contract ContractC {
uint x;
constructor() public {
}
function sendFn() public {
x = 5;
}
function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}

@ -0,0 +1,23 @@
pragma solidity >=0.4.21 <0.6.0;
contract Migrations {
address public owner;
uint public last_completed_migration;
constructor() public {
owner = msg.sender;
}
modifier restricted() {
if (msg.sender == owner) _;
}
function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}
function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}

@ -0,0 +1,15 @@
const ContractA = artifacts.require("ContractA");
contract("contracta", function(accounts) {
let instance;
before(async () => instance = await ContractA.new())
it('sends [ @skipForCoverage ]', async function(){
await instance.sendFn();
});
it('calls [ @skipForCoverage ]', async function(){
await instance.callFn();
})
});

@ -0,0 +1,15 @@
const ContractB = artifacts.require("ContractB");
contract("contractB [ @skipForCoverage ]", function(accounts) {
let instance;
before(async () => instance = await ContractB.new())
it('sends', async function(){
await instance.sendFn();
});
it('calls', async function(){
await instance.callFn();
})
});

@ -0,0 +1,20 @@
const ContractC = artifacts.require("ContractC");
contract("contractc", function(accounts) {
let instance;
before(async () => instance = await ContractC.new())
it('sends', async function(){
await instance.sendFn();
});
it('calls', async function(){
await instance.callFn();
})
it('sends', async function(){
await instance.sendFn();
});
});

@ -0,0 +1,7 @@
module.exports = {
networks: {},
mocha: {},
compilers: {
solc: {}
}
}

@ -0,0 +1,7 @@
const { loadPluginFile } = require("@nomiclabs/buidler/plugins-testing");
loadPluginFile(__dirname + "/../plugins/buidler.plugin");
usePlugin("@nomiclabs/buidler-truffle5");
module.exports={
logger: process.env.SILENT ? { log: () => {} } : console,
};

@ -0,0 +1,8 @@
const { loadPluginFile } = require("@nomiclabs/buidler/plugins-testing");
loadPluginFile(__dirname + "/../plugins/buidler.plugin");
usePlugin("@nomiclabs/buidler-truffle5");
module.exports={
defaultNetwork: "buidlerevm",
logger: process.env.SILENT ? { log: () => {} } : console,
};

@ -2,6 +2,7 @@
const fn = (msg, config) => config.logger.log(msg);
module.exports = {
skipFiles: ['Migrations.sol'],
silent: process.env.SILENT ? true : false,
istanbulReporter: ['json-summary', 'text'],
onServerReady: fn.bind(null, 'running onServerReady'),

@ -0,0 +1,7 @@
const { loadPluginFile } = require("@nomiclabs/buidler/plugins-testing");
loadPluginFile(__dirname + "/../plugins/buidler.plugin");
usePlugin("@nomiclabs/buidler-truffle5");
module.exports={
defaultNetwork: "buidlerevm",
};

@ -3,7 +3,7 @@ const ContractB = artifacts.require("ContractB");
contract("contractB", function(accounts) {
let instance;
before(async () => instance = await ContractB.deployed())
before(async () => instance = await ContractB.new())
it('sends', async function(){
await instance.sendFn();

@ -3,7 +3,7 @@ const ContractC = artifacts.require("ContractC");
contract("contractc", function(accounts) {
let instance;
before(async () => instance = await ContractC.deployed())
before(async () => instance = await ContractC.new())
it('sends', async function(){
await instance.sendFn();

@ -3,7 +3,7 @@ const ContractA = artifacts.require("ContractA");
contract("contracta", function(accounts) {
let instance;
before(async () => instance = await ContractA.deployed())
before(async () => instance = await ContractA.new())
it('sends', async function(){
await instance.sendFn();

@ -0,0 +1,4 @@
module.exports = {
"silent": false,
"istanbulReporter": [ "json-summary", "text"]
}

@ -0,0 +1,8 @@
const { loadPluginFile } = require("@nomiclabs/buidler/plugins-testing");
loadPluginFile(__dirname + "/../plugins/buidler.plugin");
usePlugin("@nomiclabs/buidler-truffle5");
module.exports={
defaultNetwork: "buidlerevm",
logger: process.env.SILENT ? { log: () => {} } : console,
};

@ -0,0 +1,17 @@
pragma solidity ^0.5.0;
contract ContractA {
uint x;
constructor() public {
}
function sendFn() public {
x = 5;
}
function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}

@ -0,0 +1,17 @@
pragma solidity ^0.5.0;
contract ContractB {
uint x;
constructor() public {
}
function sendFn() public {
x = 5;
}
function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}

@ -0,0 +1,17 @@
pragma solidity ^0.5.0;
contract ContractC {
uint x;
constructor() public {
}
function sendFn() public {
x = 5;
}
function callFn() public pure returns (uint){
uint y = 5;
return y;
}
}

@ -0,0 +1,23 @@
pragma solidity >=0.4.21 <0.6.0;
contract Migrations {
address public owner;
uint public last_completed_migration;
constructor() public {
owner = msg.sender;
}
modifier restricted() {
if (msg.sender == owner) _;
}
function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}
function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}

@ -0,0 +1,15 @@
const ContractA = artifacts.require("ContractA");
contract("contracta", function(accounts) {
let instance;
before(async () => instance = await ContractA.new())
it('sends [ @skipForCoverage ]', async function(){
await instance.sendFn();
});
it('calls [ @skipForCoverage ]', async function(){
await instance.callFn();
})
});

@ -0,0 +1,15 @@
const ContractB = artifacts.require("ContractB");
contract("contractB [ @skipForCoverage ]", function(accounts) {
let instance;
before(async () => instance = await ContractB.new())
it('sends', async function(){
await instance.sendFn();
});
it('calls', async function(){
await instance.callFn();
})
});

@ -0,0 +1,20 @@
const ContractC = artifacts.require("ContractC");
contract("contractc", function(accounts) {
let instance;
before(async () => instance = await ContractC.new())
it('sends', async function(){
await instance.sendFn();
});
it('calls', async function(){
await instance.callFn();
})
it('sends', async function(){
await instance.sendFn();
});
});

@ -0,0 +1,7 @@
module.exports = {
networks: {},
mocha: {},
compilers: {
solc: {}
}
}

@ -1,7 +1,7 @@
const Expensive = artifacts.require('./Expensive.sol');
const Expensive = artifacts.require('Expensive');
contract('Expensive', () => {
it('should deploy', async () => {
it('should deploy', async function() {
const instance = await Expensive.new()
const hash = instance.transactionHash;
const receipt = await web3.eth.getTransactionReceipt(hash);

@ -1,8 +1,7 @@
/* eslint-env node, mocha */
/* global artifacts, contract */
const Empty = artifacts.require('Empty');
const Empty = artifacts.require('./Empty.sol');
contract('Empty', () => {
it('should deploy', () => Empty.deployed());
contract('Empty', function() {
it('should deploy', async function (){
await Empty.new()
});
});

@ -1,11 +1,11 @@
const Owned = artifacts.require('./Owned.sol');
const Proxy = artifacts.require('./Proxy.sol');
const Owned = artifacts.require('Owned');
const Proxy = artifacts.require('Proxy');
contract('Proxy', accounts => {
it('Should compile and run when one contract inherits from another', () => Owned.deployed()
.then(() => Proxy.deployed())
.then(instance => instance.isOwner.call({
from: accounts[0],
}))
.then(val => assert.equal(val, true)));
it('when one contract inherits from another', async function(){
const owned = await Owned.new();
const proxy = await Proxy.new();
const val = await proxy.isOwner({from: accounts[0]});
assert.equal(val, true);
})
});

@ -1,17 +1,9 @@
/* eslint-env node, mocha */
/* global artifacts, contract, assert */
const OnlyCall = artifacts.require('./OnlyCall.sol');
const OnlyCall = artifacts.require('OnlyCall');
contract('OnlyCall', accounts => {
it('should return val + 2', done => {
OnlyCall.deployed().then(instance => {
instance.addTwo.call(5, {
from: accounts[0],
}).then(val => {
assert.equal(val, 7);
done();
});
});
});
it('should return val + 2', async function(){
const onlycall = await OnlyCall.new();
const val = await onlycall.addTwo(5);
assert.equal(val.toNumber(), 7);
})
});

@ -1,17 +1,17 @@
/* eslint-env node, mocha */
/* global artifacts, contract, assert */
const PureView = artifacts.require('./PureView.sol');
const PureView = artifacts.require('PureView');
contract('PureView', accounts => {
it('calls a pure function', async function(){
const instance = await PureView.deployed();
const instance = await PureView.new();
const value = await instance.isPure(4,5);
});
it('calls a view function', async function(){
const instance = await PureView.deployed();
const instance = await PureView.new();
const value = await instance.isView();
})
});

@ -2,7 +2,7 @@ const Simple = artifacts.require('Simple');
contract('Simple', () => {
it('should set x to 5', async function(){
const simple = await Simple.deployed()
const simple = await Simple.new()
await simple.test(5);
const val = await simple.getX.call();
assert.equal(val.toNumber(), 5);

@ -1,17 +0,0 @@
/* eslint-env node, mocha */
/* global artifacts, contract, assert */
const Simple = artifacts.require('./Simple.sol');
// This test is constructed correctly but the SimpleError.sol has a syntax error
contract('SimpleError', () => {
it('should set x to 5', () => {
let simple;
return Simple.deployed().then(instance => {
simple = instance;
return simple.test(5);
})
.then(() => simple.getX.call())
.then(val => assert.equal(val, 5));
});
});

@ -1,4 +1,4 @@
const Simple = artifacts.require('./Simple.sol');
const Simple = artifacts.require('Simple');
contract('Simple', accounts => {
@ -7,15 +7,4 @@ contract('Simple', accounts => {
balance = web3.utils.fromWei(balance);
assert(parseInt(balance) >= 776)
});
// Generate some coverage so the script doesn't exit(1) because there are no events
it('should set x to 5', () => {
let simple;
return Simple.deployed().then(instance => {
simple = instance;
return simple.test(5);
})
.then(() => simple.getX.call())
.then(val => assert.equal(val.toNumber(), 5));
});
});

@ -2,55 +2,54 @@ const UsesPure = artifacts.require('UsesPure');
contract('UsesPure', accounts => {
it('calls imported, inherited pure/view functions within its own function', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
await instance.usesThem();
});
it('calls an imported, inherited pure function', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.isPure(4, 5);
assert.equal(value.toNumber(), 20);
});
it('calls an imported, inherited view function', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.isView();
assert.equal(value.toNumber(), 5);
});
it('overrides an imported, inherited abstract pure function', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.bePure(4, 5);
assert.equal(value.toNumber(), 9);
});
it('overrides an imported, inherited abstract view function', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.beView();
assert.equal(value.toNumber(), 99);
});
it('calls a pure method implemented in an inherited class', async() => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.inheritedPure(4, 5);
assert.equal(value.toNumber(), 9);
});
it('calls a view method implemented in an inherited class', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.inheritedView();
assert.equal(value.toNumber(), 5);
});
it('calls a view method whose modifiers span lines', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
const value = await instance.multiline(5, 7)
assert.equal(value.toNumber(), 99);
});
it('calls a method who signature is defined by an interface', async () => {
const instance = await UsesPure.deployed();
const instance = await UsesPure.new();
await instance.cry();
});
});

@ -1,11 +1,11 @@
/* eslint-env node, mocha */
/* global artifacts, contract */
var Simple = artifacts.require('./Simple.sol');
var Simple = artifacts.require('Simple');
// This test should break truffle because it has a syntax error.
contract('Simple', () => {
it('should crash', function(){
return Simple.deployed().then.why.
return Simple.new().then.why.
})
})

@ -1,16 +1,20 @@
/* eslint-env node, mocha */
/* global artifacts, contract, assert */
const Simple = artifacts.require('./Simple.sol');
const Simple = artifacts.require('Simple');
contract('Simple', () => {
it('should set x to 5', () => {
let simple;
return Simple.deployed().then(instance => {
simple = instance;
return simple.test(5);
})
.then(() => simple.getX.call())
.then(val => assert.equal(val.toNumber(), 4)); // <-- Wrong result: test fails
it('should set x to 5', async function() {
let simple = await Simple.new();
await simple.test(5);
const val = await simple.getX();
assert.equal(val.toNumber(), 4) // <-- Wrong result: test fails
});
it('should set x to 2', async function() {
let simple = await Simple.new();
await simple.test(5);
const val = await simple.getX();
assert.equal(val.toNumber(), 2) // <-- Wrong result: test fails
});
});

@ -1,4 +1,4 @@
const Wallet = artifacts.require('./Wallet.sol');
const Wallet = artifacts.require('Wallet');
contract('Wallet', accounts => {
it('should should allow transfers and sends', async () => {

@ -3,7 +3,7 @@ pragma solidity ^0.5.0;
import "./Owned.sol";
contract Proxy is Owned {
function isOwner() public returns (bool) {
function isOwner() public view returns (bool) {
if (msg.sender == owner) {
return true;
} else {

@ -0,0 +1,101 @@
const assert = require('assert');
const detect = require('detect-port');
const Ganache = require('ganache-cli');
const util = require('./../util/util.js');
const API = require('./../../api.js');
const utils = require('./../../utils.js');
describe('api', () => {
let opts;
beforeEach(() => opts = {silent: true})
it('getInstrumentationData', function(){
const api = new API(opts);
const canonicalPath = 'statements/single.sol'
const source = util.getCode(canonicalPath);
api.instrument([{
source: source,
canonicalPath: canonicalPath
}]);
const data = api.getInstrumentationData();
const hash = Object.keys(data)[0];
assert(data[hash].hits === 0);
});
it('setInstrumentationData', function(){
let api = new API(opts);
const canonicalPath = 'statements/single.sol'
const source = util.getCode(canonicalPath);
api.instrument([{
source: source,
canonicalPath: canonicalPath
}]);
const cloneA = api.getInstrumentationData();
const hash = Object.keys(cloneA)[0];
// Verify cloning
cloneA[hash].hits = 5;
const cloneB = api.getInstrumentationData();
assert(cloneB[hash].hits === 0);
// Verify setting
api = new API(opts);
api.instrument([{
source: source,
canonicalPath: canonicalPath
}]);
api.setInstrumentationData(cloneA);
const cloneC = api.getInstrumentationData();
assert(cloneC[hash].hits === 5);
});
it('ganache: autoLaunchServer === false', async function(){
const api = new API(opts);
const port = api.port;
const server = await api.ganache(Ganache, false);
assert(typeof port === 'number')
assert(typeof server === 'object');
assert(typeof server.listen === 'function');
const freePort = await detect(port);
assert(freePort === port);
});
it('config: autoLaunchServer: false', async function(){
opts.autoLaunchServer = false;
const api = new API(opts);
const port = api.port;
const server = await api.ganache(Ganache);
assert(typeof port === 'number')
assert(typeof server === 'object');
assert(typeof server.listen === 'function');
const freePort = await detect(port);
assert(freePort === port);
})
it('utils', async function(){
assert(utils.assembleFiles !== undefined)
assert(utils.checkContext !== undefined)
assert(utils.finish !== undefined)
assert(utils.getTempLocations !== undefined)
assert(utils.setupTempFolders !== undefined)
assert(utils.loadSource !== undefined)
assert(utils.loadSolcoverJS !== undefined)
assert(utils.save !== undefined)
});
})

@ -0,0 +1,180 @@
const assert = require('assert');
const fs = require('fs');
const path = require('path')
const pify = require('pify')
const shell = require('shelljs');
const ganache = require('ganache-cli')
const verify = require('../../util/verifiers')
const mock = require('../../util/integration');
const plugin = require('../../../plugins/buidler.plugin');
// =======
// Errors
// =======
describe('Buidler Plugin: error cases', function() {
let buidlerConfig;
let solcoverConfig;
beforeEach(() => {
mock.clean();
mock.loggerOutput.val = '';
solcoverConfig = { skipFiles: ['Migrations.sol']};
buidlerConfig = mock.getDefaultBuidlerConfig();
verify.cleanInitialState();
})
afterEach(() => {
mock.buidlerTearDownEnv();
mock.clean();
});
it('project contains no contract sources folder', async function() {
mock.installFullProject('no-sources');
mock.buidlerSetupEnv(this);
try {
await this.env.run("coverage");
assert.fail()
} catch(err){
assert(
err.message.includes('Cannot locate expected contract sources folder'),
`Should error when contract sources cannot be found:: ${err.message}`
);
assert(
err.message.includes('sc_temp/contracts'),
`Error message should contain path:: ${err.message}`
);
}
verify.coverageNotGenerated(buidlerConfig);
});
it('.solcover.js has syntax error', async function(){
mock.installFullProject('bad-solcoverjs');
mock.buidlerSetupEnv(this);
try {
await this.env.run("coverage");
assert.fail()
} catch(err){
assert(
err.message.includes('Could not load .solcover.js config file.'),
`Should notify when solcoverjs has syntax error:: ${err.message}`
);
}
verify.coverageNotGenerated(buidlerConfig);
})
it('.solcover.js has incorrectly formatted option', async function(){
solcoverConfig.port = "Antwerpen";
mock.install('Simple', 'simple.js', solcoverConfig);
mock.buidlerSetupEnv(this);
try {
await this.env.run("coverage");
assert.fail()
} catch (err) {
assert(
err.message.includes('config option'),
`Should error on incorrect config options: ${err.message}`
);
}
});
it('tries to launch with a port already in use', async function(){
const server = ganache.server();
mock.install('Simple', 'simple.js', solcoverConfig);
mock.buidlerSetupEnv(this);
await pify(server.listen)(8555);
try {
await this.env.run("coverage");
assert.fail();
} catch(err){
assert(
err.message.includes('is already in use') &&
err.message.includes('lsof'),
`Should error on port-in-use with advice: ${err.message}`
)
}
await pify(server.close)();
});
it('uses an invalid istanbul reporter', async function() {
solcoverConfig = {
silent: process.env.SILENT ? true : false,
istanbulReporter: ['does-not-exist']
};
mock.install('Simple', 'simple.js', solcoverConfig);
mock.buidlerSetupEnv(this);
try {
await this.env.run("coverage");
assert.fail();
} catch(err){
assert(
err.message.includes('does-not-exist') &&
err.message.includes('coverage reports could not be generated'),
`Should error on invalid reporter: ${err.message}`
)
}
});
// Truffle test contains syntax error
it('truffle crashes', async function() {
mock.install('Simple', 'truffle-crash.js', solcoverConfig);
mock.buidlerSetupEnv(this);
try {
await this.env.run("coverage");
assert.fail()
} catch(err){
assert(err.toString().includes('SyntaxError'));
}
});
// Solidity syntax errors
it('compilation failure', async function(){
mock.install('SimpleError', 'simple.js', solcoverConfig);
mock.buidlerSetupEnv(this);
try {
await this.env.run("coverage");
assert.fail()
} catch(err){
assert(err.message.includes('Compilation failed'));
}
verify.coverageNotGenerated(buidlerConfig);
});
it('instrumentation failure', async function(){
mock.install('Unparseable', 'simple.js', solcoverConfig);
mock.buidlerSetupEnv(this);
try {
await this.env.run("coverage");
assert.fail()
} catch(err){
assert(
err.message.includes('Unparseable.sol.'),
`Should throw instrumentation errors with file name: ${err.toString()}`
);
assert(err.stack !== undefined, 'Should have error trace')
}
verify.coverageNotGenerated(buidlerConfig);
});
})

@ -0,0 +1,146 @@
const assert = require('assert');
const fs = require('fs');
const path = require('path')
const shell = require('shelljs');
const verify = require('../../util/verifiers')
const mock = require('../../util/integration');
const plugin = require('../../../plugins/buidler.plugin');
// =======================
// CLI Options / Flags
// =======================
describe('Buidler Plugin: command line options', function() {
let buidlerConfig;
let solcoverConfig;
beforeEach(function(){
mock.clean();
mock.loggerOutput.val = '';
solcoverConfig = {
skipFiles: ['Migrations.sol'],
silent: process.env.SILENT ? true : false,
istanbulReporter: ['json-summary', 'text']
};
buidlerConfig = mock.getDefaultBuidlerConfig();
verify.cleanInitialState();
})
afterEach(async function (){
mock.buidlerTearDownEnv();
mock.clean();
});
it('--temp', async function(){
const taskArgs = {
temp: 'special_folder'
}
mock.install('Simple', 'simple.js', solcoverConfig);
mock.buidlerSetupEnv(this);
await this.env.run("coverage", taskArgs);
const expected = [{
file: mock.pathToContract(buidlerConfig, 'Simple.sol'),
pct: 100
}];
verify.lineCoverage(expected);
});
it('--network (declared port mismatches)', async function(){
solcoverConfig.port = 8222;
mock.install('Simple', 'simple.js', solcoverConfig);
mock.buidlerSetupEnv(this);
this.env.buidlerArguments.network = "development";
await this.env.run("coverage");
assert(
mock.loggerOutput.val.includes("The 'port' values"),
`Should notify about mismatched port values: ${mock.loggerOutput.val}`
);
assert(
mock.loggerOutput.val.includes("8545"),
`Should have used default coverage port 8545: ${mock.loggerOutput.val}`
);
assert(
mock.loggerOutput.val.includes("development"),
`Should have used specified network name: ${mock.loggerOutput.val}`
);
const expected = [{
file: mock.pathToContract(buidlerConfig, 'Simple.sol'),
pct: 100
}];
verify.lineCoverage(expected);
});
it('--testfiles test/<fileName>', async function() {
const taskArgs = {
testfiles: path.join(
buidlerConfig.paths.root,
'test/specific_a.js'
)
};
mock.installFullProject('test-files');
mock.buidlerSetupEnv(this);
await this.env.run("coverage", taskArgs);
const expected = [
{
file: mock.pathToContract(buidlerConfig, 'ContractA.sol'),
pct: 100
},
{
file: mock.pathToContract(buidlerConfig, 'ContractB.sol'),
pct: 0,
},
{
file: mock.pathToContract(buidlerConfig, 'ContractC.sol'),
pct: 0,
},
];
verify.lineCoverage(expected);
});
it('--config ../.solcover.js', async function() {
// Write solcoverjs to parent dir of sc_temp (where the test project is installed)
fs.writeFileSync(
'.solcover.js',
`module.exports=${JSON.stringify(solcoverConfig)}`
);
// This relative path has to be ./ prefixed (it's path.joined to buidler's paths.root)
const taskArgs = {
solcoverjs: './../.solcover.js'
};
mock.install('Simple', 'simple.js');
mock.buidlerSetupEnv(this);
await this.env.run("coverage", taskArgs);
// The relative solcoverjs uses the json-summary reporter
const expected = [{
file: mock.pathToContract(buidlerConfig, 'Simple.sol'),
pct: 100
}];
verify.lineCoverage(expected);
shell.rm('.solcover.js');
});
});

@ -0,0 +1,277 @@
const assert = require('assert');
const fs = require('fs');
const path = require('path')
const shell = require('shelljs');
const verify = require('../../util/verifiers')
const mock = require('../../util/integration');
const plugin = require('../../../plugins/buidler.plugin');
// =======================
// Standard Use-case Tests
// =======================
describe('Buidler Plugin: standard use cases', function() {
let buidlerConfig;
let solcoverConfig;
beforeEach(() => {
mock.clean();
mock.loggerOutput.val = '';
solcoverConfig = { skipFiles: ['Migrations.sol']};
buidlerConfig = mock.getDefaultBuidlerConfig();
verify.cleanInitialState();
})
afterEach(() => {
mock.buidlerTearDownEnv();
mock.clean();
});
it('simple contract', async function(){
mock.install('Simple', 'simple.js', solcoverConfig);
mock.buidlerSetupEnv(this);
await this.env.run("coverage");
verify.coverageGenerated(buidlerConfig);
const output = mock.getOutput(buidlerConfig);
const path = Object.keys(output)[0];
assert(
output[path].fnMap['1'].name === 'test',
'coverage.json missing "test"'
);
assert(
output[path].fnMap['2'].name === 'getX',
'coverage.json missing "getX"'
);
});
it('default network ("buidlerevm")', async function(){
mock.install('Simple', 'simple.js', solcoverConfig);
mock.buidlerSetupEnv(this);
this.env.buidlerArguments.network = "buidlerevm"
await this.env.run("coverage");
assert(
mock.loggerOutput.val.includes("8555"),
`Should have used default coverage port 8555: ${mock.loggerOutput.val}`
);
assert(
mock.loggerOutput.val.includes("soliditycoverage"),
`Should have used specified network name: ${mock.loggerOutput.val}`
);
});
it('with relative path solidity imports', async function() {
mock.installFullProject('import-paths');
mock.buidlerSetupEnv(this);
await this.env.run("coverage");
});
it('uses inheritance', async function() {
mock.installDouble(
['Proxy', 'Owned'],
'inheritance.js',
solcoverConfig
);
mock.buidlerSetupEnv(this);
await this.env.run("coverage");
verify.coverageGenerated(buidlerConfig);
const output = mock.getOutput(buidlerConfig);
const ownedPath = Object.keys(output)[0];
const proxyPath = Object.keys(output)[1];
assert(
output[ownedPath].fnMap['1'].name === 'constructor',
'"constructor" not covered'
);
assert(
output[proxyPath].fnMap['1'].name === 'isOwner',
'"isOwner" not covered'
);
});
it('only uses ".call"', async function(){
mock.install('OnlyCall', 'only-call.js', solcoverConfig);
mock.buidlerSetupEnv(this);
await this.env.run("coverage");
verify.coverageGenerated(buidlerConfig);
const output = mock.getOutput(buidlerConfig);
const path = Object.keys(output)[0];
assert(
output[path].fnMap['1'].name === 'addTwo',
'cov should map "addTwo"'
);
});
it('sends / transfers to instrumented fallback', async function(){
mock.install('Wallet', 'wallet.js', solcoverConfig);
mock.buidlerSetupEnv(this);
await this.env.run("coverage");
verify.coverageGenerated(buidlerConfig);
const output = mock.getOutput(buidlerConfig);
const path = Object.keys(output)[0];
assert(
output[path].fnMap['1'].name === 'transferPayment',
'cov should map "transferPayment"'
);
});
// Truffle test asserts deployment cost is greater than 20,000,000 gas
it('deployment cost > block gasLimit', async function() {
mock.install('Expensive', 'block-gas-limit.js', solcoverConfig);
mock.buidlerSetupEnv(this);
await this.env.run("coverage");
});
// Simple.sol with a failing assertion in a truffle test
it('unit tests failing', async function() {
mock.install('Simple', 'truffle-test-fail.js', solcoverConfig);
mock.buidlerSetupEnv(this);
try {
await this.env.run("coverage");
assert.fail()
} catch(err){
assert(err.message.includes('failed under coverage'));
}
verify.coverageGenerated(buidlerConfig);
const output = mock.getOutput(buidlerConfig);
const path = Object.keys(output)[0];
assert(output[path].fnMap['1'].name === 'test', 'cov missing "test"');
assert(output[path].fnMap['2'].name === 'getX', 'cov missing "getX"');
});
// This project has [ @skipForCoverage ] tags in the test descriptions
// at selected 'contract' and 'it' blocks.
it('config: mocha options', async function() {
solcoverConfig.mocha = {
grep: '@skipForCoverage',
invert: true,
};
solcoverConfig.silent = process.env.SILENT ? true : false,
solcoverConfig.istanbulReporter = ['json-summary', 'text']
mock.installFullProject('multiple-suites', solcoverConfig);
mock.buidlerSetupEnv(this);
await this.env.run("coverage");
const expected = [
{
file: mock.pathToContract(buidlerConfig, 'ContractA.sol'),
pct: 0
},
{
file: mock.pathToContract(buidlerConfig, 'ContractB.sol'),
pct: 0,
},
{
file: mock.pathToContract(buidlerConfig, 'ContractC.sol'),
pct: 100,
},
];
verify.lineCoverage(expected);
});
// Truffle test asserts balance is 777 ether
it('config: providerOptions', async function() {
solcoverConfig.providerOptions = { default_balance_ether: 777 }
mock.install('Simple', 'testrpc-options.js', solcoverConfig);
mock.buidlerSetupEnv(this);
await this.env.run("coverage");
});
it('config: skipped file', async function() {
solcoverConfig.skipFiles = ['Migrations.sol', 'Owned.sol'];
mock.installDouble(
['Proxy', 'Owned'],
'inheritance.js',
solcoverConfig
);
mock.buidlerSetupEnv(this);
await this.env.run("coverage");
verify.coverageGenerated(buidlerConfig);
const output = mock.getOutput(buidlerConfig);
const firstKey = Object.keys(output)[0];
assert(
Object.keys(output).length === 1,
'Wrong # of contracts covered'
);
assert(
firstKey.substr(firstKey.length - 9) === 'Proxy.sol',
'Wrong contract covered'
);
});
it('config: skipped folder', async function() {
mock.installFullProject('skipping');
mock.buidlerSetupEnv(this);
await this.env.run("coverage");
const expected = [{
file: mock.pathToContract(buidlerConfig, 'ContractA.sol'),
pct: 100
}];
const missing = [{
file: mock.pathToContract(buidlerConfig, 'skipped-folder/ContractB.sol'),
}];
verify.lineCoverage(expected);
verify.coverageMissing(missing);
});
it('config: "onServerReady", "onTestsComplete", ...', async function() {
mock.installFullProject('test-files');
mock.buidlerSetupEnv(this);
await this.env.run("coverage");
assert(
mock.loggerOutput.val.includes('running onServerReady') &&
mock.loggerOutput.val.includes('running onTestsComplete') &&
mock.loggerOutput.val.includes('running onCompileComplete') &&
mock.loggerOutput.val.includes('running onIstanbulComplete'),
`Should run "on" hooks : ${mock.loggerOutput.val}`
);
});
})

@ -6,8 +6,8 @@ const shell = require('shelljs');
const ganache = require('ganache-core-sc');
const verify = require('../../util/verifiers')
const mock = require('../../util/integration.truffle');
const plugin = require('../../../dist/truffle.plugin');
const mock = require('../../util/integration');
const plugin = require('../../../plugins/truffle.plugin');
// =======
// Errors
@ -120,7 +120,6 @@ describe('Truffle Plugin: error cases', function() {
}
});
// This case *does* throw an error, but it's uncatch-able;
it('tries to launch with a port already in use', async function(){
const server = ganache.server();
@ -209,7 +208,7 @@ describe('Truffle Plugin: error cases', function() {
})
it('user runs "solidity-coverage" as shell command', function(){
const pathToCommand = './dist/bin.js';
const pathToCommand = './plugins/bin.js';
const pkg = require('../../../package.json');
assert(

@ -4,8 +4,8 @@ const path = require('path')
const shell = require('shelljs');
const verify = require('../../util/verifiers')
const mock = require('../../util/integration.truffle');
const plugin = require('../../../dist/truffle.plugin');
const mock = require('../../util/integration');
const plugin = require('../../../plugins/truffle.plugin');
// =======================
// CLI Options / Flags
@ -22,6 +22,7 @@ describe('Truffle Plugin: command line options', function() {
solcoverConfig = {};
truffleConfig = mock.getDefaultTruffleConfig();
verify.cleanInitialState();
})
afterEach(() => mock.clean());
@ -198,20 +199,7 @@ describe('Truffle Plugin: command line options', function() {
);
});
it('--usePluginTruffle', async function(){
truffleConfig.usePluginTruffle = true;
truffleConfig.logger = mock.testLogger;
mock.install('Simple', 'simple.js', solcoverConfig);
await plugin(truffleConfig);
assert(
mock.loggerOutput.val.includes('fallback Truffle library module'),
`Should notify it's using plugin truffle lib copy: ${mock.loggerOutput.val}`
);
});
it('--coverageArtifacts', async function(){
it('--temp', async function(){
truffleConfig.logger = mock.testLogger;
truffleConfig.temp = 'special_location';

@ -4,8 +4,8 @@ const path = require('path')
const shell = require('shelljs');
const verify = require('../../util/verifiers')
const mock = require('../../util/integration.truffle');
const plugin = require('../../../dist/truffle.plugin');
const mock = require('../../util/integration');
const plugin = require('../../../plugins/truffle.plugin');
// =======================
// Standard Use-case Tests
@ -46,7 +46,9 @@ describe('Truffle Plugin: standard use cases', function() {
);
});
it('with many unbracketed statements (time check)', async function() {
// Instrumentation speed is fine - but this takes solc almost a minute to compile
// Unskip whenever modifying the instrumentation files though.....
it.skip('with many unbracketed statements (time check)', async function() {
truffleConfig.compilers.solc.version = "0.4.24";
mock.install('Oraclize', 'oraclize.js', solcoverConfig, truffleConfig, true);
@ -77,6 +79,29 @@ describe('Truffle Plugin: standard use cases', function() {
verify.lineCoverage(expected);
});
it('tests in first layer and in a sub-folder', async function() {
mock.installFullProject('tests-folder');
await plugin(truffleConfig);
const expected = [
{
file: mock.pathToContract(truffleConfig, 'ContractA.sol'),
pct: 100
},
{
file: mock.pathToContract(truffleConfig, 'ContractB.sol'),
pct: 100,
},
{
file: mock.pathToContract(truffleConfig, 'ContractC.sol'),
pct: 100,
},
];
verify.lineCoverage(expected);
});
it('with relative path solidity imports', async function() {
mock.installFullProject('import-paths');
await plugin(truffleConfig);
@ -154,7 +179,10 @@ describe('Truffle Plugin: standard use cases', function() {
});
// Truffle test asserts deployment cost is greater than 20,000,000 gas
// Test times out on CircleCI @ 100000 ms. Fine locally though.
it('deployment cost > block gasLimit', async function() {
if (process.env.CI) return;
mock.install('Expensive', 'block-gas-limit.js', solcoverConfig);
await plugin(truffleConfig);
});
@ -179,6 +207,24 @@ describe('Truffle Plugin: standard use cases', function() {
assert(output[path].fnMap['2'].name === 'getX', 'cov missing "getX"');
});
// This test tightly coupled to the ganache version in truffle dev dep
it('uses the server from truffle by default', async function(){
truffleConfig.logger = mock.testLogger;
truffleConfig.version = true;
// Baseline inequality check
const truffleClientVersion = "v2.5.7";
// Truffle client
mock.install('Simple', 'simple.js', solcoverConfig);
await plugin(truffleConfig);
assert(
mock.loggerOutput.val.includes(truffleClientVersion),
`Should use truffles ganache: ${mock.loggerOutput.val}`
);
});
it('uses the fallback server', async function(){
truffleConfig.logger = mock.testLogger;
solcoverConfig.forceBackupServer = true;
@ -187,11 +233,70 @@ describe('Truffle Plugin: standard use cases', function() {
await plugin(truffleConfig);
assert(
mock.loggerOutput.val.includes("Using ganache-core-sc"),
mock.loggerOutput.val.includes("Using ganache-cli"),
`Should notify about backup server module: ${mock.loggerOutput.val}`
);
});
// This test errors if the reporter is not re-designated as 'spec' correctly
it('disables eth-gas-reporter', async function(){
truffleConfig.mocha = { reporter: 'eth-gas-reporter' };
mock.install('Simple', 'simple.js', solcoverConfig);
await plugin(truffleConfig);
});
it('disables optimization when truffle-config uses V4 format', async function(){
solcoverConfig = {
silent: process.env.SILENT ? true : false,
istanbulReporter: ['json-summary', 'text']
};
truffleConfig.solc = {
optimizer: { enabled: true, runs: 200 }
};
mock.install('Simple', 'simple.js', solcoverConfig);
await plugin(truffleConfig);
const expected = [
{
file: mock.pathToContract(truffleConfig, 'Simple.sol'),
pct: 100
}
];
verify.lineCoverage(expected);
});
// This test tightly coupled to the ganache version in production deps
// "test-files" project solcoverjs includes `client: require('ganache-cli')`
it('config: client', async function(){
truffleConfig.logger = mock.testLogger;
truffleConfig.version = true;
const configClientVersion = "v2.8.0";
// Config client
mock.installFullProject('ganache-solcoverjs');
await plugin(truffleConfig);
assert(
mock.loggerOutput.val.includes(configClientVersion),
`Should use solcover provided ganache: ${mock.loggerOutput.val}`
);
});
it('config: istanbulFolder', async function(){
solcoverConfig.istanbulFolder = mock.pathToTemp('specialFolder');
// Truffle client
mock.install('Simple', 'simple.js', solcoverConfig);
await plugin(truffleConfig);
assert(verify.pathExists(solcoverConfig.istanbulFolder));
});
// This project has [ @skipForCoverage ] tags in the test descriptions
// at selected 'contract' and 'it' blocks.
it('config: mocha options', async function() {

@ -22,7 +22,7 @@ describe('config validation', () => {
const options = [
"cwd",
"host",
"originalContractsDir",
"istanbulFolder"
]
options.forEach(name => {
@ -42,6 +42,29 @@ describe('config validation', () => {
});
});
it('validates the "boolean" options', function(){
const options = [
"silent",
"autoLaunchServer",
]
options.forEach(name => {
// Pass
solcoverjs = {};
solcoverjs[name] = false;
assert(validator.validate(solcoverjs), `${name} boolean should be valid`)
// Fail
solcoverjs[name] = "false";
try {
validator.validate(solcoverjs);
assert.fail()
} catch (err){
assert(err.message.includes(`"${name}" is not of a type(s) boolean`), err.message);
}
});
});
it('validates the "object" options', function(){
const options = [
"client",
@ -87,7 +110,7 @@ describe('config validation', () => {
});
});
it('validates string array options', function(){
it('validates the "string[]" options', function(){
const options = [
"skipFiles",
"istanbulReporter",
@ -110,9 +133,10 @@ describe('config validation', () => {
});
});
it('validates function options', function(){
it('validates the "function" options', function(){
const options = [
"onCompileComplete",
"onServerReady",
"onTestComplete",
"onIstanbulComplete",

@ -1,30 +1,43 @@
/*
Utilities for generating a mock truffle project to test plugin.
Utilities for generating & managing mock projects to test plugins.
*/
const path = require('path');
const fs = require('fs');
const shell = require('shelljs');
const TruffleConfig = require('truffle-config');
const decache = require('decache');
const TruffleConfig = require('truffle-config');
const { resetBuidlerContext } = require("@nomiclabs/buidler/plugins-testing")
const temp = './sc_temp';
const truffleConfigName = 'truffle-config.js';
const buidlerConfigName = 'buidler.config.js';
const configPath = `${temp}/.solcover.js`;
const testPath = './test/sources/js/';
const sourcesPath = './test/sources/solidity/contracts/app/';
const migrationPath = `${temp}/migrations/2_deploy.js`;
const templatePath = './test/integration/truffle/*';
const templatePath = './test/integration/generic/*';
const projectPath = './test/integration/projects/'
let previousCWD;
// ==========================
// Misc Utils
// ==========================
function decacheConfigs(){
decache(`${process.cwd()}/${temp}/.solcover.js`);
decache(`${process.cwd()}/${temp}/${truffleConfigName}`);
const paths = [
`${process.cwd()}/${temp}/.solcover.js`,
`${process.cwd()}/${temp}/${truffleConfigName}`,
`${process.cwd()}/${temp}/${buidlerConfigName}`,
`${process.cwd()}/${temp}/contracts/Simple.sol`,
`${process.cwd()}/${temp}/test/simple.js`
];
paths.forEach(pth => {
try { decache(pth) } catch (e){}
});
}
function clean() {
@ -42,13 +55,35 @@ function pathToContract(config, file) {
return path.join('contracts', file);
}
function getOutput(truffleConfig){
const jsonPath = path.join(truffleConfig.working_directory, "coverage.json");
function pathToTemp(_path) {
return path.join(temp, _path);
}
function getOutput(config){
const workingDir = config.working_directory || config.paths.root;
const jsonPath = path.join(workingDir, "coverage.json");
return JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
}
// Buidler env set up
function buidlerSetupEnv(mocha) {
const mockwd = path.join(process.cwd(), temp);
previousCWD = process.cwd();
process.chdir(mockwd);
mocha.env = require("@nomiclabs/buidler");
mocha.env.config.logger = testLogger
mocha.logger = testLogger
};
// Buidler env tear down
function buidlerTearDownEnv() {
resetBuidlerContext();
process.chdir(previousCWD);
};
// ==========================
// Configuration
// Truffle Configuration
// ==========================
function getDefaultTruffleConfig(){
const logger = process.env.SILENT ? { log: () => {} } : console;
@ -82,14 +117,66 @@ function getDefaultTruffleConfig(){
return (new TruffleConfig()).with(vals);
}
function getTruffleConfigJS(config){
if (config) {
return `module.exports = ${JSON.stringify(config, null, ' ')}`;
} else {
return `module.exports = ${JSON.stringify(getDefaultTruffleConfig(), null, ' ')}`;
}
}
// ==========================
// Buidler Configuration
// ==========================
function getDefaultBuidlerConfig() {
const logger = process.env.SILENT ? { log: () => {} } : console;
const reporter = process.env.SILENT ? 'dot' : 'spec';
const mockwd = path.join(process.cwd(), temp);
const vals = {
paths : {
root: mockwd,
artifacts: path.join(mockwd, 'artifacts'),
cache: path.join(mockwd, 'cache'),
sources: path.join(mockwd, 'contracts'),
tests: path.join(mockwd, 'test'),
},
logger: logger,
mocha: {
reporter: reporter
},
defaultNetwork: "buidlerevm",
networks: {
development: {
url: "http://127.0.0.1:8545",
}
},
}
return vals;
}
function getBuidlerConfigJS(config){
const prefix =`
const { loadPluginFile } = require("@nomiclabs/buidler/plugins-testing");
loadPluginFile(__dirname + "/../plugins/buidler.plugin");
usePlugin("@nomiclabs/buidler-truffle5");
`
if (config) {
return `${prefix}module.exports = ${JSON.stringify(config, null, ' ')}`;
} else {
return `${prefix}module.exports = ${JSON.stringify(getDefaultBuidlerConfig(), null, ' ')}`;
}
}
// ==========================
// .solcover.js Configuration
// ==========================
function getSolcoverJS(config){
return `module.exports = ${JSON.stringify(config, null, ' ')}`
}
function getTruffleConfigJS(config){
if (config) return `module.exports = ${JSON.stringify(config, null, ' ')}`
return `module.exports = ${JSON.stringify(getDefaultTruffleConfig(), null, ' ')}`
}
// ==========================
// Migration Generators
@ -117,7 +204,7 @@ function deployDouble(contractNames){
// Project Installers
// ==========================
/**
* Installs mock truffle project at ./temp with a single contract
* Installs mock truffle/buidler project at ./temp with a single contract
* and test specified by the params.
* @param {String} contract <contractName.sol> located in /test/sources/cli/
* @param {[type]} test <testName.js> located in /test/cli/
@ -125,15 +212,16 @@ function deployDouble(contractNames){
function install(
contract,
test,
config,
_truffleConfig,
solcoverConfig,
devPlatformConfig,
noMigrations
) {
let configjs;
if(config) configjs = getSolcoverJS(config);
if(solcoverConfig) solcoverJS = getSolcoverJS(solcoverConfig);
const trufflejs = getTruffleConfigJS(_truffleConfig);
const trufflejs = getTruffleConfigJS(devPlatformConfig);
const buidlerjs = getBuidlerConfigJS(devPlatformConfig);
const migration = deploySingle(contract);
// Scaffold
@ -151,15 +239,14 @@ function install(
// Configs
fs.writeFileSync(`${temp}/${truffleConfigName}`, trufflejs);
if(config) fs.writeFileSync(configPath, configjs);
fs.writeFileSync(`${temp}/${buidlerConfigName}`, buidlerjs);
if(solcoverConfig) fs.writeFileSync(configPath, solcoverJS);
decacheConfigs();
};
/**
* Installs mock truffle project with two contracts (for inheritance, libraries, etc)
*
* Installs mock truffle/buidler project with two contracts (for inheritance, libraries, etc)
*/
function installDouble(contracts, test, config) {
const configjs = getSolcoverJS(config);
@ -182,11 +269,15 @@ function installDouble(contracts, test, config) {
// Configs
fs.writeFileSync(`${temp}/${truffleConfigName}`, getTruffleConfigJS());
fs.writeFileSync(`${temp}/${buidlerConfigName}`, getBuidlerConfigJS());
fs.writeFileSync(configPath, configjs);
decacheConfigs();
};
/**
* Installs full truffle/buidler project
*/
function installFullProject(name, config) {
shell.mkdir(temp);
shell.cp('-Rf', `${projectPath}${name}/{.,}*`, temp);
@ -216,14 +307,18 @@ const testLogger = {
module.exports = {
pathToTemp: pathToTemp,
testLogger: testLogger,
loggerOutput: loggerOutput,
getDefaultTruffleConfig: getDefaultTruffleConfig,
getDefaultBuidlerConfig: getDefaultBuidlerConfig,
install: install,
installDouble: installDouble,
installFullProject: installFullProject,
clean: clean,
pathToContract: pathToContract,
getOutput: getOutput
getOutput: getOutput,
buidlerSetupEnv: buidlerSetupEnv,
buidlerTearDownEnv: buidlerTearDownEnv
}

@ -5,7 +5,7 @@
const fs = require('fs');
const path = require('path');
const solc = require('solc');
const TruffleContract = require('truffle-contract');
const TruffleContract = require('@truffle/contract');
const Instrumenter = require('./../../lib/instrumenter');
const DataCollector = require('./../../lib/collector')
@ -118,11 +118,11 @@ function initializeProvider(ganache){
}
module.exports = {
getCode: getCode,
pathPrefix: pathPrefix,
filePath: filePath,
report: report,
instrumentAndCompile: instrumentAndCompile,
bootstrapCoverage: bootstrapCoverage,
initializeProvider: initializeProvider,
}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save