rspec_fixtures 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +80 -1
- data/lib/rspec_fixtures.rb +6 -1
- data/lib/rspec_fixtures/approval_handler.rb +31 -0
- data/lib/rspec_fixtures/matchers/base.rb +55 -0
- data/lib/rspec_fixtures/matchers/match_fixture.rb +2 -61
- data/lib/rspec_fixtures/matchers/output_fixture.rb +39 -0
- data/lib/rspec_fixtures/stream_capturer.rb +33 -0
- data/lib/rspec_fixtures/version.rb +1 -1
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48ef174e72d1e605f279b0d5ae0eedbf5b596fef
|
4
|
+
data.tar.gz: 075e93191e3acbf083ddf892cb6a1a46caba54b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51fed96a9aca2abec950e09ddc47e6c9429f690ea4ab0f7e9864773aac82ed614b45bcd002842828fb6c31031efca6757cf151394c43e7202b80445d0731c15e
|
7
|
+
data.tar.gz: 9373abab432d93d9f8d823c7cbb77a21352ee94d5041e53e4fe23913bcc9fa4a84dbce932280a43cd2227a658637ebf3c1a082cd2c6cd9170043054d0f8266b2
|
data/README.md
CHANGED
@@ -3,9 +3,19 @@ RSpec Fixtures
|
|
3
3
|
|
4
4
|
[![Gem](https://img.shields.io/gem/v/rspec_fixtures.svg?style=flat-square)](https://rubygems.org/gems/rspec_fixtures)
|
5
5
|
[![Travis](https://img.shields.io/travis/DannyBen/rspec_fixtures.svg?style=flat-square)](https://travis-ci.org/DannyBen/rspec_fixtures)
|
6
|
-
[![
|
6
|
+
[![Maintainability](https://img.shields.io/codeclimate/maintainability/DannyBen/rspec_fixtures.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/rspec_fixtures)
|
7
|
+
[![Issues](https://img.shields.io/codeclimate/issues/github/DannyBen/rspec_fixtures.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/rspec_fixtures)
|
7
8
|
[![Gemnasium](https://img.shields.io/gemnasium/DannyBen/rspec_fixtures.svg?style=flat-square)](https://gemnasium.com/DannyBen/rspec_fixtures)
|
8
9
|
|
10
|
+
---
|
11
|
+
|
12
|
+
RSpec Fixtures allows you to interactively review and approve testable
|
13
|
+
content.
|
14
|
+
|
15
|
+
![Demo](/demo.gif)
|
16
|
+
|
17
|
+
---
|
18
|
+
|
9
19
|
|
10
20
|
Install
|
11
21
|
--------------------------------------------------
|
@@ -21,3 +31,72 @@ gem 'rspec_fixtures'
|
|
21
31
|
```
|
22
32
|
|
23
33
|
|
34
|
+
Usage
|
35
|
+
--------------------------------------------------
|
36
|
+
|
37
|
+
Require the gem in your spec helper:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
# spec/spec_helper.rb
|
41
|
+
require 'rspec_fixtures'
|
42
|
+
```
|
43
|
+
|
44
|
+
And use any of the matchers in your specs.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
describe 'ls' do
|
48
|
+
it "works" do
|
49
|
+
expect(`ls`).to match_fixture('ls_fixture')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
Matchers
|
55
|
+
--------------------------------------------------
|
56
|
+
|
57
|
+
### `match_fixture`
|
58
|
+
|
59
|
+
Compare a string with a pre-approved fixture.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
expect('some string').to match_fixture('fixture_filename')
|
63
|
+
```
|
64
|
+
|
65
|
+
### `output_fixture`
|
66
|
+
|
67
|
+
Compare an output (stdout or stderr) with a pre-approved fixture.
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
expect{ puts "hello" }.to output_fixture('fixture_filename')
|
71
|
+
expect{ puts "hello" }.to output_fixture('fixture_filename').to_stdout
|
72
|
+
expect{ $stderr.puts "hello" }.to output_fixture('fixture_filename').to_stderr
|
73
|
+
|
74
|
+
# The first two are the same, as the default stream is stdout.
|
75
|
+
```
|
76
|
+
|
77
|
+
|
78
|
+
Configuration
|
79
|
+
--------------------------------------------------
|
80
|
+
|
81
|
+
### `interactive_fixtures`
|
82
|
+
|
83
|
+
By default, interactive approvals are enabled in any environment that
|
84
|
+
does not define the `CI` environment variable. You can change this by
|
85
|
+
adding this to your `spec_helper`
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
RSpec.configure do |config|
|
89
|
+
config.interactive_fixtures = false # or any logic
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
### `fixtures_path`
|
94
|
+
|
95
|
+
By default, fixtures are stored in `spec/fixtures`. To change the path,
|
96
|
+
add this to your `spec_helper`.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
RSpec.configure do |config|
|
100
|
+
config.fixtures_path = 'spec/anywhere/else'
|
101
|
+
end
|
102
|
+
```
|
data/lib/rspec_fixtures.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
require 'rspec_fixtures/version'
|
2
2
|
require 'rspec_fixtures/colors'
|
3
|
+
|
4
|
+
require 'rspec_fixtures/stream_capturer'
|
5
|
+
require 'rspec_fixtures/approval_handler'
|
6
|
+
require 'rspec_fixtures/matchers/base'
|
3
7
|
require 'rspec_fixtures/matchers/match_fixture'
|
4
|
-
require 'rspec_fixtures/
|
8
|
+
require 'rspec_fixtures/matchers/output_fixture'
|
5
9
|
|
10
|
+
require 'rspec_fixtures/rspec_config'
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module RSpecFixtures
|
2
|
+
class ApprovalHandler
|
3
|
+
include Colors
|
4
|
+
|
5
|
+
def run(expected, actual, fixture_file)
|
6
|
+
puts "%{blue}--- Approval Needed ---%{reset}\n" % colors
|
7
|
+
if expected.empty?
|
8
|
+
puts actual
|
9
|
+
else
|
10
|
+
puts "--- New (Actual) ------%{reset}\n#{actual}" % colors
|
11
|
+
puts "--- Old (Fixture) -----%{reset}\n#{expected}" % colors
|
12
|
+
end
|
13
|
+
print "%{blue}--- Approve? (y/N): --> %{reset}" % colors
|
14
|
+
|
15
|
+
if get_user_answer == "y"
|
16
|
+
puts "%{green}Approved%{reset}" % colors
|
17
|
+
File.write fixture_file, actual
|
18
|
+
true
|
19
|
+
else
|
20
|
+
puts "%{red}Not Approved%{reset}" % colors
|
21
|
+
false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def get_user_answer
|
28
|
+
$stdin.getch
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module RSpecFixtures
|
2
|
+
module Matchers
|
3
|
+
|
4
|
+
class Base
|
5
|
+
attr_reader :fixture_name, :actual
|
6
|
+
|
7
|
+
def initialize(fixture_name=nil)
|
8
|
+
@fixture_name = fixture_name
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(actual)
|
12
|
+
@actual = actual
|
13
|
+
false
|
14
|
+
end
|
15
|
+
|
16
|
+
def expected
|
17
|
+
@expected ||= expected!
|
18
|
+
end
|
19
|
+
|
20
|
+
def failure_message
|
21
|
+
"expected #{actual}\nto match #{expected}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def diffable?
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
def interactive?
|
29
|
+
RSpec.configuration.interactive_fixtures
|
30
|
+
end
|
31
|
+
|
32
|
+
def fixtures_dir
|
33
|
+
RSpec.configuration.fixtures_path
|
34
|
+
end
|
35
|
+
|
36
|
+
def fixture_file
|
37
|
+
"#{fixtures_dir}/#{fixture_name}"
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def approve_fixture
|
43
|
+
approval_handler = ApprovalHandler.new
|
44
|
+
approval_handler.run expected, actual, fixture_file
|
45
|
+
end
|
46
|
+
|
47
|
+
def expected!
|
48
|
+
Dir.mkdir fixtures_dir unless Dir.exist? fixtures_dir
|
49
|
+
File.write fixture_file, nil unless File.exist? fixture_file
|
50
|
+
File.read fixture_file
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
@@ -4,15 +4,7 @@ module RSpecFixtures
|
|
4
4
|
MatchFixture.new expected
|
5
5
|
end
|
6
6
|
|
7
|
-
class MatchFixture
|
8
|
-
include Colors
|
9
|
-
|
10
|
-
attr_reader :fixture_name, :actual
|
11
|
-
|
12
|
-
def initialize(fixture_name=nil)
|
13
|
-
@fixture_name = fixture_name
|
14
|
-
end
|
15
|
-
|
7
|
+
class MatchFixture < Base
|
16
8
|
def matches?(actual)
|
17
9
|
@actual = actual
|
18
10
|
if actual == expected or !interactive?
|
@@ -21,58 +13,7 @@ module RSpecFixtures
|
|
21
13
|
approve_fixture
|
22
14
|
end
|
23
15
|
end
|
24
|
-
|
25
|
-
def expected
|
26
|
-
@expected ||= expected!
|
27
|
-
end
|
28
|
-
|
29
|
-
def failure_message
|
30
|
-
"expected #{actual}\nto match #{expected}"
|
31
|
-
end
|
32
|
-
|
33
|
-
def diffable?
|
34
|
-
true
|
35
|
-
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def interactive?
|
40
|
-
RSpec.configuration.interactive_fixtures
|
41
|
-
end
|
42
|
-
|
43
|
-
def fixtures_dir
|
44
|
-
RSpec.configuration.fixtures_path
|
45
|
-
end
|
46
|
-
|
47
|
-
def fixture_file
|
48
|
-
"#{fixtures_dir}/#{fixture_name}"
|
49
|
-
end
|
50
|
-
|
51
|
-
def approve_fixture
|
52
|
-
puts "%{blue}--- Approval Needed ---%{reset}\n" % colors
|
53
|
-
if expected.empty?
|
54
|
-
puts actual
|
55
|
-
else
|
56
|
-
puts "--- New (Actual) ------%{reset}\n#{actual}" % colors
|
57
|
-
puts "--- Old (Fixture) -----%{reset}\n#{expected}" % colors
|
58
|
-
end
|
59
|
-
print "%{blue}--- Approve? (y/N): --> %{reset}" % colors
|
60
|
-
|
61
|
-
if $stdin.getch == "y"
|
62
|
-
puts "%{green}Approved%{reset}" % colors
|
63
|
-
File.write fixture_file, actual
|
64
|
-
true
|
65
|
-
else
|
66
|
-
puts "%{red}Not Approved%{reset}" % colors
|
67
|
-
actual == expected
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def expected!
|
72
|
-
Dir.mkdir fixtures_dir unless Dir.exist? fixtures_dir
|
73
|
-
File.write fixture_file, nil unless File.exist? fixture_file
|
74
|
-
File.read fixture_file
|
75
|
-
end
|
76
16
|
end
|
17
|
+
|
77
18
|
end
|
78
19
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module RSpecFixtures
|
2
|
+
module Matchers
|
3
|
+
def output_fixture(expected)
|
4
|
+
OutputFixture.new expected
|
5
|
+
end
|
6
|
+
|
7
|
+
class OutputFixture < Base
|
8
|
+
def matches?(block)
|
9
|
+
return false unless block.is_a? Proc
|
10
|
+
@actual = stream_capturer.capture block
|
11
|
+
|
12
|
+
if actual == expected or !interactive?
|
13
|
+
actual == expected
|
14
|
+
else
|
15
|
+
approve_fixture
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def supports_block_expectations?
|
20
|
+
true
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_stdout
|
24
|
+
@stream_capturer = CaptureStdout
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_stderr
|
29
|
+
@stream_capturer = CaptureStderr
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
def stream_capturer
|
34
|
+
@stream_capturer ||= CaptureStdout
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module RSpecFixtures
|
2
|
+
# These methods are borrowed from rspec's built in matchers
|
3
|
+
# https://github.com/rspec/rspec-expectations/blob/add9b271ecb1d65f7da5bc8a9dd8c64d81d92303/lib/rspec/matchers/built_in/output.rb
|
4
|
+
module CaptureStdout
|
5
|
+
def self.capture(block)
|
6
|
+
captured_stream = StringIO.new
|
7
|
+
|
8
|
+
original_stream = $stdout
|
9
|
+
$stdout = captured_stream
|
10
|
+
|
11
|
+
block.call
|
12
|
+
|
13
|
+
captured_stream.string
|
14
|
+
ensure
|
15
|
+
$stdout = original_stream
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module CaptureStderr
|
20
|
+
def self.capture(block)
|
21
|
+
captured_stream = StringIO.new
|
22
|
+
|
23
|
+
original_stream = $stderr
|
24
|
+
$stderr = captured_stream
|
25
|
+
|
26
|
+
block.call
|
27
|
+
|
28
|
+
captured_stream.string
|
29
|
+
ensure
|
30
|
+
$stderr = original_stream
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec_fixtures
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
@@ -102,9 +102,13 @@ extra_rdoc_files: []
|
|
102
102
|
files:
|
103
103
|
- README.md
|
104
104
|
- lib/rspec_fixtures.rb
|
105
|
+
- lib/rspec_fixtures/approval_handler.rb
|
105
106
|
- lib/rspec_fixtures/colors.rb
|
107
|
+
- lib/rspec_fixtures/matchers/base.rb
|
106
108
|
- lib/rspec_fixtures/matchers/match_fixture.rb
|
109
|
+
- lib/rspec_fixtures/matchers/output_fixture.rb
|
107
110
|
- lib/rspec_fixtures/rspec_config.rb
|
111
|
+
- lib/rspec_fixtures/stream_capturer.rb
|
108
112
|
- lib/rspec_fixtures/version.rb
|
109
113
|
homepage: https://github.com/DannyBen/rspec_fixtures
|
110
114
|
licenses:
|