diff --git a/NEWS.md b/NEWS.md index 2308cc7fe9..a814a3b3cc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -203,16 +203,6 @@ Outstanding ones only. take request headers as a Hash in the second argument when the first argument is a URI. [[Feature #16686]] -* Tempfile - - * Modified method - - * `Tempfile.open { ... }` will now unlink the file at the end of the - block (https://github.com/ruby/tempfile/pull/3), such that once the - block finishes execution nothing leaks. - - - ## Compatibility issues Excluding feature bug fixes. diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 6826d58d9a..b4d2b457c7 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -2079,14 +2079,12 @@ class Reline::LineEditor end private def vi_histedit(key) - Tempfile.open { |fp| + path = Tempfile.open { |fp| fp.write @line - path = fp.path - fp.close - - system("#{ENV['EDITOR']} #{path}") - @line = File.read(path) + fp.path } + system("#{ENV['EDITOR']} #{path}") + @line = File.read(path) finish end diff --git a/spec/ruby/library/tempfile/open_spec.rb b/spec/ruby/library/tempfile/open_spec.rb index dabbe92a8a..ef2c95376f 100644 --- a/spec/ruby/library/tempfile/open_spec.rb +++ b/spec/ruby/library/tempfile/open_spec.rb @@ -38,10 +38,8 @@ describe "Tempfile.open" do end it "is passed an array [base, suffix] as first argument" do - Tempfile.open(["specs", ".tt"]) { |tempfile| - @tempfile = tempfile - tempfile.path.should =~ /specs.*\.tt$/ - } + Tempfile.open(["specs", ".tt"]) { |tempfile| @tempfile = tempfile } + @tempfile.path.should =~ /specs.*\.tt$/ end it "passes the third argument (options) to open" do @@ -67,7 +65,7 @@ describe "Tempfile.open when passed a block" do end after :each do - # Tempfile.open with block does not unlink in Ruby <= 2.7 + # Tempfile.open with block does not unlink @tempfile.close! if @tempfile end @@ -96,24 +94,4 @@ describe "Tempfile.open when passed a block" do Tempfile.open("specs") { |tempfile| @tempfile = tempfile } @tempfile.closed?.should be_true end - - ruby_version_is ""..."2.8" do - it "does not unlink the file after the block ends" do - path = Tempfile.open("specs") { |tempfile| - @tempfile = tempfile - tempfile.path - } - File.should.exist?(path) - end - end - - ruby_version_is "2.8" do - it "unlinks the file after the block ends" do - path = Tempfile.open("specs") { |tempfile| - @tempfile = tempfile - tempfile.path - } - File.should_not.exist?(path) - end - end end diff --git a/test/openssl/test_x509store.rb b/test/openssl/test_x509store.rb index e1898d62b9..1cbc73d539 100644 --- a/test/openssl/test_x509store.rb +++ b/test/openssl/test_x509store.rb @@ -33,21 +33,20 @@ class OpenSSL::TestX509Store < OpenSSL::TestCase ] cert1 = issue_cert(@ca1, @rsa1024, 1, ca_exts, nil, nil) cert2 = issue_cert(@ca2, @rsa2048, 1, ca_exts, nil, nil) - Tempfile.open { |tmpfile| - tmpfile << cert1.to_pem << cert2.to_pem - tmpfile.close + tmpfile = Tempfile.open { |f| f << cert1.to_pem << cert2.to_pem; f } - store = OpenSSL::X509::Store.new - assert_equal false, store.verify(cert1) - assert_equal false, store.verify(cert2) - store.add_file(tmpfile.path) - assert_equal true, store.verify(cert1) - assert_equal true, store.verify(cert2) + store = OpenSSL::X509::Store.new + assert_equal false, store.verify(cert1) + assert_equal false, store.verify(cert2) + store.add_file(tmpfile.path) + assert_equal true, store.verify(cert1) + assert_equal true, store.verify(cert2) - # OpenSSL < 1.1.1 leaks an error on a duplicate certificate - assert_nothing_raised { store.add_file(tmpfile.path) } - assert_equal [], OpenSSL.errors - } + # OpenSSL < 1.1.1 leaks an error on a duplicate certificate + assert_nothing_raised { store.add_file(tmpfile.path) } + assert_equal [], OpenSSL.errors + ensure + tmpfile and tmpfile.close! end def test_verify diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index c528eea0ae..fafb082154 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -2814,7 +2814,7 @@ __END__ def test_flush_in_finalizer1 bug3910 = '[ruby-dev:42341]' - Tempfile.open("bug3910") {|t| + tmp = Tempfile.open("bug3910") {|t| path = t.path t.close fds = [] @@ -2826,6 +2826,7 @@ __END__ f.print "hoge" } end + t } ensure ObjectSpace.each_object(File) {|f| @@ -2833,6 +2834,7 @@ __END__ f.close end } + tmp.close! end def test_flush_in_finalizer2 @@ -2856,6 +2858,7 @@ __END__ end } end + t.close! } end diff --git a/tool/lib/minitest/unit.rb b/tool/lib/minitest/unit.rb index 9066782322..c325134e63 100644 --- a/tool/lib/minitest/unit.rb +++ b/tool/lib/minitest/unit.rb @@ -122,11 +122,16 @@ module MiniTest return "Expected: #{mu_pp exp}\n Actual: #{mu_pp act}" unless need_to_diff + tempfile_a = nil + tempfile_b = nil + Tempfile.open("expect") do |a| + tempfile_a = a a.puts expect a.flush Tempfile.open("butwas") do |b| + tempfile_b = b b.puts butwas b.flush @@ -147,6 +152,9 @@ module MiniTest end result + ensure + tempfile_a.close! if tempfile_a + tempfile_b.close! if tempfile_b end ##