Skip to content

Commit

Permalink
Merge pull request #4 from RoleModel/test-pdf-concern
Browse files Browse the repository at this point in the history
Test Pdf Concern
  • Loading branch information
BlaineIrvin authored Aug 15, 2024
2 parents 24d8b8e + 9aa1d1d commit 23d91b5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
17 changes: 13 additions & 4 deletions lib/chromium/pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

require_relative 'railtie' if defined?(Rails::Railtie)
require_relative 'pdf/version'
require 'active_support/concern'

module Chromium
module Pdf
extend ActiveSupport::Concern

##
# @param unescaped_filename [String] The filename to save the PDF as.
# @param print_url [String] The URL of the page you want to be processed.
Expand All @@ -18,11 +21,17 @@ def generate_pdf!(unescaped_filename, print_url, arguments: ['--headless --disab
Dir.mktmpdir do |path|
filepath = "#{path}/#{filename}"

system("LD_PRELOAD='' #{chrome_path} --print-to-pdf='#{filepath}' #{arguments.join(' ')} #{print_url}")
chrome_print chrome_path, print_url, filename, filepath, arguments
end
end

protected

def chrome_print(chrome_path, print_url, filename, filepath, arguments)
system("LD_PRELOAD='' #{chrome_path} --print-to-pdf='#{filepath}' #{arguments.join(' ')} #{print_url}")

File.open(filepath) do |file|
yield file, filename
end
File.open(filepath) do |file|
yield file, filename
end
end
end
Expand Down
26 changes: 20 additions & 6 deletions test/chromium/test_pdf.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
# frozen_string_literal: true

require 'test_helper'
require 'test/jobs/test_generate_pdf_job'

class Chromium::TestPdf < Minitest::Test
# def test_that_it_has_a_version_number
# refute_nil ::Chromium::Pdf::VERSION
# end
def test_job_includes_pdf_concern
assert_respond_to TestGeneratePdfJob.new, :generate_pdf!
end

# def test_it_does_something_useful
# assert false
# end
def test_generate_pdf_calls_chrome_print
job = TestGeneratePdfJob.new
job.stub :chrome_print, :ran do
assert_equal :ran, job.generate_pdf!('filename', 'url')
end
end

def test_chrome_print_calls_file_open
job = TestGeneratePdfJob.new
File.stub :open, :ran do
result = job.send(:chrome_print, 'chrome', 'url', 'name', 'path', ['argument']) do |_file, filename|
assert_equal 'name', filename
end
assert_equal :ran, result
end
end
end
9 changes: 9 additions & 0 deletions test/jobs/test_generate_pdf_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class TestGeneratePdfJob
include Chromium::Pdf

def perform(filename, url)
generate_pdf!(filename, url)
end
end

0 comments on commit 23d91b5

Please sign in to comment.