aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/library/tempfile
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/tempfile')
-rw-r--r--spec/ruby/library/tempfile/_close_spec.rb21
-rw-r--r--spec/ruby/library/tempfile/callback_spec.rb6
-rw-r--r--spec/ruby/library/tempfile/close_spec.rb57
-rw-r--r--spec/ruby/library/tempfile/delete_spec.rb7
-rw-r--r--spec/ruby/library/tempfile/initialize_spec.rb41
-rw-r--r--spec/ruby/library/tempfile/length_spec.rb7
-rw-r--r--spec/ruby/library/tempfile/open_spec.rb82
-rw-r--r--spec/ruby/library/tempfile/path_spec.rb26
-rw-r--r--spec/ruby/library/tempfile/shared/length.rb21
-rw-r--r--spec/ruby/library/tempfile/shared/unlink.rb12
-rw-r--r--spec/ruby/library/tempfile/size_spec.rb7
-rw-r--r--spec/ruby/library/tempfile/unlink_spec.rb7
12 files changed, 294 insertions, 0 deletions
diff --git a/spec/ruby/library/tempfile/_close_spec.rb b/spec/ruby/library/tempfile/_close_spec.rb
new file mode 100644
index 0000000000..d91a3e1adc
--- /dev/null
+++ b/spec/ruby/library/tempfile/_close_spec.rb
@@ -0,0 +1,21 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'tempfile'
+
+describe "Tempfile#_close" do
+ before :each do
+ @tempfile = Tempfile.new("specs")
+ end
+
+ after :each do
+ @tempfile.close!
+ 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
+ end
+end
diff --git a/spec/ruby/library/tempfile/callback_spec.rb b/spec/ruby/library/tempfile/callback_spec.rb
new file mode 100644
index 0000000000..045252fec3
--- /dev/null
+++ b/spec/ruby/library/tempfile/callback_spec.rb
@@ -0,0 +1,6 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'tempfile'
+
+describe "Tempfile.callback" do
+ it "needs to be reviewed for spec completeness"
+end
diff --git a/spec/ruby/library/tempfile/close_spec.rb b/spec/ruby/library/tempfile/close_spec.rb
new file mode 100644
index 0000000000..aa776b840c
--- /dev/null
+++ b/spec/ruby/library/tempfile/close_spec.rb
@@ -0,0 +1,57 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'tempfile'
+
+describe "Tempfile#close when passed no argument or [false]" do
+ before :each do
+ @tempfile = Tempfile.new("specs", tmp(""))
+ end
+
+ after :each do
+ @tempfile.close!
+ end
+
+ it "closes self" do
+ @tempfile.close
+ @tempfile.closed?.should be_true
+ end
+
+ it "does not unlink self" do
+ path = @tempfile.path
+ @tempfile.close
+ File.exist?(path).should be_true
+ end
+end
+
+describe "Tempfile#close when passed [true]" do
+ before :each do
+ @tempfile = Tempfile.new("specs", tmp(""))
+ end
+
+ it "closes self" do
+ @tempfile.close(true)
+ @tempfile.closed?.should be_true
+ end
+
+ it "unlinks self" do
+ path = @tempfile.path
+ @tempfile.close(true)
+ File.exist?(path).should be_false
+ end
+end
+
+describe "Tempfile#close!" do
+ before :each do
+ @tempfile = Tempfile.new("specs", tmp(""))
+ end
+
+ it "closes self" do
+ @tempfile.close!
+ @tempfile.closed?.should be_true
+ end
+
+ it "unlinks self" do
+ path = @tempfile.path
+ @tempfile.close!
+ File.exist?(path).should be_false
+ end
+end
diff --git a/spec/ruby/library/tempfile/delete_spec.rb b/spec/ruby/library/tempfile/delete_spec.rb
new file mode 100644
index 0000000000..8e92631e62
--- /dev/null
+++ b/spec/ruby/library/tempfile/delete_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/unlink', __FILE__)
+require 'tempfile'
+
+describe "Tempfile#delete" do
+ it_behaves_like :tempfile_unlink, :delete
+end
diff --git a/spec/ruby/library/tempfile/initialize_spec.rb b/spec/ruby/library/tempfile/initialize_spec.rb
new file mode 100644
index 0000000000..79f33e3e98
--- /dev/null
+++ b/spec/ruby/library/tempfile/initialize_spec.rb
@@ -0,0 +1,41 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'tempfile'
+
+describe "Tempfile#initialize" do
+ before :each do
+ @tempfile = Tempfile.allocate
+ end
+
+ after :each do
+ @tempfile.close!
+ end
+
+ it "opens a new tempfile with the passed name in the passed directory" do
+ @tempfile.send(:initialize, "basename", tmp(""))
+ File.exist?(@tempfile.path).should be_true
+
+ tmpdir = tmp("")
+ path = @tempfile.path
+
+ platform_is :windows do
+ # on Windows, both types of slashes are OK,
+ # but the tmp helper always uses '/'
+ path.gsub!('\\', '/')
+ end
+
+ path[0, tmpdir.length].should == tmpdir
+ path.should include("basename")
+ end
+
+ platform_is_not :windows do
+ it "sets the permisssions on the tempfile to 0600" do
+ @tempfile.send(:initialize, "basename", tmp(""))
+ File.stat(@tempfile.path).mode.should == 0100600
+ end
+ end
+
+ it "accepts encoding options" do
+ @tempfile.send(:initialize, ['shiftjis', 'yml'], encoding: 'SHIFT_JIS')
+ @tempfile.external_encoding.should == Encoding::Shift_JIS
+ end
+end
diff --git a/spec/ruby/library/tempfile/length_spec.rb b/spec/ruby/library/tempfile/length_spec.rb
new file mode 100644
index 0000000000..b4f0a3b1d4
--- /dev/null
+++ b/spec/ruby/library/tempfile/length_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/length', __FILE__)
+require 'tempfile'
+
+describe "Tempfile#length" do
+ it_behaves_like :tempfile_length, :length
+end
diff --git a/spec/ruby/library/tempfile/open_spec.rb b/spec/ruby/library/tempfile/open_spec.rb
new file mode 100644
index 0000000000..3ceaeec502
--- /dev/null
+++ b/spec/ruby/library/tempfile/open_spec.rb
@@ -0,0 +1,82 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'tempfile'
+
+describe "Tempfile#open" do
+ before :each do
+ @tempfile = Tempfile.new("specs")
+ @tempfile.puts("Test!")
+ end
+
+ after :each do
+ @tempfile.close!
+ 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
+ @tempfile.close! if @tempfile
+ end
+
+ it "returns a new, open Tempfile instance" do
+ @tempfile = Tempfile.open("specs")
+ # Delegation messes up .should be_an_instance_of(Tempfile)
+ @tempfile.instance_of?(Tempfile).should be_true
+ end
+
+ 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
+
+describe "Tempfile.open when passed a block" do
+ before :each do
+ ScratchPad.clear
+ end
+
+ after :each do
+ # Tempfile.open with block does not unlink
+ @tempfile.close! if @tempfile
+ end
+
+ it "yields a new, open Tempfile instance to the block" do
+ 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
+
+ ScratchPad.recorded.should == :yielded
+ end
+
+ it "returns the value of the block" do
+ value = Tempfile.open("specs") do |tempfile|
+ @tempfile = tempfile
+ "return"
+ end
+ value.should == "return"
+ end
+
+ it "closes the yielded Tempfile after the block" do
+ Tempfile.open("specs") { |tempfile| @tempfile = tempfile }
+ @tempfile.closed?.should be_true
+ end
+end
+
diff --git a/spec/ruby/library/tempfile/path_spec.rb b/spec/ruby/library/tempfile/path_spec.rb
new file mode 100644
index 0000000000..f25e902872
--- /dev/null
+++ b/spec/ruby/library/tempfile/path_spec.rb
@@ -0,0 +1,26 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'tempfile'
+
+describe "Tempfile#path" do
+ before :each do
+ @tempfile = Tempfile.new("specs", tmp(""))
+ end
+
+ after :each do
+ @tempfile.close!
+ end
+
+ it "returns the path to the tempfile" do
+ tmpdir = tmp("")
+ path = @tempfile.path
+
+ platform_is :windows do
+ # on Windows, both types of slashes are OK,
+ # but the tmp helper always uses '/'
+ path.gsub!('\\', '/')
+ end
+
+ path[0, tmpdir.length].should == tmpdir
+ path.should include("specs")
+ end
+end
diff --git a/spec/ruby/library/tempfile/shared/length.rb b/spec/ruby/library/tempfile/shared/length.rb
new file mode 100644
index 0000000000..4d18d1f385
--- /dev/null
+++ b/spec/ruby/library/tempfile/shared/length.rb
@@ -0,0 +1,21 @@
+describe :tempfile_length, shared: true do
+ before :each do
+ @tempfile = Tempfile.new("specs")
+ end
+
+ after :each do
+ @tempfile.close!
+ end
+
+ it "returns the size of self" do
+ @tempfile.send(@method).should eql(0)
+ @tempfile.print("Test!")
+ @tempfile.send(@method).should eql(5)
+ end
+
+ it "returns the size of self even if self is closed" do
+ @tempfile.print("Test!")
+ @tempfile.close
+ @tempfile.send(@method).should eql(5)
+ end
+end
diff --git a/spec/ruby/library/tempfile/shared/unlink.rb b/spec/ruby/library/tempfile/shared/unlink.rb
new file mode 100644
index 0000000000..2b575fd391
--- /dev/null
+++ b/spec/ruby/library/tempfile/shared/unlink.rb
@@ -0,0 +1,12 @@
+describe :tempfile_unlink, shared: true do
+ before :each do
+ @tempfile = Tempfile.new("specs")
+ end
+
+ it "unlinks self" do
+ @tempfile.close
+ path = @tempfile.path
+ @tempfile.send(@method)
+ File.exist?(path).should be_false
+ end
+end
diff --git a/spec/ruby/library/tempfile/size_spec.rb b/spec/ruby/library/tempfile/size_spec.rb
new file mode 100644
index 0000000000..ac66d35906
--- /dev/null
+++ b/spec/ruby/library/tempfile/size_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/length', __FILE__)
+require 'tempfile'
+
+describe "Tempfile#size" do
+ it_behaves_like :tempfile_length, :size
+end
diff --git a/spec/ruby/library/tempfile/unlink_spec.rb b/spec/ruby/library/tempfile/unlink_spec.rb
new file mode 100644
index 0000000000..d4ef343c8d
--- /dev/null
+++ b/spec/ruby/library/tempfile/unlink_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require File.expand_path('../shared/unlink', __FILE__)
+require 'tempfile'
+
+describe "Tempfile#unlink" do
+ it_behaves_like :tempfile_unlink, :unlink
+end