Skip to content

Commit

Permalink
Fixed Tempfile specs to not leave junk behind.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Ford authored and brixen committed Jan 27, 2011
1 parent 2dcca31 commit 22576a6
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 54 deletions.
11 changes: 8 additions & 3 deletions library/tempfile/_close_spec.rb
@@ -1,15 +1,20 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/common', __FILE__)
require 'tempfile'

describe "Tempfile#_close" do
before(:each) do
before :each do
@tempfile = Tempfile.new("specs")
end


after :each do
TempfileSpecs.cleanup @tempfile
end

it "is protected" do
Tempfile.should have_protected_instance_method(:_close)
end

it "closes self" do
@tempfile.send(:_close)
@tempfile.closed?.should be_true
Expand Down
1 change: 1 addition & 0 deletions library/tempfile/callback_spec.rb
@@ -1,4 +1,5 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/common', __FILE__)
require 'tempfile'

describe "Tempfile.callback" do
Expand Down
14 changes: 7 additions & 7 deletions library/tempfile/close_spec.rb
Expand Up @@ -9,7 +9,7 @@
after(:each) do
@tempfile.unlink if @tempfile.path
end

it "closes self" do
@tempfile.close
@tempfile.closed?.should be_true
Expand All @@ -20,16 +20,16 @@
before(:each) do
@tempfile = Tempfile.new("specs", tmp(""))
end

after(:each) do
@tempfile.unlink if @tempfile.path
end

it "closes self" do
@tempfile.close(true)
@tempfile.closed?.should be_true
end

it "unlinks self" do
path = @tempfile.path
@tempfile.close(true)
Expand All @@ -41,16 +41,16 @@
before(:each) do
@tempfile = Tempfile.new("specs", tmp(""))
end

after(:each) do
@tempfile.unlink if @tempfile.path
end

it "closes self" do
@tempfile.close!
@tempfile.closed?.should be_true
end

it "unlinks self" do
path = @tempfile.path
@tempfile.close!
Expand Down
3 changes: 2 additions & 1 deletion library/tempfile/delete_spec.rb
@@ -1,6 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'tempfile'
require File.expand_path('../fixtures/common', __FILE__)
require File.expand_path('../shared/unlink', __FILE__)
require 'tempfile'

describe "Tempfile#delete" do
it_behaves_like :tempfile_unlink, :delete
Expand Down
6 changes: 6 additions & 0 deletions library/tempfile/fixtures/common.rb
@@ -0,0 +1,6 @@
module TempfileSpecs
def self.cleanup(tempfile)
tempfile.close true unless tempfile.closed?
File.delete tempfile.path if tempfile.path and File.exists? tempfile.path
end
end
8 changes: 4 additions & 4 deletions library/tempfile/initialize_spec.rb
@@ -1,14 +1,14 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/common', __FILE__)
require 'tempfile'

describe "Tempfile#initialize" do
before(:each) do
before :each do
@tempfile = Tempfile.allocate
end

after(:each) do
@tempfile.close
@tempfile.unlink if @tempfile.path
after :each do
TempfileSpecs.cleanup @tempfile
end

it "opens a new tempfile with the passed name in the passed directory" do
Expand Down
3 changes: 2 additions & 1 deletion library/tempfile/length_spec.rb
@@ -1,6 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'tempfile'
require File.expand_path('../fixtures/common', __FILE__)
require File.expand_path('../shared/length', __FILE__)
require 'tempfile'

describe "Tempfile#length" do
it_behaves_like :tempfile_length, :length
Expand Down
71 changes: 41 additions & 30 deletions library/tempfile/open_spec.rb
@@ -1,65 +1,76 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/common', __FILE__)
require 'tempfile'

describe "Tempfile#open" do
before(:each) do
before :each do
@tempfile = Tempfile.new("specs")
@tempfile.puts("Test!")
end
after(:each) do
@tempfile.close

after :each do
TempfileSpecs.cleanup @tempfile
end

it "reopens self" do
@tempfile.close
@tempfile.open
@tempfile.closed?.should be_false
end

it "reopens self in read and write mode and does not truncate" do
@tempfile.open
@tempfile.puts("Another Test!")

@tempfile.open
@tempfile.readline.should == "Another Test!\n"
end
end

