diff --git a/library/tempfile/_close_spec.rb b/library/tempfile/_close_spec.rb index 6e61d54cc3..304b214832 100644 --- a/library/tempfile/_close_spec.rb +++ b/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 diff --git a/library/tempfile/callback_spec.rb b/library/tempfile/callback_spec.rb index 045252fec3..0e0cf40cbe 100644 --- a/library/tempfile/callback_spec.rb +++ b/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 diff --git a/library/tempfile/close_spec.rb b/library/tempfile/close_spec.rb index c143a21a68..a65b2fbaba 100644 --- a/library/tempfile/close_spec.rb +++ b/library/tempfile/close_spec.rb @@ -9,7 +9,7 @@ after(:each) do @tempfile.unlink if @tempfile.path end - + it "closes self" do @tempfile.close @tempfile.closed?.should be_true @@ -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) @@ -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! diff --git a/library/tempfile/delete_spec.rb b/library/tempfile/delete_spec.rb index 7692d4304b..c76995a588 100644 --- a/library/tempfile/delete_spec.rb +++ b/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 diff --git a/library/tempfile/fixtures/common.rb b/library/tempfile/fixtures/common.rb new file mode 100644 index 0000000000..291ef52ae6 --- /dev/null +++ b/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 diff --git a/library/tempfile/initialize_spec.rb b/library/tempfile/initialize_spec.rb index 6f8967f282..279cdaff74 100644 --- a/library/tempfile/initialize_spec.rb +++ b/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 diff --git a/library/tempfile/length_spec.rb b/library/tempfile/length_spec.rb index f87f0da1ae..3aac721e51 100644 --- a/library/tempfile/length_spec.rb +++ b/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 diff --git a/library/tempfile/open_spec.rb b/library/tempfile/open_spec.rb index 7ec8a64d4b..4f749247c1 100644 --- a/library/tempfile/open_spec.rb +++ b/library/tempfile/open_spec.rb @@ -1,14 +1,15 @@ 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 @@ -16,50 +17,60 @@ @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 diff --git a/library/tempfile/path_spec.rb b/library/tempfile/path_spec.rb index 51fd7406a9..9e2cb85bb6 100644 --- a/library/tempfile/path_spec.rb +++ b/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 @@ -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 diff --git a/library/tempfile/shared/length.rb b/library/tempfile/shared/length.rb index e84ad02fc4..21fa16d81f 100644 --- a/library/tempfile/shared/length.rb +++ b/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!") diff --git a/library/tempfile/shared/unlink.rb b/library/tempfile/shared/unlink.rb index a0303b95be..e78acd45fe 100644 --- a/library/tempfile/shared/unlink.rb +++ b/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 diff --git a/library/tempfile/size_spec.rb b/library/tempfile/size_spec.rb index f2d2be555a..4c85a6b22d 100644 --- a/library/tempfile/size_spec.rb +++ b/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 diff --git a/library/tempfile/unlink_spec.rb b/library/tempfile/unlink_spec.rb index c94f31da0e..96660ace3b 100644 --- a/library/tempfile/unlink_spec.rb +++ b/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