Skip to content

Commit d6b32ef

Browse files
committed
Improve path detection fallback
1 parent bf993c8 commit d6b32ef

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

.ruby-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ruby-2.0.0
1+
2.6.4

lib/pdfkit/configuration.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,20 @@ def wkhtmltopdf
2525
end
2626

2727
def default_wkhtmltopdf
28-
@default_command_path ||= (defined?(Bundler::GemfileError) && File.exists?('Gemfile') ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
28+
# @default_command_path ||= (defined?(Bundler::GemfileError) && File.exists?('Gemfile') ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
29+
return @default_command_path if @default_command_path
30+
if defined?(Bundler::GemfileError) && File.exists?('Gemfile')
31+
@default_command_path = `bundle exec which wkhtmltopdf`.chomp
32+
end
33+
@default_command_path = `which wkhtmltopdf`.chomp if @default_command_path.nil? || @default_command_path.empty?
34+
@default_command_path
2935
end
3036

3137
def wkhtmltopdf=(path)
3238
if File.exist?(path)
3339
@wkhtmltopdf = path
3440
else
35-
warn "No executable found at #{path}. Will fall back to #{default_wkhtmltopdf}" unless File.exist?(path)
41+
warn "No executable found at #{path}. Will fall back to #{default_wkhtmltopdf}"
3642
@wkhtmltopdf = default_wkhtmltopdf
3743
end
3844
end

pdfkit.gemspec

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ Gem::Specification.new do |s|
1313
s.description = "Uses wkhtmltopdf to create PDFs using HTML"
1414
s.license = "MIT"
1515

16-
s.rubyforge_project = "pdfkit"
17-
1816
s.files = `git ls-files`.split("\n")
1917
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
2018
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }

spec/configuration_spec.rb

+41-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,48 @@
33
describe PDFKit::Configuration do
44
subject { PDFKit::Configuration.new }
55
describe "#wkhtmltopdf" do
6+
context "when explicitly configured" do
7+
it "uses configured value and don't detect" do
8+
expect(subject).not_to receive(:default_wkhtmltopdf)
9+
subject.wkhtmltopdf = "./Gemfile" # Need a file which exists
10+
expect(subject.wkhtmltopdf).to eq("./Gemfile")
11+
end
12+
13+
it "falls back to detected binary if configured path doesn't exists" do
14+
expect(subject).to receive(:default_wkhtmltopdf).twice.and_return("/bin/fallback")
15+
expect(subject).to receive(:warn).with(/No executable found/)
16+
subject.wkhtmltopdf = "./missing-file" # Need a file which doesn't exist
17+
expect(subject.wkhtmltopdf).to eq("/bin/fallback")
18+
end
19+
end
20+
621
context "when not explicitly configured" do
7-
it "detects the existance of bundler" do
8-
# Test assumes bundler is installed in your test environment
9-
expect(subject).to receive(:`).with('bundle exec which wkhtmltopdf').and_return('c:\windows\path.exe')
10-
subject.wkhtmltopdf
22+
context "when running inside bundler" do
23+
# Simulate the presence of bundler even if it's not here
24+
before { stub_const("Bundler::GemfileError", Class) }
25+
26+
it "detects the existance of bundler" do
27+
expect(subject).to receive(:`).with('bundle exec which wkhtmltopdf').and_return("c:\\windows\\path.exe\n")
28+
expect(subject.wkhtmltopdf).to eq('c:\windows\path.exe')
29+
end
30+
31+
it "falls back if bundler path fails" do
32+
# This happens when there is a wrong (buggy) version of bundler for example
33+
expect(subject).to receive(:`).with('bundle exec which wkhtmltopdf').and_return("")
34+
expect(subject).to receive(:`).with('which wkhtmltopdf').and_return("c:\\windows\\path.exe\n")
35+
expect(subject.wkhtmltopdf).to eq('c:\windows\path.exe')
36+
end
37+
end
38+
39+
context "when running without bundler" do
40+
# Simulate the absence of bundler even if it's there
41+
before { hide_const("Bundler::GemfileError") }
42+
43+
it "detects the existance of bundler" do
44+
expect(subject).not_to receive(:`).with('bundle exec which wkhtmltopdf')
45+
expect(subject).to receive(:`).with('which wkhtmltopdf').and_return('c:\windows\path.exe')
46+
expect(subject.wkhtmltopdf).to eq('c:\windows\path.exe')
47+
end
1148
end
1249
end
1350
end

0 commit comments

Comments
 (0)