ruby-recorder 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.md +112 -0
- data/Rakefile +2 -0
- data/lib/recorder.rb +76 -0
- data/lib/recorder/version.rb +3 -0
- data/recorder.gemspec +20 -0
- data/tests/recorder_tests.rb +119 -0
- metadata +88 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use ruby-1.9.3@recorder --create
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Michael Bensoussan
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# Ruby Recorder
|
2
|
+
|
3
|
+
Recorder dumps the result of your ruby code to a YAML file for faster tests or to compare the result between two execution.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ruby-recorder'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ruby-recorder
|
18
|
+
|
19
|
+
## Exemples
|
20
|
+
|
21
|
+
### Refactor
|
22
|
+
|
23
|
+
Imagine you want to refactor an endpoint on your backend api and want to make sure the response is the same:
|
24
|
+
|
25
|
+
```
|
26
|
+
Recorder.config do |c|
|
27
|
+
c.verbose = true
|
28
|
+
c.stubb = false
|
29
|
+
end
|
30
|
+
|
31
|
+
# this will call your backend and
|
32
|
+
# dump the result in parsing.yml
|
33
|
+
# let's say the response is : [{"username"=>"Michael Bensoussan"}]
|
34
|
+
Recorder.dump_to('refactor.yml') do
|
35
|
+
HTTParty.get('http://my_backend/users').parsed_response
|
36
|
+
end
|
37
|
+
|
38
|
+
# this will call your backend and compare the result to
|
39
|
+
# what's in refactor.yml ([{"username"=>"Michael Bensoussan"}])
|
40
|
+
# let's say the new response is [{"username"=>"Michael"}]
|
41
|
+
Recorder.dump_to('refactor.yml') do
|
42
|
+
HTTParty.get('http://my_backend/users').parsed_response
|
43
|
+
end
|
44
|
+
|
45
|
+
```
|
46
|
+
|
47
|
+
Executing this code will outputs a (colored) diff :
|
48
|
+
|
49
|
+
```
|
50
|
+
Recorder: result is different from last run
|
51
|
+
---
|
52
|
+
- username: Michael Bensoussan- username: Michael
|
53
|
+
```
|
54
|
+
|
55
|
+
### Faster tests
|
56
|
+
|
57
|
+
The first time you run this test, you will call the backend but for all the following runs `Recorder` will load the result from the YAML file.
|
58
|
+
|
59
|
+
```
|
60
|
+
class TestRecorder < MiniTest::Unit::TestCase
|
61
|
+
|
62
|
+
def setup
|
63
|
+
Recorder.config do |c|
|
64
|
+
c.verbose = true
|
65
|
+
c.stubb = true
|
66
|
+
c.records_dir = 'backend_responses/'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_users
|
71
|
+
users = Recorder.dump_to('refactor.yml') do
|
72
|
+
HTTParty.get('http://my_backend/users').parsed_response
|
73
|
+
end
|
74
|
+
|
75
|
+
assert_equal [{"username"=>"Michael Bensoussan"}], users
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
## Usage
|
82
|
+
|
83
|
+
```
|
84
|
+
Recorder.config do |c|
|
85
|
+
c.verbose = true
|
86
|
+
c.records_dir = 'records/'
|
87
|
+
c.stubb = false
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
### records_dir
|
92
|
+
|
93
|
+
The `records_dir` option sets the directory where your records will be saved.
|
94
|
+
|
95
|
+
### stubb
|
96
|
+
|
97
|
+
When `stubb = true`, your code will only be run once. Future runs will load the result from the yaml file.
|
98
|
+
When `stubb = false`, your code will be run every time.
|
99
|
+
|
100
|
+
### verbose
|
101
|
+
|
102
|
+
The `verbose` option will outputs a nice diff (with the `differ` gem) if the output is different from the last run.
|
103
|
+
Of course this only works if `stubb = false`.
|
104
|
+
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
1. Fork it
|
109
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
110
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
111
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
112
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/recorder.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require "recorder/version"
|
2
|
+
require 'yaml'
|
3
|
+
require 'differ'
|
4
|
+
require 'colored'
|
5
|
+
|
6
|
+
module Recorder
|
7
|
+
Config = Struct.new(:records_dir, :verbose, :stubb)
|
8
|
+
@@config = Config.new
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_reader :config
|
12
|
+
|
13
|
+
# Configure Recorder
|
14
|
+
#
|
15
|
+
# ==== Examples
|
16
|
+
# Recorder.config do |c|
|
17
|
+
# c.verbose = true
|
18
|
+
# c.stubb = true
|
19
|
+
# c.records_dir = 'records/'
|
20
|
+
# end
|
21
|
+
def config(&block)
|
22
|
+
if block_given?
|
23
|
+
@@config = Config.new
|
24
|
+
@@config.tap(&block)
|
25
|
+
else
|
26
|
+
@@config
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def dump_to file
|
31
|
+
if @@config.records_dir
|
32
|
+
Dir.mkdir @@config.records_dir unless File.directory? @@config.records_dir
|
33
|
+
file = File.join(@@config.records_dir, file)
|
34
|
+
end
|
35
|
+
|
36
|
+
if !@@config.stubb
|
37
|
+
result = yield
|
38
|
+
yaml_result = result.to_yaml
|
39
|
+
if File.exist? file
|
40
|
+
old_result = YAML.load_file(file)
|
41
|
+
if old_result != result
|
42
|
+
outputs "Recorder: result is different from last run".red
|
43
|
+
Differ.format = :color
|
44
|
+
outputs Differ.diff_by_line(yaml_result.to_s, old_result.to_s)
|
45
|
+
else
|
46
|
+
outputs "Recorder: result is the same".green
|
47
|
+
end
|
48
|
+
else
|
49
|
+
File.open(file, 'w' ) do |out|
|
50
|
+
YAML.dump(result, out)
|
51
|
+
end
|
52
|
+
outputs "Recorder: recorded in #{file}".green
|
53
|
+
end
|
54
|
+
return result
|
55
|
+
else
|
56
|
+
if File.exist? file
|
57
|
+
outputs "Recorder: loaded from #{file}".green
|
58
|
+
return YAML.load_file(file)
|
59
|
+
else
|
60
|
+
result = yield
|
61
|
+
yaml_result = result.to_yaml
|
62
|
+
File.open(file, 'w' ) do |out|
|
63
|
+
YAML.dump(result, out)
|
64
|
+
end
|
65
|
+
outputs "Recorder: recorded in #{file}".green
|
66
|
+
return result
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
def outputs msg
|
73
|
+
puts msg if @@config.verbose
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/recorder.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/recorder/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Michael Bensoussan"]
|
6
|
+
gem.email = ["mbensoussan.is@gmail.com"]
|
7
|
+
gem.description = %q{Ruby Recorder dumps the result of your ruby code to a YAML file for faster tests or to compare the result between two execution.}
|
8
|
+
gem.summary = %q{Dump your ruby to YAML for refactoring and faster tests}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "ruby-recorder"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Recorder::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency('differ')
|
19
|
+
gem.add_dependency('colored')
|
20
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require 'recorder'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
gem 'minitest'
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'minitest/unit'
|
7
|
+
|
8
|
+
class TestRecorder < MiniTest::Unit::TestCase
|
9
|
+
def test_no_verbose
|
10
|
+
Recorder.config do |c|
|
11
|
+
c.verbose = false
|
12
|
+
end
|
13
|
+
|
14
|
+
assert_silent do
|
15
|
+
Recorder.dump_to('test_verbose.yml') do
|
16
|
+
[1, 2, 3]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_verbose
|
22
|
+
Recorder.config do |c|
|
23
|
+
c.verbose = false
|
24
|
+
end
|
25
|
+
|
26
|
+
assert_output do
|
27
|
+
Recorder.dump_to('test_verbose.yml') do
|
28
|
+
[1, 2, 3]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_directory
|
34
|
+
assert_equal false, File.directory?('records')
|
35
|
+
|
36
|
+
Recorder.config do |c|
|
37
|
+
c.records_dir = 'records/'
|
38
|
+
end
|
39
|
+
|
40
|
+
Recorder.dump_to('test_directory.yml') do
|
41
|
+
[1, 2, 3]
|
42
|
+
end
|
43
|
+
|
44
|
+
assert_equal true, File.exist?("records/test_directory.yml")
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_no_stubb
|
48
|
+
Recorder.config do |c|
|
49
|
+
c.stubb = false
|
50
|
+
end
|
51
|
+
|
52
|
+
assert_output "TOTO\n" do
|
53
|
+
Recorder.dump_to('test_no_stubb.yml') do
|
54
|
+
-> {puts "TOTO"}.call
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
assert_output "TOTO\n" do
|
59
|
+
Recorder.dump_to('test_no_stubb.yml') do
|
60
|
+
-> {puts "TOTO"}.call
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
first_run = Recorder.dump_to('refactor.yml') do
|
65
|
+
[{"username"=>"Michael Bensoussan"}]
|
66
|
+
end
|
67
|
+
|
68
|
+
second_run = Recorder.dump_to('refactor.yml') do
|
69
|
+
[{"username"=>"Michael"}]
|
70
|
+
end
|
71
|
+
|
72
|
+
refute_equal first_run, second_run
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_stubb
|
76
|
+
Recorder.config do |c|
|
77
|
+
c.stubb = true
|
78
|
+
end
|
79
|
+
|
80
|
+
assert_output "TOTO\n" do
|
81
|
+
Recorder.dump_to('test_stubb.yml') do
|
82
|
+
-> {puts "TOTO"}.call
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# this won't be call
|
87
|
+
assert_silent do
|
88
|
+
Recorder.dump_to('test_stubb.yml') do
|
89
|
+
-> {puts "TOTO"}.call
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
first_run = Recorder.dump_to('refactor.yml') do
|
94
|
+
[{"username"=>"Michael Bensoussan"}]
|
95
|
+
end
|
96
|
+
|
97
|
+
second_run = Recorder.dump_to('refactor.yml') do
|
98
|
+
[{"username"=>"Michael"}]
|
99
|
+
end
|
100
|
+
|
101
|
+
assert_equal first_run, second_run
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
MiniTest::Unit.after_tests do
|
106
|
+
files_to_clean = [
|
107
|
+
"test_verbose.yml",
|
108
|
+
"records/test_directory.yml",
|
109
|
+
"test_no_stubb.yml",
|
110
|
+
"test_stubb.yml",
|
111
|
+
"refactor.yml"
|
112
|
+
]
|
113
|
+
files_to_clean.each do |file|
|
114
|
+
File.delete(File::expand_path("../../#{file}", __FILE__))
|
115
|
+
end
|
116
|
+
|
117
|
+
records_dir = File::expand_path("../../records", __FILE__)
|
118
|
+
Dir.delete(records_dir) if File.directory?(records_dir)
|
119
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-recorder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Bensoussan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: differ
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: colored
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Ruby Recorder dumps the result of your ruby code to a YAML file for faster
|
47
|
+
tests or to compare the result between two execution.
|
48
|
+
email:
|
49
|
+
- mbensoussan.is@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .rvmrc
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- lib/recorder.rb
|
61
|
+
- lib/recorder/version.rb
|
62
|
+
- recorder.gemspec
|
63
|
+
- tests/recorder_tests.rb
|
64
|
+
homepage: ''
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.24
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Dump your ruby to YAML for refactoring and faster tests
|
88
|
+
test_files: []
|