Module: Bidi2pdf::TestHelpers::PdfFileHelper

Defined in:
lib/bidi2pdf/test_helpers/pdf_file_helper.rb

Overview

This module provides helper methods for handling PDF files in tests. It includes methods for debugging, storing, and managing PDF files.

Instance Method Summary collapse

Instance Method Details

#store_pdf_file(pdf_data, filename_prefix = "test") ⇒ String

Stores the given PDF data to a file with a specified filename prefix. The file is saved in a temporary directory.

Parameters:

  • pdf_data (String)

    the PDF data to store

  • filename_prefix (String) (defaults to: "test")

    the prefix for the generated filename (default: “test”)

Returns:

  • (String)

    the full path to the saved PDF file



26
27
28
29
30
31
32
# File 'lib/bidi2pdf/test_helpers/pdf_file_helper.rb', line 26

def store_pdf_file(pdf_data, filename_prefix = "test")
  pdf_file = tmp_file("pdf-files", "#{filename_prefix}-#{Time.now.to_i}.pdf")
  FileUtils.mkdir_p(File.dirname(pdf_file))
  File.binwrite(pdf_file, pdf_data)

  pdf_file
end

#with_pdf_debug(pdf_data) {|String| ... } ⇒ Object

Executes a block with the given PDF data and handles debugging in case of test failures. If an expectation fails, the PDF data is saved to a file for debugging purposes.

Parameters:

  • pdf_data (String)

    the PDF data to debug

Yields:

  • (String)

    yields the PDF data to the given block

Raises:

  • (RSpec::Expectations::ExpectationNotMetError)

    re-raises the exception after saving the PDF



13
14
15
16
17
18
19
# File 'lib/bidi2pdf/test_helpers/pdf_file_helper.rb', line 13

def with_pdf_debug(pdf_data)
  yield pdf_data
rescue RSpec::Expectations::ExpectationNotMetError => e
  failure_output = store_pdf_file pdf_data, "test-failure"
  puts "Test failed! PDF saved to: #{failure_output}"
  raise e
end