From e52aa2929017b6e7652739ec55b9f9572e050304 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Mon, 6 Feb 2017 11:26:14 -0800 Subject: [PATCH] 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. --- ext/tempfile.rb | 7 +++++++ tests/tempfile_tests.rb | 10 ++++++++++ 2 files changed, 17 insertions(+) create mode 100644 ext/tempfile.rb create mode 100644 tests/tempfile_tests.rb diff --git a/ext/tempfile.rb b/ext/tempfile.rb new file mode 100644 index 00000000..f9a5a014 --- /dev/null +++ b/ext/tempfile.rb @@ -0,0 +1,7 @@ +class Tempfile + alias_method :size_original, :size + def size + s = size_original + s.nil? ? 0 : s + end +end diff --git a/tests/tempfile_tests.rb b/tests/tempfile_tests.rb new file mode 100644 index 00000000..ade275f6 --- /dev/null +++ b/tests/tempfile_tests.rb @@ -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