rspec-eth 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +9 -0
- data/LICENSE +21 -0
- data/README.md +93 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rspec/eth/config.rb +33 -0
- data/lib/rspec/eth/helper_methods.rb +33 -0
- data/lib/rspec/eth/rspec.rb +19 -0
- data/lib/rspec/eth/server.rb +37 -0
- data/lib/rspec/eth/version.rb +5 -0
- data/lib/rspec/eth.rb +12 -0
- data/rspec-eth.gemspec +28 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c0460e41d2af3a1fed048da7ed41ec2428ec61ad73a01b3429d4176c93544c3f
|
4
|
+
data.tar.gz: f9e19174ef7b335be205723ab619f036e4fced546cb4d723e6bcd094a7fe5201
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ccfcb90c1699125ea9085f4b2fe816c56b66090ed361a977939cad339ec6d33a1f47f6e69bb731e3e6d3f95d8a845862169663fe4f166e78c1cf5ffb0749eea2
|
7
|
+
data.tar.gz: 85ab20288129b0fcde4ec8f8995fe89b84aeabcf6bf2c7cd5f117aee21e95f5040096e6a83d2d41fec76a3512729175b1371016f0bb41dfb9802c262aa300876
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2022 Nikita Misharin
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# RSpec::Eth
|
2
|
+
|
3
|
+
RSpec extension that allows to easily test solidity smart contracts.
|
4
|
+
|
5
|
+
## What it does
|
6
|
+
|
7
|
+
* Spins up ganache server for tests
|
8
|
+
* Adds a few handy methods to ease testing of solidity contracts in ruby main ones:
|
9
|
+
1. `contract` to access contract
|
10
|
+
2. `accounts` to access addresses used by ganache
|
11
|
+
|
12
|
+
It's build on top of [etherium.rb](https://github.com/EthWorks/ethereum.rb). For documentation on how to interact with etherium blockchain please refer to its documentation
|
13
|
+
|
14
|
+
## Example Usage
|
15
|
+
Given a simple [Greeter contract](https://github.com/TheSmartnik/rspec-eth/blob/master/contracts/simple_greeter.sol). Here is an example of basic spec
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
RSpec.describe 'SimpleGreeter', type: :smart_contract do
|
19
|
+
before { contract.deploy_and_wait }
|
20
|
+
|
21
|
+
it 'sets greeting' do
|
22
|
+
expect(contract.call.greet).to eq("Hello, World!")
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'changes message' do
|
26
|
+
contract.transact_and_wait.set_super_greeting("Yo")
|
27
|
+
|
28
|
+
expect(contract.call.greet).to eq("Yo")
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when sender not owner' do
|
32
|
+
before { contract.sender = accounts[1] }
|
33
|
+
|
34
|
+
it 'trying to set not from owner' do
|
35
|
+
expect {
|
36
|
+
contract.transact_and_wait.set_super_greeting("Yo")
|
37
|
+
}.to raise_exception(IOError, "VM Exception while processing transaction: revert Only owner")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
## Installation
|
44
|
+
|
45
|
+
### Prerequisite
|
46
|
+
|
47
|
+
1. Install ganache-cli
|
48
|
+
```
|
49
|
+
npm install -g ganache-cli
|
50
|
+
```
|
51
|
+
2. [Install solidity compilier](https://docs.soliditylang.org/en/v0.8.9/installing-solidity.html)
|
52
|
+
```
|
53
|
+
brew install solidity
|
54
|
+
```
|
55
|
+
|
56
|
+
3. Add gem to a Gemfile
|
57
|
+
Add this line to your application's Gemfile:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
gem 'rspec-eth'
|
61
|
+
```
|
62
|
+
|
63
|
+
4. Require extension in `spec_helper.rb` or `rails_helper.rb`
|
64
|
+
```
|
65
|
+
require 'rspec/eth'
|
66
|
+
```
|
67
|
+
|
68
|
+
## Configuration
|
69
|
+
|
70
|
+
`RSpec::Eth` provides a few configuration option that you probably won't need
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
# spec_helper.rb
|
74
|
+
|
75
|
+
RSpec::Eth.configure do |config|
|
76
|
+
config.account_keys_path = temp_path # Path for accounts created
|
77
|
+
config.host = '127.0.0.1' # Host of ganache server
|
78
|
+
config.port = '8545' # Port of ganache server
|
79
|
+
config.contracts_path = 'contracts'# Set paths for your contracts
|
80
|
+
end
|
81
|
+
|
82
|
+
```
|
83
|
+
|
84
|
+
|
85
|
+
## Development
|
86
|
+
|
87
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rspec` to run the tests.
|
88
|
+
|
89
|
+
|
90
|
+
## Contributing
|
91
|
+
|
92
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rspec-eth.
|
93
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rspec/eth"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Eth
|
5
|
+
class Config
|
6
|
+
class << self
|
7
|
+
attr_writer :account_keys_path, :port, :host, :contracts_path
|
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
|
+
def contracts_path
|
22
|
+
@contracts_path || 'contracts'
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def accounts_tempfile
|
28
|
+
@accounts_tempfile ||= Tempfile.new('rspec-eth')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
module Eth
|
5
|
+
module HelperMethods
|
6
|
+
def client
|
7
|
+
@client ||= begin
|
8
|
+
Ethereum::HttpClient.new("http://#{RSpec::Eth::Config.host}:#{RSpec::Eth::Config.port}")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def contract
|
13
|
+
@contract ||= begin
|
14
|
+
Ethereum::Contract.create(file: contract_path, client: client)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def accounts
|
19
|
+
@accounts ||= begin
|
20
|
+
accounts_data = File.read(RSpec::Eth::Config.account_keys_path)
|
21
|
+
JSON.parse(accounts_data)['addresses'].keys
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def contract_path
|
26
|
+
@contract_path ||= begin
|
27
|
+
filename = self.class.metadata[:file_path].split('/').last.gsub('_spec', '').gsub('.rb', '')
|
28
|
+
File.join(RSpec::Eth::Config.contracts_path, "#{filename}.sol")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rspec/core'
|
2
|
+
require 'rspec/eth/helper_methods'
|
3
|
+
require 'rspec/eth/server'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.include RSpec::Eth::HelperMethods, type: :smart_contract
|
7
|
+
|
8
|
+
config.after(:all) do |_example|
|
9
|
+
if self.class.include?(RSpec::Eth::HelperMethods)
|
10
|
+
RSpec::Eth::Server.stop
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
config.before(:all) do |_example|
|
15
|
+
if self.class.include?(RSpec::Eth::HelperMethods)
|
16
|
+
RSpec::Eth::Server.start
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Eth
|
3
|
+
class Server
|
4
|
+
class << self
|
5
|
+
attr_accessor :ganache_pid, :ganache_read_output
|
6
|
+
|
7
|
+
def start(wait_until_ready: true)
|
8
|
+
if !ganache_pid
|
9
|
+
self.ganache_read_output, ganache_write_output = IO.pipe
|
10
|
+
options = ["--quiet", "--account_keys_path #{Config.account_keys_path}", "-p #{Config.port}", "-h #{Config.host}"]
|
11
|
+
self.ganache_pid = Process.spawn("ganache-cli #{options.join(" ")}", out: ganache_write_output, err: STDOUT)
|
12
|
+
|
13
|
+
# Enough to understand that that server is ready
|
14
|
+
ganache_read_output.read(60) if wait_until_ready
|
15
|
+
else
|
16
|
+
warn("Server already runnin")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def stop
|
21
|
+
if ganache_pid
|
22
|
+
Process.kill("TERM", ganache_pid)
|
23
|
+
self.ganache_pid = nil
|
24
|
+
self.ganache_read_output = nil
|
25
|
+
else
|
26
|
+
warn("Server isn't running")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def restart
|
31
|
+
start
|
32
|
+
stop
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/rspec/eth.rb
ADDED
data/rspec-eth.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'lib/rspec/eth/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "rspec-eth"
|
5
|
+
spec.version = RSpec::Eth::VERSION
|
6
|
+
spec.authors = ["TheSmartnik"]
|
7
|
+
spec.email = ["misharinn@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{RSpec extension that allows to easily test solidity smart contracts}
|
10
|
+
spec.description = %q{RSpec extension that spins up ganache server for tests and adds a few handy methods. Expected to be used with ethereum.rb}
|
11
|
+
spec.homepage = "https://github.com/TheSmartnik/rspec-eth"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
13
|
+
spec.licenses = ['MIT']
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = "https://github.com/TheSmartnik/rspec-eth"
|
16
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|contracts)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_dependency 'ethereum.rb'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-eth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TheSmartnik
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-03-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ethereum.rb
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: RSpec extension that spins up ganache server for tests and adds a few
|
28
|
+
handy methods. Expected to be used with ethereum.rb
|
29
|
+
email:
|
30
|
+
- misharinn@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- ".rspec"
|
37
|
+
- ".travis.yml"
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- bin/console
|
43
|
+
- bin/setup
|
44
|
+
- lib/rspec/eth.rb
|
45
|
+
- lib/rspec/eth/config.rb
|
46
|
+
- lib/rspec/eth/helper_methods.rb
|
47
|
+
- lib/rspec/eth/rspec.rb
|
48
|
+
- lib/rspec/eth/server.rb
|
49
|
+
- lib/rspec/eth/version.rb
|
50
|
+
- rspec-eth.gemspec
|
51
|
+
homepage: https://github.com/TheSmartnik/rspec-eth
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
metadata:
|
55
|
+
homepage_uri: https://github.com/TheSmartnik/rspec-eth
|
56
|
+
source_code_uri: https://github.com/TheSmartnik/rspec-eth
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 2.3.0
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubygems_version: 3.1.6
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: RSpec extension that allows to easily test solidity smart contracts
|
76
|
+
test_files: []
|