Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve path detection fallback #460

Merged
merged 3 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.0.0
2.6.4
serene marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Unreleased
=================
* Improve wkhtmltopdf path detection when `bundle exec which` is failing (#460)

2019-02-22
=================
* Bump to 0.8.4.1
Expand Down
9 changes: 7 additions & 2 deletions lib/pdfkit/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ def wkhtmltopdf
end

def default_wkhtmltopdf
@default_command_path ||= (defined?(Bundler::GemfileError) && File.exists?('Gemfile') ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
return @default_command_path if @default_command_path
if defined?(Bundler::GemfileError) && File.exists?('Gemfile')
@default_command_path = `bundle exec which wkhtmltopdf`.chomp
end
@default_command_path = `which wkhtmltopdf`.chomp if @default_command_path.nil? || @default_command_path.empty?
@default_command_path
end

def wkhtmltopdf=(path)
if File.exist?(path)
@wkhtmltopdf = path
else
warn "No executable found at #{path}. Will fall back to #{default_wkhtmltopdf}" unless File.exist?(path)
warn "No executable found at #{path}. Will fall back to #{default_wkhtmltopdf}"
serene marked this conversation as resolved.
Show resolved Hide resolved
@wkhtmltopdf = default_wkhtmltopdf
end
end
Expand Down
2 changes: 0 additions & 2 deletions pdfkit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ Gem::Specification.new do |s|
s.description = "Uses wkhtmltopdf to create PDFs using HTML"
s.license = "MIT"

s.rubyforge_project = "pdfkit"

serene marked this conversation as resolved.
Show resolved Hide resolved
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
Expand Down
45 changes: 41 additions & 4 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,48 @@
describe PDFKit::Configuration do
subject { PDFKit::Configuration.new }
describe "#wkhtmltopdf" do
context "when explicitly configured" do
it "uses configured value and don't detect" do
expect(subject).not_to receive(:default_wkhtmltopdf)
subject.wkhtmltopdf = "./Gemfile" # Need a file which exists
expect(subject.wkhtmltopdf).to eq("./Gemfile")
end

it "falls back to detected binary if configured path doesn't exists" do
expect(subject).to receive(:default_wkhtmltopdf).twice.and_return("/bin/fallback")
expect(subject).to receive(:warn).with(/No executable found/)
subject.wkhtmltopdf = "./missing-file" # Need a file which doesn't exist
expect(subject.wkhtmltopdf).to eq("/bin/fallback")
end
end

context "when not explicitly configured" do
it "detects the existance of bundler" do
# Test assumes bundler is installed in your test environment
expect(subject).to receive(:`).with('bundle exec which wkhtmltopdf').and_return('c:\windows\path.exe')
subject.wkhtmltopdf
context "when running inside bundler" do
# Simulate the presence of bundler even if it's not here
before { stub_const("Bundler::GemfileError", Class) }

it "detects the existance of bundler" do
expect(subject).to receive(:`).with('bundle exec which wkhtmltopdf').and_return("c:\\windows\\path.exe\n")
expect(subject.wkhtmltopdf).to eq('c:\windows\path.exe')
end

it "falls back if bundler path fails" do
# This happens when there is a wrong (buggy) version of bundler for example
expect(subject).to receive(:`).with('bundle exec which wkhtmltopdf').and_return("")
expect(subject).to receive(:`).with('which wkhtmltopdf').and_return("c:\\windows\\path.exe\n")
expect(subject.wkhtmltopdf).to eq('c:\windows\path.exe')
end
end

context "when running without bundler" do
# Simulate the absence of bundler even if it's there
before { hide_const("Bundler::GemfileError") }

it "detects the existance of bundler" do
expect(subject).not_to receive(:`).with('bundle exec which wkhtmltopdf')
expect(subject).to receive(:`).with('which wkhtmltopdf').and_return('c:\windows\path.exe')
expect(subject.wkhtmltopdf).to eq('c:\windows\path.exe')
end
end
end
end
Expand Down