diff options
Diffstat (limited to 'spec/ruby/library/tempfile')
| -rw-r--r-- | spec/ruby/library/tempfile/_close_spec.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/callback_spec.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/close_spec.rb | 14 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/create_spec.rb | 176 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/delete_spec.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/initialize_spec.rb | 13 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/length_spec.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/open_spec.rb | 33 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/path_spec.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/shared/length.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/shared/unlink.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/size_spec.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/unlink_spec.rb | 4 |
13 files changed, 233 insertions, 43 deletions
diff --git a/spec/ruby/library/tempfile/_close_spec.rb b/spec/ruby/library/tempfile/_close_spec.rb index d91a3e1adc..344b08dc17 100644 --- a/spec/ruby/library/tempfile/_close_spec.rb +++ b/spec/ruby/library/tempfile/_close_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'tempfile' describe "Tempfile#_close" do @@ -11,11 +11,11 @@ describe "Tempfile#_close" do end it "is protected" do - Tempfile.should have_protected_instance_method(:_close) + Tempfile.protected_instance_methods(false).should.include?(:_close) end it "closes self" do @tempfile.send(:_close) - @tempfile.closed?.should be_true + @tempfile.closed?.should == true end end diff --git a/spec/ruby/library/tempfile/callback_spec.rb b/spec/ruby/library/tempfile/callback_spec.rb deleted file mode 100644 index 045252fec3..0000000000 --- a/spec/ruby/library/tempfile/callback_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -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 index aa776b840c..7e95ae1d7e 100644 --- a/spec/ruby/library/tempfile/close_spec.rb +++ b/spec/ruby/library/tempfile/close_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'tempfile' describe "Tempfile#close when passed no argument or [false]" do @@ -12,13 +12,13 @@ describe "Tempfile#close when passed no argument or [false]" do it "closes self" do @tempfile.close - @tempfile.closed?.should be_true + @tempfile.closed?.should == true end it "does not unlink self" do path = @tempfile.path @tempfile.close - File.exist?(path).should be_true + File.should.exist?(path) end end @@ -29,13 +29,13 @@ describe "Tempfile#close when passed [true]" do it "closes self" do @tempfile.close(true) - @tempfile.closed?.should be_true + @tempfile.closed?.should == true end it "unlinks self" do path = @tempfile.path @tempfile.close(true) - File.exist?(path).should be_false + File.should_not.exist?(path) end end @@ -46,12 +46,12 @@ describe "Tempfile#close!" do it "closes self" do @tempfile.close! - @tempfile.closed?.should be_true + @tempfile.closed?.should == true end it "unlinks self" do path = @tempfile.path @tempfile.close! - File.exist?(path).should be_false + File.should_not.exist?(path) end end diff --git a/spec/ruby/library/tempfile/create_spec.rb b/spec/ruby/library/tempfile/create_spec.rb new file mode 100644 index 0000000000..be6d21e218 --- /dev/null +++ b/spec/ruby/library/tempfile/create_spec.rb @@ -0,0 +1,176 @@ +require_relative '../../spec_helper' +require 'tempfile' + +describe "Tempfile.create" do + after :each do + if @tempfile + @tempfile.close + File.unlink(@tempfile.path) if File.file?(@tempfile.path) + end + end + + it "returns a new, open regular File instance placed in tmpdir" do + @tempfile = Tempfile.create + # Unlike Tempfile.open this returns a true File, + # but `.should.instance_of?(File)` would be true either way. + @tempfile.instance_of?(File).should == true + + @tempfile.should_not.closed? + File.file?(@tempfile.path).should == true + + @tempfile.path.should.start_with?(Dir.tmpdir) + @tempfile.path.should_not == "#{Dir.tmpdir}/" + end + + it "returns file in w+ mode" do + @tempfile = Tempfile.create + @tempfile << "Test!\nMore test!" + @tempfile.rewind + @tempfile.read.should == "Test!\nMore test!" + + # Not "a+" mode, which would write at the end of the file. + @tempfile.rewind + @tempfile.print "Trust" + @tempfile.rewind + @tempfile.read.should == "Trust\nMore test!" + end + + platform_is_not :windows do + it "returns a private, readable and writable file" do + @tempfile = Tempfile.create + stat = @tempfile.stat + stat.should.readable? + stat.should.writable? + stat.should_not.executable? + stat.should_not.world_readable? + stat.should_not.world_writable? + end + end + + platform_is :windows do + it "returns a public, readable and writable file" do + @tempfile = Tempfile.create + stat = @tempfile.stat + stat.should.readable? + stat.should.writable? + stat.should_not.executable? + stat.should.world_readable? + stat.should.world_writable? + end + end + + context "when called with a block" do + it "returns the value of the block" do + value = Tempfile.create do |tempfile| + tempfile << "Test!" + "return" + end + value.should == "return" + end + + it "closes and unlinks file after block execution" do + Tempfile.create do |tempfile| + @tempfile = tempfile + @tempfile.should_not.closed? + File.exist?(@tempfile.path).should == true + end + + @tempfile.should.closed? + File.exist?(@tempfile.path).should == false + end + end + + context "when called with a single positional argument" do + it "uses a String as a prefix for the filename" do + @tempfile = Tempfile.create("create_spec") + @tempfile.path.should.start_with?("#{Dir.tmpdir}/create_spec") + @tempfile.path.should_not == "#{Dir.tmpdir}/create_spec" + end + + it "uses an array of one String as a prefix for the filename" do + @tempfile = Tempfile.create(["create_spec"]) + @tempfile.path.should.start_with?("#{Dir.tmpdir}/create_spec") + @tempfile.path.should_not == "#{Dir.tmpdir}/create_spec" + end + + it "uses an array of two Strings as a prefix and suffix for the filename" do + @tempfile = Tempfile.create(["create_spec", ".temp"]) + @tempfile.path.should.start_with?("#{Dir.tmpdir}/create_spec") + @tempfile.path.should.end_with?(".temp") + end + + it "ignores excessive array elements after the first two" do + @tempfile = Tempfile.create(["create_spec", ".temp", :".txt"]) + @tempfile.path.should.start_with?("#{Dir.tmpdir}/create_spec") + @tempfile.path.should.end_with?(".temp") + end + + it "raises ArgumentError if passed something else than a String or an array of Strings" do + -> { Tempfile.create(:create_spec) }.should.raise(ArgumentError, "unexpected prefix: :create_spec") + -> { Tempfile.create([:create_spec]) }.should.raise(ArgumentError, "unexpected prefix: :create_spec") + -> { Tempfile.create(["create_spec", :temp]) }.should.raise(ArgumentError, "unexpected suffix: :temp") + end + end + + context "when called with a second positional argument" do + it "uses it as a directory for the tempfile" do + @tempfile = Tempfile.create("create_spec", "./") + @tempfile.path.should.start_with?("./create_spec") + end + + it "raises TypeError if argument can not be converted to a String" do + -> { Tempfile.create("create_spec", :temp) }.should.raise(TypeError, "no implicit conversion of Symbol into String") + end + end + + context "when called with a mode option" do + it "ORs it with the default mode, forcing it to be readable and writable" do + @tempfile = Tempfile.create(mode: File::RDONLY) + @tempfile.puts "test" + @tempfile.rewind + @tempfile.read.should == "test\n" + end + + it "raises NoMethodError if passed a String mode" do + -> { Tempfile.create(mode: "wb") }.should.raise(NoMethodError, /undefined method ['`]|' for .+String/) + end + end + + ruby_version_is "3.4" do + context "when called with anonymous: true" do + it "returns an already unlinked File without a proper path" do + @tempfile = Tempfile.create(anonymous: true) + @tempfile.should_not.closed? + @tempfile.path.should == "#{Dir.tmpdir}/" + File.file?(@tempfile.path).should == false + end + + it "unlinks file before calling the block" do + Tempfile.create(anonymous: true) do |tempfile| + @tempfile = tempfile + @tempfile.should_not.closed? + @tempfile.path.should == "#{Dir.tmpdir}/" + File.file?(@tempfile.path).should == false + end + @tempfile.should.closed? + end + end + + context "when called with anonymous: false" do + it "returns a usual File with a path" do + @tempfile = Tempfile.create(anonymous: false) + @tempfile.should_not.closed? + @tempfile.path.should.start_with?(Dir.tmpdir) + File.file?(@tempfile.path).should == true + end + end + end + + context "when called with other options" do + it "passes them along to File.open" do + @tempfile = Tempfile.create(encoding: "IBM037:IBM037", binmode: true) + @tempfile.external_encoding.should == Encoding.find("IBM037") + @tempfile.binmode?.should == true + end + end +end diff --git a/spec/ruby/library/tempfile/delete_spec.rb b/spec/ruby/library/tempfile/delete_spec.rb index 8e92631e62..0332b44dde 100644 --- a/spec/ruby/library/tempfile/delete_spec.rb +++ b/spec/ruby/library/tempfile/delete_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/unlink', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/unlink' require 'tempfile' describe "Tempfile#delete" do diff --git a/spec/ruby/library/tempfile/initialize_spec.rb b/spec/ruby/library/tempfile/initialize_spec.rb index 79f33e3e98..0e882a3f0c 100644 --- a/spec/ruby/library/tempfile/initialize_spec.rb +++ b/spec/ruby/library/tempfile/initialize_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'tempfile' describe "Tempfile#initialize" do @@ -12,7 +12,7 @@ describe "Tempfile#initialize" do 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 + File.should.exist?(@tempfile.path) tmpdir = tmp("") path = @tempfile.path @@ -24,11 +24,11 @@ describe "Tempfile#initialize" do end path[0, tmpdir.length].should == tmpdir - path.should include("basename") + path.should.include?("basename") end platform_is_not :windows do - it "sets the permisssions on the tempfile to 0600" do + it "sets the permissions on the tempfile to 0600" do @tempfile.send(:initialize, "basename", tmp("")) File.stat(@tempfile.path).mode.should == 0100600 end @@ -38,4 +38,9 @@ describe "Tempfile#initialize" do @tempfile.send(:initialize, ['shiftjis', 'yml'], encoding: 'SHIFT_JIS') @tempfile.external_encoding.should == Encoding::Shift_JIS end + + it "does not try to modify the arguments" do + @tempfile.send(:initialize, ['frozen'.freeze, 'txt'.freeze], encoding: Encoding::IBM437) + @tempfile.external_encoding.should == Encoding::IBM437 + end end diff --git a/spec/ruby/library/tempfile/length_spec.rb b/spec/ruby/library/tempfile/length_spec.rb index b4f0a3b1d4..bc622b9a70 100644 --- a/spec/ruby/library/tempfile/length_spec.rb +++ b/spec/ruby/library/tempfile/length_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/length', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/length' require 'tempfile' describe "Tempfile#length" do diff --git a/spec/ruby/library/tempfile/open_spec.rb b/spec/ruby/library/tempfile/open_spec.rb index 3ceaeec502..0993a2c5ee 100644 --- a/spec/ruby/library/tempfile/open_spec.rb +++ b/spec/ruby/library/tempfile/open_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'tempfile' describe "Tempfile#open" do @@ -14,7 +14,7 @@ describe "Tempfile#open" do it "reopens self" do @tempfile.close @tempfile.open - @tempfile.closed?.should be_false + @tempfile.closed?.should == false end it "reopens self in read and write mode and does not truncate" do @@ -33,14 +33,30 @@ describe "Tempfile.open" do 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 + # Delegation messes up .should.instance_of?(Tempfile) + @tempfile.instance_of?(Tempfile).should == 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 + + it "passes the third argument (options) to open" do + Tempfile.open("specs", Dir.tmpdir, encoding: "IBM037:IBM037", binmode: true) do |tempfile| + @tempfile = tempfile + tempfile.external_encoding.should == Encoding.find("IBM037") + tempfile.binmode?.should == true + end + end + + it "uses a blank string for basename when passed no arguments" do + Tempfile.open() do |tempfile| + @tempfile = tempfile + tempfile.closed?.should == false + end + @tempfile.should_not == nil + end end describe "Tempfile.open when passed a block" do @@ -58,9 +74,9 @@ describe "Tempfile.open when passed a block" do @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 + # Delegation messes up .should.instance_of?(Tempfile) + tempfile.instance_of?(Tempfile).should == true + tempfile.closed?.should == false end ScratchPad.recorded.should == :yielded @@ -76,7 +92,6 @@ describe "Tempfile.open when passed a block" do it "closes the yielded Tempfile after the block" do Tempfile.open("specs") { |tempfile| @tempfile = tempfile } - @tempfile.closed?.should be_true + @tempfile.closed?.should == true end end - diff --git a/spec/ruby/library/tempfile/path_spec.rb b/spec/ruby/library/tempfile/path_spec.rb index f25e902872..be56bd87c8 100644 --- a/spec/ruby/library/tempfile/path_spec.rb +++ b/spec/ruby/library/tempfile/path_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' require 'tempfile' describe "Tempfile#path" do @@ -21,6 +21,6 @@ describe "Tempfile#path" do end path[0, tmpdir.length].should == tmpdir - path.should include("specs") + path.should.include?("specs") end end diff --git a/spec/ruby/library/tempfile/shared/length.rb b/spec/ruby/library/tempfile/shared/length.rb index 4d18d1f385..1a89ff7b4d 100644 --- a/spec/ruby/library/tempfile/shared/length.rb +++ b/spec/ruby/library/tempfile/shared/length.rb @@ -8,14 +8,14 @@ describe :tempfile_length, shared: true do end it "returns the size of self" do - @tempfile.send(@method).should eql(0) + @tempfile.send(@method).should.eql?(0) @tempfile.print("Test!") - @tempfile.send(@method).should eql(5) + @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) + @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 index 2b575fd391..e821228d70 100644 --- a/spec/ruby/library/tempfile/shared/unlink.rb +++ b/spec/ruby/library/tempfile/shared/unlink.rb @@ -7,6 +7,6 @@ describe :tempfile_unlink, shared: true do @tempfile.close path = @tempfile.path @tempfile.send(@method) - File.exist?(path).should be_false + File.should_not.exist?(path) end end diff --git a/spec/ruby/library/tempfile/size_spec.rb b/spec/ruby/library/tempfile/size_spec.rb index ac66d35906..f4824601c7 100644 --- a/spec/ruby/library/tempfile/size_spec.rb +++ b/spec/ruby/library/tempfile/size_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/length', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/length' require 'tempfile' describe "Tempfile#size" do diff --git a/spec/ruby/library/tempfile/unlink_spec.rb b/spec/ruby/library/tempfile/unlink_spec.rb index d4ef343c8d..eac7df8472 100644 --- a/spec/ruby/library/tempfile/unlink_spec.rb +++ b/spec/ruby/library/tempfile/unlink_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../shared/unlink', __FILE__) +require_relative '../../spec_helper' +require_relative 'shared/unlink' require 'tempfile' describe "Tempfile#unlink" do |
