Skip to content

Commit

Permalink
Fix ethers wrapped provider data collection / re-enable independent n…
Browse files Browse the repository at this point in the history
…ode tests (#30)
  • Loading branch information
cgewecke committed Oct 13, 2020
1 parent 7c6093a commit d779c7a
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 8 deletions.
41 changes: 39 additions & 2 deletions scripts/run-tests.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,46 @@
#!/usr/bin/env bash

set -o errexit
trap cleanup EXIT

# Truffle tests
cleanup() {
if [ -n "$ganache_pid" ] && ps -p $ganache_pid > /dev/null; then
echo "Killing ganache."
kill -9 $ganache_pid
fi

if [ -n "$buidlerevm_pid" ] && ps -p $buidlerevm_pid > /dev/null; then
echo "Killing buidlerevm."
kill -9 $buidlerevm_pid
fi
}

start_ganache() {
echo "Launching ganache..."
node_modules/.bin/ganache-cli > /dev/null &
ganache_pid=$!
sleep 4
}

start_buidlerevm() {
echo "Launching buidlerevm..."
node_modules/.bin/buidler node > /dev/null &
buidlerevm_pid=$!
sleep 4
}


# Truffle + BuidlerEVM
npx mocha test/truffle.ts --timeout 100000 --exit

# Ethers tests
# Ethers + BuidlerEVM
npx mocha test/ethers.ts --timeout 100000 --exit

# Ethers + Buidler Node
start_buidlerevm
npx mocha test/buidlerevm.node.ts --timeout 100000 --exit
cleanup

# Truffle + Ganache
start_ganache
npx mocha test/ganache.node.ts --timeout 100000 --exit
19 changes: 15 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { readFileSync } from "fs";
import { TASK_TEST_RUN_MOCHA_TESTS } from "@nomiclabs/buidler/builtin-tasks/task-names";
import { internalTask } from "@nomiclabs/buidler/config";
import { ensurePluginLoadedWithUsePlugin, BUIDLEREVM_NETWORK_NAME } from "@nomiclabs/buidler/plugins";
import {
ensurePluginLoadedWithUsePlugin,
BUIDLEREVM_NETWORK_NAME
} from "@nomiclabs/buidler/plugins";
import { wrapSend } from "@nomiclabs/buidler/internal/core/providers/wrapper";
import AsyncProvider from "./provider";

Expand Down Expand Up @@ -71,7 +74,6 @@ function getDefaultOptions(
return {
artifactType: artifactor.bind(null, config.paths.artifacts),
enabled: true,
fast: true,
url: <string>url,
metadata: {
compiler: {
Expand Down Expand Up @@ -104,14 +106,23 @@ function createGasMeasuringProvider(
provider: IEthereumProvider
){
return wrapSend(provider, async (method, params) => {
// Truffle
if (method === "eth_getTransactionReceipt") {
const receipt = await provider.send(method, params);

if (receipt.status && receipt.transactionHash){
const tx = await provider.send("eth_getTransactionByHash", [receipt.transactionHash]);
await mochaConfig.attachments.recordTransaction(receipt, tx);
}
return receipt;

// Ethers: will get run twice for deployments (e.g both receipt and txhash are fetched)
} else if (method === 'eth_getTransactionByHash'){
const receipt = await provider.send("eth_getTransactionReceipt", params)
const tx = await provider.send(method, params)
if (receipt.status){
await mochaConfig.attachments.recordTransaction(receipt, tx)
}
return tx;
}
return provider.send(method, params);
});
Expand All @@ -132,7 +143,7 @@ export default function() {
mochaConfig.reporter = "eth-gas-reporter";
mochaConfig.reporterOptions = options;

if (options.fast){
if (bre.network.name === BUIDLEREVM_NETWORK_NAME || options.fast){
bre.network.provider = createGasMeasuringProvider(bre.network.provider);
mochaConfig.reporterOptions.provider = new AsyncProvider(bre.network.provider);
mochaConfig.reporterOptions.blockLimit = (<any>bre.network.config).blockGasLimit as number;
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export interface EthGasReporterConfig {
metadata: any;
artifactType: any;
url: string;
fast: boolean;
fast?: boolean;
}
6 changes: 6 additions & 0 deletions test/buidler-truffle-project/buidler.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ module.exports = {
runs: 100
}
},
networks: {
development: {
gas: 5000000,
url: "http://localhost:8545"
}
},
gasReporter: {
onlyCalledMethods: false
}
Expand Down
12 changes: 12 additions & 0 deletions test/buidlerevm.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TASK_TEST } from "@nomiclabs/buidler/builtin-tasks/task-names";
// tslint:disable-next-line no-implicit-dependencies
import { assert } from "chai";
import { useEnvironment } from "./helpers";

describe("Ethers plugin", function() {
useEnvironment(__dirname + "/buidler-ethers-project", "localhost");

it("no options", async function() {
await this.env.run(TASK_TEST, { testFiles: [] });
});
});
14 changes: 14 additions & 0 deletions test/ganache.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { TASK_TEST } from "@nomiclabs/buidler/builtin-tasks/task-names";
// tslint:disable-next-line no-implicit-dependencies
import { assert } from "chai";

import { useEnvironment } from "./helpers";

describe("Truffle plugin", function() {
useEnvironment(__dirname + "/buidler-truffle-project", "development");

it("default", async function() {
this.env.config.gasReporter.onlyCalledMethods = true;
await this.env.run(TASK_TEST, { testFiles: [] });
});
});
7 changes: 6 additions & 1 deletion test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ declare module "mocha" {
}
}

export function useEnvironment(projectPath: string) {
export function useEnvironment(projectPath: string, networkName?: string) {
let previousCWD: string;

beforeEach("Loading buidler environment", function() {
previousCWD = process.cwd();
process.chdir(projectPath);

if (networkName !== undefined){
process.env.BUIDLER_NETWORK = networkName;
}

this.env = require("@nomiclabs/buidler");
});

Expand Down

0 comments on commit d779c7a

Please sign in to comment.