From 1e5e04003243df66a6cfc677729691719b66e869 Mon Sep 17 00:00:00 2001 From: Mykel Pereira Date: Tue, 20 Feb 2018 14:34:15 -0800 Subject: [PATCH 1/5] add portfinder --- package.json | 1 + yarn.lock | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) 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/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" From 2098421b35bd9072f7c614db1311be8492c57ca8 Mon Sep 17 00:00:00 2001 From: Mykel Pereira Date: Tue, 20 Feb 2018 14:34:36 -0800 Subject: [PATCH 2/5] Update to use portfinder for finding an available port --- src/server.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/server.js b/src/server.js index 52f37c3..440f3c8 100644 --- a/src/server.js +++ b/src/server.js @@ -1,6 +1,8 @@ import Ganache from "ganache-core"; import Web3 from "web3"; import { Profiler, Contracts, Migrate } from "./truffle/external"; +import portfinder from "portfinder"; +portfinder.basePort = 8545; export default class Server { constructor() { @@ -12,11 +14,14 @@ export default class Server { } async start() { - await this.ganache.listen(8545, (err, chain) => { - if (err) { - console.log("Error: ", err); - } - }); + const port = await portfinder.getPortPromise(); + + try { + await this.ganache.listen(port); + console.log("Launched test RPC on port: ", port); + } catch (err) { + console.log("Error: ", err); + } this.web3.setProvider(this.ganache.provider); this.accounts = await this.getAccounts(); } From 9bbdf7a5eaa5e66b68147bec364a6ed4e14069d5 Mon Sep 17 00:00:00 2001 From: Mykel Pereira Date: Tue, 20 Feb 2018 14:57:47 -0800 Subject: [PATCH 3/5] Update cli to enable an option to supply a port number --- src/cli.js | 6 ++++-- src/espresso.js | 3 ++- src/server.js | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) 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..e9cbab6 100644 --- a/src/espresso.js +++ b/src/espresso.js @@ -26,6 +26,7 @@ export default class Espresso { this.reporter = options.reporter; this.server = new Server(); this.mocha = new MochaParallel(); + this.port = options.port || 8545; //this.mocha = new Mocha(); } @@ -102,7 +103,7 @@ export default class Espresso { } async run() { - await this.server.start(); + await this.server.start(this.port); this.resolver = new Resolver(this.config); this.testSource = new TestSource(this.config); diff --git a/src/server.js b/src/server.js index 440f3c8..1e385d3 100644 --- a/src/server.js +++ b/src/server.js @@ -2,7 +2,6 @@ import Ganache from "ganache-core"; import Web3 from "web3"; import { Profiler, Contracts, Migrate } from "./truffle/external"; import portfinder from "portfinder"; -portfinder.basePort = 8545; export default class Server { constructor() { @@ -13,7 +12,8 @@ export default class Server { this.accounts = []; } - async start() { + async start(_port) { + portfinder.basePort = _port; const port = await portfinder.getPortPromise(); try { From ccd93183dd447f9ff6c8b498cd31c82081fd1c3f Mon Sep 17 00:00:00 2001 From: Mykel Pereira Date: Wed, 21 Feb 2018 09:33:56 -0800 Subject: [PATCH 4/5] Update port logic --- src/espresso.js | 5 +++-- src/server.js | 13 +++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/espresso.js b/src/espresso.js index e9cbab6..24076b0 100644 --- a/src/espresso.js +++ b/src/espresso.js @@ -24,9 +24,10 @@ 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.port = options.port || 8545; //this.mocha = new Mocha(); } diff --git a/src/server.js b/src/server.js index 1e385d3..92edc0e 100644 --- a/src/server.js +++ b/src/server.js @@ -4,21 +4,22 @@ 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 = []; + this.startingPort = options.port || 8545; + portfinder.basePort = this.startingPort; } - async start(_port) { - portfinder.basePort = _port; - const port = await portfinder.getPortPromise(); + async start() { + this.port = await portfinder.getPortPromise(); try { - await this.ganache.listen(port); - console.log("Launched test RPC on port: ", port); + await this.ganache.listen(this.port); + console.log("Launched test RPC on port: ", this.port); } catch (err) { console.log("Error: ", err); } From a00afb7181dd403cff9a7ea2ad8009f0a43503ef Mon Sep 17 00:00:00 2001 From: Mykel Pereira Date: Wed, 21 Feb 2018 11:11:33 -0800 Subject: [PATCH 5/5] Remove unnecessary code --- src/espresso.js | 2 +- src/server.js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/espresso.js b/src/espresso.js index 24076b0..64d86cd 100644 --- a/src/espresso.js +++ b/src/espresso.js @@ -104,7 +104,7 @@ export default class Espresso { } async run() { - await this.server.start(this.port); + await this.server.start(); this.resolver = new Resolver(this.config); this.testSource = new TestSource(this.config); diff --git a/src/server.js b/src/server.js index 92edc0e..d1187f5 100644 --- a/src/server.js +++ b/src/server.js @@ -10,8 +10,7 @@ export default class Server { }); this.web3 = new Web3(); this.accounts = []; - this.startingPort = options.port || 8545; - portfinder.basePort = this.startingPort; + portfinder.basePort = options.port || 8545; } async start() {