diff options
Diffstat (limited to 'spec/ruby/library/tempfile')
| -rw-r--r-- | spec/ruby/library/tempfile/_close_spec.rb | 21 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/close_spec.rb | 57 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/create_spec.rb | 176 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/delete_spec.rb | 7 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/initialize_spec.rb | 46 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/length_spec.rb | 7 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/open_spec.rb | 97 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/path_spec.rb | 26 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/shared/length.rb | 21 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/shared/unlink.rb | 12 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/size_spec.rb | 7 | ||||
| -rw-r--r-- | spec/ruby/library/tempfile/unlink_spec.rb | 7 |
12 files changed, 484 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..344b08dc17 --- /dev/null +++ b/spec/ruby/library/tempfile/_close_spec.rb @@ -0,0 +1,21 @@ +require_relative '../../spec_helper' +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.protected_instance_methods(false).should.include?(:_close) + end + + it "closes self" do + @tempfile.send(:_close) + @tempfile.closed?.should == true + end +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..7e95ae1d7e --- /dev/null +++ b/spec/ruby/library/tempfile/close_spec.rb @@ -0,0 +1,57 @@ +require_relative '../../spec_helper' +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 == true + end + + it "does not unlink self" do + path = @tempfile.path + @tempfile.close + File.should.exist?(path) + 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 == true + end + + it "unlinks self" do + path = @tempfile.path + @tempfile.close(true) + File.should_not.exist?(path) + end +end + +describe "Tempfile#close!" do + before :each do + @tempfile = Tempfile.new("specs", tmp("")) + end + + it "closes self" do + @tempfile.close! + @tempfile.closed?.should == true + end + + it "unlinks self" do + path = @tempfile.path + @tempfile.close! + 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 new file mode 100644 index 0000000000..0332b44dde --- /dev/null +++ b/spec/ruby/library/tempfile/delete_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative 'shared/unlink' +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..0e882a3f0c --- /dev/null +++ b/spec/ruby/library/tempfile/initialize_spec.rb @@ -0,0 +1,46 @@ +require_relative '../../spec_helper' +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.should.exist?(@tempfile.path) + + 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 permissions 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 + + 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 new file mode 100644 index 0000000000..bc622b9a70 --- /dev/null +++ b/spec/ruby/library/tempfile/length_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative 'shared/length' +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..0993a2c5ee --- /dev/null +++ b/spec/ruby/library/tempfile/open_spec.rb @@ -0,0 +1,97 @@ +require_relative '../../spec_helper' +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 == 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.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 + 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.instance_of?(Tempfile) + tempfile.instance_of?(Tempfile).should == true + tempfile.closed?.should == 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 == 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..be56bd87c8 --- /dev/null +++ b/spec/ruby/library/tempfile/path_spec.rb @@ -0,0 +1,26 @@ +require_relative '../../spec_helper' +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..1a89ff7b4d --- /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..e821228d70 --- /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.should_not.exist?(path) + 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..f4824601c7 --- /dev/null +++ b/spec/ruby/library/tempfile/size_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative 'shared/length' +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..eac7df8472 --- /dev/null +++ b/spec/ruby/library/tempfile/unlink_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require_relative 'shared/unlink' +require 'tempfile' + +describe "Tempfile#unlink" do + it_behaves_like :tempfile_unlink, :unlink +end |
