wicked_pdf 1.0.6 → 2.6.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +5 -5
  2. data/.github/issue_template.md +15 -0
  3. data/.github/workflows/ci.yml +56 -0
  4. data/.rubocop.yml +60 -0
  5. data/.rubocop_todo.yml +83 -29
  6. data/CHANGELOG.md +182 -35
  7. data/README.md +188 -30
  8. data/Rakefile +13 -10
  9. data/gemfiles/5.0.gemfile +8 -0
  10. data/gemfiles/5.1.gemfile +8 -0
  11. data/gemfiles/5.2.gemfile +9 -0
  12. data/gemfiles/6.0.gemfile +10 -0
  13. data/gemfiles/6.1.gemfile +12 -0
  14. data/gemfiles/7.0.gemfile +12 -0
  15. data/generators/wicked_pdf/templates/wicked_pdf.rb +9 -0
  16. data/lib/generators/wicked_pdf_generator.rb +5 -9
  17. data/lib/wicked_pdf/binary.rb +65 -0
  18. data/lib/wicked_pdf/middleware.rb +1 -1
  19. data/lib/wicked_pdf/option_parser.rb +229 -0
  20. data/lib/wicked_pdf/pdf_helper.rb +99 -85
  21. data/lib/wicked_pdf/progress.rb +33 -0
  22. data/lib/wicked_pdf/railtie.rb +6 -33
  23. data/lib/wicked_pdf/tempfile.rb +38 -7
  24. data/lib/wicked_pdf/version.rb +1 -1
  25. data/lib/wicked_pdf/wicked_pdf_helper/assets.rb +213 -91
  26. data/lib/wicked_pdf/wicked_pdf_helper.rb +29 -26
  27. data/lib/wicked_pdf.rb +37 -272
  28. data/test/fixtures/database.yml +4 -0
  29. data/test/fixtures/manifest.js +3 -0
  30. data/test/fixtures/wicked.js +1 -0
  31. data/test/functional/pdf_helper_test.rb +74 -5
  32. data/test/functional/wicked_pdf_helper_assets_test.rb +27 -9
  33. data/test/functional/wicked_pdf_helper_test.rb +15 -13
  34. data/test/test_helper.rb +16 -7
  35. data/test/unit/wicked_pdf_binary_test.rb +26 -0
  36. data/test/unit/wicked_pdf_option_parser_test.rb +128 -0
  37. data/test/unit/wicked_pdf_test.rb +11 -149
  38. data/test/unit/wkhtmltopdf_location_test.rb +48 -0
  39. data/wicked_pdf.gemspec +21 -14
  40. metadata +68 -37
  41. data/.travis.yml +0 -66
  42. data/gemfiles/2.3.gemfile +0 -10
  43. data/gemfiles/3.0.gemfile +0 -7
  44. data/gemfiles/3.1.gemfile +0 -8
  45. data/gemfiles/3.2.gemfile +0 -7
  46. data/gemfiles/4.0.gemfile +0 -6
  47. data/gemfiles/4.1.gemfile +0 -6
  48. data/gemfiles/4.2.gemfile +0 -6
  49. data/gemfiles/rails_edge.gemfile +0 -6
data/lib/wicked_pdf.rb CHANGED
@@ -1,48 +1,35 @@
1
1
  # wkhtml2pdf Ruby interface
2
- # http://code.google.com/p/wkhtmltopdf/
2
+ # http://wkhtmltopdf.org/
3
3
 
4
4
  require 'logger'
5
5
  require 'digest/md5'
6
6
  require 'rbconfig'
7
+ require 'open3'
7
8
 
