Skip to content

Commit d5e6143

Browse files
committed
Add server, config and unit tests
1 parent 9eb3b37 commit d5e6143

File tree

8 files changed

+122
-10
lines changed

8 files changed

+122
-10
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ gemspec
55

66
gem "rake", "~> 12.0"
77
gem "rspec", "~> 3.0"
8+
gem 'rspec-its'
9+
gem 'pry'

lib/rspec/eth.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
module Rspec
44
module Eth
5-
class Error < StandardError; end
6-
# Your code goes here...
5+
76
end
87
end

lib/rspec/eth/config.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'tempfile'
2+
3+
module Rspec
4+
module Eth
5+
class Config
6+
class << self
7+
attr_writer :account_keys_path, :port, :host
8+
9+
def account_keys_path
10+
@account_keys_path || accounts_tempfile.path
11+
end
12+
13+
def port
14+
@port || 8545
15+
end
16+
17+
def host
18+
@host || '127.0.0.1'
19+
end
20+
21+
private
22+
23+
def accounts_tempfile
24+
@accounts_tempfile ||= Tempfile.new('rspec-eth')
25+
end
26+
end
27+
end
28+
end
29+
end

lib/rspec/eth/server.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Rspec
2+
module Eth
3+
class Server
4+
class << self
5+
attr_accessor :ganache_pid
6+
7+
def start
8+
if !ganache_pid
9+
options = ["--quiet", "--account_keys_path #{Config.account_keys_path}", "-p #{Config.port}", "-h #{Config.host}"]
10+
self.ganache_pid = Process.spawn("ganache-cli #{options.join(" ")}", out: "/dev/null", err: STDOUT)
11+
else
12+
warn("Server already runnin")
13+
end
14+
end
15+
16+
def stop
17+
if ganache_pid
18+
Process.kill("TERM", ganache_pid)
19+
self.ganache_pid = nil
20+
else
21+
warn("Server isn't running")
22+
end
23+
end
24+
25+
def restart
26+
start
27+
stop
28+
end
29+
end
30+
end
31+
end
32+
end

rspec-eth.gemspec

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ Gem::Specification.new do |spec|
66
spec.authors = ["TheSmartnik"]
77
spec.email = ["[email protected]"]
88

9-
spec.summary = %q{TODO: Write a short summary, because RubyGems requires one.}
10-
spec.description = %q{TODO: Write a longer description or delete this line.}
11-
spec.homepage = "TODO: Put your gem's website or public repo URL here."
9+
spec.summary = %q{Write a short summary, because RubyGems requires one.}
10+
spec.description = %q{Write a longer description or delete this line.}
11+
# spec.homepage = "TODO: Put your gem's website or public repo URL here."
1212
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
1313

14-
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
15-
16-
spec.metadata["homepage_uri"] = spec.homepage
17-
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
18-
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
14+
# spec.metadata["homepage_uri"] = spec.homepage
15+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
16+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
1917

2018
# Specify which files should be added to the gem when it is released.
2119
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.

spec/rspec/unit/eth/config_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'spec_helper'
2+
require 'rspec/eth/config'
3+
4+
RSpec.describe Rspec::Eth::Config do
5+
subject(:config) { described_class }
6+
7+
describe 'default options' do
8+
its(:port) { is_expected.to eq(8545) }
9+
its(:host) { is_expected.to eq('127.0.0.1') }
10+
it { expect(File.exists?(config.account_keys_path)).to eq(true) }
11+
end
12+
13+
describe 'write' do
14+
{ port: 8000, host: '8.8.8.8', account_keys_path: '.something' }.each do |option, expectation|
15+
it "sets #{option} correctly" do
16+
config.public_send("#{option}=", expectation)
17+
expect(config.public_send(option)).to eq(expectation)
18+
end
19+
end
20+
end
21+
end

spec/rspec/unit/eth/server_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'spec_helper'
2+
require 'rspec/eth/config'
3+
require 'rspec/eth/server'
4+
require 'pry'
5+
6+
RSpec.describe Rspec::Eth::Server do
7+
subject(:server) { described_class }
8+
9+
describe 'general behaviour' do
10+
describe 'start' do
11+
after { server.stop }
12+
13+
it 'starts server' do
14+
expect { server.start }.to change { server.ganache_pid }.from(nil)
15+
16+
expect(`ps aux | grep #{server.ganache_pid}`).to include("ganache-cli --quiet")
17+
end
18+
end
19+
20+
it 'stops server' do
21+
server.start
22+
expect(`ps aux | grep #{server.ganache_pid}`).to include("ganache-cli --quiet")
23+
24+
pid = server.ganache_pid
25+
26+
expect { server.stop }.to change { server.ganache_pid }.to(nil)
27+
expect(`ps aux | grep #{pid}`).to_not include("ganache-cli --quiet")
28+
end
29+
end
30+
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "bundler/setup"
22
require "rspec/eth"
3+
require 'rspec/its'
34

45
RSpec.configure do |config|
56
# Enable flags like --only-failures and --next-failure

0 commit comments

Comments
 (0)