diff options
Diffstat (limited to 'spec/ruby/library/tempfile')
| -rw-r--r-- | spec/ruby/library/tempfile/_close_spec.rb | 4 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/callback_spec.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/close_spec.rb | 6 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/create_spec.rb | 176 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/initialize_spec.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/open_spec.rb | 18 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/path_spec.rb | 2 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/shared/length.rb | 6 |
8 files changed, 195 insertions, 25 deletions
diff --git a/spec/ruby/library/tempfile/_close_spec.rb b/spec/ruby/library/tempfile/_close_spec.rb index c08f425b6f..344b08dc17 100644 --- a/spec/ruby/library/tempfile/_close_spec.rb +++ b/spec/ruby/library/tempfile/_close_spec.rb @@ -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 c0b1518326..0000000000 --- a/spec/ruby/library/tempfile/callback_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -require_relative '../../spec_helper' -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 db0eae3fa5..7e95ae1d7e 100644 --- a/spec/ruby/library/tempfile/close_spec.rb +++ b/spec/ruby/library/tempfile/close_spec.rb @@ -12,7 +12,7 @@ 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 @@ -29,7 +29,7 @@ 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 @@ -46,7 +46,7 @@ describe "Tempfile#close!" do it "closes self" do @tempfile.close! - @tempfile.closed?.should be_true + @tempfile.closed?.should == true end it "unlinks self" do 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/initialize_spec.rb b/spec/ruby/library/tempfile/initialize_spec.rb index f2e786d7d8..0e882a3f0c 100644 --- a/spec/ruby/library/tempfile/initialize_spec.rb +++ b/spec/ruby/library/tempfile/initialize_spec.rb @@ -24,7 +24,7 @@ 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 diff --git a/spec/ruby/library/tempfile/open_spec.rb b/spec/ruby/library/tempfile/open_spec.rb index ef2c95376f..0993a2c5ee 100644 --- a/spec/ruby/library/tempfile/open_spec.rb +++ b/spec/ruby/library/tempfile/open_spec.rb @@ -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,8 +33,8 @@ 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 @@ -46,14 +46,14 @@ describe "Tempfile.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 be_true + 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 be_false + tempfile.closed?.should == false end @tempfile.should_not == nil end @@ -74,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 @@ -92,6 +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 07f75b3e10..be56bd87c8 100644 --- a/spec/ruby/library/tempfile/path_spec.rb +++ b/spec/ruby/library/tempfile/path_spec.rb @@ -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 |