8
- if (RbConfig::CONFIG['target_os'] =~ /mswin|mingw/) && (RUBY_VERSION < '1.9')
9
- require 'win32/open3'
10
- else
11
- require 'open3'
12
- end
13
-
14
- begin
15
- require 'active_support/core_ext/module/attribute_accessors'
16
- rescue LoadError
17
- require 'active_support/core_ext/class/attribute_accessors'
18
- end
19
-
20
- begin
21
- require 'active_support/core_ext/object/blank'
22
- rescue LoadError
23
- require 'active_support/core_ext/blank'
24
- end
9
+ require 'active_support/core_ext/module/attribute_accessors'
10
+ require 'active_support/core_ext/object/blank'
25
11
 
26
12
  require 'wicked_pdf/version'
27
13
  require 'wicked_pdf/railtie'
14
+ require 'wicked_pdf/option_parser'
28
15
  require 'wicked_pdf/tempfile'
16
+ require 'wicked_pdf/binary'
29
17
  require 'wicked_pdf/middleware'
18
+ require 'wicked_pdf/progress'
30
19
 
31
20
  class WickedPdf
32
21
  DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9')
33
- BINARY_VERSION_WITHOUT_DASHES = Gem::Version.new('0.12.0')
34
- EXE_NAME = 'wkhtmltopdf'.freeze
35
22
  @@config = {}
36
23
  cattr_accessor :config
37
- attr_accessor :binary_version
24
+
25
+ include Progress
38
26
 
39
27
  def initialize(wkhtmltopdf_binary_path = nil)
40
- @exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path
41
- raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
42
- raise "Bad #{EXE_NAME}'s path: #{@exe_path}" unless File.exist?(@exe_path)
43
- raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
28
+ @binary = Binary.new(wkhtmltopdf_binary_path, DEFAULT_BINARY_VERSION)
29
+ end
44
30
 
45
- retrieve_binary_version
31
+ def binary_version
32
+ @binary.version
46
33
  end
47
34
 
48
35
  def pdf_from_html_file(filepath, options = {})
@@ -52,44 +39,44 @@ class WickedPdf
52
39
  def pdf_from_string(string, options = {})
53
40
  options = options.dup
54
41
  options.merge!(WickedPdf.config) { |_key, option, _config| option }
55
- string_file = WickedPdfTempfile.new('wicked_pdf.html', options[:temp_path])
56
- string_file.binmode
57
- string_file.write(string)
58
- string_file.close
59
-
60
- pdf = pdf_from_html_file(string_file.path, options)
61
- pdf
62
- rescue => e
63
- raise "Error: #{e}"
42
+ string_file = WickedPdf::Tempfile.new('wicked_pdf.html', options[:temp_path])
43
+ string_file.write_in_chunks(string)
44
+ pdf_from_html_file(string_file.path, options)
64
45
  ensure
65
- string_file.close! if string_file
46
+ string_file.close if string_file
66
47
  end
67
48
 
68
49
  def pdf_from_url(url, options = {})
69
50
  # merge in global config options
70
51
  options.merge!(WickedPdf.config) { |_key, option, _config| option }
71
- generated_pdf_file = WickedPdfTempfile.new('wicked_pdf_generated_file.pdf', options[:temp_path])
72
- command = [@exe_path]
73
- command << '-q' unless on_windows? # suppress errors on stdout
52
+ generated_pdf_file = WickedPdf::Tempfile.new('wicked_pdf_generated_file.pdf', options[:temp_path])
53
+ command = [@binary.path]
54
+ command.unshift(@binary.xvfb_run_path) if options[:use_xvfb]
74
55
  command += parse_options(options)
75
56
  command << url
76
57
  command << generated_pdf_file.path.to_s
77
58
 
78
59
  print_command(command.inspect) if in_development_mode?
79
60
 
80
- err = Open3.popen3(*command) do |_stdin, _stdout, stderr|
81
- stderr.read
61
+ if track_progress?(options)
62
+ invoke_with_progress(command, options)
63
+ else
64
+ err = Open3.popen3(*command) do |_stdin, _stdout, stderr|
65
+ stderr.read
66
+ end
82
67
  end
