Skip to content

Commit

Permalink
Ensure cleanup tempfile after some specs (crystal-lang#5810)
Browse files Browse the repository at this point in the history
* Ensure cleanup tempfile after some specs

* Fix compiler spec
  • Loading branch information
bew authored and RX14 committed Apr 5, 2018
1 parent ef85244 commit 12488c2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
4 changes: 4 additions & 0 deletions spec/compiler/codegen/private_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/compiler/compiler_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
6 changes: 6 additions & 0 deletions spec/std/callstack_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
2 changes: 2 additions & 0 deletions spec/std/process_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,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
Expand Down
29 changes: 20 additions & 9 deletions spec/std/tempfile_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,34 +38,39 @@ 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
tempfile = Tempfile.open("foo") do |f|
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
Expand All @@ -76,19 +83,23 @@ 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

it "returns configure directory for tempfiles" 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

0 comments on commit 12488c2

Please sign in to comment.