gnawrnip 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,6 +7,11 @@ Gnawrnip is a [TurnipFormatter](https://github.com/gongo/turnip_formatter) Add-o
7
7
  [![Code Climate](https://codeclimate.com/github/gongo/gnawrnip.png)](https://codeclimate.com/github/gongo/gnawrnip)
8
8
  [![Dependency Status](https://gemnasium.com/gongo/gnawrnip.png)](https://gemnasium.com/gongo/gnawrnip)
9
9
 
10
+ ## IMPORTANT!
11
+
12
+ This project is currently in development (frequent releases).
13
+ So it have potential for massive refactorings (that could be API breaking).
14
+
10
15
 
11
16
  ## Requirements
12
17
 
@@ -45,26 +50,25 @@ You can do to customize a screenshot.
45
50
 
46
51
  ```ruby
47
52
  Gnawrnip.configure do |c|
48
- c.photographer_driver = :js
49
- c.frame_interval = 1000 # milliseconds
50
- c.frame_size = [640, 360] # width, height
53
+ c.publisher_driver = :js
54
+ c.make_animation = true
55
+ c.frame_interval = 1000 # milliseconds
56
+ c.frame_size = [640, 360] # width, height
51
57
  end
52
-
53
- Gnawrnip.ready!
54
58
  ```
55
59
 
56
- `Gnawrnip.readty!` must be issued after `Gnawrnip.configure` in setup file.
57
-
58
- * `photographer_driver` (Symbol) A driver that make screenshot like animation GIF.
60
+ * `publisher_driver` (Symbol) A driver that make screenshot like animation GIF.
59
61
  * `:js`: use jQuery and image files. **The size of the report file tends to be large this driver**
60
62
  * `:rmagick`: Make pure animation GIF using [RMagick](http://rmagick.rubyforge.org/). This driver is requires Gem `rmagick`. Add this line to your application's Gemfile:
61
63
 
62
64
  ```
63
65
  gem 'rmagick'
64
66
  ```
67
+ * `make_animation` (Boolean) Whether to make animation GIF. (Default: true)
65
68
  * `frame_interval` (Integer) A time (millisecond) between each image in an animation. Default is `1000` (1sec)
69
+ * This option is enabled only when the `make_animation = true`.
66
70
  * `frame_size` (Array) A size of screenshot (width, height). Default is `nil` (`nil` means that use `Capybara.current_driver.browser` size) .
67
- * This option is enabled only when the `photographer_driver = :rmagick`.
71
+ * This option is enabled only when the `publisher_driver = :rmagick`.
68
72
 
69
73
  As example, see [example/spec/spec_helper.rb](https://github.com/gongo/gnawrnip/tree/master/example/spec/spec_helper.rb) .
70
74
 
@@ -21,10 +21,10 @@ Capybara.default_driver = :poltergeist
21
21
  Capybara.javascript_driver = :selenium
22
22
 
23
23
  Gnawrnip.configure do |c|
24
- c.photographer_driver = :js
25
- #c.photographer_driver = :rmagick
24
+ # c.publisher_driver = :js
25
+ c.publisher_driver = :rmagick
26
26
  c.frame_interval = 1000 # milliseconds
27
27
  c.frame_size = [640, 360] # width, height
28
+ c.make_animation = false
28
29
  end
29
30
 
30
- Gnawrnip.ready!
@@ -10,7 +10,7 @@ module Capybara
10
10
  alias_method "after_hook_#{method}".to_sym, method
11
11
 
12
12
  define_method method do |*args, &block|
13
- Gnawrnip::Animation.add_frame
13
+ Gnawrnip.photographer.take_shot
14
14
  send("after_hook_#{method}", *args, &block)
15
15
  end
16
16
  end
@@ -1,22 +1,25 @@
1
- require 'tempfile'
2
- require 'base64'
1
+ require 'gnawrnip/screenshot'
3
2
 
4
3
  module Gnawrnip
5
- module Photographer
6
- def animation(paths)
7
- raise NotImplementedError
4
+ class Photographer
5
+ def take_shot
6
+ frames << Screenshot.take
8
7
  end
9
8
 
10
- def single(path)
11
- image_tag(image_base64(path))
9
+ def reset!
10
+ frames.clear
12
11
  end
13
12
 
14
- def image_base64(path)
15
- Base64.strict_encode64(File.read(path))
13
+ #
14
+ # Close tempfiles.
15
+ #
16
+ def discard!
17
+ frames.each(&:close!)
18
+ reset!
16
19
  end
17
20
 
18
- def image_tag(data, format = :png)
19
- '<img src="data:image/' + format.to_s + ';base64,' + data + '"/>'
21
+ def frames
22
+ @frames ||= []
20
23
  end
21
24
  end
22
25
  end
@@ -1,12 +1,14 @@
1
- require 'gnawrnip/photographer'
1
+ require 'gnawrnip/publisher'
2
2
  require 'turnip_formatter/template'
3
3
 
4
- module Gnawrnip::JS
5
- class Photographer
6
- include Gnawrnip::Photographer
4
+ module Gnawrnip
5
+ module Publisher
6
+ class JS
7
+ include Gnawrnip::Publisher
7
8
 
8
- def animation(images)
9
- images.map { |img| single(img) }.join
9
+ def animation(images)
10
+ images.map { |img| single(img) }.join
11
+ end
10
12
  end
11
13
  end
12
14
  end
@@ -0,0 +1,47 @@
1
+ require 'gnawrnip/publisher'
2
+
3
+ module Gnawrnip
4
+ module Publisher
5
+ class RMagick
6
+ include Gnawrnip::Publisher
7
+
8
+ def initialize
9
+ Kernel.require 'RMagick'
10
+ rescue LoadError => e
11
+ if e.message =~ /cannot load.*RMagick/
12
+ raise LoadError, "Please install the gem and add `gem 'rmagick'` to your Gemfile if you are using bundler."
13
+ else
14
+ raise e
15
+ end
16
+ end
17
+
18
+ def animation(images)
19
+ creator = photo_creator(images)
20
+ tempfile = Tempfile.new(['gnawrnip', '.gif'])
21
+ creator.write(tempfile.path)
22
+
23
+ image_tag(image_base64(tempfile.path), :gif)
24
+ end
25
+
26
+ def single(image)
27
+ animation([image])
28
+ end
29
+
30
+ private
31
+
32
+ def photo_creator(images)
33
+ paths = images.map(&:path)
34
+ photos = ::Magick::ImageList.new(*paths)
35
+
36
+ photos.delay = Gnawrnip.frame_interval / 10.0
37
+ unless Gnawrnip.frame_size.nil?
38
+ photos.each do |p|
39
+ p.scale!(*Gnawrnip.frame_size)
40
+ end
41
+ end
42
+
43
+ photos
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,21 @@
1
+ require 'base64'
2
+
3
+ module Gnawrnip
4
+ module Publisher
5
+ def animation(paths)
6
+ raise NotImplementedError
7
+ end
8
+
9
+ def single(path)
10
+ image_tag(image_base64(path))
11
+ end
12
+
13
+ def image_base64(path)
14
+ Base64.strict_encode64(File.read(path))
15
+ end
16
+
17
+ def image_tag(data, format = :png)
18
+ '<img src="data:image/' + format.to_s + ';base64,' + data + '"/>'
19
+ end
20
+ end
21
+ end
@@ -1,16 +1,22 @@
1
1
  require 'rspec'
2
- require 'tempfile'
3
- require 'base64'
4
2
 
5
3
  RSpec.configure do |config|
4
+ config.before(:all) do
5
+ Gnawrnip.ready!
6
+ end
7
+
6
8
  config.before(:each, turnip: true) do
7
- Gnawrnip::Animation.reset!
9
+ Gnawrnip.photographer.reset!
10
+ example.metadata[:gnawrnip] = {}
8
11
  end
9
12
 
10
13
  config.after(:each, turnip: true) do
11
14
  if example.exception
12
- example.metadata[:gnawrnip] = {}
13
- example.metadata[:gnawrnip][:screenshot] = Gnawrnip::Animation.frames.compact
15
+ Gnawrnip.photographer.take_shot
16
+ screenshots = Gnawrnip.photographer.frames.compact
17
+ example.metadata[:gnawrnip][:screenshot] = screenshots
18
+ else
19
+ Gnawrnip.photographer.discard!
14
20
  end
15
21
  end
16
22
  end
@@ -21,13 +21,13 @@ module Gnawrnip
21
21
 
22
22
  def animation_image(paths)
23
23
  text = '<div class="screenshot animation">'
24
- text += Gnawrnip.photographer.animation(paths)
24
+ text += Gnawrnip.publisher.animation(paths)
25
25
  text + '</div>'
26
26
  end
27
27
 
28
28
  def single_image(path)
29
29
  text = '<div class="screenshot">'
30
- text += Gnawrnip.photographer.single(path)
30
+ text += Gnawrnip.publisher.single(path)
31
31
  text + '</div>'
32
32
  end
33
33
  end
@@ -1,3 +1,3 @@
1
1
  module Gnawrnip
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/gnawrnip.rb CHANGED
@@ -1,40 +1,48 @@
1
1
  require "gnawrnip/version"
2
- require 'gnawrnip/rmagick/photographer'
3
- require 'gnawrnip/ext/capybara/session'
4
- require 'gnawrnip/animation'
5
- require 'gnawrnip/screenshot'
6
- require 'gnawrnip/step_screenshot'
7
2
  require 'gnawrnip/rspec'
3
+ require 'gnawrnip/photographer'
4
+ require 'gnawrnip/step_screenshot'
8
5
 
9
6
  module Gnawrnip
10
7
  class << self
11
- attr_accessor :photographer_driver
8
+ attr_accessor :publisher_driver
12
9
  attr_accessor :frame_interval
13
10
  attr_accessor :frame_size
11
+ attr_accessor :make_animation
14
12
 
15
13
  def configure
16
14
  yield self
17
15
  end
18
16
 
19
17
  def ready!
20
- photographer # Try to load driver library.
18
+ require 'gnawrnip/ext/capybara/session' if animation?
19
+ publisher
20
+ end
21
+
22
+ def animation?
23
+ make_animation
21
24
  end
22
25
 
23
26
  def photographer
24
- @photographer ||= case photographer_driver
25
- when :rmagick
26
- require 'gnawrnip/rmagick/photographer'
27
- @photographer = RMagick::Photographer.new
28
- else # :js
29
- require 'gnawrnip/js/photographer'
30
- @photographer = JS::Photographer.new
31
- end
27
+ @photographer ||= Gnawrnip::Photographer.new
28
+ end
29
+
30
+ def publisher
31
+ @publisher ||= case publisher_driver
32
+ when :rmagick
33
+ require 'gnawrnip/publisher/rmagick'
34
+ @publisher = Publisher::RMagick.new
35
+ else # :js
36
+ require 'gnawrnip/publisher/js'
37
+ @publisher = Publisher::JS.new
38
+ end
32
39
  end
33
40
  end
34
41
  end
35
42
 
36
43
  Gnawrnip.configure do |c|
37
- c.photographer_driver = :js
38
- c.frame_interval = 1000
39
- c.frame_size = nil
44
+ c.publisher_driver = :js
45
+ c.frame_interval = 1000
46
+ c.frame_size = nil
47
+ c.make_animation = true
40
48
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+ require 'gnawrnip/photographer'
3
+
4
+ module Gnawrnip
5
+ describe Photographer do
6
+ let(:photographer) do
7
+ Photographer.new
8
+ end
9
+
10
+ before do
11
+ photographer.reset!
12
+ photographer.take_shot
13
+ photographer.take_shot
14
+ end
15
+
16
+ describe '.add_frame' do
17
+ subject { photographer.frames }
18
+ it { should have(2).elements }
19
+ end
20
+
21
+ describe '.reset!' do
22
+ before { photographer.reset! }
23
+ subject { photographer.frames }
24
+ it { should be_empty }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'gnawrnip/publisher/rmagick'
3
+ require 'RMagick'
4
+
5
+ module Gnawrnip
6
+ module Publisher
7
+ describe RMagick do
8
+ let(:photographer) { RMagick.new }
9
+
10
+ describe '.new' do
11
+ subject { lambda { photographer } }
12
+
13
+ context 'cannot load rmagick' do
14
+ before do
15
+ error = LoadError.new("LoadError: cannot load such file -- RMagick")
16
+ Kernel.stub(:require).and_raise(error)
17
+ end
18
+
19
+ it { should raise_error LoadError, /gem 'rmagick'/ }
20
+ end
21
+
22
+ context 'cannot load other library' do
23
+ before do
24
+ error = LoadError.new("LoadError: cannot load such file -- samurai")
25
+ Kernel.stub(:require).and_raise(error)
26
+ end
27
+
28
+ it { should raise_error LoadError, /such file -- samurai/ }
29
+ end
30
+ end
31
+
32
+ describe '#animation' do
33
+ let(:screenshot_list) {
34
+ [GnawrnipTest.image('hoge'), GnawrnipTest.image('fuga')]
35
+ }
36
+
37
+ let(:creator) {
38
+ d = double
39
+ d.stub(:write) { |path| File.write(path, d.data) }
40
+ d
41
+ }
42
+
43
+ context 'exists image files' do
44
+ before do
45
+ photographer.stub(:photo_creator) do |args|
46
+ creator.stub(:data).and_return(args.map(&:read).join)
47
+ end.and_return(creator)
48
+ end
49
+
50
+ subject { photographer.animation(screenshot_list) }
51
+ it { should eq '<img src="data:image/gif;base64,aG9nZWZ1Z2E="/>' }
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -16,7 +16,7 @@ module Gnawrnip
16
16
  end
17
17
 
18
18
  before do
19
- Gnawrnip::Animation.stub(:frames) { ['aiueo', nil, 'lllll'] }
19
+ Gnawrnip.photographer.stub(:frames) { ['aiueo', nil, 'lllll'] }
20
20
  end
21
21
 
22
22
  context '"turnip" spec group' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gnawrnip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-10 00:00:00.000000000 Z
12
+ date: 2013-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capybara
@@ -145,18 +145,18 @@ files:
145
145
  - example/web.rb
146
146
  - gnawrnip.gemspec
147
147
  - lib/gnawrnip.rb
148
- - lib/gnawrnip/animation.rb
149
148
  - lib/gnawrnip/ext/capybara/session.rb
150
- - lib/gnawrnip/js/photographer.rb
151
149
  - lib/gnawrnip/no_screenshot.png
152
150
  - lib/gnawrnip/photographer.rb
153
- - lib/gnawrnip/rmagick/photographer.rb
151
+ - lib/gnawrnip/publisher.rb
152
+ - lib/gnawrnip/publisher/js.rb
153
+ - lib/gnawrnip/publisher/rmagick.rb
154
154
  - lib/gnawrnip/rspec.rb
155
155
  - lib/gnawrnip/screenshot.rb
156
156
  - lib/gnawrnip/step_screenshot.rb
157
157
  - lib/gnawrnip/version.rb
158
- - spec/gnawrnip/animation_spec.rb
159
- - spec/gnawrnip/rmagick/photographer_spec.rb
158
+ - spec/gnawrnip/photographer_spec.rb
159
+ - spec/gnawrnip/publisher/rmagick.rb
160
160
  - spec/gnawrnip/rspec_spec.rb
161
161
  - spec/gnawrnip/screenshot_spec.rb
162
162
  - spec/gnawrnip/step_screenshot_spec.rb
@@ -174,12 +174,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
174
174
  - - ! '>='
175
175
  - !ruby/object:Gem::Version
176
176
  version: '0'
177
+ segments:
178
+ - 0
179
+ hash: 2586147401268872003
177
180
  required_rubygems_version: !ruby/object:Gem::Requirement
178
181
  none: false
179
182
  requirements:
180
183
  - - ! '>='
181
184
  - !ruby/object:Gem::Version
182
185
  version: '0'
186
+ segments:
187
+ - 0
188
+ hash: 2586147401268872003
183
189
  requirements: []
184
190
  rubyforge_project:
185
191
  rubygems_version: 1.8.23
@@ -187,8 +193,8 @@ signing_key:
187
193
  specification_version: 3
188
194
  summary: Add-on for TurnipFormatter with Capybara
189
195
  test_files:
190
- - spec/gnawrnip/animation_spec.rb
191
- - spec/gnawrnip/rmagick/photographer_spec.rb
196
+ - spec/gnawrnip/photographer_spec.rb
197
+ - spec/gnawrnip/publisher/rmagick.rb
192
198
  - spec/gnawrnip/rspec_spec.rb
193
199
  - spec/gnawrnip/screenshot_spec.rb
194
200
  - spec/gnawrnip/step_screenshot_spec.rb
@@ -1,21 +0,0 @@
1
- require 'gnawrnip/screenshot'
2
- require 'base64'
3
-
4
- module Gnawrnip
5
- class Animation
6
- class << self
7
- def reset!
8
- frames.clear
9
- end
10
-
11
- def frames
12
- @frames ||= []
13
- end
14
-
15
- def add_frame
16
- image = Screenshot.take
17
- frames << image
18
- end
19
- end
20
- end
21
- end
@@ -1,43 +0,0 @@
1
- require 'gnawrnip/photographer'
2
-
3
- module Gnawrnip::RMagick
4
- class Photographer
5
- include Gnawrnip::Photographer
6
-
7
- def initialize
8
- Kernel.require 'RMagick'
9
- rescue LoadError => e
10
- if e.message =~ /cannot load.*RMagick/
11
- raise LoadError, "Please install the gem and add `gem 'rmagick'` to your Gemfile if you are using bundler."
12
- else
13
- raise e
14
- end
15
- end
16
-
17
- def animation(images)
18
- creator = photo_creator(images)
19
- tempfile = Tempfile.new(['gnawrnip', '.gif'])
20
- creator.write(tempfile.path)
21
-
22
- image_tag(image_base64(tempfile.path), :gif)
23
- end
24
-
25
- def single(image)
26
- animation([image])
27
- end
28
-
29
- def photo_creator(images)
30
- paths = images.map(&:path)
31
- photos = ::Magick::ImageList.new(*paths)
32
-
33
- photos.delay = Gnawrnip.frame_interval / 10.0
34
- unless Gnawrnip.frame_size.nil?
35
- photos.each do |p|
36
- p.scale!(*Gnawrnip.frame_size)
37
- end
38
- end
39
-
40
- photos
41
- end
42
- end
43
- end
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
- require 'gnawrnip/animation'
3
-
4
- module Gnawrnip
5
- describe Animation do
6
- before do
7
- Animation.reset!
8
- Animation.add_frame
9
- Animation.add_frame
10
- end
11
-
12
- describe '.add_frame' do
13
- subject { Animation.frames }
14
- it { should have(2).elements }
15
- end
16
-
17
- describe '.reset!' do
18
- before { Animation.reset! }
19
- subject { Animation.frames }
20
- it { should be_empty }
21
- end
22
- end
23
- end
@@ -1,54 +0,0 @@
1
- require 'spec_helper'
2
- require 'gnawrnip/rmagick/photographer'
3
- require 'RMagick'
4
-
5
- module Gnawrnip::RMagick
6
- describe Photographer do
7
- let(:photographer) { Photographer.new }
8
-
9
- describe '.new' do
10
- subject { lambda { photographer } }
11
-
12
- context 'cannot load rmagick' do
13
- before do
14
- error = LoadError.new("LoadError: cannot load such file -- RMagick")
15
- Kernel.stub(:require).and_raise(error)
16
- end
17
-
18
- it { should raise_error LoadError, /gem 'rmagick'/ }
19
- end
20
-
21
- context 'cannot load other library' do
22
- before do
23
- error = LoadError.new("LoadError: cannot load such file -- samurai")
24
- Kernel.stub(:require).and_raise(error)
25
- end
26
-
27
- it { should raise_error LoadError, /such file -- samurai/ }
28
- end
29
- end
30
-
31
- describe '#animation' do
32
- let(:screenshot_list) {
33
- [GnawrnipTest.image('hoge'), GnawrnipTest.image('fuga')]
34
- }
35
-
36
- let(:creator) {
37
- d = double
38
- d.stub(:write) { |path| File.write(path, d.data) }
39
- d
40
- }
41
-
42
- context 'exists image files' do
43
- before do
44
- photographer.stub(:photo_creator) do |args|
45
- creator.stub(:data).and_return(args.map(&:read).join)
46
- end.and_return(creator)
47
- end
48
-
49
- subject { photographer.animation(screenshot_list) }
50
- it { should eq '<img src="data:image/gif;base64,aG9nZWZ1Z2E="/>' }
51
- end
52
- end
53
- end
54
- end