diff --git a/package.json b/package.json index 113dca5..4df0f0d 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "mocha": "^5.0.1", "mocha-better-spec-reporter": "^3.1.0", "mocha-parallel-tests": "^1.2.10", + "portfinder": "^1.0.13", "truffle": "^4.0.5", "truffle-artifactor": "^3.0.3", "truffle-compile": "^3.0.5", diff --git a/src/cli.js b/src/cli.js index d72156d..25bba99 100755 --- a/src/cli.js +++ b/src/cli.js @@ -12,6 +12,7 @@ program .option("-w, --watch", "Watch tests") .option("-v, --verbose", "Verbose tests") .option("-f, --fun", "Fun tests") + .option("-p, --port [port]", "Port number to launch test RPC on") .action(function(path) { testPath = path; }) @@ -29,8 +30,9 @@ if (program.watch) { const instance = new Espresso({ testPath, - watch: program.watch, - reporter + reporter, + port: program.port, + watch: program.watch }); global = Object.assign(global, instance.globalScope); diff --git a/src/espresso.js b/src/espresso.js index e51a173..64d86cd 100644 --- a/src/espresso.js +++ b/src/espresso.js @@ -24,7 +24,9 @@ export default class Espresso { this.testPath = options.testPath || "."; this.watch = options.watch; this.reporter = options.reporter; - this.server = new Server(); + this.server = new Server({ + port: options.port + }); this.mocha = new MochaParallel(); //this.mocha = new Mocha(); } diff --git a/src/server.js b/src/server.js index 52f37c3..d1187f5 100644 --- a/src/server.js +++ b/src/server.js @@ -1,22 +1,27 @@ import Ganache from "ganache-core"; import Web3 from "web3"; import { Profiler, Contracts, Migrate } from "./truffle/external"; +import portfinder from "portfinder"; export default class Server { - constructor() { + constructor(options = {}) { this.ganache = Ganache.server({ default_balance_ether: 1000 }); this.web3 = new Web3(); this.accounts = []; + portfinder.basePort = options.port || 8545; } async start() { - await this.ganache.listen(8545, (err, chain) => { - if (err) { - console.log("Error: ", err); - } - }); + this.port = await portfinder.getPortPromise(); + + try { + await this.ganache.listen(this.port); + console.log("Launched test RPC on port: ", this.port); + } catch (err) { + console.log("Error: ", err); + } this.web3.setProvider(this.ganache.provider); this.accounts = await this.getAccounts(); } diff --git a/yarn.lock b/yarn.lock index 2971da0..9a2d9cb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3396,7 +3396,7 @@ mississippi@^2.0.0: stream-each "^1.1.0" through2 "^2.0.0" -mkdirp@0.5.1, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@~0.5.1: +mkdirp@0.5.1, mkdirp@0.5.x, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@~0.5.1: version "0.5.1" resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" dependencies: @@ -4093,6 +4093,14 @@ pinkie@^2.0.0: version "2.0.4" resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870" +portfinder@^1.0.13: + version "1.0.13" + resolved "https://registry.yarnpkg.com/portfinder/-/portfinder-1.0.13.tgz#bb32ecd87c27104ae6ee44b5a3ccbf0ebb1aede9" + dependencies: + async "^1.5.2" + debug "^2.2.0" + mkdirp "0.5.x" + prepend-file@^1.3.1: version "1.3.1" resolved "https://registry.yarnpkg.com/prepend-file/-/prepend-file-1.3.1.tgz#83b16e0b4ac1901fce88dbd945a22f4cc81df579"