Fix tempfile returning nil for empty files

This is an "monkey patch" to have Tempfile return 0 instead of nil when
a tempfile is empty. This has caused an unexplained exception in our
logs for years, and I was really surprised when I caught it, and more
surprised that nobody has ever reported it to us before.

I really think this is a bug, and have filed it as a bug on the ruby
tracker. I haven't seen any consequences (yet) to doing this, so I'm
just going to run with it and see what happens. If it blows
something up (how? what scenario?), I'll try a different approach.
This commit is contained in:
Kyle Drake 2017-02-06 11:26:14 -08:00
parent 28000fc73c
commit e52aa29290
2 changed files with 17 additions and 0 deletions

7
ext/tempfile.rb Normal file
View file

@ -0,0 +1,7 @@
class Tempfile
alias_method :size_original, :size
def size
s = size_original
s.nil? ? 0 : s
end
end

10
tests/tempfile_tests.rb Normal file
View file

@ -0,0 +1,10 @@
require_relative './environment.rb'
describe Tempfile do
it 'should return 0 when no data is written' do
tmp = Tempfile.new
tmp.write ''
tmp.close
tmp.size.must_equal 0
end
end