83
68
  if options[:return_file]
84
69
  return_file = options.delete(:return_file)
85
70
  return generated_pdf_file
86
71
  end
87
- generated_pdf_file.rewind
88
- generated_pdf_file.binmode
89
- pdf = generated_pdf_file.read
90
- fail "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?
72
+
73
+ pdf = generated_pdf_file.read_in_chunks
74
+
75
+ raise "Error generating PDF\n Command Error: #{err}" if options[:raise_on_all_errors] && !err.empty?
76
+ raise "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?
77
+
91
78
  pdf
92
- rescue => e
79
+ rescue StandardError => e
93
80
  raise "Failed to execute:\n#{command}\nError: #{e}"
94
81
  ensure
95
82
  generated_pdf_file.close! if generated_pdf_file && !return_file
@@ -98,7 +85,8 @@ class WickedPdf
98
85
  private
99
86
 
100
87
  def in_development_mode?
101
- return Rails.env == 'development' if defined?(Rails)
88
+ return Rails.env == 'development' if defined?(Rails.env)
89
+
102
90
  RAILS_ENV == 'development' if defined?(RAILS_ENV)
103
91
  end
104
92
 
@@ -107,233 +95,10 @@ class WickedPdf
107
95
  end
108
96
 
109
97
  def print_command(cmd)
110
- p '*' * 15 + cmd + '*' * 15
111
- end
112
-
113
- def retrieve_binary_version
114
- _stdin, stdout, _stderr = Open3.popen3(@exe_path + ' -V')
115
- @binary_version = parse_version(stdout.gets(nil))
116
- rescue StandardError
117
- DEFAULT_BINARY_VERSION
118
- end
119
-
120
- def parse_version(version_info)
121
- match_data = /wkhtmltopdf\s*(\d*\.\d*\.\d*\w*)/.match(version_info)
122
- if match_data && (2 == match_data.length)
123
- Gem::Version.new(match_data[1])
124
- else
125
- DEFAULT_BINARY_VERSION
126
- end
98
+ Rails.logger.debug '[wicked_pdf]: ' + cmd
127
99
  end
128
100
 
129
101
  def parse_options(options)