describe "Tempfile.open" do
after :each do
TempfileSpecs.cleanup @tempfile
end

it "returns a new, open Tempfile instance" do
# Tempfile.open("specs").should be_kind_of(Tempfile) # => fails!
t = Tempfile.open("specs")
t.instance_of?(Tempfile).should be_true
t.close
@tempfile = Tempfile.open("specs")
# Delegation messes up .should be_an_instance_of(Tempfile)
@tempfile.instance_of?(Tempfile).should be_true
end

ruby_version_is "1.8.7" do
it "is passed an array [base, suffix] as first argument" do
Tempfile.open(["specs", ".tt"]) { |tempfile| @tempfile = tempfile }
@tempfile.path.should =~ /specs.*\.tt$/
end
end
end

describe "Tempfile.open when passed a block" do
before :each do
ScratchPad.clear
end

after :each do
TempfileSpecs.cleanup @tempfile
end

it "yields a new, open Tempfile instance to the block" do
yielded = false
Tempfile.open("specs") do |tmpfile|
yielded = true
#tmpfile.should be_kind_of(Tempfile)
tmpfile.instance_of?(Tempfile).should be_true
tmpfile.closed?.should be_false
Tempfile.open("specs") do |tempfile|
@tempfile = tempfile
ScratchPad.record :yielded

# Delegation messes up .should be_an_instance_of(Tempfile)
tempfile.instance_of?(Tempfile).should be_true
tempfile.closed?.should be_false
end
yielded.should be_true

ScratchPad.recorded.should == :yielded
end

it "closes the yielded Tempfile after the block" do
tempfile = nil
Tempfile.open("specs") { |x| tempfile = x }
tempfile.closed?.should be_true
Tempfile.open("specs") { |tempfile| @tempfile = tempfile }
@tempfile.closed?.should be_true
end
end

ruby_version_is '1.8.7' .. '1.9' do
describe "Tempfile.open" do
it "is passed an array [base,suffix] as first argument" do
tempfile = nil
Tempfile.open(["specs", ".tt"]) { |x| tempfile = x }
tempfile.path.should =~ /specs.*\.tt$/
end
end
end
4 changes: 2 additions & 2 deletions library/tempfile/path_spec.rb
@@ -1,4 +1,5 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/common', __FILE__)
require 'tempfile'

describe "Tempfile#path" do
Expand All @@ -7,8 +8,7 @@
end

after :each do
@tempfile.close
@tempfile.unlink if @tempfile.path
TempfileSpecs.cleanup @tempfile
end

it "returns the path to the tempfile" do
Expand Down
6 changes: 5 additions & 1 deletion library/tempfile/shared/length.rb
@@ -1,8 +1,12 @@
describe :tempfile_length, :shared => true do
before(:each) do
before :each do
@tempfile = Tempfile.new("specs")
end

after :each do
TempfileSpecs.cleanup @tempfile
end

it "returns the size of self" do
@tempfile.send(@method).should eql(0)
@tempfile.print("Test!")
Expand Down
6 changes: 3 additions & 3 deletions library/tempfile/shared/unlink.rb
@@ -1,10 +1,10 @@
describe :tempfile_unlink, :shared => true do
before(:each) do
before :each do
@tempfile = Tempfile.new("specs")
end

after(:each) do
@tempfile.close rescue nil
after :each do
TempfileSpecs.cleanup @tempfile
end

ruby_bug "", "1.8.6" do
Expand Down
3 changes: 2 additions & 1 deletion library/tempfile/size_spec.rb
@@ -1,6 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'tempfile'
require File.expand_path('../fixtures/common', __FILE__)
require File.expand_path('../shared/length', __FILE__)
require 'tempfile'

describe "Tempfile#size" do
it_behaves_like :tempfile_length, :size
Expand Down
3 changes: 2 additions & 1 deletion library/tempfile/unlink_spec.rb
@@ -1,6 +1,7 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'tempfile'
require File.expand_path('../fixtures/common', __FILE__)
require File.expand_path('../shared/unlink', __FILE__)
require 'tempfile'

describe "Tempfile#unlink" do
it_behaves_like :tempfile_unlink, :unlink
Expand Down

0 comments on commit 22576a6

Please sign in to comment.