From 9331813fadad84d0ba6d6ae58536776074fa76a8 Mon Sep 17 00:00:00 2001 From: Benoit de Chezelles Date: Thu, 5 Apr 2018 07:49:51 -0700 Subject: [PATCH] Ensure cleanup tempfile after some specs (#5810) * Ensure cleanup tempfile after some specs * Fix compiler spec --- spec/compiler/codegen/private_spec.cr | 4 ++++ spec/compiler/compiler_spec.cr | 4 ++++ spec/std/callstack_spec.cr | 6 ++++++ spec/std/process_spec.cr | 2 ++ spec/std/tempfile_spec.cr | 29 ++++++++++++++++++--------- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/spec/compiler/codegen/private_spec.cr b/spec/compiler/codegen/private_spec.cr index f3f69f757740..eee389178232 100644 --- a/spec/compiler/codegen/private_spec.cr +++ b/spec/compiler/codegen/private_spec.cr @@ -20,6 +20,8 @@ describe "Codegen: private" do tempfile.close compiler.compile sources, output_filename + ensure + File.delete(tempfile.path) if tempfile end it "codegens overloaded private def in same file" do @@ -45,6 +47,8 @@ describe "Codegen: private" do tempfile.close compiler.compile sources, output_filename + ensure + File.delete(tempfile.path) if tempfile end it "doesn't include filename for private types" do diff --git a/spec/compiler/compiler_spec.cr b/spec/compiler/compiler_spec.cr index c06cb59e9f63..ec7c8db19aac 100644 --- a/spec/compiler/compiler_spec.cr +++ b/spec/compiler/compiler_spec.cr @@ -11,6 +11,8 @@ describe "Compiler" do File.exists?(tempfile.path).should be_true `#{tempfile.path}`.should eq("Hello!") + ensure + File.delete(tempfile.path) if tempfile end it "runs subcommand in preference to a filename " do @@ -23,6 +25,8 @@ describe "Compiler" do File.exists?(tempfile.path).should be_true `#{tempfile.path}`.should eq("Hello!") + ensure + File.delete(tempfile.path) if tempfile end end end diff --git a/spec/std/callstack_spec.cr b/spec/std/callstack_spec.cr index 8fa0dc34142a..78c131734163 100644 --- a/spec/std/callstack_spec.cr +++ b/spec/std/callstack_spec.cr @@ -33,6 +33,8 @@ describe "Backtrace" do output.should_not match(/src\/callstack\.cr/) output.should_not match(/src\/exception\.cr/) output.should_not match(/src\/raise\.cr/) + ensure + File.delete(tempfile.path) if tempfile end it "prints exception backtrace to stderr" do @@ -49,6 +51,8 @@ describe "Backtrace" do output.to_s.empty?.should be_true error.to_s.should contain("IndexError") + ensure + File.delete(tempfile.path) if tempfile end it "prints crash backtrace to stderr" do @@ -65,5 +69,7 @@ describe "Backtrace" do output.to_s.empty?.should be_true error.to_s.should contain("Invalid memory access") + ensure + File.delete(tempfile.path) if tempfile end end diff --git a/spec/std/process_spec.cr b/spec/std/process_spec.cr index 8b5b30a4e707..94a9c60a1544 100644 --- a/spec/std/process_spec.cr +++ b/spec/std/process_spec.cr @@ -221,6 +221,8 @@ describe Process do File.exists?(tmpfile.path).should be_true tmpfile.unlink + ensure + File.delete(tmpfile.path) if tmpfile && File.exists?(tmpfile.path) end it "checks for existence" do diff --git a/spec/std/tempfile_spec.cr b/spec/std/tempfile_spec.cr index 2b52176a6b0e..7f01d835015a 100644 --- a/spec/std/tempfile_spec.cr +++ b/spec/std/tempfile_spec.cr @@ -23,6 +23,8 @@ describe Tempfile do File.exists?(tempfile.path).should be_true File.read(tempfile.path).should eq("Hello!") + ensure + File.delete(tempfile.path) if tempfile end it "has given extension if passed to constructor" do @@ -36,6 +38,8 @@ describe Tempfile do tempfile.delete File.exists?(tempfile.path).should be_false + ensure + File.delete(tempfile.path) if tempfile && File.exists?(tempfile.path) end it "doesn't delete on open with block" do @@ -43,27 +47,30 @@ describe Tempfile do f.print "Hello!" end File.exists?(tempfile.path).should be_true + ensure + File.delete(tempfile.path) if tempfile end it "has given extension if passed to open" do tempfile = Tempfile.open("foo", ".pdf") { |f| } File.extname(tempfile.path).should eq(".pdf") + ensure + File.delete(tempfile.path) if tempfile end it "creates and writes with TMPDIR environment variable" do old_tmpdir = ENV["TMPDIR"]? ENV["TMPDIR"] = "/tmp" - begin - tempfile = Tempfile.new "foo" - tempfile.print "Hello!" - tempfile.close + tempfile = Tempfile.new "foo" + tempfile.print "Hello!" + tempfile.close - File.exists?(tempfile.path).should be_true - File.read(tempfile.path).should eq("Hello!") - ensure - ENV["TMPDIR"] = old_tmpdir if old_tmpdir - end + File.exists?(tempfile.path).should be_true + File.read(tempfile.path).should eq("Hello!") + ensure + ENV["TMPDIR"] = old_tmpdir if old_tmpdir + File.delete(tempfile.path) if tempfile end it "is seekable" do @@ -76,12 +83,15 @@ describe Tempfile do tempfile.pos = 0 tempfile.gets(chomp: false).should eq("Hello!\n") tempfile.close + ensure + File.delete(tempfile.path) if tempfile end it "returns default directory for tempfiles" do old_tmpdir = ENV["TMPDIR"]? ENV.delete("TMPDIR") Tempfile.dirname.should eq("/tmp") + ensure ENV["TMPDIR"] = old_tmpdir if old_tmpdir end @@ -89,6 +99,7 @@ describe Tempfile do old_tmpdir = ENV["TMPDIR"]? ENV["TMPDIR"] = "/my/tmp" Tempfile.dirname.should eq("/my/tmp") + ensure ENV["TMPDIR"] = old_tmpdir if old_tmpdir end end