130
- [
131
- parse_extra(options),
132
- parse_others(options),
133
- parse_global(options),
134
- parse_outline(options.delete(:outline)),
135
- parse_header_footer(:header => options.delete(:header),
136
- :footer => options.delete(:footer),
137
- :layout => options[:layout]),
138
- parse_cover(options.delete(:cover)),
139
- parse_toc(options.delete(:toc)),
140
- parse_basic_auth(options)
141
- ].flatten
142
- end
143
-
144
- def parse_extra(options)
145
- return [] if options[:extra].nil?
146
- return options[:extra].split if options[:extra].respond_to?(:split)
147
- options[:extra]
148
- end
149
-
150
- def parse_basic_auth(options)
151
- if options[:basic_auth]
152
- user, passwd = Base64.decode64(options[:basic_auth]).split(':')
153
- ['--username', user, '--password', passwd]
154
- else
155
- []
156
- end
157
- end
158
-
159
- def make_option(name, value, type = :string)
160
- if value.is_a?(Array)
161
- return value.collect { |v| make_option(name, v, type) }
162
- end
163
- if type == :name_value
164
- parts = value.to_s.split(' ')
165
- ["--#{name.tr('_', '-')}", *parts]
166
- elsif type == :boolean
167
- ["--#{name.tr('_', '-')}"]
168
- else
169
- ["--#{name.tr('_', '-')}", value.to_s]
170
- end
171
- end
172
-
173
- def valid_option(name)
174
- if binary_version < BINARY_VERSION_WITHOUT_DASHES
175
- "--#{name}"
176
- else
177
- name
178
- end
179
- end
180
-
181
- def make_options(options, names, prefix = '', type = :string)
182
- return [] if options.nil?
183
- names.collect do |o|
184
- if options[o].blank?
185
- []
186
- else
187
- make_option("#{prefix.blank? ? '' : prefix + '-'}#{o}",
188
- options[o],
189
- type)
190
- end
191
- end
192
- end
193
-
194
- def parse_header_footer(options)
195
- r = []
196
- [:header, :footer].collect do |hf|
197
- next if options[hf].blank?
198
- opt_hf = options[hf]
199
- r += make_options(opt_hf, [:center, :font_name, :left, :right], hf.to_s)
200
- r += make_options(opt_hf, [:font_size, :spacing], hf.to_s, :numeric)
201
- r += make_options(opt_hf, [:line], hf.to_s, :boolean)
202
- if options[hf] && options[hf][:content]
203
- @hf_tempfiles = [] unless defined?(@hf_tempfiles)
204
- @hf_tempfiles.push(tf = WickedPdfTempfile.new("wicked_#{hf}_pdf.html"))
205
- tf.write options[hf][:content]
206
- tf.flush
207
- options[hf][:html] = {}
208
- options[hf][:html][:url] = "file:///#{tf.path}"
209
- end
210
- unless opt_hf[:html].blank?
211
- r += make_option("#{hf}-html", opt_hf[:html][:url]) unless opt_hf[:html][:url].blank?
212
- end
213
- end unless options.blank?
214
- r
215
- end
216
-
217
- def parse_cover(argument)
218
- arg = argument.to_s
219
- return [] if arg.blank?
220
- # Filesystem path or URL - hand off to wkhtmltopdf
221
- if argument.is_a?(Pathname) || (arg[0, 4] == 'http')
222
- [valid_option('cover'), arg]
223
- else # HTML content
224
- @hf_tempfiles ||= []
225
- @hf_tempfiles << tf = WickedPdfTempfile.new('wicked_cover_pdf.html')
226
- tf.write arg
227
- tf.flush
228
- [valid_option('cover'), tf.path]
229
- end
230
- end
231
-
232
- def parse_toc(options)
233
- return [] if options.nil?
234
- r = [valid_option('toc')]
235
- unless options.blank?
236
- r += make_options(options, [:font_name, :header_text], 'toc')
237
- r += make_options(options, [:xsl_style_sheet])
238
- r += make_options(options, [:depth,
239
- :header_fs,
240
- :text_size_shrink,
241
- :l1_font_size,
242
- :l2_font_size,
243
- :l3_font_size,
244
- :l4_font_size,
245
- :l5_font_size,
246
- :l6_font_size,
247
- :l7_font_size,
248
- :level_indentation,
249
- :l1_indentation,
250
- :l2_indentation,
251
- :l3_indentation,
252
- :l4_indentation,
253
- :l5_indentation,
254
- :l6_indentation,
255
- :l7_indentation], 'toc', :numeric)
256
- r += make_options(options, [:no_dots,
257
- :disable_links,
258
- :disable_back_links], 'toc', :boolean)
259
- r += make_options(options, [:disable_dotted_lines,
260
- :disable_toc_links], nil, :boolean)
261
- end
262
- r
263
- end
264
-
265
- def parse_outline(options)
266
- r = []
267
- unless options.blank?
268
- r = make_options(options, [:outline], '', :boolean)
269
- r += make_options(options, [:outline_depth], '', :numeric)
270
- end
271
- r
272
- end
273
-
274
- def parse_margins(options)
275
- make_options(options, [:top, :bottom, :left, :right], 'margin', :numeric)
276
- end
277
-
278
- def parse_global(options)
279
- r = []
280
- unless options.blank?
281
- r += make_options(options, [:orientation,
282
- :dpi,
283
- :page_size,
284
- :page_width,
285
- :title])
286
- r += make_options(options, [:lowquality,
287
- :grayscale,
288
- :no_pdf_compression], '', :boolean)
289
- r += make_options(options, [:image_dpi,
290
- :image_quality,
291
- :page_height], '', :numeric)
292
- r += parse_margins(options.delete(:margin))
293
- end
294
- r
295
- end
296
-
297
- def parse_others(options)
298
- r = []
299
- unless options.blank?
300
- r += make_options(options, [:proxy,
301
- :username,
302
- :password,
303
- :encoding,
304
- :user_style_sheet,
305
- :viewport_size])
306
- r += make_options(options, [:cookie,
307
- :post], '', :name_value)
308
- r += make_options(options, [:redirect_delay,
309
- :zoom,
310
- :page_offset,
311
- :javascript_delay], '', :numeric)
312
- r += make_options(options, [:book,
313
- :default_header,
314
- :disable_javascript,
315
- :enable_plugins,
316
- :disable_internal_links,
317
- :disable_external_links,
318
- :print_media_type,
319
- :disable_smart_shrinking,
320
- :use_xserver,
321
- :no_background], '', :boolean)
322
- r += make_options(options, [:no_stop_slow_scripts], '', nil)
323
- end
324
- r
325
- end
326
-
327
- def find_wkhtmltopdf_binary_path
328
- possible_locations = (ENV['PATH'].split(':') + %w(/usr/bin /usr/local/bin ~/bin)).uniq
329
- exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
330
- exe_path ||= begin
331
- detected_path = (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
332
- detected_path.present? && detected_path
333
- rescue
334
- nil
335
- end
336
- exe_path ||= possible_locations.map { |l| File.expand_path("#{l}/#{EXE_NAME}") }.find { |location| File.exist?(location) }
337
- exe_path || ''
102
+ OptionParser.new(binary_version).parse(options)
338
103
  end
339
104
  end
@@ -0,0 +1,4 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
4
+ timeout: 500
@@ -0,0 +1,3 @@
1
+ //= link_tree ../images
2
+ //= link wicked.js
3
+ //= link wicked.css
@@ -0,0 +1 @@
1
+ // Wicked js
@@ -5,10 +5,32 @@ module ActionController
5
5
  def render_to_string(opts = {})
6
6
  opts.to_s
7
7
  end
8
+
9
+ def self.alias_method_chain(_target, _feature); end
10
+ end
11
+ end
12
+
13
+ module ActionControllerMock
14
+ class Base
15
+ def render(_)
16
+ [:base]
17
+ end
18
+
19
+ def render_to_string; end
20
+
21
+ def self.after_action(_); end
8
22
  end
9
23
  end
10
24
 
11
25
  class PdfHelperTest < ActionController::TestCase
26
+ module SomePatch
27
+ def render(_)
28
+ super.tap do |s|
29
+ s << :patched
30
+ end
31
+ end
32
+ end
33
+
12
34
  def setup
13
35
  @ac = ActionController::Base.new
14
36
  end
@@ -17,11 +39,58 @@ class PdfHelperTest < ActionController::TestCase
17
39
  @ac = nil
18
40
  end
19
41
 
20
- if Rails::VERSION::MAJOR == 2
21
- test 'should prerender header and footer :template options' do
22
- options = @ac.send(:prerender_header_and_footer,
23
- :header => { :html => { :template => 'hf.html.erb' } })
24
- assert_match %r(^file:\/\/\/.*wicked_header_pdf.*\.html), options[:header][:html][:url]
42
+ test 'should prerender header and footer :template options' do
43
+ options = @ac.send(:prerender_header_and_footer,
44
+ :header => { :html => { :template => 'hf.html.erb' } })
45
+ assert_match %r{^file:\/\/\/.*wicked_header_pdf.*\.html}, options[:header][:html][:url]
46
+ end
47
+
48
+ test 'should not interfere with already prepended patches' do
49
+ # Emulate railtie
50
+ if Rails::VERSION::MAJOR >= 5
51
+ # this spec tests the following:
52
+ # if another gem prepends a render method to ActionController::Base
53
+ # before wicked_pdf does, does calling render trigger an infinite loop?
54
+ # this spec fails with 6392bea1fe3a41682dfd7c20fd9c179b5a758f59 because PdfHelper
55
+ # aliases the render method prepended by the other gem to render_without_pdf, then
56
+ # base_evals its own definition of render, which calls render_with_pdf -> render_without_pdf.
57
+ # If the other gem uses the prepend inhertinance pattern (calling super instead of aliasing),
58
+ # when it calls super it calls the base_eval'd version of render instead of going up the
59
+ # inheritance chain, causing an infinite loop.
60
+
61
+ # This fiddling with consts is required to get around the fact that PdfHelper checks
62
+ # that it is being prepended to ActionController::Base
63
+ OriginalBase = ActionController::Base
64
+ ActionController.send(:remove_const, :Base)
65
+ ActionController.const_set(:Base, ActionControllerMock::Base)
66
+
67
+ # Emulate another gem being loaded before wicked
68
+ ActionController::Base.prepend(SomePatch)
69
+ ActionController::Base.prepend(::WickedPdf::PdfHelper)
70
+
71
+ begin
72
+ # test that wicked's render method is actually called
73
+ ac = ActionController::Base.new
74
+ ac.expects(:render_with_wicked_pdf)
75
+ ac.render(:cats)
76
+
77
+ # test that calling render does not trigger infinite loop
78
+ ac = ActionController::Base.new
79
+ assert_equal %i[base patched], ac.render(:cats)
80
+ rescue SystemStackError
81
+ assert_equal true, false # force spec failure
82
+ ensure
83
+ ActionController.send(:remove_const, :Base)
84
+ ActionController.const_set(:Base, OriginalBase)
85
+ end
86
+ end
87
+ end
88
+
89
+ test 'should call after_action instead of after_filter when able' do
90
+ ActionController::Base.expects(:after_filter).with(:clean_temp_files).never
91
+ ActionController::Base.expects(:after_action).with(:clean_temp_files).once
92
+ ActionController::Base.class_eval do
93
+ include ::WickedPdf::PdfHelper
25
94
  end
26
95
  end
27
96
  end
@@ -2,11 +2,29 @@ require 'test_helper'
2
2
  require 'action_view/test_case'
3
3
 
4
4
  class WickedPdfHelperAssetsTest < ActionView::TestCase
5
- include WickedPdfHelper::Assets
5
+ include WickedPdf::WickedPdfHelper::Assets
6
6
 
7
7
  if Rails::VERSION::MAJOR > 3 || (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0)
8
8
  test 'wicked_pdf_asset_base64 returns a base64 encoded asset' do
9
- assert_match %r(data:text\/css;base64,.+), wicked_pdf_asset_base64('wicked.css')
9
+ assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked.css')
10
+ end
11
+
12
+ test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
13
+ Rails.configuration.assets.expects(:compile => true)
14
+ assert_equal "<style type='text/css'>/* Wicked styles */\n\n</style>",
15
+ wicked_pdf_stylesheet_link_tag('wicked')
16
+ end
17
+
18
+ test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
19
+ Rails.configuration.assets.expects(:compile => true)
20
+ assert_equal image_tag("file:///#{Rails.root.join('public', 'pdf')}"),
21
+ wicked_pdf_image_tag('pdf')
22
+ end
23
+
24
+ test 'wicked_pdf_javascript_include_tag should inline the javascripts passed in' do
25
+ Rails.configuration.assets.expects(:compile => true)
26
+ assert_equal "<script type='text/javascript'>// Wicked js\n;\n</script>",
27
+ wicked_pdf_javascript_include_tag('wicked')
10
28
  end
11
29
 
12
30
  test 'wicked_pdf_asset_path should return a url when assets are served by an asset server' do
@@ -86,13 +104,13 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
86
104
  end
87
105
 
88
106
  test 'WickedPdfHelper::Assets::ASSET_URL_REGEX should match various URL data type formats' do
89
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'/asset/stylesheets/application.css\');'
90
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("/asset/stylesheets/application.css");'
91
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(/asset/stylesheets/application.css);'
92
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'http://assets.domain.com/dummy.png\');'
93
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("http://assets.domain.com/dummy.png");'
94
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(http://assets.domain.com/dummy.png);'
95
- assert_no_match WickedPdfHelper::Assets::ASSET_URL_REGEX, '.url { \'http://assets.domain.com/dummy.png\' }'
107
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'/asset/stylesheets/application.css\');'
108
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("/asset/stylesheets/application.css");'
109
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(/asset/stylesheets/application.css);'
110
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'http://assets.domain.com/dummy.png\');'
111
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("http://assets.domain.com/dummy.png");'
112
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(http://assets.domain.com/dummy.png);'
113
+ assert_no_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, '.url { \'http://assets.domain.com/dummy.png\' }'
96
114
  end
97
115
 
98
116
  test 'prepend_protocol should properly set the protocol when the asset is precompiled' do
@@ -2,25 +2,27 @@ require 'test_helper'
2
2
  require 'action_view/test_case'
3
3
 
4
4
  class WickedPdfHelperTest < ActionView::TestCase
5
- if Rails::VERSION::MAJOR == 2
6
- test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
7
- assert_equal "<style type='text/css'>/* Wicked styles */\n</style>",
8
- wicked_pdf_stylesheet_link_tag('../../../fixtures/wicked')
9
- end
5
+ include WickedPdf::WickedPdfHelper
10
6
 
11
- test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
12
- assert_equal image_tag("file:///#{Rails.root.join('public', 'images', 'pdf')}"),
13
- wicked_pdf_image_tag('pdf')
14
- end
7
+ test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
8
+ assert_equal "<style type='text/css'>/* Wicked styles */\n</style>",
9
+ wicked_pdf_stylesheet_link_tag('../../../fixtures/wicked')
10
+ end
15
11
 
12
+ test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
13
+ assert_equal image_tag("file:///#{Rails.root.join('public', 'images', 'pdf')}"),
14
+ wicked_pdf_image_tag('pdf')
15
+ end
16
+
17
+ if Rails::VERSION::MAJOR == 2
16
18
  test 'wicked_pdf_javascript_src_tag should return the same as javascript_src_tag when passed a full path' do
17
19
  assert_equal javascript_src_tag("file:///#{Rails.root.join('public', 'javascripts', 'pdf')}", {}),
18
20
  wicked_pdf_javascript_src_tag('pdf')
19
21
  end
22
+ end
20
23
 
21
- test 'wicked_pdf_include_tag should return many wicked_pdf_javascript_src_tags' do
22
- assert_equal [wicked_pdf_javascript_src_tag('foo'), wicked_pdf_javascript_src_tag('bar')].join("\n"),
23
- wicked_pdf_javascript_include_tag('foo', 'bar')
24
- end
24
+ test 'wicked_pdf_include_tag should return many wicked_pdf_javascript_src_tags' do
25
+ assert_equal [wicked_pdf_javascript_src_tag('foo'), wicked_pdf_javascript_src_tag('bar')].join("\n"),
26
+ wicked_pdf_javascript_include_tag('foo', 'bar')
25
27
  end
26
28
  end
data/test/test_helper.rb CHANGED
@@ -5,20 +5,29 @@ require File.expand_path('../dummy/config/environment.rb', __FILE__)
5
5
 
6
6
  require 'test/unit'
7
7
  require 'mocha'
8
-
9
- if Rails::VERSION::MAJOR == 2
10
- require 'test_help'
11
- else
12
- require 'rails/test_help'
13
- require 'mocha/test_unit'
14
- end
8
+ require 'rails/test_help'
9
+ require 'mocha/test_unit'
15
10
 
16
11
  require 'wicked_pdf'
17
12
 
18
13
  Rails.backtrace_cleaner.remove_silencers!
19
14
 
20
15
  if (assets_dir = Rails.root.join('app/assets')) && File.directory?(assets_dir)
16
+ # Copy CSS file
21
17
  destination = assets_dir.join('stylesheets/wicked.css')
22
18
  source = File.read('test/fixtures/wicked.css')
23
19
  File.open(destination, 'w') { |f| f.write(source) }
20
+
21
+ # Copy JS file
22
+ js_dir = assets_dir.join('javascripts')
23
+ Dir.mkdir(js_dir) unless File.directory?(js_dir)
24
+ destination = js_dir.join('wicked.js')
25
+ source = File.read('test/fixtures/wicked.js')
26
+ File.open(destination, 'w') { |f| f.write(source) }
27
+
28
+ config_dir = assets_dir.join('config')
29
+ Dir.mkdir(config_dir) unless File.directory?(config_dir)
30
+ source = File.read('test/fixtures/manifest.js')
31
+ destination = config_dir.join('manifest.js')
32
+ File.open(destination, 'w') { |f| f.write(source) }
24
33
  end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class WickedPdfBinaryTest < ActiveSupport::TestCase
4
+ test 'should extract old wkhtmltopdf version' do
5
+ version_info_sample = "Name:\n wkhtmltopdf 0.9.9\n\nLicense:\n Copyright (C) 2008,2009 Wkhtmltopdf Authors.\n\n\n\n License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n This is free software: you are free to change and redistribute it. There is NO\n WARRANTY, to the extent permitted by law.\n\nAuthors:\n Written by Jakob Truelsen. Patches by Mrio Silva, Benoit Garret and Emmanuel\n Bouthenot.\n"
6
+ assert_equal WickedPdf::DEFAULT_BINARY_VERSION, binary.parse_version_string(version_info_sample)
7
+ end
8
+
9
+ test 'should extract new wkhtmltopdf version' do
10
+ version_info_sample = "Name:\n wkhtmltopdf 0.11.0 rc2\n\nLicense:\n Copyright (C) 2010 wkhtmltopdf/wkhtmltoimage Authors.\n\n\n\n License LGPLv3+: GNU Lesser General Public License version 3 or later\n <http://gnu.org/licenses/lgpl.html>. This is free software: you are free to\n change and redistribute it. There is NO WARRANTY, to the extent permitted by\n law.\n\nAuthors:\n Written by Jan Habermann, Christian Sciberras and Jakob Truelsen. Patches by\n Mehdi Abbad, Lyes Amazouz, Pascal Bach, Emmanuel Bouthenot, Benoit Garret and\n Mario Silva."
11
+ assert_equal Gem::Version.new('0.11.0'), binary.parse_version_string(version_info_sample)
12
+ end
13
+
14
+ test 'should extract wkhtmltopdf version with nondigit symbols' do
15
+ version_info_sample = "Name:\n wkhtmltopdf 0.10.4b\n\nLicense:\n Copyright (C) 2008,2009 Wkhtmltopdf Authors.\n\n\n\n License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n This is free software: you are free to change and redistribute it. There is NO\n WARRANTY, to the extent permitted by law.\n\nAuthors:\n Written by Jakob Truelsen. Patches by Mrio Silva, Benoit Garret and Emmanuel\n Bouthenot.\n"
16
+ assert_equal Gem::Version.new('0.10.4b'), binary.parse_version_string(version_info_sample)
17
+ end
18
+
19
+ test 'should fallback to default version on parse error' do
20
+ assert_equal WickedPdf::DEFAULT_BINARY_VERSION, binary.parse_version_string('')
21
+ end
22
+
23
+ def binary(path = nil)
24
+ WickedPdf::Binary.new(path)
25
+ end
26
+ end