Skip to content

Commit

Permalink
specs : rules the binary content of damn pdfs (and fix 1.9.2 compatib…
Browse files Browse the repository at this point in the history
…iliy issue)
  • Loading branch information
elmatou committed Nov 23, 2011
1 parent f783243 commit 8f55554
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
21 changes: 11 additions & 10 deletions spec/active_pdftk/wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def get_input(input_type, file_name = 'spec.fields.pdf')
when :hash
{path_to_pdf(file_name) => nil}
when :file
File.new(path_to_pdf(file_name))
File.new(path_to_pdf(file_name), 'rb')
when :tempfile
t = Tempfile.new('input.spec')
t.write(File.read(path_to_pdf(file_name)))
Expand All @@ -25,7 +25,7 @@ def get_output(output_type)
when :path
path_to_pdf('output.spec')
when :file
File.new(path_to_pdf('output.spec'), 'w+')
File.new(path_to_pdf('output.spec'), 'wb+')
when :tempfile
Tempfile.new('output.spec')
when :stringio
Expand Down Expand Up @@ -88,6 +88,7 @@ def map_output_type(output_specified)
if example.metadata[:genesis] && @output.is_a?(String)
FileUtils.copy_entry(@output, @example_expect.to_s, true, false, true)
else
#cleanup_file_content!(File.open(@output, 'r:binary').read).should == cleanup_file_content!(File.open(@example_expect, 'r:binary').read) if @output.is_a?(String) # lets keep this line for debugging purpose.
@call_output.should look_like_the_same_pdf_as(@example_expect)
end
end
Expand Down Expand Up @@ -200,29 +201,29 @@ def map_output_type(output_specified)
end
end

describe "#background" do
it_behaves_like "a working command" do
describe "#background", :focus => true do
it_behaves_like "a combination command" do
before(:all) { @example_expect = fixtures_path('background/expect.pdf') }
before(:each) { @call_output = @pdftk.background(get_input(input_type, 'multi.pdf'), path_to_pdf('poly.pdf'), :output => @output) }
end
end

describe "#multibackground" do
it_behaves_like "a working command" do
describe "#multibackground", :focus => true do
it_behaves_like "a combination command" do
before(:all) { @example_expect = fixtures_path('multibackground/expect.pdf') }
before(:each) { @call_output = @pdftk.multibackground(get_input(input_type, 'multi.pdf'), path_to_pdf('poly.pdf'), :output => @output) }
end
end

describe "#stamp" do
it_behaves_like "a working command" do
describe "#stamp", :focus => true do
it_behaves_like "a combination command" do
before(:all) { @example_expect = fixtures_path('stamp/expect.pdf') }
before(:each) { @call_output = @pdftk.stamp(get_input(input_type, 'multi.pdf'), path_to_pdf('poly.pdf'), :output => @output) }
end
end

describe "#multistamp" do
it_behaves_like "a working command" do
describe "#multistamp", :focus => true do
it_behaves_like "a combination command" do
before(:all) { @example_expect = fixtures_path('multistamp/expect.pdf') }
before(:each) { @call_output = @pdftk.multistamp(get_input(input_type, 'multi.pdf'), path_to_pdf('poly.pdf'), :output => @output) }
end
Expand Down
16 changes: 14 additions & 2 deletions spec/support/inputs_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,21 @@ def open_or_rewind(target)
end

def cleanup_file_content!(text)

unless @filter
@filter = {
:date => /\(D\:.*\)/, # Remove dates ex: /CreationDate (D:20111106104455-05'00')
:ids => /\/ID \[<\w*><\w*>\]/, # Remove ID values ex: /ID [<4ba02a4cf55b1fc842299e6f01eb838e><33bec7dc37839cadf7ab76f3be4d4306>]
:stream => /stream .* 9|10 0 obj /m, # Remove some binary stream
:content => /\/Contents \[.*\]/,
:xref => /^\d{10} \d{5} n|f $/ # Remove Cross-references dictionnary
}
@filter.each {|k,reg| @filter[k] = Regexp.new(reg.source.encode('ASCII-8BIT'), reg.options) if reg.source.respond_to? :encode }
end


text.force_encoding('ASCII-8BIT') if text.respond_to? :force_encoding # PDF embed some binary data breaking gsub with ruby 1.9.2
text.gsub!(/\(D\:.*\)/, '') # Remove dates ex: /CreationDate (D:20111106104455-05'00')
text.gsub!(/\/ID \[<\w*><\w*>\]/, '') # Remove ID values ex: /ID [<4ba02a4cf55b1fc842299e6f01eb838e><33bec7dc37839cadf7ab76f3be4d4306>]
@filter.each {|k,reg| text.gsub!(reg, '')}
text
end

Expand Down
2 changes: 1 addition & 1 deletion spec/support/matchers/content_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def sha256_hash_of_almost(entry)
if entry.directory?
sha256_hash_of_almost(Dir.new(entry))
elsif entry.file?
sha256_hash_of_almost(File.read(entry))
sha256_hash_of_almost(File.open(entry, 'r:binary').read)
end
end
end

0 comments on commit 8f55554

Please sign in to comment.