Skip to content

Commit 965c1bb

Browse files
authored
Update for new Ruby and Rails versions (#499)
- Update default ruby version to latest 2.6 release. - Test Ruby 2.7, 3.0, and 3.1, and Rails 6.1 and 7.0. Use versions as strings to make sure 3.0 tests 3.0. - Use latest rubygems and bundler to fix the bundler cache. - Exclude incompatible test matrix combinations. - Show Ruby deprecation warnings in tests where supported. - File.exists? is deprecated; use File.exist?. - URI.escape and URI.decode are deprecated. - Add frozen string literal comments. - Make sure constants are frozen. - Make sure that the code is frozen-string safe. - Adjust dev dependencies for Ruby 3.0+ compatibility. - Fix Ruby warnings in pdfkit_spec.rb - Comment out bad syntax in release-drafter action. Fixes #498
1 parent ecd18e6 commit 965c1bb

22 files changed

+117
-28
lines changed

.github/workflows/release-drafter.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
steps:
1313
# Drafts your next Release notes as Pull Requests are merged into "master"
1414
- uses: release-drafter/release-drafter@v5
15-
with:
15+
# with:
1616
# (Optional) specify config name to use, relative to .github/. Default: release-drafter.yml
1717
# config-name: my-config.yml
1818
env:

.github/workflows/test.yml

+31-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,35 @@ jobs:
88
fail-fast: false
99
matrix:
1010
os: [ubuntu-latest]
11-
ruby: [2.5, 2.6, 2.7]
12-
rails: ['~> 4.1', '~> 5.2', '~> 6.0']
11+
ruby:
12+
- '2.5'
13+
- '2.6'
14+
- '2.7'
15+
- '3.0'
16+
- '3.1'
17+
rails:
18+
- '~> 4.1'
19+
- '~> 5.2'
20+
- '~> 6.0.0'
21+
- '~> 6.1'
22+
- '~> 7.0.0'
23+
exclude:
24+
- ruby: '2.5'
25+
rails: '~> 7.0.0'
26+
- ruby: '2.6'
27+
rails: '~> 7.0.0'
28+
- ruby: '2.7'
29+
rails: '~> 4.1'
30+
- ruby: '3.0'
31+
rails: '~> 4.1'
32+
- ruby: '3.0'
33+
rails: '~> 5.2'
34+
- ruby: '3.1'
35+
rails: '~> 4.1'
36+
- ruby: '3.1'
37+
rails: '~> 5.2'
38+
- ruby: '3.1'
39+
rails: '~> 6.0.0'
1340
runs-on: ${{ matrix.os }}
1441
env:
1542
RAILS_VERSION: ${{ matrix.rails }}
@@ -18,6 +45,8 @@ jobs:
1845
- uses: ruby/setup-ruby@v1
1946
with:
2047
ruby-version: ${{ matrix.ruby }}
48+
rubygems: latest
49+
bundler: latest
2150
bundler-cache: true
2251

2352
- name: Setup wkhtmltopdf

.ruby-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.6.4
1+
2.6.9

.travis.yml

+24
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,36 @@ language: ruby
33
rvm:
44
- 2.5
55
- 2.6
6+
- 2.7
7+
- 3.0
8+
- 3.1
69

710
env:
811
matrix:
912
- RAILS_VERSION="~> 4.1"
1013
- RAILS_VERSION="~> 5.2"
1114
- RAILS_VERSION="~> 6.0.0"
15+
- RAILS_VERSION="~> 6.1"
16+
- RAILS_VERSION="~> 7.0.0"
17+
18+
jobs:
19+
exclude:
20+
- rvm: 2.5
21+
env: RAILS_VERSION="~> 7.0.0"
22+
- rvm: 2.6
23+
env: RAILS_VERSION="~> 7.0.0"
24+
- rvm: 2.7
25+
env: RAILS_VERSION="~> 4.1"
26+
- rvm: 3.0
27+
env: RAILS_VERSION="~> 4.1"
28+
- rvm: 3.0
29+
env: RAILS_VERSION="~> 5.2"
30+
- rvm: 3.1
31+
env: RAILS_VERSION="~> 4.1"
32+
- rvm: 3.1
33+
env: RAILS_VERSION="~> 5.2"
34+
- rvm: 3.1
35+
env: RAILS_VERSION="~> 6.0.0"
1236

1337
cache: bundler
1438

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Create PDFs using plain old HTML+CSS. Uses [wkhtmltopdf](http://github.com/antia
44

55
## Supported versions
66

7-
- Ruby 2.5, 2.6
8-
- Rails 4.2, 5.2, 6.0
7+
- Ruby 2.5, 2.6, 2.7, 3.0, 3.1
8+
- Rails 4.2, 5.2, 6.0, 6.1, 7.0
99

1010
## Install
1111

lib/pdfkit.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'pdfkit/source'
24
require 'pdfkit/pdfkit'
35
require 'pdfkit/middleware'

lib/pdfkit/configuration.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class PDFKit
24
class Configuration
35
attr_accessor :meta_tag_prefix, :root_url
@@ -26,7 +28,7 @@ def wkhtmltopdf
2628

2729
def default_wkhtmltopdf
2830
return @default_command_path if @default_command_path
29-
if defined?(Bundler::GemfileError) && File.exists?('Gemfile')
31+
if defined?(Bundler::GemfileError) && File.exist?('Gemfile')
3032
@default_command_path = `bundle exec which wkhtmltopdf`.chomp.lines.last
3133
end
3234
@default_command_path = `which wkhtmltopdf`.chomp if @default_command_path.nil? || @default_command_path.empty?

lib/pdfkit/html_preprocessor.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class PDFKit
24
module HTMLPreprocessor
35

lib/pdfkit/middleware.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class PDFKit
24
class Middleware
35
def initialize(app, options = {}, conditions = {})

lib/pdfkit/os.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'rbconfig'
24

35
class PDFKit

lib/pdfkit/pdfkit.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'shellwords'
24
require 'tempfile'
35

@@ -6,8 +8,8 @@ class Error < StandardError; end
68

79
class NoExecutableError < Error
810
def initialize
9-
msg = "No wkhtmltopdf executable found at #{PDFKit.configuration.wkhtmltopdf}\n"
10-
msg << ">> Please install wkhtmltopdf - https://github.com/pdfkit/PDFKit/wiki/Installing-WKHTMLTOPDF"
11+
msg = "No wkhtmltopdf executable found at #{PDFKit.configuration.wkhtmltopdf}\n" \
12+
">> Please install wkhtmltopdf - https://github.com/pdfkit/PDFKit/wiki/Installing-WKHTMLTOPDF"
1113
super(msg)
1214
end
1315
end
@@ -40,7 +42,7 @@ def initialize(url_file_or_html, options = {})
4042
@renderer = WkHTMLtoPDF.new options
4143
@renderer.normalize_options
4244

43-
raise NoExecutableError unless File.exists?(PDFKit.configuration.wkhtmltopdf)
45+
raise NoExecutableError unless File.exist?(PDFKit.configuration.wkhtmltopdf)
4446
end
4547

4648
def command(path = nil)

lib/pdfkit/source.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'tempfile'
24
require 'uri'
35

@@ -7,6 +9,8 @@ class Source
79

810
def initialize(url_file_or_html)
911
@source = url_file_or_html
12+
# @source is assumed to be modifiable, so make sure it is.
13+
@source = @source.dup if @source.is_a?(String) && @source.frozen?
1014
end
1115

1216
def url?
@@ -38,11 +42,11 @@ def to_s
3842
private
3943

4044
def shell_safe_url
41-
url_needs_escaping? ? URI::escape(@source) : @source
45+
url_needs_escaping? ? URI::DEFAULT_PARSER.escape(@source) : @source
4246
end
4347

4448
def url_needs_escaping?
45-
URI::decode(@source) == @source
49+
URI::DEFAULT_PARSER.unescape(@source) == @source
4650
end
4751
end
4852
end

lib/pdfkit/version.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class PDFKit
24
VERSION = '0.8.5'
35
end

lib/pdfkit/wkhtmltopdf.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
# frozen_string_literal: true
2+
13
class PDFKit
24
class WkHTMLtoPDF
35
attr_reader :options
46
# Pulled from:
57
# https://github.com/wkhtmltopdf/wkhtmltopdf/blob/ebf9b6cfc4c58a31349fb94c568b254fac37b3d3/README_WKHTMLTOIMAGE#L27
6-
REPEATABLE_OPTIONS = %w[--allow --cookie --custom-header --post --post-file --run-script]
7-
SPECIAL_OPTIONS = %w[cover toc]
8+
REPEATABLE_OPTIONS = %w[--allow --cookie --custom-header --post --post-file --run-script].freeze
9+
SPECIAL_OPTIONS = %w[cover toc].freeze
810

911
def initialize(options)
1012
@options = options

pdfkit.gemspec

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ Gem::Specification.new do |s|
1818
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
1919
s.require_paths = ["lib"]
2020

21-
s.required_ruby_version = '>= 2.5'
21+
s.required_ruby_version = ">= 2.5"
2222

2323
s.requirements << "wkhtmltopdf"
2424

2525
# Development Dependencies
2626
s.add_development_dependency(%q<activesupport>, [">= 4.1.11"])
2727
s.add_development_dependency(%q<mocha>, [">= 0.9.10"])
2828
s.add_development_dependency(%q<rack-test>, [">= 0.5.6"])
29-
s.add_development_dependency(%q<rake>, ["~>12.3.3"])
30-
s.add_development_dependency(%q<rdoc>, ["~> 4.0.1"])
29+
s.add_development_dependency(%q<rake>, [">= 12.3.3"])
30+
s.add_development_dependency(%q<rdoc>, [">= 4.0.1"])
3131
s.add_development_dependency(%q<rspec>, ["~> 3.0"])
3232
end

spec/configuration_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'spec_helper'
24

35
describe PDFKit::Configuration do

spec/html_preprocessor_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'spec_helper'
24

35
describe PDFKit::HTMLPreprocessor do

spec/middleware_spec.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'spec_helper'
24

35
def app; Rack::Lint.new(@app); end
@@ -267,16 +269,16 @@ def mock_app(options = {}, conditions = {}, custom_headers = {})
267269
describe "saving generated pdf to disk" do
268270
before do
269271
#make sure tests don't find an old test_save.pdf
270-
File.delete('spec/test_save.pdf') if File.exists?('spec/test_save.pdf')
271-
expect(File.exists?('spec/test_save.pdf')).to eq(false)
272+
File.delete('spec/test_save.pdf') if File.exist?('spec/test_save.pdf')
273+
expect(File.exist?('spec/test_save.pdf')).to eq(false)
272274
end
273275

274276
context "when header PDFKit-save-pdf is present" do
275277
it "saves the .pdf to disk" do
276278
headers = { 'PDFKit-save-pdf' => 'spec/test_save.pdf' }
277279
mock_app({}, {only: '/public'}, headers)
278280
get 'http://www.example.org/public/test_save.pdf'
279-
expect(File.exists?('spec/test_save.pdf')).to eq(true)
281+
expect(File.exist?('spec/test_save.pdf')).to eq(true)
280282
end
281283

282284
it "does not raise when target directory does not exist" do
@@ -292,7 +294,7 @@ def mock_app(options = {}, conditions = {}, custom_headers = {})
292294
it "does not saved the .pdf to disk" do
293295
mock_app({}, {only: '/public'}, {} )
294296
get 'http://www.example.org/public/test_save.pdf'
295-
expect(File.exists?('spec/test_save.pdf')).to eq(false)
297+
expect(File.exist?('spec/test_save.pdf')).to eq(false)
296298
end
297299
end
298300
end

spec/os_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require 'spec_helper'
35
require 'rbconfig'
46

spec/pdfkit_spec.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#encoding: UTF-8
2+
# frozen_string_literal: true
3+
24
require 'spec_helper'
35

46
describe PDFKit do
@@ -27,7 +29,7 @@
2729
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
2830
pdfkit = PDFKit.new(Tempfile.new(file_path))
2931
expect(pdfkit.source).to be_file
30-
expect(pdfkit.source.to_s).to match /^#{Dir.tmpdir}/
32+
expect(pdfkit.source.to_s).to match(/^#{Dir.tmpdir}/)
3133
end
3234

3335
# Options
@@ -239,12 +241,12 @@
239241

240242
it "read the source from stdin if it is html" do
241243
pdfkit = PDFKit.new('html')
242-
expect(pdfkit.command).to match /- -$/
244+
expect(pdfkit.command).to match(/- -$/)
243245
end
244246

245247
it "specifies the URL to the source if it is a url" do
246248
pdfkit = PDFKit.new('http://google.com')
247-
expect(pdfkit.command).to match /"http:\/\/google.com" -$/
249+
expect(pdfkit.command).to match(/"http:\/\/google.com" -$/)
248250
end
249251

250252
it "does not break Windows paths" do
@@ -256,19 +258,19 @@
256258
it "specifies the path to the source if it is a file" do
257259
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
258260
pdfkit = PDFKit.new(File.new(file_path))
259-
expect(pdfkit.command).to match /#{file_path} -$/
261+
expect(pdfkit.command).to match(/#{file_path} -$/)
260262
end
261263

262264
it "specifies the path to the source if it is a tempfile" do
263265
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
264266
pdfkit = PDFKit.new(Tempfile.new(file_path))
265-
expect(pdfkit.command).to match /#{Dir.tmpdir}\S+ -$/
267+
expect(pdfkit.command).to match(/#{Dir.tmpdir}\S+ -$/)
266268
end
267269

268270
it "specifies the path for the ouput if a path is given" do
269271
file_path = "/path/to/output.pdf"
270272
pdfkit = PDFKit.new("html")
271-
expect(pdfkit.command(file_path)).to match /#{file_path}$/
273+
expect(pdfkit.command(file_path)).to match(/#{file_path}$/)
272274
end
273275

274276
it "detects special pdfkit meta tags" do

spec/source_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'spec_helper'
24

35
describe PDFKit::Source do

spec/spec_helper.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
SPEC_ROOT = File.dirname(__FILE__)
24
$LOAD_PATH.unshift(SPEC_ROOT)
35
$LOAD_PATH.unshift(File.join(SPEC_ROOT, '..', 'lib'))
@@ -6,13 +8,15 @@
68
add_filter 'spec/'
79
end
810

11+
Warning[:deprecated] = true if defined?(Warning.[]=)
12+
913
require 'pdfkit'
1014
require 'rspec'
1115
require 'mocha'
1216
require 'rack'
1317
require 'rack/test'
1418
require 'active_support'
15-
require 'custom_wkhtmltopdf_path' if File.exists?(File.join(SPEC_ROOT, 'custom_wkhtmltopdf_path.rb'))
19+
require 'custom_wkhtmltopdf_path' if File.exist?(File.join(SPEC_ROOT, 'custom_wkhtmltopdf_path.rb'))
1620

1721
RSpec.configure do |config|
1822
include Rack::Test::Methods

0 commit comments

Comments
 (0)