Skip to content

Commit

Permalink
Determine accounts and users dynamically in tests. Reformat.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyclemson committed Jan 20, 2021
1 parent dd86ccb commit 26f4367
Show file tree
Hide file tree
Showing 14 changed files with 9,967 additions and 6,915 deletions.
1 change: 0 additions & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Contributor Covenant Code of Conduct

## Our Pledge
Expand Down
59 changes: 26 additions & 33 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
require 'random-port'
require 'childprocess'
require_relative 'lib/ganache'

task :default => :"contracts:compile"

namespace :ganache do
task :start, [:port] do |_, args|
port = args.port.to_s
port = args.port

puts "Starting ganache on port #{port}..."
process = ChildProcess.build(
'./node_modules/.bin/ganache-cli',
'--allowUnlimitedContractSize',
'-p', port)
# process.io.inherit!
process.leader = true
process.detach = true
process.start

FileUtils.mkdir_p('run/pid')
File.open("run/pid/ganache-#{port}.pid", "w") do |pidfile|
pidfile.write(process.pid)
end
ganache = Ganache.builder
.on_port(port)
.allowing_unlimited_contract_size
.build
ganache.start
puts "Started ganache on port #{port}"
puts " - with pidfile at #{ganache.pidfile}"
puts " - with account keys file at #{ganache.account_keys_file}"
end

task :stop, [:port] do |_, args|
port = args.port.to_s
port = args.port

puts "Stopping ganache on port #{port}..."
pid = File.read("run/pid/ganache-#{port}.pid").to_i

Process.kill('INT', pid)
File.unlink("run/pid/ganache-#{port}.pid")
ganache = Ganache.builder
.on_port(port)
.build
ganache.stop
puts "Stopped ganache on port #{port}"
end
end

Expand All @@ -44,18 +39,16 @@ end
namespace :test do
desc "Run all contract integration tests"
task :integration do
RandomPort::Pool.new.acquire do |port|
begin
Rake::Task[:'ganache:start'].invoke(port)

puts "Running integration tests against node listening on #{port}..."
sh({
"HOST" => "127.0.0.1",
"PORT" => "#{port}"
}, 'npm run test:integration')
ensure
Rake::Task[:'ganache:stop'].invoke(port)
end
Ganache.on_available_port(
allow_unlimited_contract_size: true) do |ganache|
puts "Running integration tests against ganache node listening on " +
"#{ganache.port}..."

sh({
"HOST" => "127.0.0.1",
"PORT" => "#{ganache.port}",
"ACCOUNT_KEYS_FILE" => "#{ganache.account_keys_file}"
}, 'npm run test:integration')
end
end
end
131 changes: 131 additions & 0 deletions lib/ganache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
require 'childprocess'
require 'random-port'
require 'fileutils'

module Ganache
def self.builder
Builder.new
end

def self.on_available_port(options = {}, &block)
RandomPort::Pool.new.acquire do |port|
begin
instance = Builder.new(options.merge(port: port)).build
instance.start
block.call(instance)
ensure
instance.stop
end
end
end

class Instance
attr_reader(
:binary,
:port,
:account_keys_directory,
:pidfile_directory,
:allow_unlimited_contract_size)

def initialize(options)
@binary = options[:binary]
@port = options[:port]
@account_keys_directory = options[:account_keys_directory]
@pidfile_directory = options[:pidfile_directory]
@allow_unlimited_contract_size = options[:allow_unlimited_contract_size]

@started = false
end

def started?
@started
end

def pidfile
"#{@pidfile_directory}/ganache-#{@port}.pid"
end

def account_keys_file
"#{@account_keys_directory}/accounts-#{@port}.json"
end

def start
FileUtils.mkdir_p(@pidfile_directory)
FileUtils.mkdir_p(@account_keys_directory)

command = [@binary, '--port', @port.to_s]
if @allow_unlimited_contract_size
command = command.concat(['--allowUnlimitedContractSize'])
end
if @account_keys_directory
command = command.concat(['--acctKeys', account_keys_file])
end

process = ChildProcess.build(*command)
# process.io.inherit!
process.leader = true
process.detach = true
process.start

File.open(pidfile, "w") { |pidfile| pidfile.write(process.pid) }

@started = true
end

def stop
pid = File.read(pidfile).to_i

Process.kill('INT', pid)
File.unlink(pidfile)
File.unlink(account_keys_file)

@started = false
end
end

class Builder
def initialize(options = {})
@binary = options[:binary] || './node_modules/.bin/ganache-cli'
@port = options[:port] || 8545
@pidfile_directory = options[:pidfile_directory] || 'run/pid'
@account_keys_directory =
options[:account_keys_directory] || 'build/ganache'
@allow_unlimited_contract_size =
options[:allow_unlimited_contract_size] || false
end

def clone(options)
Builder.new(to_h.merge(options))
end

def on_port(port)
clone(port: port)
end

def allowing_unlimited_contract_size
clone(allow_unlimited_contract_size: true)
end

def saving_account_keys_to(directory)
clone(account_keys_directory: directory)
end

def saving_pidfile_to(directory)
clone(pidfile_directory: directory)
end

def build
Instance.new(to_h)
end

def to_h
{
binary: @binary,
port: @port,
pidfile_directory: @pidfile_directory,
account_keys_directory: @account_keys_directory,
allow_unlimited_contract_size: @allow_unlimited_contract_size
}
end
end
end
Loading

0 comments on commit 26f4367

Please sign in to comment.