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

Close opened image files #31

Merged
merged 1 commit into from
Feb 29, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

* Ruby 2.7.0 support

### Bug Fixes

* Close opened image file #31

## 3.1.0

### Minor Enhancements
Expand Down
11 changes: 10 additions & 1 deletion lib/prawn/emoji/drawer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,16 @@ def draw_emoji(text, text_options:, base_text:)
x = image.adjust_x(base_x + @document.width_of(base_text + text.left, text_options))
y = image.adjust_y(base_y)

@document.image image.path, at: [x, y], width: image.width
# Prawn 2.2 does not close the image file when Pathname is passed to Document#image method.
#
# FIXME: This issue has been fixed by https://github.com/prawnpdf/prawn/pull/1090 but not released.
# Fix as follows after the PR released.
#
# @document.image(image_file.path, at: [x, y], width: image.width)
#
File.open(image.path, 'r') do |image_file|
@document.image(image_file, at: [x, y], width: image.width)
end
end
end
end
Expand Down
31 changes: 30 additions & 1 deletion test/units/prawn/emoji/drawer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
sushi_image.adjust_x(x),
sushi_image.adjust_y(200)
]
mock(document).image(sushi_image.path, at: position, width: sushi_image.width).once
mock(document).image(is_a(File), at: position, width: sushi_image.width).once
end
subject
end
Expand Down Expand Up @@ -117,4 +117,33 @@
it { _(subject).must_equal [sub_char * 2, japanese, sub_char * 2, japanese, sub_char * 2].join }
end
end

describe 'Closing opened image files' do
let(:text) { '🍣' }
let(:text_options) { { at: [100, 200], font_size: 12 } }

it do
opened_files = { before: count_opened_files, after: nil }

disable_gc {
subject
opened_files[:after] = count_opened_files
}

_(opened_files[:after]).must_equal opened_files[:before]
end

def count_opened_files
ObjectSpace.each_object(File).reject(&:closed?).count
end

def disable_gc
was_disabled = GC.disable
begin
yield
ensure
GC.enable unless was_disabled
end
end
end
end