diff options
Diffstat (limited to 'spec/ruby/core/io')
95 files changed, 3950 insertions, 856 deletions
diff --git a/spec/ruby/core/io/advise_spec.rb b/spec/ruby/core/io/advise_spec.rb index 81d5a49849..651fc52378 100644 --- a/spec/ruby/core/io/advise_spec.rb +++ b/spec/ruby/core/io/advise_spec.rb @@ -12,37 +12,37 @@ describe "IO#advise" do end it "raises a TypeError if advise is not a Symbol" do - lambda { + -> { @io.advise("normal") }.should raise_error(TypeError) end it "raises a TypeError if offset cannot be coerced to an Integer" do - lambda { + -> { @io.advise(:normal, "wat") }.should raise_error(TypeError) end it "raises a TypeError if len cannot be coerced to an Integer" do - lambda { + -> { @io.advise(:normal, 0, "wat") }.should raise_error(TypeError) end it "raises a RangeError if offset is too big" do - lambda { + -> { @io.advise(:normal, 10 ** 32) }.should raise_error(RangeError) end it "raises a RangeError if len is too big" do - lambda { + -> { @io.advise(:normal, 0, 10 ** 32) }.should raise_error(RangeError) end it "raises a NotImplementedError if advise is not recognized" do - lambda{ + ->{ @io.advise(:foo) }.should raise_error(NotImplementedError) end @@ -73,25 +73,14 @@ describe "IO#advise" do end end - platform_is :linux do + guard -> { platform_is :linux and kernel_version_is '3.6' } do # [ruby-core:65355] tmpfs is not supported it "supports the willneed advice type" do - require 'etc' - uname = if Etc.respond_to?(:uname) - Etc.uname[:release] - else - `uname -r`.chomp - end - if (uname.split('.').map(&:to_i) <=> [3,6]) < 0 - # [ruby-core:65355] tmpfs is not supported - 1.should == 1 - else - @io.advise(:willneed).should be_nil - end + @io.advise(:willneed).should be_nil end end it "raises an IOError if the stream is closed" do @io.close - lambda { @io.advise(:normal) }.should raise_error(IOError) + -> { @io.advise(:normal) }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/autoclose_spec.rb b/spec/ruby/core/io/autoclose_spec.rb new file mode 100644 index 0000000000..715ada7c93 --- /dev/null +++ b/spec/ruby/core/io/autoclose_spec.rb @@ -0,0 +1,77 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' + +describe "IO#autoclose?" do + before :each do + @io = IOSpecs.io_fixture "lines.txt" + end + + after :each do + @io.autoclose = true unless @io.closed? + @io.close unless @io.closed? + end + + it "is set to true by default" do + @io.should.autoclose? + end + + it "cannot be queried on a closed IO object" do + @io.close + -> { @io.autoclose? }.should raise_error(IOError, /closed stream/) + end +end + +describe "IO#autoclose=" do + before :each do + @io = IOSpecs.io_fixture "lines.txt" + end + + after :each do + @io.autoclose = true unless @io.closed? + @io.close unless @io.closed? + end + + it "can be set to true" do + @io.autoclose = false + @io.autoclose = true + @io.should.autoclose? + end + + it "can be set to false" do + @io.autoclose = true + @io.autoclose = false + @io.should_not.autoclose? + end + + it "can be set to any truthy value" do + @io.autoclose = false + @io.autoclose = 42 + @io.should.autoclose? + + @io.autoclose = false + @io.autoclose = Object.new + @io.should.autoclose? + end + + it "can be set to any falsy value" do + @io.autoclose = true + @io.autoclose = nil + @io.should_not.autoclose? + end + + it "can be set multiple times" do + @io.autoclose = true + @io.should.autoclose? + + @io.autoclose = false + @io.should_not.autoclose? + + @io.autoclose = true + @io.should.autoclose? + end + + it "cannot be set on a closed IO object" do + @io.close + -> { @io.autoclose = false }.should raise_error(IOError, /closed stream/) + end +end diff --git a/spec/ruby/core/io/binmode_spec.rb b/spec/ruby/core/io/binmode_spec.rb index cc10e297dd..342cac2a9b 100644 --- a/spec/ruby/core/io/binmode_spec.rb +++ b/spec/ruby/core/io/binmode_spec.rb @@ -17,7 +17,7 @@ describe "IO#binmode" do end it "raises an IOError on closed stream" do - lambda { IOSpecs.closed_io.binmode }.should raise_error(IOError) + -> { IOSpecs.closed_io.binmode }.should raise_error(IOError) end it "sets external encoding to binary" do @@ -57,4 +57,8 @@ describe "IO#binmode?" do @duped = @file.dup @duped.binmode?.should == @file.binmode? end + + it "raises an IOError on closed stream" do + -> { IOSpecs.closed_io.binmode? }.should raise_error(IOError) + end end diff --git a/spec/ruby/core/io/binread_spec.rb b/spec/ruby/core/io/binread_spec.rb index 961044da58..9e36b84da9 100644 --- a/spec/ruby/core/io/binread_spec.rb +++ b/spec/ruby/core/io/binread_spec.rb @@ -38,10 +38,20 @@ describe "IO.binread" do end it "raises an ArgumentError when not passed a valid length" do - lambda { IO.binread @fname, -1 }.should raise_error(ArgumentError) + -> { IO.binread @fname, -1 }.should raise_error(ArgumentError) end it "raises an Errno::EINVAL when not passed a valid offset" do - lambda { IO.binread @fname, 0, -1 }.should raise_error(Errno::EINVAL) + -> { IO.binread @fname, 0, -1 }.should raise_error(Errno::EINVAL) + end + + ruby_version_is "3.3"..."4.0" do + # https://bugs.ruby-lang.org/issues/19630 + it "warns about deprecation given a path with a pipe" do + cmd = "|echo ok" + -> { + IO.binread(cmd) + }.should complain(/IO process creation with a leading '\|'/) + end end end diff --git a/spec/ruby/core/io/buffer/empty_spec.rb b/spec/ruby/core/io/buffer/empty_spec.rb new file mode 100644 index 0000000000..e1fd4ab6a2 --- /dev/null +++ b/spec/ruby/core/io/buffer/empty_spec.rb @@ -0,0 +1,29 @@ +require_relative '../../../spec_helper' +require_relative 'shared/null_and_empty' + +describe "IO::Buffer#empty?" do + after :each do + @buffer&.free + @buffer = nil + end + + it_behaves_like :io_buffer_null_and_empty, :empty? + + it "is true for a 0-length String-backed buffer created with .for" do + @buffer = IO::Buffer.for("") + @buffer.empty?.should be_true + end + + ruby_version_is "3.3" do + it "is true for a 0-length String-backed buffer created with .string" do + IO::Buffer.string(0) do |buffer| + buffer.empty?.should be_true + end + end + end + + it "is true for a 0-length slice of a buffer with size > 0" do + @buffer = IO::Buffer.new(4) + @buffer.slice(3, 0).empty?.should be_true + end +end diff --git a/spec/ruby/core/io/buffer/external_spec.rb b/spec/ruby/core/io/buffer/external_spec.rb new file mode 100644 index 0000000000..4377a38357 --- /dev/null +++ b/spec/ruby/core/io/buffer/external_spec.rb @@ -0,0 +1,108 @@ +require_relative '../../../spec_helper' + +describe "IO::Buffer#external?" do + after :each do + @buffer&.free + @buffer = nil + end + + context "with a buffer created with .new" do + it "is false for an internal buffer" do + @buffer = IO::Buffer.new(4) + @buffer.external?.should be_false + end + + it "is false for a mapped buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + @buffer.external?.should be_false + end + end + + context "with a file-backed buffer created with .map" do + it "is true for a regular mapping" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.external?.should be_true + end + end + + ruby_version_is "3.3" do + it "is false for a private mapping" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY | IO::Buffer::PRIVATE) + @buffer.external?.should be_false + end + end + end + end + + context "with a String-backed buffer created with .for" do + it "is true for a buffer created without a block" do + @buffer = IO::Buffer.for("test") + @buffer.external?.should be_true + end + + it "is true for a buffer created with a block" do + IO::Buffer.for(+"test") do |buffer| + buffer.external?.should be_true + end + end + end + + ruby_version_is "3.3" do + context "with a String-backed buffer created with .string" do + it "is true" do + IO::Buffer.string(4) do |buffer| + buffer.external?.should be_true + end + end + end + end + + # Always false for slices + context "with a slice of a buffer" do + context "created with .new" do + it "is false when slicing an internal buffer" do + @buffer = IO::Buffer.new(4) + @buffer.slice.external?.should be_false + end + + it "is false when slicing a mapped buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + @buffer.slice.external?.should be_false + end + end + + context "created with .map" do + it "is false" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.slice.external?.should be_false + end + end + end + + context "created with .for" do + it "is false when slicing a buffer created without a block" do + @buffer = IO::Buffer.for("test") + @buffer.slice.external?.should be_false + end + + it "is false when slicing a buffer created with a block" do + IO::Buffer.for(+"test") do |buffer| + buffer.slice.external?.should be_false + end + end + end + + ruby_version_is "3.3" do + context "created with .string" do + it "is false" do + IO::Buffer.string(4) do |buffer| + buffer.slice.external?.should be_false + end + end + end + end + end +end diff --git a/spec/ruby/core/io/buffer/free_spec.rb b/spec/ruby/core/io/buffer/free_spec.rb new file mode 100644 index 0000000000..f3a4918978 --- /dev/null +++ b/spec/ruby/core/io/buffer/free_spec.rb @@ -0,0 +1,104 @@ +require_relative '../../../spec_helper' + +describe "IO::Buffer#free" do + context "with a buffer created with .new" do + it "frees internal memory and nullifies the buffer" do + buffer = IO::Buffer.new(4) + buffer.free + buffer.null?.should be_true + end + + it "frees mapped memory and nullifies the buffer" do + buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + buffer.free + buffer.null?.should be_true + end + end + + context "with a file-backed buffer created with .map" do + it "frees mapped memory and nullifies the buffer" do + File.open(__FILE__, "r") do |file| + buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + buffer.free + buffer.null?.should be_true + end + end + end + + context "with a String-backed buffer created with .for" do + context "without a block" do + it "disassociates the buffer from the string and nullifies the buffer" do + string = +"test" + buffer = IO::Buffer.for(string) + # Read-only buffer, can't modify the string. + buffer.free + buffer.null?.should be_true + end + end + + context "with a block" do + it "disassociates the buffer from the string and nullifies the buffer" do + string = +"test" + IO::Buffer.for(string) do |buffer| + buffer.set_string("meat") + buffer.free + buffer.null?.should be_true + end + string.should == "meat" + end + end + end + + ruby_version_is "3.3" do + context "with a String-backed buffer created with .string" do + it "disassociates the buffer from the string and nullifies the buffer" do + string = + IO::Buffer.string(4) do |buffer| + buffer.set_string("meat") + buffer.free + buffer.null?.should be_true + end + string.should == "meat" + end + end + end + + it "can be called repeatedly without an error" do + buffer = IO::Buffer.new(4) + buffer.free + buffer.null?.should be_true + buffer.free + buffer.null?.should be_true + end + + it "is disallowed while locked, raising IO::Buffer::LockedError" do + buffer = IO::Buffer.new(4) + buffer.locked do + -> { buffer.free }.should raise_error(IO::Buffer::LockedError, "Buffer is locked!") + end + buffer.free + buffer.null?.should be_true + end + + context "with a slice of a buffer" do + it "nullifies the slice, not touching the buffer" do + buffer = IO::Buffer.new(4) + slice = buffer.slice(0, 2) + + slice.free + slice.null?.should be_true + buffer.null?.should be_false + + buffer.free + end + + it "nullifies buffer, invalidating the slice" do + buffer = IO::Buffer.new(4) + slice = buffer.slice(0, 2) + + buffer.free + slice.null?.should be_false + slice.valid?.should be_false + end + end +end diff --git a/spec/ruby/core/io/buffer/initialize_spec.rb b/spec/ruby/core/io/buffer/initialize_spec.rb new file mode 100644 index 0000000000..c86d1e7f1d --- /dev/null +++ b/spec/ruby/core/io/buffer/initialize_spec.rb @@ -0,0 +1,103 @@ +require_relative '../../../spec_helper' + +describe "IO::Buffer#initialize" do + after :each do + @buffer&.free + @buffer = nil + end + + it "creates a new zero-filled buffer with default size" do + @buffer = IO::Buffer.new + @buffer.size.should == IO::Buffer::DEFAULT_SIZE + @buffer.each(:U8).should.all? { |_offset, value| value.eql?(0) } + end + + it "creates a buffer with default state" do + @buffer = IO::Buffer.new + @buffer.should_not.shared? + @buffer.should_not.readonly? + + @buffer.should_not.empty? + @buffer.should_not.null? + + # This is run-time state, set by #locked. + @buffer.should_not.locked? + end + + context "with size argument" do + it "creates a new internal buffer if size is less than IO::Buffer::PAGE_SIZE" do + size = IO::Buffer::PAGE_SIZE - 1 + @buffer = IO::Buffer.new(size) + @buffer.size.should == size + @buffer.should.internal? + @buffer.should_not.mapped? + @buffer.should_not.empty? + end + + it "creates a new mapped buffer if size is greater than or equal to IO::Buffer::PAGE_SIZE" do + size = IO::Buffer::PAGE_SIZE + @buffer = IO::Buffer.new(size) + @buffer.size.should == size + @buffer.should_not.internal? + @buffer.should.mapped? + @buffer.should_not.empty? + end + + it "creates a null buffer if size is 0" do + @buffer = IO::Buffer.new(0) + @buffer.size.should.zero? + @buffer.should_not.internal? + @buffer.should_not.mapped? + @buffer.should.null? + @buffer.should.empty? + end + + it "raises TypeError if size is not an Integer" do + -> { IO::Buffer.new(nil) }.should raise_error(TypeError, "not an Integer") + -> { IO::Buffer.new(10.0) }.should raise_error(TypeError, "not an Integer") + end + + it "raises ArgumentError if size is negative" do + -> { IO::Buffer.new(-1) }.should raise_error(ArgumentError, "Size can't be negative!") + end + end + + context "with size and flags arguments" do + it "forces mapped buffer with IO::Buffer::MAPPED flag" do + @buffer = IO::Buffer.new(IO::Buffer::PAGE_SIZE - 1, IO::Buffer::MAPPED) + @buffer.should.mapped? + @buffer.should_not.internal? + @buffer.should_not.empty? + end + + it "forces internal buffer with IO::Buffer::INTERNAL flag" do + @buffer = IO::Buffer.new(IO::Buffer::PAGE_SIZE, IO::Buffer::INTERNAL) + @buffer.should.internal? + @buffer.should_not.mapped? + @buffer.should_not.empty? + end + + it "raises IO::Buffer::AllocationError if neither IO::Buffer::MAPPED nor IO::Buffer::INTERNAL is given" do + -> { IO::Buffer.new(10, IO::Buffer::READONLY) }.should raise_error(IO::Buffer::AllocationError, "Could not allocate buffer!") + -> { IO::Buffer.new(10, 0) }.should raise_error(IO::Buffer::AllocationError, "Could not allocate buffer!") + end + + ruby_version_is "3.3" do + it "raises ArgumentError if flags is negative" do + -> { IO::Buffer.new(10, -1) }.should raise_error(ArgumentError, "Flags can't be negative!") + end + end + + ruby_version_is ""..."3.3" do + it "raises IO::Buffer::AllocationError with non-Integer flags" do + -> { IO::Buffer.new(10, 0.0) }.should raise_error(IO::Buffer::AllocationError, "Could not allocate buffer!") + end + end + + ruby_version_is "3.3" do + it "raises TypeError with non-Integer flags" do + -> { IO::Buffer.new(10, 0.0) }.should raise_error(TypeError, "not an Integer") + end + end + end +end diff --git a/spec/ruby/core/io/buffer/internal_spec.rb b/spec/ruby/core/io/buffer/internal_spec.rb new file mode 100644 index 0000000000..409699cc3c --- /dev/null +++ b/spec/ruby/core/io/buffer/internal_spec.rb @@ -0,0 +1,108 @@ +require_relative '../../../spec_helper' + +describe "IO::Buffer#internal?" do + after :each do + @buffer&.free + @buffer = nil + end + + context "with a buffer created with .new" do + it "is true for an internal buffer" do + @buffer = IO::Buffer.new(4) + @buffer.internal?.should be_true + end + + it "is false for a mapped buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + @buffer.internal?.should be_false + end + end + + context "with a file-backed buffer created with .map" do + it "is false for a regular mapping" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.internal?.should be_false + end + end + + ruby_version_is "3.3" do + it "is false for a private mapping" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY | IO::Buffer::PRIVATE) + @buffer.internal?.should be_false + end + end + end + end + + context "with a String-backed buffer created with .for" do + it "is false for a buffer created without a block" do + @buffer = IO::Buffer.for("test") + @buffer.internal?.should be_false + end + + it "is false for a buffer created with a block" do + IO::Buffer.for(+"test") do |buffer| + buffer.internal?.should be_false + end + end + end + + ruby_version_is "3.3" do + context "with a String-backed buffer created with .string" do + it "is false" do + IO::Buffer.string(4) do |buffer| + buffer.internal?.should be_false + end + end + end + end + + # Always false for slices + context "with a slice of a buffer" do + context "created with .new" do + it "is false when slicing an internal buffer" do + @buffer = IO::Buffer.new(4) + @buffer.slice.internal?.should be_false + end + + it "is false when slicing a mapped buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + @buffer.slice.internal?.should be_false + end + end + + context "created with .map" do + it "is false" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.slice.internal?.should be_false + end + end + end + + context "created with .for" do + it "is false when slicing a buffer created without a block" do + @buffer = IO::Buffer.for("test") + @buffer.slice.internal?.should be_false + end + + it "is false when slicing a buffer created with a block" do + IO::Buffer.for(+"test") do |buffer| + buffer.slice.internal?.should be_false + end + end + end + + ruby_version_is "3.3" do + context "created with .string" do + it "is false" do + IO::Buffer.string(4) do |buffer| + buffer.slice.internal?.should be_false + end + end + end + end + end +end diff --git a/spec/ruby/core/io/buffer/locked_spec.rb b/spec/ruby/core/io/buffer/locked_spec.rb new file mode 100644 index 0000000000..4ffa569fd2 --- /dev/null +++ b/spec/ruby/core/io/buffer/locked_spec.rb @@ -0,0 +1,75 @@ +require_relative '../../../spec_helper' + +describe "IO::Buffer#locked" do + after :each do + @buffer&.free + @buffer = nil + end + + context "when buffer is locked" do + it "allows reading and writing operations on the buffer" do + @buffer = IO::Buffer.new(4) + @buffer.set_string("test") + @buffer.locked do + @buffer.get_string.should == "test" + @buffer.set_string("meat") + end + @buffer.get_string.should == "meat" + end + + it "disallows operations changing buffer itself, raising IO::Buffer::LockedError" do + @buffer = IO::Buffer.new(4) + @buffer.locked do + # Just an example, each method is responsible for checking the lock state. + -> { @buffer.resize(8) }.should raise_error(IO::Buffer::LockedError) + end + end + end + + it "disallows reentrant locking, raising IO::Buffer::LockedError" do + @buffer = IO::Buffer.new(4) + @buffer.locked do + -> { @buffer.locked {} }.should raise_error(IO::Buffer::LockedError, "Buffer already locked!") + end + end + + it "does not propagate to buffer's slices" do + @buffer = IO::Buffer.new(4) + slice = @buffer.slice(0, 2) + @buffer.locked do + @buffer.locked?.should be_true + slice.locked?.should be_false + slice.locked { slice.locked?.should be_true } + end + end + + it "does not propagate backwards from buffer's slices" do + @buffer = IO::Buffer.new(4) + slice = @buffer.slice(0, 2) + slice.locked do + slice.locked?.should be_true + @buffer.locked?.should be_false + @buffer.locked { @buffer.locked?.should be_true } + end + end +end + +describe "IO::Buffer#locked?" do + after :each do + @buffer&.free + @buffer = nil + end + + it "is false by default" do + @buffer = IO::Buffer.new(4) + @buffer.locked?.should be_false + end + + it "is true only inside of #locked block" do + @buffer = IO::Buffer.new(4) + @buffer.locked do + @buffer.locked?.should be_true + end + @buffer.locked?.should be_false + end +end diff --git a/spec/ruby/core/io/buffer/mapped_spec.rb b/spec/ruby/core/io/buffer/mapped_spec.rb new file mode 100644 index 0000000000..b3610207ff --- /dev/null +++ b/spec/ruby/core/io/buffer/mapped_spec.rb @@ -0,0 +1,108 @@ +require_relative '../../../spec_helper' + +describe "IO::Buffer#mapped?" do + after :each do + @buffer&.free + @buffer = nil + end + + context "with a buffer created with .new" do + it "is false for an internal buffer" do + @buffer = IO::Buffer.new(4) + @buffer.mapped?.should be_false + end + + it "is true for a mapped buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + @buffer.mapped?.should be_true + end + end + + context "with a file-backed buffer created with .map" do + it "is true for a regular mapping" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.mapped?.should be_true + end + end + + ruby_version_is "3.3" do + it "is true for a private mapping" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY | IO::Buffer::PRIVATE) + @buffer.mapped?.should be_true + end + end + end + end + + context "with a String-backed buffer created with .for" do + it "is false for a buffer created without a block" do + @buffer = IO::Buffer.for("test") + @buffer.mapped?.should be_false + end + + it "is false for a buffer created with a block" do + IO::Buffer.for(+"test") do |buffer| + buffer.mapped?.should be_false + end + end + end + + ruby_version_is "3.3" do + context "with a String-backed buffer created with .string" do + it "is false" do + IO::Buffer.string(4) do |buffer| + buffer.mapped?.should be_false + end + end + end + end + + # Always false for slices + context "with a slice of a buffer" do + context "created with .new" do + it "is false when slicing an internal buffer" do + @buffer = IO::Buffer.new(4) + @buffer.slice.mapped?.should be_false + end + + it "is false when slicing a mapped buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + @buffer.slice.mapped?.should be_false + end + end + + context "created with .map" do + it "is false" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.slice.mapped?.should be_false + end + end + end + + context "created with .for" do + it "is false when slicing a buffer created without a block" do + @buffer = IO::Buffer.for("test") + @buffer.slice.mapped?.should be_false + end + + it "is false when slicing a buffer created with a block" do + IO::Buffer.for(+"test") do |buffer| + buffer.slice.mapped?.should be_false + end + end + end + + ruby_version_is "3.3" do + context "created with .string" do + it "is false" do + IO::Buffer.string(4) do |buffer| + buffer.slice.mapped?.should be_false + end + end + end + end + end +end diff --git a/spec/ruby/core/io/buffer/null_spec.rb b/spec/ruby/core/io/buffer/null_spec.rb new file mode 100644 index 0000000000..3fb1144d0e --- /dev/null +++ b/spec/ruby/core/io/buffer/null_spec.rb @@ -0,0 +1,29 @@ +require_relative '../../../spec_helper' +require_relative 'shared/null_and_empty' + +describe "IO::Buffer#null?" do + after :each do + @buffer&.free + @buffer = nil + end + + it_behaves_like :io_buffer_null_and_empty, :null? + + it "is false for a 0-length String-backed buffer created with .for" do + @buffer = IO::Buffer.for("") + @buffer.null?.should be_false + end + + ruby_version_is "3.3" do + it "is false for a 0-length String-backed buffer created with .string" do + IO::Buffer.string(0) do |buffer| + buffer.null?.should be_false + end + end + end + + it "is false for a 0-length slice of a buffer with size > 0" do + @buffer = IO::Buffer.new(4) + @buffer.slice(3, 0).null?.should be_false + end +end diff --git a/spec/ruby/core/io/buffer/private_spec.rb b/spec/ruby/core/io/buffer/private_spec.rb new file mode 100644 index 0000000000..7aa308997b --- /dev/null +++ b/spec/ruby/core/io/buffer/private_spec.rb @@ -0,0 +1,111 @@ +require_relative '../../../spec_helper' + +ruby_version_is "3.3" do + describe "IO::Buffer#private?" do + after :each do + @buffer&.free + @buffer = nil + end + + context "with a buffer created with .new" do + it "is false for an internal buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::INTERNAL) + @buffer.private?.should be_false + end + + it "is false for a mapped buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + @buffer.private?.should be_false + end + end + + context "with a file-backed buffer created with .map" do + it "is false for a regular mapping" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.private?.should be_false + end + end + + it "is true for a private mapping" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY | IO::Buffer::PRIVATE) + @buffer.private?.should be_true + end + end + end + + context "with a String-backed buffer created with .for" do + it "is false for a buffer created without a block" do + @buffer = IO::Buffer.for("test") + @buffer.private?.should be_false + end + + it "is false for a buffer created with a block" do + IO::Buffer.for(+"test") do |buffer| + buffer.private?.should be_false + end + end + end + + context "with a String-backed buffer created with .string" do + it "is false" do + IO::Buffer.string(4) do |buffer| + buffer.private?.should be_false + end + end + end + + # Always false for slices + context "with a slice of a buffer" do + context "created with .new" do + it "is false when slicing an internal buffer" do + @buffer = IO::Buffer.new(4) + @buffer.slice.private?.should be_false + end + + it "is false when slicing a mapped buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + @buffer.slice.private?.should be_false + end + end + + context "created with .map" do + it "is false when slicing a regular file-backed buffer" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.slice.private?.should be_false + end + end + + it "is false when slicing a private file-backed buffer" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY | IO::Buffer::PRIVATE) + @buffer.slice.private?.should be_false + end + end + end + + context "created with .for" do + it "is false when slicing a buffer created without a block" do + @buffer = IO::Buffer.for("test") + @buffer.slice.private?.should be_false + end + + it "is false when slicing a buffer created with a block" do + IO::Buffer.for(+"test") do |buffer| + buffer.slice.private?.should be_false + end + end + end + + context "created with .string" do + it "is false" do + IO::Buffer.string(4) do |buffer| + buffer.slice.private?.should be_false + end + end + end + end + end +end diff --git a/spec/ruby/core/io/buffer/readonly_spec.rb b/spec/ruby/core/io/buffer/readonly_spec.rb new file mode 100644 index 0000000000..0014a876ed --- /dev/null +++ b/spec/ruby/core/io/buffer/readonly_spec.rb @@ -0,0 +1,143 @@ +require_relative '../../../spec_helper' + +describe "IO::Buffer#readonly?" do + after :each do + @buffer&.free + @buffer = nil + end + + context "with a buffer created with .new" do + it "is false for an internal buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::INTERNAL) + @buffer.readonly?.should be_false + end + + it "is false for a mapped buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + @buffer.readonly?.should be_false + end + end + + context "with a file-backed buffer created with .map" do + it "is false for a writable mapping" do + File.open(__FILE__, "r+") do |file| + @buffer = IO::Buffer.map(file) + @buffer.readonly?.should be_false + end + end + + it "is true for a readonly mapping" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.readonly?.should be_true + end + end + + ruby_version_is "3.3" do + it "is false for a private mapping" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::PRIVATE) + @buffer.readonly?.should be_false + end + end + end + end + + context "with a String-backed buffer created with .for" do + it "is true for a buffer created without a block" do + @buffer = IO::Buffer.for(+"test") + @buffer.readonly?.should be_true + end + + it "is false for a buffer created with a block" do + IO::Buffer.for(+"test") do |buffer| + buffer.readonly?.should be_false + end + end + + it "is true for a buffer created with a block from a frozen string" do + IO::Buffer.for(-"test") do |buffer| + buffer.readonly?.should be_true + end + end + end + + ruby_version_is "3.3" do + context "with a String-backed buffer created with .string" do + it "is false" do + IO::Buffer.string(4) do |buffer| + buffer.readonly?.should be_false + end + end + end + end + + # This seems to be the only flag propagated from the source buffer to the slice. + context "with a slice of a buffer" do + context "created with .new" do + it "is false when slicing an internal buffer" do + @buffer = IO::Buffer.new(4) + @buffer.slice.readonly?.should be_false + end + + it "is false when slicing a mapped buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + @buffer.slice.readonly?.should be_false + end + end + + context "created with .map" do + it "is false when slicing a read-write file-backed buffer" do + File.open(__FILE__, "r+") do |file| + @buffer = IO::Buffer.map(file) + @buffer.slice.readonly?.should be_false + end + end + + it "is true when slicing a readonly file-backed buffer" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.slice.readonly?.should be_true + end + end + + ruby_version_is "3.3" do + it "is false when slicing a private file-backed buffer" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::PRIVATE) + @buffer.slice.readonly?.should be_false + end + end + end + end + + context "created with .for" do + it "is true when slicing a buffer created without a block" do + @buffer = IO::Buffer.for(+"test") + @buffer.slice.readonly?.should be_true + end + + it "is false when slicing a buffer created with a block" do + IO::Buffer.for(+"test") do |buffer| + buffer.slice.readonly?.should be_false + end + end + + it "is true when slicing a buffer created with a block from a frozen string" do + IO::Buffer.for(-"test") do |buffer| + buffer.slice.readonly?.should be_true + end + end + end + + ruby_version_is "3.3" do + context "created with .string" do + it "is false" do + IO::Buffer.string(4) do |buffer| + buffer.slice.readonly?.should be_false + end + end + end + end + end +end diff --git a/spec/ruby/core/io/buffer/resize_spec.rb b/spec/ruby/core/io/buffer/resize_spec.rb new file mode 100644 index 0000000000..0da3a23356 --- /dev/null +++ b/spec/ruby/core/io/buffer/resize_spec.rb @@ -0,0 +1,155 @@ +require_relative '../../../spec_helper' + +describe "IO::Buffer#resize" do + after :each do + @buffer&.free + @buffer = nil + end + + context "with a buffer created with .new" do + it "resizes internal buffer, preserving type" do + @buffer = IO::Buffer.new(4) + @buffer.resize(IO::Buffer::PAGE_SIZE) + @buffer.size.should == IO::Buffer::PAGE_SIZE + @buffer.internal?.should be_true + @buffer.mapped?.should be_false + end + + platform_is :linux do + it "resizes mapped buffer, preserving type" do + @buffer = IO::Buffer.new(IO::Buffer::PAGE_SIZE, IO::Buffer::MAPPED) + @buffer.resize(4) + @buffer.size.should == 4 + @buffer.internal?.should be_false + @buffer.mapped?.should be_true + end + end + + platform_is_not :linux do + it "resizes mapped buffer, changing type to internal" do + @buffer = IO::Buffer.new(IO::Buffer::PAGE_SIZE, IO::Buffer::MAPPED) + @buffer.resize(4) + @buffer.size.should == 4 + @buffer.internal?.should be_true + @buffer.mapped?.should be_false + end + end + end + + context "with a file-backed buffer created with .map" do + it "disallows resizing shared buffer, raising IO::Buffer::AccessError" do + File.open(__FILE__, "r+") do |file| + @buffer = IO::Buffer.map(file) + -> { @buffer.resize(10) }.should raise_error(IO::Buffer::AccessError, "Cannot resize external buffer!") + end + end + + ruby_version_is "3.3" do + it "resizes private buffer, discarding excess contents" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::PRIVATE) + @buffer.resize(10) + @buffer.size.should == 10 + @buffer.get_string.should == "require_re" + @buffer.resize(12) + @buffer.size.should == 12 + @buffer.get_string.should == "require_re\0\0" + end + end + end + end + + context "with a String-backed buffer created with .for" do + context "without a block" do + it "disallows resizing, raising IO::Buffer::AccessError" do + @buffer = IO::Buffer.for(+"test") + -> { @buffer.resize(10) }.should raise_error(IO::Buffer::AccessError, "Cannot resize external buffer!") + end + end + + context "with a block" do + it "disallows resizing, raising IO::Buffer::AccessError" do + IO::Buffer.for(+'test') do |buffer| + -> { buffer.resize(10) }.should raise_error(IO::Buffer::AccessError, "Cannot resize external buffer!") + end + end + end + end + + ruby_version_is "3.3" do + context "with a String-backed buffer created with .string" do + it "disallows resizing, raising IO::Buffer::AccessError" do + IO::Buffer.string(4) do |buffer| + -> { buffer.resize(10) }.should raise_error(IO::Buffer::AccessError, "Cannot resize external buffer!") + end + end + end + end + + context "with a null buffer" do + it "allows resizing a 0-sized buffer, creating a regular buffer according to new size" do + @buffer = IO::Buffer.new(0) + @buffer.resize(IO::Buffer::PAGE_SIZE) + @buffer.size.should == IO::Buffer::PAGE_SIZE + @buffer.internal?.should be_false + @buffer.mapped?.should be_true + end + + it "allows resizing after a free, creating a regular buffer according to new size" do + @buffer = IO::Buffer.for("test") + @buffer.free + @buffer.resize(10) + @buffer.size.should == 10 + @buffer.internal?.should be_true + @buffer.mapped?.should be_false + end + end + + it "allows resizing to 0, freeing memory" do + @buffer = IO::Buffer.new(4) + @buffer.resize(0) + @buffer.null?.should be_true + end + + it "can be called repeatedly" do + @buffer = IO::Buffer.new(4) + @buffer.resize(10) + @buffer.resize(27) + @buffer.resize(1) + @buffer.size.should == 1 + end + + it "always clears extra memory" do + @buffer = IO::Buffer.new(4) + @buffer.set_string("test") + # This should not cause a re-allocation, just a technical resizing, + # even with very aggressive memory allocation. + @buffer.resize(2) + @buffer.resize(4) + @buffer.get_string.should == "te\0\0" + end + + it "is disallowed while locked, raising IO::Buffer::LockedError" do + @buffer = IO::Buffer.new(4) + @buffer.locked do + -> { @buffer.resize(10) }.should raise_error(IO::Buffer::LockedError, "Cannot resize locked buffer!") + end + end + + it "raises ArgumentError if size is negative" do + @buffer = IO::Buffer.new(4) + -> { @buffer.resize(-1) }.should raise_error(ArgumentError, "Size can't be negative!") + end + + it "raises TypeError if size is not an Integer" do + @buffer = IO::Buffer.new(4) + -> { @buffer.resize(nil) }.should raise_error(TypeError, "not an Integer") + -> { @buffer.resize(10.0) }.should raise_error(TypeError, "not an Integer") + end + + context "with a slice of a buffer" do + # Current behavior of slice resizing seems unintended (it's undocumented, too). + # It either creates a completely new buffer, or breaks the slice on size 0. + it "needs to be reviewed for spec completeness" + end +end diff --git a/spec/ruby/core/io/buffer/shared/null_and_empty.rb b/spec/ruby/core/io/buffer/shared/null_and_empty.rb new file mode 100644 index 0000000000..c8fe9e5e46 --- /dev/null +++ b/spec/ruby/core/io/buffer/shared/null_and_empty.rb @@ -0,0 +1,59 @@ +describe :io_buffer_null_and_empty, shared: true do + it "is false for a buffer with size > 0" do + @buffer = IO::Buffer.new(1) + @buffer.send(@method).should be_false + end + + it "is false for a slice with length > 0" do + @buffer = IO::Buffer.new(4) + @buffer.slice(1, 2).send(@method).should be_false + end + + it "is false for a file-mapped buffer" do + File.open(__FILE__, "rb") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.send(@method).should be_false + end + end + + it "is false for a non-empty String-backed buffer created with .for" do + @buffer = IO::Buffer.for("test") + @buffer.send(@method).should be_false + end + + ruby_version_is "3.3" do + it "is false for a non-empty String-backed buffer created with .string" do + IO::Buffer.string(4) do |buffer| + buffer.send(@method).should be_false + end + end + end + + it "is true for a 0-sized buffer" do + @buffer = IO::Buffer.new(0) + @buffer.send(@method).should be_true + end + + it "is true for a slice of a 0-sized buffer" do + @buffer = IO::Buffer.new(0) + @buffer.slice(0, 0).send(@method).should be_true + end + + it "is true for a freed buffer" do + @buffer = IO::Buffer.new(1) + @buffer.free + @buffer.send(@method).should be_true + end + + it "is true for a buffer resized to 0" do + @buffer = IO::Buffer.new(1) + @buffer.resize(0) + @buffer.send(@method).should be_true + end + + it "is true for a buffer whose memory was transferred" do + buffer = IO::Buffer.new(1) + @buffer = buffer.transfer + buffer.send(@method).should be_true + end +end diff --git a/spec/ruby/core/io/buffer/shared_spec.rb b/spec/ruby/core/io/buffer/shared_spec.rb new file mode 100644 index 0000000000..f2a638cf39 --- /dev/null +++ b/spec/ruby/core/io/buffer/shared_spec.rb @@ -0,0 +1,117 @@ +require_relative '../../../spec_helper' + +describe "IO::Buffer#shared?" do + after :each do + @buffer&.free + @buffer = nil + end + + context "with a buffer created with .new" do + it "is false for an internal buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::INTERNAL) + @buffer.shared?.should be_false + end + + it "is false for a mapped buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + @buffer.shared?.should be_false + end + end + + context "with a file-backed buffer created with .map" do + it "is true for a regular mapping" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.shared?.should be_true + end + end + + ruby_version_is "3.3" do + it "is false for a private mapping" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY | IO::Buffer::PRIVATE) + @buffer.shared?.should be_false + end + end + end + end + + context "with a String-backed buffer created with .for" do + it "is false for a buffer created without a block" do + @buffer = IO::Buffer.for("test") + @buffer.shared?.should be_false + end + + it "is false for a buffer created with a block" do + IO::Buffer.for(+"test") do |buffer| + buffer.shared?.should be_false + end + end + end + + ruby_version_is "3.3" do + context "with a String-backed buffer created with .string" do + it "is false" do + IO::Buffer.string(4) do |buffer| + buffer.shared?.should be_false + end + end + end + end + + # Always false for slices + context "with a slice of a buffer" do + context "created with .new" do + it "is false when slicing an internal buffer" do + @buffer = IO::Buffer.new(4) + @buffer.slice.shared?.should be_false + end + + it "is false when slicing a mapped buffer" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + @buffer.slice.shared?.should be_false + end + end + + context "created with .map" do + it "is false when slicing a regular file-backed buffer" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.slice.shared?.should be_false + end + end + + ruby_version_is "3.3" do + it "is false when slicing a private file-backed buffer" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY | IO::Buffer::PRIVATE) + @buffer.slice.shared?.should be_false + end + end + end + end + + context "created with .for" do + it "is false when slicing a buffer created without a block" do + @buffer = IO::Buffer.for("test") + @buffer.slice.shared?.should be_false + end + + it "is false when slicing a buffer created with a block" do + IO::Buffer.for(+"test") do |buffer| + buffer.slice.shared?.should be_false + end + end + end + + ruby_version_is "3.3" do + context "created with .string" do + it "is false" do + IO::Buffer.string(4) do |buffer| + buffer.slice.shared?.should be_false + end + end + end + end + end +end diff --git a/spec/ruby/core/io/buffer/transfer_spec.rb b/spec/ruby/core/io/buffer/transfer_spec.rb new file mode 100644 index 0000000000..cb8c843ff2 --- /dev/null +++ b/spec/ruby/core/io/buffer/transfer_spec.rb @@ -0,0 +1,118 @@ +require_relative '../../../spec_helper' + +describe "IO::Buffer#transfer" do + after :each do + @buffer&.free + @buffer = nil + end + + context "with a buffer created with .new" do + it "transfers internal memory to a new buffer, nullifying the original" do + buffer = IO::Buffer.new(4) + info = buffer.to_s + @buffer = buffer.transfer + @buffer.to_s.should == info + buffer.null?.should be_true + end + + it "transfers mapped memory to a new buffer, nullifying the original" do + buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + info = buffer.to_s + @buffer = buffer.transfer + @buffer.to_s.should == info + buffer.null?.should be_true + end + end + + context "with a file-backed buffer created with .map" do + it "transfers mapped memory to a new buffer, nullifying the original" do + File.open(__FILE__, "r") do |file| + buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + info = buffer.to_s + @buffer = buffer.transfer + @buffer.to_s.should == info + buffer.null?.should be_true + end + end + end + + context "with a String-backed buffer created with .for" do + context "without a block" do + it "transfers memory to a new buffer, nullifying the original" do + buffer = IO::Buffer.for("test") + info = buffer.to_s + @buffer = buffer.transfer + @buffer.to_s.should == info + buffer.null?.should be_true + end + end + + context "with a block" do + it "transfers memory to a new buffer, breaking the transaction by nullifying the original" do + IO::Buffer.for(+"test") do |buffer| + info = buffer.to_s + @buffer = buffer.transfer + @buffer.to_s.should == info + buffer.null?.should be_true + end + @buffer.null?.should be_false + end + end + end + + ruby_version_is "3.3" do + context "with a String-backed buffer created with .string" do + it "transfers memory to a new buffer, breaking the transaction by nullifying the original" do + IO::Buffer.string(4) do |buffer| + info = buffer.to_s + @buffer = buffer.transfer + @buffer.to_s.should == info + buffer.null?.should be_true + end + @buffer.null?.should be_false + end + end + end + + it "allows multiple transfers" do + buffer_1 = IO::Buffer.new(4) + buffer_2 = buffer_1.transfer + @buffer = buffer_2.transfer + buffer_1.null?.should be_true + buffer_2.null?.should be_true + @buffer.null?.should be_false + end + + it "is disallowed while locked, raising IO::Buffer::LockedError" do + @buffer = IO::Buffer.new(4) + @buffer.locked do + -> { @buffer.transfer }.should raise_error(IO::Buffer::LockedError, "Cannot transfer ownership of locked buffer!") + end + end + + context "with a slice of a buffer" do + it "transfers source to a new slice, not touching the buffer" do + @buffer = IO::Buffer.new(4) + slice = @buffer.slice(0, 2) + @buffer.set_string("test") + + new_slice = slice.transfer + slice.null?.should be_true + new_slice.null?.should be_false + @buffer.null?.should be_false + + new_slice.set_string("ea") + @buffer.get_string.should == "east" + end + + it "nullifies buffer, invalidating the slice" do + buffer = IO::Buffer.new(4) + slice = buffer.slice(0, 2) + @buffer = buffer.transfer + + slice.null?.should be_false + slice.valid?.should be_false + -> { slice.get_string }.should raise_error(IO::Buffer::InvalidatedError, "Buffer has been invalidated!") + end + end +end diff --git a/spec/ruby/core/io/buffer/valid_spec.rb b/spec/ruby/core/io/buffer/valid_spec.rb new file mode 100644 index 0000000000..680a35ae9a --- /dev/null +++ b/spec/ruby/core/io/buffer/valid_spec.rb @@ -0,0 +1,110 @@ +require_relative '../../../spec_helper' + +describe "IO::Buffer#valid?" do + after :each do + @buffer&.free + @buffer = nil + end + + # Non-slices are always valid + context "with a non-slice buffer" do + it "is true for a regular buffer" do + @buffer = IO::Buffer.new(4) + @buffer.valid?.should be_true + end + + it "is true for a 0-size buffer" do + @buffer = IO::Buffer.new(0) + @buffer.valid?.should be_true + end + + it "is true for a freed buffer" do + @buffer = IO::Buffer.new(4) + @buffer.free + @buffer.valid?.should be_true + end + + it "is true for a freed file-backed buffer" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + @buffer.valid?.should be_true + @buffer.free + @buffer.valid?.should be_true + end + end + + it "is true for a freed string-backed buffer" do + @buffer = IO::Buffer.for("hello") + @buffer.valid?.should be_true + @buffer.free + @buffer.valid?.should be_true + end + end + + # "A buffer becomes invalid if it is a slice of another buffer (or string) + # which has been freed or re-allocated at a different address." + context "with a slice" do + it "is true for a slice of a live buffer" do + @buffer = IO::Buffer.new(4) + slice = @buffer.slice(0, 2) + slice.valid?.should be_true + end + + context "when buffer is resized" do + it "is false when slice becomes outside the buffer" do + @buffer = IO::Buffer.new(4) + slice = @buffer.slice(2, 2) + @buffer.resize(3) + slice.valid?.should be_false + end + + platform_is_not :linux do + # This test does not cause a copy-resize on Linux. + # `#resize` MAY cause the buffer to move, but there is no guarantee. + it "is false when buffer is copied on resize" do + @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED) + slice = @buffer.slice(0, 2) + @buffer.resize(8) + slice.valid?.should be_false + end + end + end + + it "is false for a slice of a transferred buffer" do + buffer = IO::Buffer.new(4) + slice = buffer.slice(0, 2) + @buffer = buffer.transfer + slice.valid?.should be_false + end + + it "is false for a slice of a freed buffer" do + @buffer = IO::Buffer.new(4) + slice = @buffer.slice(0, 2) + @buffer.free + slice.valid?.should be_false + end + + it "is false for a slice of a freed file-backed buffer" do + File.open(__FILE__, "r") do |file| + @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY) + slice = @buffer.slice(0, 2) + slice.valid?.should be_true + @buffer.free + slice.valid?.should be_false + end + end + + it "is true for a slice of a freed string-backed buffer while string is alive" do + @buffer = IO::Buffer.for("alive") + slice = @buffer.slice(0, 2) + slice.valid?.should be_true + @buffer.free + slice.valid?.should be_true + end + + # There probably should be a test with a garbage-collected string, + # but it's not clear how to force that. + + it "needs to be reviewed for spec completeness" + end +end diff --git a/spec/ruby/core/io/bytes_spec.rb b/spec/ruby/core/io/bytes_spec.rb deleted file mode 100644 index 2d2bd950f1..0000000000 --- a/spec/ruby/core/io/bytes_spec.rb +++ /dev/null @@ -1,43 +0,0 @@ -# -*- encoding: utf-8 -*- -require_relative '../../spec_helper' -require_relative 'fixtures/classes' - -describe "IO#bytes" do - before :each do - @io = IOSpecs.io_fixture "lines.txt" - end - - after :each do - @io.close unless @io.closed? - end - - it "returns an enumerator of the next bytes from the stream" do - enum = @io.bytes - enum.should be_an_instance_of(Enumerator) - @io.readline.should == "Voici la ligne une.\n" - enum.first(5).should == [81, 117, 105, 32, 195] - end - - it "yields each byte" do - count = 0 - ScratchPad.record [] - @io.each_byte do |byte| - ScratchPad << byte - break if 4 < count += 1 - end - - ScratchPad.recorded.should == [86, 111, 105, 99, 105] - end - - it "raises an IOError on closed stream" do - enum = IOSpecs.closed_io.bytes - lambda { enum.first }.should raise_error(IOError) - end - - it "raises an IOError on an enumerator for a stream that has been closed" do - enum = @io.bytes - enum.first.should == 86 - @io.close - lambda { enum.first }.should raise_error(IOError) - end -end diff --git a/spec/ruby/core/io/chars_spec.rb b/spec/ruby/core/io/chars_spec.rb deleted file mode 100644 index cd5dbbce4f..0000000000 --- a/spec/ruby/core/io/chars_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -# -*- encoding: utf-8 -*- -require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/chars' - -describe "IO#chars" do - it_behaves_like :io_chars, :chars -end - -describe "IO#chars" do - it_behaves_like :io_chars_empty, :chars -end diff --git a/spec/ruby/core/io/close_on_exec_spec.rb b/spec/ruby/core/io/close_on_exec_spec.rb index d6ba3c3cef..4e89e08d61 100644 --- a/spec/ruby/core/io/close_on_exec_spec.rb +++ b/spec/ruby/core/io/close_on_exec_spec.rb @@ -14,24 +14,24 @@ describe "IO#close_on_exec=" do guard -> { platform_is_not :windows } do it "sets the close-on-exec flag if true" do @io.close_on_exec = true - @io.close_on_exec?.should == true + @io.should.close_on_exec? end it "sets the close-on-exec flag if non-false" do @io.close_on_exec = :true - @io.close_on_exec?.should == true + @io.should.close_on_exec? end it "unsets the close-on-exec flag if false" do @io.close_on_exec = true @io.close_on_exec = false - @io.close_on_exec?.should == false + @io.should_not.close_on_exec? end it "unsets the close-on-exec flag if nil" do @io.close_on_exec = true @io.close_on_exec = nil - @io.close_on_exec?.should == false + @io.should_not.close_on_exec? end it "ensures the IO's file descriptor is closed in exec'ed processes" do @@ -42,11 +42,7 @@ describe "IO#close_on_exec=" do it "raises IOError if called on a closed IO" do @io.close - lambda { @io.close_on_exec = true }.should raise_error(IOError) - end - - it "returns nil" do - @io.send(:close_on_exec=, true).should be_nil + -> { @io.close_on_exec = true }.should raise_error(IOError) end end end @@ -64,17 +60,17 @@ describe "IO#close_on_exec?" do guard -> { platform_is_not :windows } do it "returns true by default" do - @io.close_on_exec?.should == true + @io.should.close_on_exec? end it "returns true if set" do @io.close_on_exec = true - @io.close_on_exec?.should == true + @io.should.close_on_exec? end it "raises IOError if called on a closed IO" do @io.close - lambda { @io.close_on_exec? }.should raise_error(IOError) + -> { @io.close_on_exec? }.should raise_error(IOError) end end end diff --git a/spec/ruby/core/io/close_read_spec.rb b/spec/ruby/core/io/close_read_spec.rb index 9783cb252a..e700e85bd9 100644 --- a/spec/ruby/core/io/close_read_spec.rb +++ b/spec/ruby/core/io/close_read_spec.rb @@ -4,7 +4,8 @@ require_relative 'fixtures/classes' describe "IO#close_read" do before :each do - @io = IO.popen 'cat', "r+" + cmd = platform_is(:windows) ? 'rem' : 'cat' + @io = IO.popen cmd, "r+" @path = tmp('io.close.txt') end @@ -16,7 +17,7 @@ describe "IO#close_read" do it "closes the read end of a duplex I/O stream" do @io.close_read - lambda { @io.read }.should raise_error(IOError) + -> { @io.read }.should raise_error(IOError) end it "does nothing on subsequent invocations" do @@ -28,14 +29,14 @@ describe "IO#close_read" do it "allows subsequent invocation of close" do @io.close_read - lambda { @io.close }.should_not raise_error + -> { @io.close }.should_not raise_error end it "raises an IOError if the stream is writable and not duplexed" do io = File.open @path, 'w' begin - lambda { io.close_read }.should raise_error(IOError) + -> { io.close_read }.should raise_error(IOError) ensure io.close unless io.closed? end @@ -49,7 +50,7 @@ describe "IO#close_read" do io.close_read - io.closed?.should == true + io.should.closed? end it "does nothing on closed stream" do diff --git a/spec/ruby/core/io/close_spec.rb b/spec/ruby/core/io/close_spec.rb index b7aa2276d1..3a44cc8b17 100644 --- a/spec/ruby/core/io/close_spec.rb +++ b/spec/ruby/core/io/close_spec.rb @@ -14,7 +14,7 @@ describe "IO#close" do it "closes the stream" do @io.close - @io.closed?.should == true + @io.should.closed? end it "returns nil" do @@ -23,19 +23,19 @@ describe "IO#close" do it "raises an IOError reading from a closed IO" do @io.close - lambda { @io.read }.should raise_error(IOError) + -> { @io.read }.should raise_error(IOError) end it "raises an IOError writing to a closed IO" do @io.close - lambda { @io.write "data" }.should raise_error(IOError) + -> { @io.write "data" }.should raise_error(IOError) end it 'does not close the stream if autoclose is false' do other_io = IO.new(@io.fileno) other_io.autoclose = false other_io.close - lambda { @io.write "data" }.should_not raise_error(IOError) + -> { @io.write "data" }.should_not raise_error(IOError) end it "does nothing if already closed" do @@ -44,22 +44,43 @@ describe "IO#close" do @io.close.should be_nil end - ruby_version_is '2.5' do - it 'raises an IOError with a clear message' do - read_io, write_io = IO.pipe - going_to_read = false - thread = Thread.new do - lambda do - going_to_read = true - read_io.read - end.should raise_error(IOError, 'stream closed in another thread') - end + it "does not call the #flush method but flushes the stream internally" do + @io.should_not_receive(:flush) + @io.close + @io.should.closed? + end - Thread.pass until going_to_read && thread.stop? - read_io.close - thread.join - write_io.close - end + it 'raises an IOError with a clear message' do + matching_exception = nil + + -> do + IOSpecs::THREAD_CLOSE_RETRIES.times do + read_io, write_io = IO.pipe + going_to_read = false + + thread = Thread.new do + begin + going_to_read = true + read_io.read + rescue IOError => ioe + if ioe.message == IOSpecs::THREAD_CLOSE_ERROR_MESSAGE + matching_exception = ioe + end + # try again + end + end + + # best attempt to ensure the thread is actually blocked on read + Thread.pass until going_to_read && thread.stop? + sleep(0.001) + + read_io.close + thread.join + write_io.close + + matching_exception&.tap {|ex| raise ex} + end + end.should raise_error(IOError, IOSpecs::THREAD_CLOSE_ERROR_MESSAGE) end end @@ -72,7 +93,7 @@ describe "IO#close on an IO.popen stream" do io.close - lambda { io.pid }.should raise_error(IOError) + -> { io.pid }.should raise_error(IOError) end it "sets $?" do diff --git a/spec/ruby/core/io/close_write_spec.rb b/spec/ruby/core/io/close_write_spec.rb index 8643659025..70610a3e9d 100644 --- a/spec/ruby/core/io/close_write_spec.rb +++ b/spec/ruby/core/io/close_write_spec.rb @@ -3,7 +3,8 @@ require_relative 'fixtures/classes' describe "IO#close_write" do before :each do - @io = IO.popen 'cat', 'r+' + cmd = platform_is(:windows) ? 'rem' : 'cat' + @io = IO.popen cmd, 'r+' @path = tmp('io.close.txt') end @@ -15,7 +16,7 @@ describe "IO#close_write" do it "closes the write end of a duplex I/O stream" do @io.close_write - lambda { @io.write "attempt to write" }.should raise_error(IOError) + -> { @io.write "attempt to write" }.should raise_error(IOError) end it "does nothing on subsequent invocations" do @@ -27,14 +28,14 @@ describe "IO#close_write" do it "allows subsequent invocation of close" do @io.close_write - lambda { @io.close }.should_not raise_error + -> { @io.close }.should_not raise_error end it "raises an IOError if the stream is readable and not duplexed" do io = File.open @path, 'w+' begin - lambda { io.close_write }.should raise_error(IOError) + -> { io.close_write }.should raise_error(IOError) ensure io.close unless io.closed? end @@ -45,15 +46,18 @@ describe "IO#close_write" do io.close_write - io.closed?.should == true + io.should.closed? end - it "flushes and closes the write stream" do - @io.puts '12345' + # Windows didn't have command like cat + platform_is_not :windows do + it "flushes and closes the write stream" do + @io.puts '12345' - @io.close_write + @io.close_write - @io.read.should == "12345\n" + @io.read.should == "12345\n" + end end it "does nothing on closed stream" do diff --git a/spec/ruby/core/io/codepoints_spec.rb b/spec/ruby/core/io/codepoints_spec.rb deleted file mode 100644 index 915d99c027..0000000000 --- a/spec/ruby/core/io/codepoints_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -require_relative '../../spec_helper' -require_relative 'fixtures/classes' -require_relative 'shared/codepoints' - -# See redmine #1667 -describe "IO#codepoints" do - it_behaves_like :io_codepoints, :codepoints -end - -describe "IO#codepoints" do - before :each do - @io = IOSpecs.io_fixture "lines.txt" - end - - after :each do - @io.close unless @io.closed? - end - - it "calls the given block" do - r = [] - @io.codepoints { |c| r << c } - r[24].should == 232 - r.last.should == 10 - end -end diff --git a/spec/ruby/core/io/copy_stream_spec.rb b/spec/ruby/core/io/copy_stream_spec.rb index 622be0818b..ffa2ea992c 100644 --- a/spec/ruby/core/io/copy_stream_spec.rb +++ b/spec/ruby/core/io/copy_stream_spec.rb @@ -31,7 +31,7 @@ describe :io_copy_stream_to_file, shared: true do obj = mock("io_copy_stream_to") obj.should_receive(:to_path).and_return(1) - lambda { IO.copy_stream(@object.from, obj) }.should raise_error(TypeError) + -> { IO.copy_stream(@object.from, obj) }.should raise_error(TypeError) end end @@ -69,9 +69,12 @@ describe :io_copy_stream_to_io, shared: true do end it "raises an IOError if the destination IO is not open for writing" do - @to_io.close - @to_io = new_io @to_name, "r" - lambda { IO.copy_stream @object.from, @to_io }.should raise_error(IOError) + to_io = new_io __FILE__, "r" + begin + -> { IO.copy_stream @object.from, to_io }.should raise_error(IOError) + ensure + to_io.close + end end it "does not close the destination IO" do @@ -109,7 +112,8 @@ describe "IO.copy_stream" do end after :each do - rm_r @to_name, @from_bigfile + rm_r @to_name if @to_name + rm_r @from_bigfile end describe "from an IO" do @@ -125,7 +129,7 @@ describe "IO.copy_stream" do it "raises an IOError if the source IO is not open for reading" do @from_io.close @from_io = new_io @from_bigfile, "a" - lambda { IO.copy_stream @from_io, @to_name }.should raise_error(IOError) + -> { IO.copy_stream @from_io, @to_name }.should raise_error(IOError) end it "does not close the source IO" do @@ -164,6 +168,25 @@ describe "IO.copy_stream" do it_behaves_like :io_copy_stream_to_io, nil, IOSpecs::CopyStream it_behaves_like :io_copy_stream_to_io_with_offset, nil, IOSpecs::CopyStream end + + describe "to a Tempfile" do + before :all do + require 'tempfile' + end + + before :each do + @to_io = Tempfile.new("rubyspec_copy_stream", encoding: Encoding::BINARY, mode: File::RDONLY) + @to_name = @to_io.path + end + + after :each do + @to_io.close! + @to_name = nil # do not rm_r it, already done by Tempfile#close! + end + + it_behaves_like :io_copy_stream_to_io, nil, IOSpecs::CopyStream + it_behaves_like :io_copy_stream_to_io_with_offset, nil, IOSpecs::CopyStream + end end describe "from a file name" do @@ -183,7 +206,7 @@ describe "IO.copy_stream" do obj = mock("io_copy_stream_from") obj.should_receive(:to_path).and_return(1) - lambda { IO.copy_stream(obj, @to_name) }.should raise_error(TypeError) + -> { IO.copy_stream(obj, @to_name) }.should raise_error(TypeError) end describe "to a file name" do @@ -222,7 +245,7 @@ describe "IO.copy_stream" do platform_is_not :windows do it "raises an error when an offset is specified" do - lambda { IO.copy_stream(@from_io, @to_name, 8, 4) }.should raise_error(Errno::ESPIPE) + -> { IO.copy_stream(@from_io, @to_name, 8, 4) }.should raise_error(Errno::ESPIPE) end end @@ -277,6 +300,44 @@ describe "IO.copy_stream" do @io.should_not_receive(:pos) IO.copy_stream(@io, @to_name) end + end + + describe "with a destination that does partial reads" do + before do + @from_out, @from_in = IO.pipe + @to_out, @to_in = IO.pipe + end + after do + [@from_out, @from_in, @to_out, @to_in].each {|io| io.close rescue nil} + end + + it "calls #write repeatedly on the destination Object" do + @from_in.write "1234" + @from_in.close + + th = Thread.new do + IO.copy_stream(@from_out, @to_in) + end + + copied = "" + 4.times do + copied += @to_out.read(1) + end + + th.join + + copied.should == "1234" + end + + end +end + +describe "IO.copy_stream" do + it "does not use buffering when writing to STDOUT" do + IO.popen([*ruby_exe, fixture(__FILE__ , "copy_in_out.rb")], "r+") do |io| + io.write("bar") + io.read(3).should == "bar" + end end end diff --git a/spec/ruby/core/io/dup_spec.rb b/spec/ruby/core/io/dup_spec.rb index c88d109ec9..564e007438 100644 --- a/spec/ruby/core/io/dup_spec.rb +++ b/spec/ruby/core/io/dup_spec.rb @@ -25,53 +25,53 @@ describe "IO#dup" do @i.fileno.should_not == @f.fileno end -quarantine! do # This does not appear to be consistent across platforms - it "shares the original stream between the two IOs" do - start = @f.pos - @i.pos.should == start + quarantine! do # This does not appear to be consistent across platforms + it "shares the original stream between the two IOs" do + start = @f.pos + @i.pos.should == start - s = "Hello, wo.. wait, where am I?\n" - s2 = "<evil voice> Muhahahaa!" + s = "Hello, wo.. wait, where am I?\n" + s2 = "<evil voice> Muhahahaa!" - @f.write s - @i.pos.should == @f.pos + @f.write s + @i.pos.should == @f.pos - @i.rewind - @i.gets.should == s + @i.rewind + @i.gets.should == s - @i.rewind - @i.write s2 + @i.rewind + @i.write s2 - @f.rewind - @f.gets.should == "#{s2}\n" + @f.rewind + @f.gets.should == "#{s2}\n" + end end -end it "allows closing the new IO without affecting the original" do @i.close - lambda { @f.gets }.should_not raise_error(Exception) + -> { @f.gets }.should_not raise_error(Exception) - @i.closed?.should == true - @f.closed?.should == false + @i.should.closed? + @f.should_not.closed? end it "allows closing the original IO without affecting the new one" do @f.close - lambda { @i.gets }.should_not raise_error(Exception) + -> { @i.gets }.should_not raise_error(Exception) - @i.closed?.should == false - @f.closed?.should == true + @i.should_not.closed? + @f.should.closed? end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.dup }.should raise_error(IOError) + -> { IOSpecs.closed_io.dup }.should raise_error(IOError) end it "always sets the close-on-exec flag for the new IO object" do @f.close_on_exec = true dup = @f.dup begin - dup.close_on_exec?.should == true + dup.should.close_on_exec? ensure dup.close end @@ -79,9 +79,28 @@ end @f.close_on_exec = false dup = @f.dup begin - dup.close_on_exec?.should == true + dup.should.close_on_exec? + ensure + dup.close + end + end + + it "always sets the autoclose flag for the new IO object" do + @f.autoclose = true + dup = @f.dup + begin + dup.should.autoclose? + ensure + dup.close + end + + @f.autoclose = false + dup = @f.dup + begin + dup.should.autoclose? ensure dup.close + @f.autoclose = true end end end diff --git a/spec/ruby/core/io/each_byte_spec.rb b/spec/ruby/core/io/each_byte_spec.rb index 9cdb1ac0c9..ea618e8c0c 100644 --- a/spec/ruby/core/io/each_byte_spec.rb +++ b/spec/ruby/core/io/each_byte_spec.rb @@ -12,7 +12,7 @@ describe "IO#each_byte" do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.each_byte {} }.should raise_error(IOError) + -> { IOSpecs.closed_io.each_byte {} }.should raise_error(IOError) end it "yields each byte" do diff --git a/spec/ruby/core/io/each_codepoint_spec.rb b/spec/ruby/core/io/each_codepoint_spec.rb index 19824c38e4..07a4037c8a 100644 --- a/spec/ruby/core/io/each_codepoint_spec.rb +++ b/spec/ruby/core/io/each_codepoint_spec.rb @@ -4,7 +4,7 @@ require_relative 'shared/codepoints' # See redmine #1667 describe "IO#each_codepoint" do - it_behaves_like :io_codepoints, :codepoints + it_behaves_like :io_codepoints, :each_codepoint end describe "IO#each_codepoint" do @@ -38,6 +38,6 @@ describe "IO#each_codepoint" do end it "raises an exception at incomplete character before EOF when conversion takes place" do - lambda { @io.each_codepoint {} }.should raise_error(ArgumentError) + -> { @io.each_codepoint {} }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/core/io/eof_spec.rb b/spec/ruby/core/io/eof_spec.rb index 3ab389af09..b4850df437 100644 --- a/spec/ruby/core/io/eof_spec.rb +++ b/spec/ruby/core/io/eof_spec.rb @@ -12,11 +12,11 @@ describe "IO#eof?" do end it "returns true on an empty stream that has just been opened" do - File.open(@name) { |empty| empty.eof?.should == true } + File.open(@name) { |empty| empty.should.eof? } end it "raises IOError on stream not opened for reading" do - lambda do + -> do File.open(@name, "w") { |f| f.eof? } end.should raise_error(IOError) end @@ -34,51 +34,51 @@ describe "IO#eof?" do it "returns false when not at end of file" do @io.read 1 - @io.eof?.should == false + @io.should_not.eof? end it "returns true after reading with read with no parameters" do @io.read() - @io.eof?.should == true + @io.should.eof? end it "returns true after reading with read" do @io.read(File.size(@name)) - @io.eof?.should == true + @io.should.eof? end it "returns true after reading with sysread" do @io.sysread(File.size(@name)) - @io.eof?.should == true + @io.should.eof? end it "returns true after reading with readlines" do @io.readlines - @io.eof?.should == true + @io.should.eof? end it "returns false on just opened non-empty stream" do - @io.eof?.should == false + @io.should_not.eof? end it "does not consume the data from the stream" do - @io.eof?.should == false + @io.should_not.eof? @io.getc.should == 'V' end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.eof? }.should raise_error(IOError) + -> { IOSpecs.closed_io.eof? }.should raise_error(IOError) end it "raises IOError on stream closed for reading by close_read" do @io.close_read - lambda { @io.eof? }.should raise_error(IOError) + -> { @io.eof? }.should raise_error(IOError) end it "returns true on one-byte stream after single-byte read" do - File.open(File.dirname(__FILE__) + '/fixtures/one_byte.txt') { |one_byte| + File.open(__dir__ + '/fixtures/one_byte.txt') { |one_byte| one_byte.read(1) - one_byte.eof?.should == true + one_byte.should.eof? } end end @@ -92,16 +92,16 @@ describe "IO#eof?" do it "returns true on receiving side of Pipe when writing side is closed" do @r, @w = IO.pipe @w.close - @r.eof?.should == true + @r.should.eof? end it "returns false on receiving side of Pipe when writing side wrote some data" do @r, @w = IO.pipe @w.puts "hello" - @r.eof?.should == false + @r.should_not.eof? @w.close - @r.eof?.should == false + @r.should_not.eof? @r.read - @r.eof?.should == true + @r.should.eof? end end diff --git a/spec/ruby/core/io/external_encoding_spec.rb b/spec/ruby/core/io/external_encoding_spec.rb index 9666974647..7765c6c0f5 100644 --- a/spec/ruby/core/io/external_encoding_spec.rb +++ b/spec/ruby/core/io/external_encoding_spec.rb @@ -1,4 +1,5 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe :io_external_encoding_write, shared: true do describe "when Encoding.default_internal is nil" do @@ -93,6 +94,12 @@ describe "IO#external_encoding" do rm_r @name end + it "can be retrieved from a closed stream" do + io = IOSpecs.io_fixture("lines.txt", "r") + io.close + io.external_encoding.should equal(Encoding.default_external) + end + describe "with 'r' mode" do describe "when Encoding.default_internal is nil" do before :each do diff --git a/spec/ruby/core/io/fcntl_spec.rb b/spec/ruby/core/io/fcntl_spec.rb index 049f92c0a2..30b4876fe3 100644 --- a/spec/ruby/core/io/fcntl_spec.rb +++ b/spec/ruby/core/io/fcntl_spec.rb @@ -3,6 +3,6 @@ require_relative 'fixtures/classes' describe "IO#fcntl" do it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.fcntl(5, 5) }.should raise_error(IOError) + -> { IOSpecs.closed_io.fcntl(5, 5) }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/fileno_spec.rb b/spec/ruby/core/io/fileno_spec.rb index d7aff99e72..647609bf42 100644 --- a/spec/ruby/core/io/fileno_spec.rb +++ b/spec/ruby/core/io/fileno_spec.rb @@ -7,6 +7,6 @@ describe "IO#fileno" do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.fileno }.should raise_error(IOError) + -> { IOSpecs.closed_io.fileno }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/fixtures/classes.rb b/spec/ruby/core/io/fixtures/classes.rb index 460dd62387..204a2a101b 100644 --- a/spec/ruby/core/io/fixtures/classes.rb +++ b/spec/ruby/core/io/fixtures/classes.rb @@ -1,9 +1,24 @@ # -*- encoding: utf-8 -*- module IOSpecs + THREAD_CLOSE_RETRIES = 10 + THREAD_CLOSE_ERROR_MESSAGE = 'stream closed in another thread' + class SubIO < IO end + class SubIOWithRedefinedNew < IO + def self.new(...) + ScratchPad << :redefined_new_called + super + end + + def initialize(...) + ScratchPad << :call_original_initialize + super + end + end + def self.collector Proc.new { |x| ScratchPad << x } end @@ -105,6 +120,14 @@ module IOSpecs "linha ", "cinco.\nHere ", "is ", "line ", "six.\n" ] end + def self.lines_space_separator_without_trailing_spaces + [ "Voici", "la", "ligne", "une.\nQui", + "\303\250", "la", "linea", "due.\n\n\nAqu\303\255", + "est\303\241", "la", "l\303\255nea", "tres.\nHier", + "ist", "Zeile", "vier.\n\nEst\303\241", "aqui", "a", + "linha", "cinco.\nHere", "is", "line", "six.\n" ] + end + def self.lines_arbitrary_separator [ "Voici la ligne une.\nQui \303\250", " la linea due.\n\n\nAqu\303\255 est\303\241 la l\303\255nea tres.\nHier ist Zeile vier.\n\nEst\303\241 aqui a linha cinco.\nHere is line six.\n" ] @@ -116,6 +139,12 @@ module IOSpecs "Est\303\241 aqui a linha cinco.\nHere is line six.\n" ] end + def self.paragraphs_without_trailing_new_line_characters + [ "Voici la ligne une.\nQui \303\250 la linea due.", + "Aqu\303\255 est\303\241 la l\303\255nea tres.\nHier ist Zeile vier.", + "Est\303\241 aqui a linha cinco.\nHere is line six.\n" ] + end + # Creates an IO instance for an existing fixture file. The # file should obviously not be deleted. def self.io_fixture(name, mode = "r:utf-8") @@ -164,7 +193,7 @@ module IOSpecs @io = io end - def read(size, buf=nil) + def read(size, buf) @io.read size, buf end @@ -178,7 +207,7 @@ module IOSpecs @io = io end - def readpartial(size, buf=nil) + def readpartial(size, buf) @io.readpartial size, buf end diff --git a/spec/ruby/core/io/fixtures/copy_in_out.rb b/spec/ruby/core/io/fixtures/copy_in_out.rb new file mode 100644 index 0000000000..b9d4085a47 --- /dev/null +++ b/spec/ruby/core/io/fixtures/copy_in_out.rb @@ -0,0 +1,2 @@ +STDOUT.sync = false +IO.copy_stream(STDIN, STDOUT) diff --git a/spec/ruby/core/io/flush_spec.rb b/spec/ruby/core/io/flush_spec.rb index c81ff74a69..f7d5ba77fc 100644 --- a/spec/ruby/core/io/flush_spec.rb +++ b/spec/ruby/core/io/flush_spec.rb @@ -3,6 +3,35 @@ require_relative 'fixtures/classes' describe "IO#flush" do it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.flush }.should raise_error(IOError) + -> { IOSpecs.closed_io.flush }.should raise_error(IOError) + end + + describe "on a pipe" do + before :each do + @r, @w = IO.pipe + end + + after :each do + @r.close + begin + @w.close + rescue Errno::EPIPE + end + end + + # [ruby-core:90895] RJIT worker may leave fd open in a forked child. + # For instance, RJIT creates a worker before @r.close with fork(), @r.close happens, + # and the RJIT worker keeps the pipe open until the worker execve(). + # TODO: consider acquiring GVL from RJIT worker. + guard_not -> { defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled? } do + it "raises Errno::EPIPE if sync=false and the read end is closed" do + @w.sync = false + @w.write "foo" + @r.close + + -> { @w.flush }.should raise_error(Errno::EPIPE, /Broken pipe/) + -> { @w.close }.should raise_error(Errno::EPIPE, /Broken pipe/) + end + end end end diff --git a/spec/ruby/core/io/foreach_spec.rb b/spec/ruby/core/io/foreach_spec.rb index c2276cf544..6abe8901ba 100644 --- a/spec/ruby/core/io/foreach_spec.rb +++ b/spec/ruby/core/io/foreach_spec.rb @@ -14,31 +14,48 @@ describe "IO.foreach" do IO.foreach(@name) { $..should == @count += 1 } end - describe "when the filename starts with |" do - it "gets data from the standard out of the subprocess" do - cmd = "|sh -c 'echo hello;echo line2'" - platform_is :windows do - cmd = "|cmd.exe /C echo hello&echo line2" + ruby_version_is ""..."4.0" do + describe "when the filename starts with |" do + it "gets data from the standard out of the subprocess" do + cmd = "|sh -c 'echo hello;echo line2'" + platform_is :windows do + cmd = "|cmd.exe /C echo hello&echo line2" + end + + suppress_warning do # https://bugs.ruby-lang.org/issues/19630 + IO.foreach(cmd) { |l| ScratchPad << l } + end + ScratchPad.recorded.should == ["hello\n", "line2\n"] end - IO.foreach(cmd) { |l| ScratchPad << l } - ScratchPad.recorded.should == ["hello\n", "line2\n"] - end - platform_is_not :windows do - it "gets data from a fork when passed -" do - parent_pid = $$ + platform_is_not :windows do + it "gets data from a fork when passed -" do + parent_pid = $$ - IO.foreach("|-") { |l| ScratchPad << l } + suppress_warning do # https://bugs.ruby-lang.org/issues/19630 + IO.foreach("|-") { |l| ScratchPad << l } + end - if $$ == parent_pid - ScratchPad.recorded.should == ["hello\n", "from a fork\n"] - else # child - puts "hello" - puts "from a fork" - exit! + if $$ == parent_pid + ScratchPad.recorded.should == ["hello\n", "from a fork\n"] + else # child + puts "hello" + puts "from a fork" + exit! + end end end end + + ruby_version_is "3.3" do + # https://bugs.ruby-lang.org/issues/19630 + it "warns about deprecation given a path with a pipe" do + cmd = "|echo ok" + -> { + IO.foreach(cmd).to_a + }.should complain(/IO process creation with a leading '\|'/) + end + end end end diff --git a/spec/ruby/core/io/fsync_spec.rb b/spec/ruby/core/io/fsync_spec.rb index 0261939631..6e6123de94 100644 --- a/spec/ruby/core/io/fsync_spec.rb +++ b/spec/ruby/core/io/fsync_spec.rb @@ -12,7 +12,7 @@ describe "IO#fsync" do end it "raises an IOError on closed stream" do - lambda { IOSpecs.closed_io.fsync }.should raise_error(IOError) + -> { IOSpecs.closed_io.fsync }.should raise_error(IOError) end it "writes the buffered data to permanent storage" do diff --git a/spec/ruby/core/io/getbyte_spec.rb b/spec/ruby/core/io/getbyte_spec.rb index 6b665029d6..b4351160e6 100644 --- a/spec/ruby/core/io/getbyte_spec.rb +++ b/spec/ruby/core/io/getbyte_spec.rb @@ -23,7 +23,7 @@ describe "IO#getbyte" do end it "raises an IOError on closed stream" do - lambda { IOSpecs.closed_io.getbyte }.should raise_error(IOError) + -> { IOSpecs.closed_io.getbyte }.should raise_error(IOError) end end @@ -40,3 +40,19 @@ describe "IO#getbyte" do @io.getbyte.should == nil end end + +describe "IO#getbyte" do + before :each do + @name = tmp("io_getbyte.txt") + @io = new_io(@name, 'w') + end + + after :each do + @io.close if @io + rm_r @name if @name + end + + it "raises an IOError if the stream is not readable" do + -> { @io.getbyte }.should raise_error(IOError) + end +end diff --git a/spec/ruby/core/io/getc_spec.rb b/spec/ruby/core/io/getc_spec.rb index 7c1c18cd90..3949b5cb28 100644 --- a/spec/ruby/core/io/getc_spec.rb +++ b/spec/ruby/core/io/getc_spec.rb @@ -23,7 +23,7 @@ describe "IO#getc" do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.getc }.should raise_error(IOError) + -> { IOSpecs.closed_io.getc }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/gets_spec.rb b/spec/ruby/core/io/gets_spec.rb index 371c1c3d3b..ca64bf860e 100644 --- a/spec/ruby/core/io/gets_spec.rb +++ b/spec/ruby/core/io/gets_spec.rb @@ -24,13 +24,19 @@ describe "IO#gets" do end end + it "sets $_ to nil after the last line has been read" do + while @io.gets + end + $_.should be_nil + end + it "returns nil if called at the end of the stream" do IOSpecs.lines.length.times { @io.gets } @io.gets.should == nil end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.gets }.should raise_error(IOError) + -> { IOSpecs.closed_io.gets }.should raise_error(IOError) end describe "with no separator" do @@ -38,12 +44,6 @@ describe "IO#gets" do IOSpecs.lines.each { |line| line.should == @io.gets } end - it "returns tainted strings" do - while line = @io.gets - line.tainted?.should == true - end - end - it "updates lineno with each invocation" do while @io.gets @io.lineno.should == @count += 1 @@ -62,12 +62,6 @@ describe "IO#gets" do @io.gets(nil).should == IOSpecs.lines.join("") end - it "returns tainted strings" do - while line = @io.gets(nil) - line.tainted?.should == true - end - end - it "updates lineno with each invocation" do while @io.gets(nil) @io.lineno.should == @count += 1 @@ -96,12 +90,6 @@ describe "IO#gets" do @io.gets.should == IOSpecs.lines[4] end - it "returns tainted strings" do - while line = @io.gets("") - line.tainted?.should == true - end - end - it "updates lineno with each invocation" do while @io.gets("") @io.lineno.should == @count += 1 @@ -120,12 +108,6 @@ describe "IO#gets" do @io.gets("la linea").should == "Voici la ligne une.\nQui \303\250 la linea" end - it "returns tainted strings" do - while line = @io.gets("la") - line.tainted?.should == true - end - end - it "updates lineno with each invocation" do while (@io.gets("la")) @io.lineno.should == @count += 1 @@ -137,12 +119,49 @@ describe "IO#gets" do $..should == @count += 1 end end + + describe "that consists of multiple bytes" do + platform_is_not :windows do + it "should match the separator even if the buffer is filled over successive reads" do + IO.pipe do |read, write| + + # Write part of the string with the separator split between two write calls. We want + # the read to intertwine such that when the read starts the full data isn't yet + # available in the buffer. + write.write("Aquí está la línea tres\r\n") + + t = Thread.new do + # Continue reading until the separator is encountered or the pipe is closed. + read.gets("\r\n\r\n") + end + + # Write the other half of the separator, which should cause the `gets` call to now + # match. Explicitly close the pipe for good measure so a bug in `gets` doesn't block forever. + Thread.pass until t.stop? + + write.write("\r\nelse\r\n\r\n") + write.close + + t.value.bytes.should == "Aquí está la línea tres\r\n\r\n".bytes + read.read(8).bytes.should == "else\r\n\r\n".bytes + end + end + end + end end describe "when passed chomp" do it "returns the first line without a trailing newline character" do @io.gets(chomp: true).should == IOSpecs.lines_without_newline_characters[0] end + + it "raises exception when options passed as Hash" do + -> { @io.gets({ chomp: true }) }.should raise_error(TypeError) + + -> { + @io.gets("\n", 1, { chomp: true }) + }.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 0..2)") + end end end @@ -156,11 +175,11 @@ describe "IO#gets" do end it "raises an IOError if the stream is opened for append only" do - lambda { File.open(@name, "a:utf-8") { |f| f.gets } }.should raise_error(IOError) + -> { File.open(@name, "a:utf-8") { |f| f.gets } }.should raise_error(IOError) end it "raises an IOError if the stream is opened for writing only" do - lambda { File.open(@name, "w:utf-8") { |f| f.gets } }.should raise_error(IOError) + -> { File.open(@name, "w:utf-8") { |f| f.gets } }.should raise_error(IOError) end end @@ -224,6 +243,16 @@ describe "IO#gets" do it "reads all bytes when pass a separator and reading more than all bytes" do @io.gets("\t", 100).should == "one\n\ntwo\n\nthree\nfour\n" end + + it "returns empty string when 0 passed as a limit" do + @io.gets(0).should == "" + @io.gets(nil, 0).should == "" + @io.gets("", 0).should == "" + end + + it "does not accept limit that doesn't fit in a C off_t" do + -> { @io.gets(2**128) }.should raise_error(RangeError) + end end describe "IO#gets" do @@ -309,11 +338,23 @@ describe "IO#gets" do @io.gets.encoding.should == Encoding::BINARY end - it "transcodes to internal encoding if the IO object's external encoding is BINARY" do - Encoding.default_external = Encoding::BINARY - Encoding.default_internal = Encoding::UTF_8 - @io = new_io @name, 'r' - @io.set_encoding Encoding::BINARY, Encoding::UTF_8 - @io.gets.encoding.should == Encoding::UTF_8 + ruby_version_is ''...'3.3' do + it "transcodes to internal encoding if the IO object's external encoding is BINARY" do + Encoding.default_external = Encoding::BINARY + Encoding.default_internal = Encoding::UTF_8 + @io = new_io @name, 'r' + @io.set_encoding Encoding::BINARY, Encoding::UTF_8 + @io.gets.encoding.should == Encoding::UTF_8 + end + end + + ruby_version_is '3.3' do + it "ignores the internal encoding if the IO object's external encoding is BINARY" do + Encoding.default_external = Encoding::BINARY + Encoding.default_internal = Encoding::UTF_8 + @io = new_io @name, 'r' + @io.set_encoding Encoding::BINARY, Encoding::UTF_8 + @io.gets.encoding.should == Encoding::BINARY + end end end diff --git a/spec/ruby/core/io/initialize_spec.rb b/spec/ruby/core/io/initialize_spec.rb index 4858e0360c..026252a13d 100644 --- a/spec/ruby/core/io/initialize_spec.rb +++ b/spec/ruby/core/io/initialize_spec.rb @@ -4,7 +4,7 @@ require_relative 'fixtures/classes' describe "IO#initialize" do before :each do @name = tmp("io_initialize.txt") - @io = new_io @name + @io = IO.new(new_fd(@name)) @fd = @io.fileno end @@ -13,7 +13,7 @@ describe "IO#initialize" do rm_r @name end - it "reassociates the IO instance with the new descriptor when passed a Fixnum" do + it "reassociates the IO instance with the new descriptor when passed an Integer" do fd = new_fd @name, "r:utf-8" @io.send :initialize, fd, 'r' @io.fileno.should == fd @@ -27,23 +27,34 @@ describe "IO#initialize" do @io.fileno.should == fd end + it "accepts options as keyword arguments" do + fd = new_fd @name, "w:utf-8" + + @io.send(:initialize, fd, "w", flags: File::CREAT) + @io.fileno.should == fd + + -> { + @io.send(:initialize, fd, "w", {flags: File::CREAT}) + }.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 1..2)") + end + it "raises a TypeError when passed an IO" do - lambda { @io.send :initialize, STDOUT, 'w' }.should raise_error(TypeError) + -> { @io.send :initialize, STDOUT, 'w' }.should raise_error(TypeError) end it "raises a TypeError when passed nil" do - lambda { @io.send :initialize, nil, 'w' }.should raise_error(TypeError) + -> { @io.send :initialize, nil, 'w' }.should raise_error(TypeError) end it "raises a TypeError when passed a String" do - lambda { @io.send :initialize, "4", 'w' }.should raise_error(TypeError) + -> { @io.send :initialize, "4", 'w' }.should raise_error(TypeError) end it "raises IOError on closed stream" do - lambda { @io.send :initialize, IOSpecs.closed_io.fileno }.should raise_error(IOError) + -> { @io.send :initialize, IOSpecs.closed_io.fileno }.should raise_error(IOError) end it "raises an Errno::EBADF when given an invalid file descriptor" do - lambda { @io.send :initialize, -1, 'w' }.should raise_error(Errno::EBADF) + -> { @io.send :initialize, -1, 'w' }.should raise_error(Errno::EBADF) end end diff --git a/spec/ruby/core/io/internal_encoding_spec.rb b/spec/ruby/core/io/internal_encoding_spec.rb index 10ebf28707..7a583d4bcb 100644 --- a/spec/ruby/core/io/internal_encoding_spec.rb +++ b/spec/ruby/core/io/internal_encoding_spec.rb @@ -1,4 +1,5 @@ require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe :io_internal_encoding, shared: true do describe "when Encoding.default_internal is not set" do @@ -112,6 +113,12 @@ describe "IO#internal_encoding" do Encoding.default_internal = @internal end + it "can be retrieved from a closed stream" do + io = IOSpecs.io_fixture("lines.txt", "r") + io.close + io.internal_encoding.should equal(Encoding.default_internal) + end + describe "with 'r' mode" do it_behaves_like :io_internal_encoding, nil, "r" end diff --git a/spec/ruby/core/io/ioctl_spec.rb b/spec/ruby/core/io/ioctl_spec.rb index 0f2b67ac44..3f7b5ad5d7 100644 --- a/spec/ruby/core/io/ioctl_spec.rb +++ b/spec/ruby/core/io/ioctl_spec.rb @@ -4,7 +4,7 @@ require_relative 'fixtures/classes' describe "IO#ioctl" do platform_is_not :windows do it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.ioctl(5, 5) }.should raise_error(IOError) + -> { IOSpecs.closed_io.ioctl(5, 5) }.should raise_error(IOError) end end @@ -12,7 +12,7 @@ describe "IO#ioctl" do guard -> { RUBY_PLATFORM.include?("86") } do # x86 / x86_64 it "resizes an empty String to match the output size" do File.open(__FILE__, 'r') do |f| - buffer = '' + buffer = +'' # FIONREAD in /usr/include/asm-generic/ioctls.h f.ioctl 0x541B, buffer buffer.unpack('I').first.should be_kind_of(Integer) @@ -20,12 +20,12 @@ describe "IO#ioctl" do end end - it "raises an Errno error when ioctl fails" do + it "raises a system call error when ioctl fails" do File.open(__FILE__, 'r') do |f| - lambda { + -> { # TIOCGWINSZ in /usr/include/asm-generic/ioctls.h f.ioctl 0x5413, nil - }.should raise_error(Errno::ENOTTY) + }.should raise_error(SystemCallError) end end end diff --git a/spec/ruby/core/io/lineno_spec.rb b/spec/ruby/core/io/lineno_spec.rb index 322e60d643..e82cdd9f17 100644 --- a/spec/ruby/core/io/lineno_spec.rb +++ b/spec/ruby/core/io/lineno_spec.rb @@ -11,7 +11,26 @@ describe "IO#lineno" do end it "raises an IOError on a closed stream" do - lambda { IOSpecs.closed_io.lineno }.should raise_error(IOError) + -> { IOSpecs.closed_io.lineno }.should raise_error(IOError) + end + + it "raises an IOError on a write-only stream" do + name = tmp("io_lineno.txt") + begin + File.open(name, 'w') do |f| + -> { f.lineno }.should raise_error(IOError) + end + ensure + rm_r name + end + end + + it "raises an IOError on a duplexed stream with the read side closed" do + cmd = platform_is(:windows) ? 'rem' : 'cat' + IO.popen(cmd, 'r+') do |p| + p.close_read + -> { p.lineno }.should raise_error(IOError) + end end it "returns the current line number" do @@ -37,7 +56,26 @@ describe "IO#lineno=" do end it "raises an IOError on a closed stream" do - lambda { IOSpecs.closed_io.lineno = 5 }.should raise_error(IOError) + -> { IOSpecs.closed_io.lineno = 5 }.should raise_error(IOError) + end + + it "raises an IOError on a write-only stream" do + name = tmp("io_lineno.txt") + begin + File.open(name, 'w') do |f| + -> { f.lineno = 0 }.should raise_error(IOError) + end + ensure + rm_r name + end + end + + it "raises an IOError on a duplexed stream with the read side closed" do + cmd = platform_is(:windows) ? 'rem' : 'cat' + IO.popen(cmd, 'r+') do |p| + p.close_read + -> { p.lineno = 0 }.should raise_error(IOError) + end end it "calls #to_int on a non-numeric argument" do @@ -56,8 +94,13 @@ describe "IO#lineno=" do @io.lineno.should == 92233 end - it "raises TypeError on nil argument" do - lambda { @io.lineno = nil }.should raise_error(TypeError) + it "raises TypeError if cannot convert argument to Integer implicitly" do + -> { @io.lineno = "1" }.should raise_error(TypeError, 'no implicit conversion of String into Integer') + -> { @io.lineno = nil }.should raise_error(TypeError, 'no implicit conversion from nil to integer') + end + + it "does not accept Integers that don't fit in a C int" do + -> { @io.lineno = 2**32 }.should raise_error(RangeError) end it "sets the current line number to the given value" do diff --git a/spec/ruby/core/io/lines_spec.rb b/spec/ruby/core/io/lines_spec.rb deleted file mode 100644 index a8b8023a2a..0000000000 --- a/spec/ruby/core/io/lines_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -# -*- encoding: utf-8 -*- -require_relative '../../spec_helper' -require_relative 'fixtures/classes' - -describe "IO#lines" do - before :each do - @io = IOSpecs.io_fixture "lines.txt" - end - - after :each do - @io.close if @io - end - - it "returns an Enumerator" do - @io.lines.should be_an_instance_of(Enumerator) - end - - describe "when no block is given" do - it "returns an Enumerator" do - @io.lines.should be_an_instance_of(Enumerator) - end - - describe "returned Enumerator" do - describe "size" do - it "should return nil" do - @io.lines.size.should == nil - end - end - end - end - - it "returns a line when accessed" do - enum = @io.lines - enum.first.should == IOSpecs.lines[0] - end - - it "yields each line to the passed block" do - ScratchPad.record [] - @io.lines { |s| ScratchPad << s } - ScratchPad.recorded.should == IOSpecs.lines - end -end diff --git a/spec/ruby/core/io/new_spec.rb b/spec/ruby/core/io/new_spec.rb index 3597098caf..979ac0efcb 100644 --- a/spec/ruby/core/io/new_spec.rb +++ b/spec/ruby/core/io/new_spec.rb @@ -1,8 +1,16 @@ require_relative '../../spec_helper' require_relative 'shared/new' +# NOTE: should be synchronized with library/stringio/initialize_spec.rb + describe "IO.new" do it_behaves_like :io_new, :new + + it "does not use the given block and warns to use IO::open" do + -> { + @io = IO.send(@method, @fd) { raise } + }.should complain(/warning: IO::new\(\) does not take block; use IO::open\(\) instead/) + end end describe "IO.new" do diff --git a/spec/ruby/core/io/nonblock_spec.rb b/spec/ruby/core/io/nonblock_spec.rb new file mode 100644 index 0000000000..99dc0cafd0 --- /dev/null +++ b/spec/ruby/core/io/nonblock_spec.rb @@ -0,0 +1,48 @@ +require_relative '../../spec_helper' + +platform_is_not :windows do + describe "IO#nonblock?" do + before :all do + require 'io/nonblock' + end + + it "returns false for a file by default" do + File.open(__FILE__) do |f| + f.nonblock?.should == false + end + end + + it "returns true for pipe by default" do + r, w = IO.pipe + begin + r.nonblock?.should == true + w.nonblock?.should == true + ensure + r.close + w.close + end + end + + it "returns true for socket by default" do + require 'socket' + TCPServer.open(0) do |socket| + socket.nonblock?.should == true + end + end + end + + describe "IO#nonblock=" do + before :all do + require 'io/nonblock' + end + + it "changes the IO to non-blocking mode" do + File.open(__FILE__) do |f| + f.nonblock = true + f.nonblock?.should == true + f.nonblock = false + f.nonblock?.should == false + end + end + end +end diff --git a/spec/ruby/core/io/open_spec.rb b/spec/ruby/core/io/open_spec.rb index f26753cde7..d151da9ce5 100644 --- a/spec/ruby/core/io/open_spec.rb +++ b/spec/ruby/core/io/open_spec.rb @@ -37,8 +37,21 @@ describe "IO.open" do ScratchPad.recorded.should == :called end + it "propagate an exception in the block after calling #close" do + -> do + IO.open(@fd, "w") do |io| + IOSpecs.io_mock(io, :close) do + super() + ScratchPad.record :called + end + raise Exception + end + end.should raise_error(Exception) + ScratchPad.recorded.should == :called + end + it "propagates an exception raised by #close that is not a StandardError" do - lambda do + -> do IO.open(@fd, "w") do |io| IOSpecs.io_mock(io, :close) do super() @@ -51,7 +64,7 @@ describe "IO.open" do end it "propagates an exception raised by #close that is a StandardError" do - lambda do + -> do IO.open(@fd, "w") do |io| IOSpecs.io_mock(io, :close) do super() @@ -63,7 +76,7 @@ describe "IO.open" do ScratchPad.recorded.should == :called end - it "does not propagate a IOError with 'closed stream' message raised by #close" do + it "does not propagate an IOError with 'closed stream' message raised by #close" do IO.open(@fd, "w") do |io| IOSpecs.io_mock(io, :close) do super() @@ -74,7 +87,7 @@ describe "IO.open" do ScratchPad.recorded.should == :called end - it "does not set last error when a IOError with 'closed stream' raised by #close" do + it "does not set last error when an IOError with 'closed stream' raised by #close" do IO.open(@fd, "w") do |io| IOSpecs.io_mock(io, :close) do super() diff --git a/spec/ruby/core/io/output_spec.rb b/spec/ruby/core/io/output_spec.rb index d3ec71c563..2aafb305f4 100644 --- a/spec/ruby/core/io/output_spec.rb +++ b/spec/ruby/core/io/output_spec.rb @@ -3,24 +3,24 @@ require_relative 'fixtures/classes' describe "IO#<<" do it "writes an object to the IO stream" do - lambda { + -> { $stderr << "Oh noes, an error!" }.should output_to_fd("Oh noes, an error!", $stderr) end it "calls #to_s on the object to print it" do - lambda { + -> { $stderr << 1337 }.should output_to_fd("1337", $stderr) end it "raises an error if the stream is closed" do io = IOSpecs.closed_io - lambda { io << "test" }.should raise_error(IOError) + -> { io << "test" }.should raise_error(IOError) end it "returns self" do - lambda { + -> { ($stderr << "to_stderr").should == $stderr }.should output(nil, "to_stderr") end diff --git a/spec/ruby/core/io/path_spec.rb b/spec/ruby/core/io/path_spec.rb new file mode 100644 index 0000000000..798adb2163 --- /dev/null +++ b/spec/ruby/core/io/path_spec.rb @@ -0,0 +1,12 @@ +require_relative '../../spec_helper' + +describe "IO#path" do + it "returns the path of the file associated with the IO object" do + path = tmp("io_path.txt") + File.open(path, "w") do |file| + IO.new(file.fileno, path: file.path, autoclose: false).path.should == file.path + end + ensure + File.unlink(path) + end +end diff --git a/spec/ruby/core/io/pid_spec.rb b/spec/ruby/core/io/pid_spec.rb index 97b8a8529d..bc09fe7c3b 100644 --- a/spec/ruby/core/io/pid_spec.rb +++ b/spec/ruby/core/io/pid_spec.rb @@ -30,6 +30,6 @@ describe "IO#pid" do it "raises an IOError on closed stream" do @io.close - lambda { @io.pid }.should raise_error(IOError) + -> { @io.pid }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/pipe_spec.rb b/spec/ruby/core/io/pipe_spec.rb index 005f60fab6..aee0d9003f 100644 --- a/spec/ruby/core/io/pipe_spec.rb +++ b/spec/ruby/core/io/pipe_spec.rb @@ -25,6 +25,17 @@ describe "IO.pipe" do @r.should be_an_instance_of(IOSpecs::SubIO) @w.should be_an_instance_of(IOSpecs::SubIO) end + + it "does not use IO.new method to create pipes and allows its overriding" do + ScratchPad.record [] + + # so redefined .new is not called, but original #initialize is + @r, @w = IOSpecs::SubIOWithRedefinedNew.pipe + ScratchPad.recorded.should == [:call_original_initialize, :call_original_initialize] # called 2 times - for each pipe (r and w) + + @r.should be_an_instance_of(IOSpecs::SubIOWithRedefinedNew) + @w.should be_an_instance_of(IOSpecs::SubIOWithRedefinedNew) + end end describe "IO.pipe" do @@ -44,21 +55,21 @@ describe "IO.pipe" do r, w = IO.pipe do |_r, _w| [_r, _w] end - r.closed?.should == true - w.closed?.should == true + r.should.closed? + w.should.closed? end it "closes both IO objects when the block raises" do r = w = nil - lambda do + -> do IO.pipe do |_r, _w| r = _r w = _w raise RuntimeError end end.should raise_error(RuntimeError) - r.closed?.should == true - w.closed?.should == true + r.should.closed? + w.should.closed? end it "allows IO objects to be closed within the block" do @@ -67,8 +78,8 @@ describe "IO.pipe" do _w.close [_r, _w] end - r.closed?.should == true - w.closed?.should == true + r.should.closed? + w.should.closed? end end end @@ -179,7 +190,7 @@ describe "IO.pipe" do it "calls #to_hash to convert an options argument" do options = mock("io pipe encoding options") options.should_receive(:to_hash).and_return({ invalid: :replace }) - IO.pipe("UTF-8", "ISO-8859-1", options) { |r, w| } + IO.pipe("UTF-8", "ISO-8859-1", **options) { |r, w| } end it "calls #to_str to convert the first argument to a String" do diff --git a/spec/ruby/core/io/popen_spec.rb b/spec/ruby/core/io/popen_spec.rb index 289bb076e4..6043862614 100644 --- a/spec/ruby/core/io/popen_spec.rb +++ b/spec/ruby/core/io/popen_spec.rb @@ -1,13 +1,22 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +require_relative '../process/fixtures/common' describe "IO.popen" do + ProcessSpecs.use_system_ruby(self) + before :each do + @fname = tmp("IO_popen_spec") @io = nil + @var = "$FOO" + platform_is :windows do + @var = "%FOO%" + end end after :each do - @io.close if @io + @io.close if @io and !@io.closed? + rm_r @fname end it "returns an open IO" do @@ -16,27 +25,15 @@ describe "IO.popen" do end it "reads a read-only pipe" do - @io = IO.popen(ruby_cmd('puts "foo"'), "r") + @io = IO.popen('echo foo', "r") @io.read.should == "foo\n" end it "raises IOError when writing a read-only pipe" do - @io = IO.popen(ruby_cmd('puts "foo"'), "r") - lambda { @io.write('bar') }.should raise_error(IOError) + @io = IO.popen('echo foo', "r") + -> { @io.write('bar') }.should raise_error(IOError) @io.read.should == "foo\n" end -end - -describe "IO.popen" do - before :each do - @fname = tmp("IO_popen_spec") - @io = nil - end - - after :each do - @io.close if @io and !@io.closed? - rm_r @fname - end it "sees an infinitely looping subprocess exit when read pipe is closed" do io = IO.popen ruby_cmd('r = loop{puts "y"; 0} rescue 1; exit r'), 'r' @@ -55,7 +52,7 @@ describe "IO.popen" do it "raises IOError when reading a write-only pipe" do @io = IO.popen(ruby_cmd('IO.copy_stream(STDIN,STDOUT)'), "w") - lambda { @io.read }.should raise_error(IOError) + -> { @io.read }.should raise_error(IOError) end it "reads and writes a read/write pipe" do @@ -80,10 +77,10 @@ describe "IO.popen" do Process.kill "KILL", pid @io.close platform_is_not :windows do - $?.signaled?.should == true + $?.should.signaled? end platform_is :windows do - $?.exited?.should == true + $?.should.exited? end end @@ -97,15 +94,21 @@ describe "IO.popen" do mode.should_receive(:to_str).and_return("r") @io = IO.popen(ruby_cmd('exit 0'), mode) end -end -describe "IO.popen" do - before :each do - @io = nil + it "accepts a path using the chdir: keyword argument" do + path = File.dirname(@fname) + + @io = IO.popen(ruby_cmd("puts Dir.pwd"), "r", chdir: path) + @io.read.chomp.should == path end - after :each do - @io.close if @io + it "accepts a path using the chdir: keyword argument and a coercible path" do + path = File.dirname(@fname) + object = mock("path") + object.should_receive(:to_path).and_return(path) + + @io = IO.popen(ruby_cmd("puts Dir.pwd"), "r", chdir: object) + @io.read.chomp.should == path end describe "with a block" do @@ -171,13 +174,13 @@ describe "IO.popen" do context "with a leading ENV Hash" do it "accepts a single String command" do - IO.popen({"FOO" => "bar"}, ruby_cmd('puts ENV["FOO"]')) do |io| + IO.popen({"FOO" => "bar"}, "echo #{@var}") do |io| io.read.should == "bar\n" end end it "accepts a single String command, and an IO mode" do - IO.popen({"FOO" => "bar"}, ruby_cmd('puts ENV["FOO"]'), "r") do |io| + IO.popen({"FOO" => "bar"}, "echo #{@var}", "r") do |io| io.read.should == "bar\n" end end diff --git a/spec/ruby/core/io/pread_spec.rb b/spec/ruby/core/io/pread_spec.rb index b5b516fa53..dc7bcedf3e 100644 --- a/spec/ruby/core/io/pread_spec.rb +++ b/spec/ruby/core/io/pread_spec.rb @@ -1,52 +1,140 @@ # -*- encoding: utf-8 -*- require_relative '../../spec_helper' -ruby_version_is "2.5" do - platform_is_not :windows do - describe "IO#pread" do - before :each do - @fname = tmp("io_pread.txt") - @contents = "1234567890" - touch(@fname) { |f| f.write @contents } - @file = File.open(@fname, "r+") - end +guard -> { platform_is_not :windows or ruby_version_is "3.3" } do + describe "IO#pread" do + before :each do + @fname = tmp("io_pread.txt") + @contents = "1234567890" + touch(@fname) { |f| f.write @contents } + @file = File.open(@fname, "r+") + end - after :each do - @file.close - rm_r @fname - end + after :each do + @file.close + rm_r @fname + end - it "accepts a length, and an offset" do - @file.pread(4, 0).should == "1234" - @file.pread(3, 4).should == "567" - end + it "accepts a length, and an offset" do + @file.pread(4, 0).should == "1234" + @file.pread(3, 4).should == "567" + end - it "accepts a length, an offset, and an output buffer" do - buffer = "foo" - @file.pread(3, 4, buffer) - buffer.should == "567" - end + it "accepts a length, an offset, and an output buffer" do + buffer = +"foo" + @file.pread(3, 4, buffer).should.equal?(buffer) + buffer.should == "567" + end - it "does not advance the file pointer" do - @file.pread(4, 0).should == "1234" - @file.read.should == "1234567890" - end + it "shrinks the buffer in case of less bytes read" do + buffer = +"foo" + @file.pread(1, 0, buffer) + buffer.should == "1" + end - it "raises EOFError if end-of-file is reached" do - lambda { @file.pread(1, 10) }.should raise_error(EOFError) - end + it "grows the buffer in case of more bytes read" do + buffer = +"foo" + @file.pread(5, 0, buffer) + buffer.should == "12345" + end - it "raises IOError when file is not open in read mode" do - File.open(@fname, "w") do |file| - lambda { file.pread(1, 1) }.should raise_error(IOError) - end - end + it "preserves the encoding of the given buffer" do + buffer = ''.encode(Encoding::ISO_8859_1) + @file.pread(10, 0, buffer) + + buffer.encoding.should == Encoding::ISO_8859_1 + end + + it "does not advance the file pointer" do + @file.pread(4, 0).should == "1234" + @file.read.should == "1234567890" + end + + it "ignores the current offset" do + @file.pos = 3 + @file.pread(4, 0).should == "1234" + end + + it "returns an empty string for maxlen = 0" do + @file.pread(0, 4).should == "" + end + + it "returns a buffer for maxlen = 0 when buffer specified" do + buffer = +"foo" + @file.pread(0, 4, buffer).should.equal?(buffer) + buffer.should == "foo" + end + + it "ignores the offset for maxlen = 0, even if it is out of file bounds" do + @file.pread(0, 400).should == "" + end + + it "does not reset the buffer when reading with maxlen = 0" do + buffer = +"foo" + @file.pread(0, 4, buffer) + buffer.should == "foo" + + @file.pread(0, 400, buffer) + buffer.should == "foo" + end + + it "converts maxlen to Integer using #to_int" do + maxlen = mock('maxlen') + maxlen.should_receive(:to_int).and_return(4) + @file.pread(maxlen, 0).should == "1234" + end + + it "converts offset to Integer using #to_int" do + offset = mock('offset') + offset.should_receive(:to_int).and_return(0) + @file.pread(4, offset).should == "1234" + end + + it "converts a buffer to String using to_str" do + buffer = mock('buffer') + buffer.should_receive(:to_str).at_least(1).and_return(+"foo") + @file.pread(4, 0, buffer) + buffer.should_not.is_a?(String) + buffer.to_str.should == "1234" + end + + it "raises TypeError if maxlen is not an Integer and cannot be coerced into Integer" do + maxlen = Object.new + -> { @file.pread(maxlen, 0) }.should raise_error(TypeError, 'no implicit conversion of Object into Integer') + end - it "raises IOError when file is closed" do - file = File.open(@fname, "r+") - file.close - lambda { file.pread(1, 1) }.should raise_error(IOError) + it "raises TypeError if offset is not an Integer and cannot be coerced into Integer" do + offset = Object.new + -> { @file.pread(4, offset) }.should raise_error(TypeError, 'no implicit conversion of Object into Integer') + end + + it "raises ArgumentError for negative values of maxlen" do + -> { @file.pread(-4, 0) }.should raise_error(ArgumentError, 'negative string size (or size too big)') + end + + it "raised Errno::EINVAL for negative values of offset" do + -> { @file.pread(4, -1) }.should raise_error(Errno::EINVAL, /Invalid argument/) + end + + it "raises TypeError if the buffer is not a String and cannot be coerced into String" do + buffer = Object.new + -> { @file.pread(4, 0, buffer) }.should raise_error(TypeError, 'no implicit conversion of Object into String') + end + + it "raises EOFError if end-of-file is reached" do + -> { @file.pread(1, 10) }.should raise_error(EOFError) + end + + it "raises IOError when file is not open in read mode" do + File.open(@fname, "w") do |file| + -> { file.pread(1, 1) }.should raise_error(IOError) end end + + it "raises IOError when file is closed" do + file = File.open(@fname, "r+") + file.close + -> { file.pread(1, 1) }.should raise_error(IOError) + end end end diff --git a/spec/ruby/core/io/print_spec.rb b/spec/ruby/core/io/print_spec.rb index 0dd48344ce..085852024c 100644 --- a/spec/ruby/core/io/print_spec.rb +++ b/spec/ruby/core/io/print_spec.rb @@ -1,18 +1,29 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' -describe IO, "#print" do +describe "IO#print" do before :each do - @old_separator = $\ - $\ = '->' + @old_record_separator = $\ + @old_field_separator = $, + suppress_warning { + $\ = '->' + $, = '^^' + } @name = tmp("io_print") end after :each do - $\ = @old_separator + suppress_warning { + $\ = @old_record_separator + $, = @old_field_separator + } rm_r @name end + it "returns nil" do + touch(@name) { |f| f.print.should be_nil } + end + it "writes $_.to_s followed by $\\ (if any) to the stream if no arguments given" do o = mock('o') o.should_receive(:to_s).and_return("mockmockmock") @@ -38,16 +49,18 @@ describe IO, "#print" do IO.read(@name).should == "hello#{$\}" end - it "writes each obj.to_s to the stream and appends $\\ (if any) given multiple objects" do + it "writes each obj.to_s to the stream separated by $, (if any) and appends $\\ (if any) given multiple objects" do o, o2 = Object.new, Object.new def o.to_s(); 'o'; end def o2.to_s(); 'o2'; end - touch(@name) { |f| f.print(o, o2) } - IO.read(@name).should == "#{o.to_s}#{o2.to_s}#{$\}" + suppress_warning { + touch(@name) { |f| f.print(o, o2) } + } + IO.read(@name).should == "#{o.to_s}#{$,}#{o2.to_s}#{$\}" end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.print("stuff") }.should raise_error(IOError) + -> { IOSpecs.closed_io.print("stuff") }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/printf_spec.rb b/spec/ruby/core/io/printf_spec.rb index be4e5c339e..baa00f14ce 100644 --- a/spec/ruby/core/io/printf_spec.rb +++ b/spec/ruby/core/io/printf_spec.rb @@ -27,6 +27,6 @@ describe "IO#printf" do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.printf("stuff") }.should raise_error(IOError) + -> { IOSpecs.closed_io.printf("stuff") }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/puts_spec.rb b/spec/ruby/core/io/puts_spec.rb index e8d599730f..a186ddaa5d 100644 --- a/spec/ruby/core/io/puts_spec.rb +++ b/spec/ruby/core/io/puts_spec.rb @@ -6,7 +6,7 @@ describe "IO#puts" do @before_separator = $/ @name = tmp("io_puts.txt") @io = new_io @name - ScratchPad.record "" + ScratchPad.record(+"") def @io.write(str) ScratchPad << str end @@ -16,7 +16,7 @@ describe "IO#puts" do ScratchPad.clear @io.close if @io rm_r @name - $/ = @before_separator + suppress_warning {$/ = @before_separator} end it "writes just a newline when given no args" do @@ -25,7 +25,7 @@ describe "IO#puts" do end it "writes just a newline when given just a newline" do - lambda { $stdout.puts "\n" }.should output_to_fd("\n", STDOUT) + -> { $stdout.puts "\n" }.should output_to_fd("\n", $stdout) end it "writes empty string with a newline when given nil as an arg" do @@ -33,7 +33,7 @@ describe "IO#puts" do ScratchPad.recorded.should == "\n" end - it "writes empty string with a newline when when given nil as multiple args" do + it "writes empty string with a newline when given nil as multiple args" do @io.puts(nil, nil).should == nil ScratchPad.recorded.should == "\n\n" end @@ -105,13 +105,13 @@ describe "IO#puts" do end it "ignores the $/ separator global" do - $/ = ":" + suppress_warning {$/ = ":"} @io.puts(5).should == nil ScratchPad.recorded.should == "5\n" end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.puts("stuff") }.should raise_error(IOError) + -> { IOSpecs.closed_io.puts("stuff") }.should raise_error(IOError) end it "writes crlf when IO is opened with newline: :crlf" do diff --git a/spec/ruby/core/io/pwrite_spec.rb b/spec/ruby/core/io/pwrite_spec.rb index fd3d1b98e9..2bc508b37d 100644 --- a/spec/ruby/core/io/pwrite_spec.rb +++ b/spec/ruby/core/io/pwrite_spec.rb @@ -1,45 +1,69 @@ # -*- encoding: utf-8 -*- require_relative '../../spec_helper' -ruby_version_is "2.5" do - platform_is_not :windows do - describe "IO#pwrite" do - before :each do - @fname = tmp("io_pwrite.txt") - @file = File.open(@fname, "w+") - end +guard -> { platform_is_not :windows or ruby_version_is "3.3" } do + describe "IO#pwrite" do + before :each do + @fname = tmp("io_pwrite.txt") + @file = File.open(@fname, "w+") + end - after :each do - @file.close - rm_r @fname - end + after :each do + @file.close + rm_r @fname + end - it "returns the number of bytes written" do - @file.pwrite("foo", 0).should == 3 - end + it "returns the number of bytes written" do + @file.pwrite("foo", 0).should == 3 + end - it "accepts a string and an offset" do - @file.pwrite("foo", 2) - @file.pread(3, 2).should == "foo" - end + it "accepts a string and an offset" do + @file.pwrite("foo", 2) + @file.pread(3, 2).should == "foo" + end - it "does not advance the pointer in the file" do - @file.pwrite("bar", 3) - @file.write("foo") - @file.pread(6, 0).should == "foobar" - end + it "does not advance the pointer in the file" do + @file.pwrite("bar", 3) + @file.write("foo") + @file.pread(6, 0).should == "foobar" + end - it "raises IOError when file is not open in write mode" do - File.open(@fname, "r") do |file| - lambda { file.pwrite("foo", 1) }.should raise_error(IOError) - end - end + it "calls #to_s on the object to be written" do + object = mock("to_s") + object.should_receive(:to_s).and_return("foo") + @file.pwrite(object, 0) + @file.pread(3, 0).should == "foo" + end - it "raises IOError when file is closed" do - file = File.open(@fname, "w+") - file.close - lambda { file.pwrite("foo", 1) }.should raise_error(IOError) + it "calls #to_int on the offset" do + offset = mock("to_int") + offset.should_receive(:to_int).and_return(2) + @file.pwrite("foo", offset) + @file.pread(3, 2).should == "foo" + end + + it "raises IOError when file is not open in write mode" do + File.open(@fname, "r") do |file| + -> { file.pwrite("foo", 1) }.should raise_error(IOError, "not opened for writing") end end + + it "raises IOError when file is closed" do + file = File.open(@fname, "w+") + file.close + -> { file.pwrite("foo", 1) }.should raise_error(IOError, "closed stream") + end + + it "raises a NoMethodError if object does not respond to #to_s" do + -> { + @file.pwrite(BasicObject.new, 0) + }.should raise_error(NoMethodError, /undefined method [`']to_s'/) + end + + it "raises a TypeError if the offset cannot be converted to an Integer" do + -> { + @file.pwrite("foo", Object.new) + }.should raise_error(TypeError, "no implicit conversion of Object into Integer") + end end end diff --git a/spec/ruby/core/io/read_nonblock_spec.rb b/spec/ruby/core/io/read_nonblock_spec.rb index 59f5064922..51e7cd6bd2 100644 --- a/spec/ruby/core/io/read_nonblock_spec.rb +++ b/spec/ruby/core/io/read_nonblock_spec.rb @@ -12,7 +12,7 @@ describe "IO#read_nonblock" do end it "raises an exception extending IO::WaitReadable when there is no data" do - lambda { @read.read_nonblock(5) }.should raise_error(IO::WaitReadable) { |e| + -> { @read.read_nonblock(5) }.should raise_error(IO::WaitReadable) { |e| platform_is_not :windows do e.should be_kind_of(Errno::EAGAIN) end @@ -46,7 +46,7 @@ describe "IO#read_nonblock" do require 'io/nonblock' @write.write "abc" @read.read_nonblock(1).should == "a" - @read.nonblock?.should == true + @read.should.nonblock? end end @@ -55,6 +55,27 @@ describe "IO#read_nonblock" do @read.read_nonblock(4).should == "hell" end + it "reads after ungetc with data in the buffer" do + @write.write("foobar") + @read.set_encoding( + 'utf-8', universal_newline: false + ) + c = @read.getc + @read.ungetc(c) + @read.read_nonblock(3).should == "foo" + @read.read_nonblock(3).should == "bar" + end + + it "raises an exception after ungetc with data in the buffer and character conversion enabled" do + @write.write("foobar") + @read.set_encoding( + 'utf-8', universal_newline: true + ) + c = @read.getc + @read.ungetc(c) + -> { @read.read_nonblock(3).should == "foo" }.should raise_error(IOError) + end + it "returns less data if that is all that is available" do @write << "hello" @read.read_nonblock(10).should == "hello" @@ -70,22 +91,41 @@ describe "IO#read_nonblock" do @read.read_nonblock(1).should == "1" end + it "raises ArgumentError when length is less than 0" do + -> { @read.read_nonblock(-1) }.should raise_error(ArgumentError) + end + it "reads into the passed buffer" do - buffer = "" + buffer = +"" @write.write("1") @read.read_nonblock(1, buffer) buffer.should == "1" end it "returns the passed buffer" do - buffer = "" + buffer = +"" @write.write("1") output = @read.read_nonblock(1, buffer) output.should equal(buffer) end + it "discards the existing buffer content upon successful read" do + buffer = +"existing content" + @write.write("hello world") + @write.close + @read.read_nonblock(11, buffer) + buffer.should == "hello world" + end + + it "discards the existing buffer content upon error" do + buffer = +"existing content" + @write.close + -> { @read.read_nonblock(1, buffer) }.should raise_error(EOFError) + buffer.should be_empty + end + it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.read_nonblock(5) }.should raise_error(IOError) + -> { IOSpecs.closed_io.read_nonblock(5) }.should raise_error(IOError) end it "raises EOFError when the end is reached" do @@ -94,6 +134,15 @@ describe "IO#read_nonblock" do @read.read_nonblock(5) - lambda { @read.read_nonblock(5) }.should raise_error(EOFError) + -> { @read.read_nonblock(5) }.should raise_error(EOFError) + end + + it "preserves the encoding of the given buffer" do + buffer = ''.encode(Encoding::ISO_8859_1) + @write.write("abc") + @write.close + @read.read_nonblock(10, buffer) + + buffer.encoding.should == Encoding::ISO_8859_1 end end diff --git a/spec/ruby/core/io/read_spec.rb b/spec/ruby/core/io/read_spec.rb index e005e14fdf..988ec2ce30 100644 --- a/spec/ruby/core/io/read_spec.rb +++ b/spec/ruby/core/io/read_spec.rb @@ -23,36 +23,72 @@ describe "IO.read" do IO.read(p) end + # https://bugs.ruby-lang.org/issues/19354 + it "accepts options as keyword arguments" do + IO.read(@fname, 3, 0, mode: "r+").should == @contents[0, 3] + + -> { + IO.read(@fname, 3, 0, {mode: "r+"}) + }.should raise_error(ArgumentError, /wrong number of arguments/) + end + it "accepts an empty options Hash" do - IO.read(@fname, {}).should == @contents + IO.read(@fname, **{}).should == @contents end it "accepts a length, and empty options Hash" do - IO.read(@fname, 3, {}).should == @contents[0, 3] + IO.read(@fname, 3, **{}).should == @contents[0, 3] end it "accepts a length, offset, and empty options Hash" do - IO.read(@fname, 3, 0, {}).should == @contents[0, 3] + IO.read(@fname, 3, 0, **{}).should == @contents[0, 3] end it "raises an IOError if the options Hash specifies write mode" do - lambda { IO.read(@fname, 3, 0, {mode: "w"}) }.should raise_error(IOError) + -> { IO.read(@fname, 3, 0, mode: "w") }.should raise_error(IOError) end it "raises an IOError if the options Hash specifies append only mode" do - lambda { IO.read(@fname, {mode: "a"}) }.should raise_error(IOError) + -> { IO.read(@fname, mode: "a") }.should raise_error(IOError) end it "reads the file if the options Hash includes read mode" do - IO.read(@fname, {mode: "r"}).should == @contents + IO.read(@fname, mode: "r").should == @contents end it "reads the file if the options Hash includes read/write mode" do - IO.read(@fname, {mode: "r+"}).should == @contents + IO.read(@fname, mode: "r+").should == @contents end it "reads the file if the options Hash includes read/write append mode" do - IO.read(@fname, {mode: "a+"}).should == @contents + IO.read(@fname, mode: "a+").should == @contents + end + + platform_is_not :windows do + ruby_version_is ""..."3.3" do + it "uses an :open_args option" do + string = IO.read(@fname, nil, 0, open_args: ["r", nil, {encoding: Encoding::US_ASCII}]) + string.encoding.should == Encoding::US_ASCII + + string = IO.read(@fname, nil, 0, open_args: ["r", nil, {}]) + string.encoding.should == Encoding::UTF_8 + end + end + end + + it "disregards other options if :open_args is given" do + string = IO.read(@fname,mode: "w", encoding: Encoding::UTF_32LE, open_args: ["r", encoding: Encoding::UTF_8]) + string.encoding.should == Encoding::UTF_8 + end + + it "doesn't require mode to be specified in :open_args" do + string = IO.read(@fname, nil, 0, open_args: [{encoding: Encoding::US_ASCII}]) + string.encoding.should == Encoding::US_ASCII + end + + it "doesn't require mode to be specified in :open_args even if flags option passed" do + string = IO.read(@fname, nil, 0, open_args: [{encoding: Encoding::US_ASCII, flags: File::CREAT}]) + string.encoding.should == Encoding::US_ASCII end it "treats second nil argument as no length limit" do @@ -77,22 +113,40 @@ describe "IO.read" do IO.read(@fname, 1, 10).should == nil end + it "returns an empty string when reading zero bytes" do + IO.read(@fname, 0).should == '' + end + + it "returns a String in BINARY when passed a size" do + IO.read(@fname, 1).encoding.should == Encoding::BINARY + IO.read(@fname, 0).encoding.should == Encoding::BINARY + end + it "raises an Errno::ENOENT when the requested file does not exist" do rm_r @fname - lambda { IO.read @fname }.should raise_error(Errno::ENOENT) + -> { IO.read @fname }.should raise_error(Errno::ENOENT) end it "raises a TypeError when not passed a String type" do - lambda { IO.read nil }.should raise_error(TypeError) + -> { IO.read nil }.should raise_error(TypeError) end it "raises an ArgumentError when not passed a valid length" do - lambda { IO.read @fname, -1 }.should raise_error(ArgumentError) + -> { IO.read @fname, -1 }.should raise_error(ArgumentError) + end + + ruby_version_is ''...'3.3' do + it "raises an Errno::EINVAL when not passed a valid offset" do + -> { IO.read @fname, 0, -1 }.should raise_error(Errno::EINVAL) + -> { IO.read @fname, -1, -1 }.should raise_error(Errno::EINVAL) + end end - it "raises an Errno::EINVAL when not passed a valid offset" do - lambda { IO.read @fname, 0, -1 }.should raise_error(Errno::EINVAL) - lambda { IO.read @fname, -1, -1 }.should raise_error(Errno::EINVAL) + ruby_version_is '3.3' do + it "raises an ArgumentError when not passed a valid offset" do + -> { IO.read @fname, 0, -1 }.should raise_error(ArgumentError) + -> { IO.read @fname, -1, -1 }.should raise_error(ArgumentError) + end end it "uses the external encoding specified via the :external_encoding option" do @@ -104,57 +158,91 @@ describe "IO.read" do str = IO.read(@fname, encoding: Encoding::ISO_8859_1) str.encoding.should == Encoding::ISO_8859_1 end -end -describe "IO.read from a pipe" do - it "runs the rest as a subprocess and returns the standard output" do - cmd = "|sh -c 'echo hello'" - platform_is :windows do - cmd = "|cmd.exe /C echo hello" + platform_is :windows do + it "reads the file in text mode" do + # 0x1A is CTRL+Z and is EOF in Windows text mode. + File.binwrite(@fname, "\x1Abbb") + IO.read(@fname).should.empty? end - IO.read(cmd).should == "hello\n" end +end - platform_is_not :windows do - it "opens a pipe to a fork if the rest is -" do - str = IO.read("|-") - if str # parent - str.should == "hello from child\n" - else #child - puts "hello from child" - exit! +ruby_version_is ""..."4.0" do + describe "IO.read from a pipe" do + it "runs the rest as a subprocess and returns the standard output" do + cmd = "|sh -c 'echo hello'" + platform_is :windows do + cmd = "|cmd.exe /C echo hello" + end + + suppress_warning do # https://bugs.ruby-lang.org/issues/19630 + IO.read(cmd).should == "hello\n" end end - end - it "reads only the specified number of bytes requested" do - cmd = "|sh -c 'echo hello'" - platform_is :windows do - cmd = "|cmd.exe /C echo hello" + platform_is_not :windows do + it "opens a pipe to a fork if the rest is -" do + str = nil + suppress_warning do # https://bugs.ruby-lang.org/issues/19630 + str = IO.read("|-") + end + + if str # parent + str.should == "hello from child\n" + else #child + puts "hello from child" + exit! + end + end end - IO.read(cmd, 1).should == "h" - end - platform_is_not :windows do - it "raises Errno::ESPIPE if passed an offset" do - lambda { - IO.read("|sh -c 'echo hello'", 1, 1) - }.should raise_error(Errno::ESPIPE) + it "reads only the specified number of bytes requested" do + cmd = "|sh -c 'echo hello'" + platform_is :windows do + cmd = "|cmd.exe /C echo hello" + end + + suppress_warning do # https://bugs.ruby-lang.org/issues/19630 + IO.read(cmd, 1).should == "h" + end end - end -quarantine! do # The process tried to write to a nonexistent pipe. - platform_is :windows do - # TODO: It should raise Errno::ESPIPE on Windows as well - # once https://bugs.ruby-lang.org/issues/12230 is fixed. - it "raises Errno::EINVAL if passed an offset" do - lambda { - IO.read("|cmd.exe /C echo hello", 1, 1) - }.should raise_error(Errno::EINVAL) + platform_is_not :windows do + it "raises Errno::ESPIPE if passed an offset" do + -> { + suppress_warning do # https://bugs.ruby-lang.org/issues/19630 + IO.read("|sh -c 'echo hello'", 1, 1) + end + }.should raise_error(Errno::ESPIPE) + end + end + + quarantine! do # The process tried to write to a nonexistent pipe. + platform_is :windows do + # TODO: It should raise Errno::ESPIPE on Windows as well + # once https://bugs.ruby-lang.org/issues/12230 is fixed. + it "raises Errno::EINVAL if passed an offset" do + -> { + suppress_warning do # https://bugs.ruby-lang.org/issues/19630 + IO.read("|cmd.exe /C echo hello", 1, 1) + end + }.should raise_error(Errno::EINVAL) + end + end + end + + ruby_version_is "3.3" do + # https://bugs.ruby-lang.org/issues/19630 + it "warns about deprecation" do + cmd = "|echo ok" + -> { + IO.read(cmd) + }.should complain(/IO process creation with a leading '\|'/) + end end end end -end describe "IO.read on an empty file" do before :each do @@ -197,26 +285,60 @@ describe "IO#read" do @io.read(4).should == '7890' end + it "treats first nil argument as no length limit" do + @io.read(nil).should == @contents + end + + it "raises an ArgumentError when not passed a valid length" do + -> { @io.read(-1) }.should raise_error(ArgumentError) + end + it "clears the output buffer if there is nothing to read" do @io.pos = 10 - buf = 'non-empty string' + buf = +'non-empty string' @io.read(10, buf).should == nil buf.should == '' + + buf = +'non-empty string' + + @io.read(nil, buf).should == "" + + buf.should == '' + + buf = +'non-empty string' + + @io.read(0, buf).should == "" + + buf.should == '' + end + + it "raise FrozenError if the output buffer is frozen" do + @io.read + -> { @io.read(0, 'frozen-string'.freeze) }.should raise_error(FrozenError) + -> { @io.read(1, 'frozen-string'.freeze) }.should raise_error(FrozenError) + -> { @io.read(nil, 'frozen-string'.freeze) }.should raise_error(FrozenError) + end + + ruby_bug "", ""..."3.3" do + it "raise FrozenError if the output buffer is frozen (2)" do + @io.read + -> { @io.read(1, ''.freeze) }.should raise_error(FrozenError) + end end it "consumes zero bytes when reading zero bytes" do @io.read(0).should == '' @io.pos.should == 0 - @io.getc.chr.should == '1' + @io.getc.should == '1' end it "is at end-of-file when everything has been read" do @io.read - @io.eof?.should == true + @io.should.eof? end it "reads the contents of a file" do @@ -224,46 +346,68 @@ describe "IO#read" do end it "places the specified number of bytes in the buffer" do - buf = "" + buf = +"" @io.read 5, buf buf.should == "12345" end it "expands the buffer when too small" do - buf = "ABCDE" + buf = +"ABCDE" @io.read nil, buf buf.should == @contents end it "overwrites the buffer" do - buf = "ABCDEFGHIJ" + buf = +"ABCDEFGHIJ" @io.read nil, buf buf.should == @contents end it "truncates the buffer when too big" do - buf = "ABCDEFGHIJKLMNO" + buf = +"ABCDEFGHIJKLMNO" @io.read nil, buf buf.should == @contents @io.rewind - buf = "ABCDEFGHIJKLMNO" + buf = +"ABCDEFGHIJKLMNO" @io.read 5, buf buf.should == @contents[0..4] end + it "preserves the encoding of the given buffer" do + buffer = ''.encode(Encoding::ISO_8859_1) + @io.read(10, buffer) + + buffer.encoding.should == Encoding::ISO_8859_1 + end + + # https://bugs.ruby-lang.org/issues/20416 + it "does not preserve the encoding of the given buffer when max length is not provided" do + buffer = ''.encode(Encoding::ISO_8859_1) + @io.read(nil, buffer) + + buffer.encoding.should_not == Encoding::ISO_8859_1 + end + it "returns the given buffer" do - buf = "" + buf = +"" @io.read(nil, buf).should equal buf end + it "returns the given buffer when there is nothing to read" do + buf = +"" + + @io.read + @io.read(nil, buf).should equal buf + end + it "coerces the second argument to string and uses it as a buffer" do - buf = "ABCDE" + buf = +"ABCDE" obj = mock("buff") obj.should_receive(:to_str).any_number_of_times.and_return(buf) @@ -301,9 +445,12 @@ describe "IO#read" do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.read }.should raise_error(IOError) + -> { IOSpecs.closed_io.read }.should raise_error(IOError) end + it "raises ArgumentError when length is less than 0" do + -> { @io.read(-1) }.should raise_error(ArgumentError) + end platform_is_not :windows do it "raises IOError when stream is closed by another thread" do @@ -384,13 +531,6 @@ describe "IO#read in binary mode" do xE2 = [226].pack('C*') result.should == ("abc" + xE2 + "def").force_encoding(Encoding::BINARY) end - - it "does not transcode file contents when an internal encoding is specified" do - result = File.open(@name, "r:binary:utf-8") { |f| f.read }.chomp - result.encoding.should == Encoding::BINARY - xE2 = [226].pack('C*') - result.should == ("abc" + xE2 + "def").force_encoding(Encoding::BINARY) - end end describe "IO#read in text mode" do @@ -465,13 +605,13 @@ describe :io_read_internal_encoding, shared: true do describe "when passed nil for limit" do it "sets the buffer to a transcoded String" do - result = @io.read(nil, buf = "") + result = @io.read(nil, buf = +"") buf.should equal(result) buf.should == "ありがとう\n" end it "sets the buffer's encoding to the internal encoding" do - buf = "".force_encoding Encoding::ISO_8859_1 + buf = "".dup.force_encoding Encoding::ISO_8859_1 @io.read(nil, buf) buf.encoding.should equal(Encoding::UTF_8) end @@ -485,17 +625,18 @@ describe :io_read_size_internal_encoding, shared: true do it "returns a String in BINARY when passed a size" do @io.read(4).encoding.should equal(Encoding::BINARY) + @io.read(0).encoding.should equal(Encoding::BINARY) end it "does not change the buffer's encoding when passed a limit" do - buf = "".force_encoding Encoding::ISO_8859_1 + buf = "".dup.force_encoding Encoding::ISO_8859_1 @io.read(4, buf) buf.should == [164, 162, 164, 234].pack('C*').force_encoding(Encoding::ISO_8859_1) buf.encoding.should equal(Encoding::ISO_8859_1) end it "truncates the buffer but does not change the buffer's encoding when no data remains" do - buf = "abc".force_encoding Encoding::ISO_8859_1 + buf = "abc".dup.force_encoding Encoding::ISO_8859_1 @io.read @io.read(1, buf).should be_nil diff --git a/spec/ruby/core/io/readbyte_spec.rb b/spec/ruby/core/io/readbyte_spec.rb index eb25975f58..14426c28ac 100644 --- a/spec/ruby/core/io/readbyte_spec.rb +++ b/spec/ruby/core/io/readbyte_spec.rb @@ -17,7 +17,7 @@ describe "IO#readbyte" do it "raises EOFError on EOF" do @io.seek(999999) - lambda do + -> do @io.readbyte end.should raise_error EOFError end diff --git a/spec/ruby/core/io/readchar_spec.rb b/spec/ruby/core/io/readchar_spec.rb index 74f78b5a6e..a66773851a 100644 --- a/spec/ruby/core/io/readchar_spec.rb +++ b/spec/ruby/core/io/readchar_spec.rb @@ -1,6 +1,16 @@ require_relative '../../spec_helper' require_relative 'fixtures/classes' +describe :io_readchar_internal_encoding, shared: true do + it "returns a transcoded String" do + @io.readchar.should == "あ" + end + + it "sets the String encoding to the internal encoding" do + @io.readchar.encoding.should equal(Encoding::UTF_8) + end +end + describe "IO#readchar" do before :each do @io = IOSpecs.io_fixture "lines.txt" @@ -21,11 +31,67 @@ describe "IO#readchar" do it "raises an EOFError when invoked at the end of the stream" do @io.read - lambda { @io.readchar }.should raise_error(EOFError) + -> { @io.readchar }.should raise_error(EOFError) end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.readchar }.should raise_error(IOError) + -> { IOSpecs.closed_io.readchar }.should raise_error(IOError) + end +end + +describe "IO#readchar with internal encoding" do + after :each do + @io.close if @io + end + + describe "not specified" do + before :each do + @io = IOSpecs.io_fixture "read_euc_jp.txt", "r:euc-jp" + end + + it "does not transcode the String" do + @io.readchar.should == ("あ").encode(Encoding::EUC_JP) + end + + it "sets the String encoding to the external encoding" do + @io.readchar.encoding.should equal(Encoding::EUC_JP) + end + end + + describe "specified by open mode" do + before :each do + @io = IOSpecs.io_fixture "read_euc_jp.txt", "r:euc-jp:utf-8" + end + + it_behaves_like :io_readchar_internal_encoding, nil + end + + describe "specified by mode: option" do + before :each do + @io = IOSpecs.io_fixture "read_euc_jp.txt", mode: "r:euc-jp:utf-8" + end + + it_behaves_like :io_readchar_internal_encoding, nil + end + + describe "specified by internal_encoding: option" do + before :each do + options = { mode: "r", + internal_encoding: "utf-8", + external_encoding: "euc-jp" } + @io = IOSpecs.io_fixture "read_euc_jp.txt", options + end + + it_behaves_like :io_readchar_internal_encoding, nil + end + + describe "specified by encoding: option" do + before :each do + options = { mode: "r", encoding: "euc-jp:utf-8" } + @io = IOSpecs.io_fixture "read_euc_jp.txt", options + end + + it_behaves_like :io_readchar_internal_encoding, nil end end @@ -39,6 +105,6 @@ describe "IO#readchar" do end it "raises EOFError on empty stream" do - lambda { @io.readchar }.should raise_error(EOFError) + -> { @io.readchar }.should raise_error(EOFError) end end diff --git a/spec/ruby/core/io/readline_spec.rb b/spec/ruby/core/io/readline_spec.rb index 3eae6bfa47..a814c1be90 100644 --- a/spec/ruby/core/io/readline_spec.rb +++ b/spec/ruby/core/io/readline_spec.rb @@ -29,11 +29,11 @@ describe "IO#readline" do it "raises EOFError on end of stream" do IOSpecs.lines.length.times { @io.readline } - lambda { @io.readline }.should raise_error(EOFError) + -> { @io.readline }.should raise_error(EOFError) end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.readline }.should raise_error(IOError) + -> { IOSpecs.closed_io.readline }.should raise_error(IOError) end it "assigns the returned line to $_" do @@ -43,9 +43,42 @@ describe "IO#readline" do end end + describe "when passed limit" do + it "reads limit bytes" do + @io.readline(3).should == "Voi" + end + + it "returns an empty string when passed 0 as a limit" do + @io.readline(0).should == "" + end + + it "does not accept Integers that don't fit in a C off_t" do + -> { @io.readline(2**128) }.should raise_error(RangeError) + end + end + + describe "when passed separator and limit" do + it "reads limit bytes till the separator" do + # Voici la ligne une.\ + @io.readline(" ", 4).should == "Voic" + @io.readline(" ", 4).should == "i " + @io.readline(" ", 4).should == "la " + @io.readline(" ", 4).should == "lign" + @io.readline(" ", 4).should == "e " + end + end + describe "when passed chomp" do it "returns the first line without a trailing newline character" do @io.readline(chomp: true).should == IOSpecs.lines_without_newline_characters[0] end + + it "raises exception when options passed as Hash" do + -> { @io.readline({ chomp: true }) }.should raise_error(TypeError) + + -> { + @io.readline("\n", 1, { chomp: true }) + }.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 0..2)") + end end end diff --git a/spec/ruby/core/io/readlines_spec.rb b/spec/ruby/core/io/readlines_spec.rb index 5a7e9b5aa8..b4770775d1 100644 --- a/spec/ruby/core/io/readlines_spec.rb +++ b/spec/ruby/core/io/readlines_spec.rb @@ -17,16 +17,16 @@ describe "IO#readlines" do it "raises an IOError if the stream is closed" do @io.close - lambda { @io.readlines }.should raise_error(IOError) + -> { @io.readlines }.should raise_error(IOError) end describe "when passed no arguments" do before :each do - @sep, $/ = $/, " " + suppress_warning {@sep, $/ = $/, " "} end after :each do - $/ = @sep + suppress_warning {$/ = @sep} end it "returns an Array containing lines based on $/" do @@ -102,28 +102,33 @@ describe "IO#readlines" do end end - describe "when passed a string that starts with a |" do - it "gets data from the standard out of the subprocess" do - cmd = "|sh -c 'echo hello;echo line2'" - platform_is :windows do - cmd = "|cmd.exe /C echo hello&echo line2" - end - lines = IO.readlines(cmd) - lines.should == ["hello\n", "line2\n"] + describe "when passed limit" do + it "raises ArgumentError when passed 0 as a limit" do + -> { @io.readlines(0) }.should raise_error(ArgumentError) end - platform_is_not :windows do - it "gets data from a fork when passed -" do - lines = IO.readlines("|-") + it "does not accept Integers that don't fit in a C off_t" do + -> { @io.readlines(2**128) }.should raise_error(RangeError) + end + end - if lines # parent - lines.should == ["hello\n", "from a fork\n"] - else - puts "hello" - puts "from a fork" - exit! - end - end + describe "when passed chomp" do + it "returns the first line without a trailing newline character" do + @io.readlines(chomp: true).should == IOSpecs.lines_without_newline_characters + end + + it "raises exception when options passed as Hash" do + -> { @io.readlines({ chomp: true }) }.should raise_error(TypeError) + + -> { + @io.readlines("\n", 1, { chomp: true }) + }.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 0..2)") + end + end + + describe "when passed arbitrary keyword argument" do + it "tolerates it" do + @io.readlines(chomp: true, foo: :bar).should == IOSpecs.lines_without_newline_characters end end end @@ -138,13 +143,13 @@ describe "IO#readlines" do end it "raises an IOError if the stream is opened for append only" do - lambda do + -> do File.open(@name, "a:utf-8") { |f| f.readlines } end.should raise_error(IOError) end it "raises an IOError if the stream is opened for write only" do - lambda do + -> do File.open(@name, "w:utf-8") { |f| f.readlines } end.should raise_error(IOError) end @@ -169,6 +174,50 @@ describe "IO.readlines" do $_.should == "test" end + ruby_version_is ""..."4.0" do + describe "when passed a string that starts with a |" do + it "gets data from the standard out of the subprocess" do + cmd = "|sh -c 'echo hello;echo line2'" + platform_is :windows do + cmd = "|cmd.exe /C echo hello&echo line2" + end + + lines = nil + suppress_warning do # https://bugs.ruby-lang.org/issues/19630 + lines = IO.readlines(cmd) + end + lines.should == ["hello\n", "line2\n"] + end + + platform_is_not :windows do + it "gets data from a fork when passed -" do + lines = nil + suppress_warning do # https://bugs.ruby-lang.org/issues/19630 + lines = IO.readlines("|-") + end + + if lines # parent + lines.should == ["hello\n", "from a fork\n"] + else + puts "hello" + puts "from a fork" + exit! + end + end + end + end + + ruby_version_is "3.3" do + # https://bugs.ruby-lang.org/issues/19630 + it "warns about deprecation given a path with a pipe" do + cmd = "|echo ok" + -> { + IO.readlines(cmd) + }.should complain(/IO process creation with a leading '\|'/) + end + end + end + it_behaves_like :io_readlines, :readlines it_behaves_like :io_readlines_options_19, :readlines end @@ -184,7 +233,7 @@ describe "IO.readlines" do after :each do Encoding.default_external = @external Encoding.default_internal = @internal - $/ = @dollar_slash + suppress_warning {$/ = @dollar_slash} end it "encodes lines using the default external encoding" do @@ -196,7 +245,7 @@ describe "IO.readlines" do it "encodes lines using the default internal encoding, when set" do Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_16 - $/ = $/.encode Encoding::UTF_16 + suppress_warning {$/ = $/.encode Encoding::UTF_16} lines = IO.readlines(@name) lines.all? { |s| s.encoding == Encoding::UTF_16 }.should be_true end diff --git a/spec/ruby/core/io/readpartial_spec.rb b/spec/ruby/core/io/readpartial_spec.rb index 1ab01dcf49..176c33cf9e 100644 --- a/spec/ruby/core/io/readpartial_spec.rb +++ b/spec/ruby/core/io/readpartial_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -15,10 +15,10 @@ describe "IO#readpartial" do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.readpartial(10) }.should raise_error(IOError) + -> { IOSpecs.closed_io.readpartial(10) }.should raise_error(IOError) @rd.close - lambda { @rd.readpartial(10) }.should raise_error(IOError) + -> { @rd.readpartial(10) }.should raise_error(IOError) end it "reads at most the specified number of bytes" do @@ -59,10 +59,10 @@ describe "IO#readpartial" do end it "discards the existing buffer content upon successful read" do - buffer = "existing" + buffer = +"existing content" @wr.write("hello world") @wr.close - @rd.readpartial(11, buffer) + @rd.readpartial(11, buffer).should.equal?(buffer) buffer.should == "hello world" end @@ -70,27 +70,46 @@ describe "IO#readpartial" do @wr.write("abc") @wr.close @rd.readpartial(10).should == 'abc' - lambda { @rd.readpartial(10) }.should raise_error(EOFError) + -> { @rd.readpartial(10) }.should raise_error(EOFError) end it "discards the existing buffer content upon error" do - buffer = 'hello' + buffer = +'hello' @wr.close - lambda { @rd.readpartial(1, buffer) }.should raise_error(EOFError) + -> { @rd.readpartial(1, buffer) }.should raise_error(EOFError) buffer.should be_empty end it "raises IOError if the stream is closed" do @wr.close - lambda { @rd.readpartial(1) }.should raise_error(IOError) + -> { @rd.readpartial(1) }.should raise_error(IOError) end it "raises ArgumentError if the negative argument is provided" do - lambda { @rd.readpartial(-1) }.should raise_error(ArgumentError) + -> { @rd.readpartial(-1) }.should raise_error(ArgumentError) end it "immediately returns an empty string if the length argument is 0" do @rd.readpartial(0).should == "" end + it "raises IOError if the stream is closed and the length argument is 0" do + @rd.close + -> { @rd.readpartial(0) }.should raise_error(IOError, "closed stream") + end + + it "clears and returns the given buffer if the length argument is 0" do + buffer = +"existing content" + @rd.readpartial(0, buffer).should == buffer + buffer.should == "" + end + + it "preserves the encoding of the given buffer" do + buffer = ''.encode(Encoding::ISO_8859_1) + @wr.write("abc") + @wr.close + @rd.readpartial(10, buffer) + + buffer.encoding.should == Encoding::ISO_8859_1 + end end diff --git a/spec/ruby/core/io/reopen_spec.rb b/spec/ruby/core/io/reopen_spec.rb index 84c23472b7..8ff0f217f4 100644 --- a/spec/ruby/core/io/reopen_spec.rb +++ b/spec/ruby/core/io/reopen_spec.rb @@ -33,29 +33,29 @@ describe "IO#reopen" do it "raises an IOError if the object returned by #to_io is closed" do obj = mock("io") obj.should_receive(:to_io).and_return(IOSpecs.closed_io) - lambda { @io.reopen obj }.should raise_error(IOError) + -> { @io.reopen obj }.should raise_error(IOError) end it "raises a TypeError if #to_io does not return an IO instance" do obj = mock("io") obj.should_receive(:to_io).and_return("something else") - lambda { @io.reopen obj }.should raise_error(TypeError) + -> { @io.reopen obj }.should raise_error(TypeError) end it "raises an IOError when called on a closed stream with an object" do @io.close obj = mock("io") obj.should_not_receive(:to_io) - lambda { @io.reopen(STDOUT) }.should raise_error(IOError) + -> { @io.reopen(STDOUT) }.should raise_error(IOError) end it "raises an IOError if the IO argument is closed" do - lambda { @io.reopen(IOSpecs.closed_io) }.should raise_error(IOError) + -> { @io.reopen(IOSpecs.closed_io) }.should raise_error(IOError) end it "raises an IOError when called on a closed stream with an IO" do @io.close - lambda { @io.reopen(STDOUT) }.should raise_error(IOError) + -> { @io.reopen(STDOUT) }.should raise_error(IOError) end end @@ -150,25 +150,25 @@ describe "IO#reopen with a String" do @io.close_on_exec = true @io.reopen @other_name - @io.close_on_exec?.should == true + @io.should.close_on_exec? @io.close_on_exec = false @io.reopen @other_name - @io.close_on_exec?.should == true + @io.should.close_on_exec? end it "creates the file if it doesn't exist if the IO is opened in write mode" do @io = new_io @name, "w" @io.reopen(@other_name) - File.exist?(@other_name).should be_true + File.should.exist?(@other_name) end it "creates the file if it doesn't exist if the IO is opened in write mode" do @io = new_io @name, "a" @io.reopen(@other_name) - File.exist?(@other_name).should be_true + File.should.exist?(@other_name) end end @@ -188,7 +188,7 @@ describe "IO#reopen with a String" do it "raises an Errno::ENOENT if the file does not exist and the IO is not opened in write mode" do @io = new_io @name, "r" - lambda { @io.reopen(@other_name) }.should raise_error(Errno::ENOENT) + -> { @io.reopen(@other_name) }.should raise_error(Errno::ENOENT) end end @@ -230,7 +230,7 @@ describe "IO#reopen with an IO" do end @io = new_io @name - @other_io = new_io @other_name, "r" + @other_io = IO.new(new_fd(@other_name, "r"), "r") end after :each do @@ -293,12 +293,12 @@ describe "IO#reopen with an IO" do @other_io.close_on_exec = true @io.close_on_exec = true @io.reopen @other_io - @io.close_on_exec?.should == true + @io.should.close_on_exec? @other_io.close_on_exec = false @io.close_on_exec = false @io.reopen @other_io - @io.close_on_exec?.should == true + @io.should.close_on_exec? end it "may change the class of the instance" do diff --git a/spec/ruby/core/io/rewind_spec.rb b/spec/ruby/core/io/rewind_spec.rb index d1ec7a69c7..5579cbd988 100644 --- a/spec/ruby/core/io/rewind_spec.rb +++ b/spec/ruby/core/io/rewind_spec.rb @@ -18,10 +18,21 @@ describe "IO#rewind" do @io.readline.should == "Voici la ligne une.\n" end + it "positions the instance to the beginning of output for write-only IO" do + name = tmp("io_rewind_spec") + io = File.open(name, "w") + io.write("Voici la ligne une.\n") + io.rewind + io.pos.should == 0 + ensure + io.close + rm_r name + end + it "positions the instance to the beginning of input and clears EOF" do value = @io.read @io.rewind - @io.eof?.should == false + @io.should_not.eof? value.should == @io.read end @@ -32,7 +43,11 @@ describe "IO#rewind" do @io.lineno.should == 0 end + it "returns 0" do + @io.rewind.should == 0 + end + it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.rewind }.should raise_error(IOError) + -> { IOSpecs.closed_io.rewind }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/seek_spec.rb b/spec/ruby/core/io/seek_spec.rb index 9978d7c0e7..2fa4a73ac9 100644 --- a/spec/ruby/core/io/seek_spec.rb +++ b/spec/ruby/core/io/seek_spec.rb @@ -17,7 +17,7 @@ describe "IO#seek" do end it "moves the read position relative to the current position with SEEK_CUR" do - lambda { @io.seek(-1) }.should raise_error(Errno::EINVAL) + -> { @io.seek(-1) }.should raise_error(Errno::EINVAL) @io.seek(10, IO::SEEK_CUR) @io.readline.should == "igne une.\n" @io.seek(-5, IO::SEEK_CUR) @@ -44,21 +44,21 @@ describe "IO#seek" do it "moves the read position and clears EOF with SEEK_SET" do value = @io.read @io.seek(0, IO::SEEK_SET) - @io.eof?.should == false + @io.should_not.eof? value.should == @io.read end it "moves the read position and clears EOF with SEEK_CUR" do value = @io.read @io.seek(-1, IO::SEEK_CUR) - @io.eof?.should == false + @io.should_not.eof? value[-1].should == @io.read[0] end it "moves the read position and clears EOF with SEEK_END" do value = @io.read @io.seek(-1, IO::SEEK_END) - @io.eof?.should == false + @io.should_not.eof? value[-1].should == @io.read[0] end diff --git a/spec/ruby/core/io/select_spec.rb b/spec/ruby/core/io/select_spec.rb index e69669efd2..3893e7620f 100644 --- a/spec/ruby/core/io/select_spec.rb +++ b/spec/ruby/core/io/select_spec.rb @@ -55,8 +55,8 @@ describe "IO.select" do end end - it "returns supplied objects correctly even when monitoring the same object in different arrays" do - filename = tmp("IO_select_pipe_file") + $$.to_s + it "returns supplied objects correctly when monitoring the same object in different arrays" do + filename = tmp("IO_select_pipe_file") io = File.open(filename, 'w+') result = IO.select [io], [io], nil, 0 result.should == [[io], [io], []] @@ -64,6 +64,17 @@ describe "IO.select" do rm_r filename end + it "returns the pipe read end in read set if the pipe write end is closed concurrently" do + main = Thread.current + t = Thread.new { + Thread.pass until main.stop? + @wr.close + } + IO.select([@rd]).should == [[@rd], [], []] + ensure + t.join + end + it "invokes to_io on supplied objects that are not IO and returns the supplied objects" do # make some data available @wr.write("foobar") @@ -80,28 +91,61 @@ describe "IO.select" do end it "raises TypeError if supplied objects are not IO" do - lambda { IO.select([Object.new]) }.should raise_error(TypeError) - lambda { IO.select(nil, [Object.new]) }.should raise_error(TypeError) + -> { IO.select([Object.new]) }.should raise_error(TypeError) + -> { IO.select(nil, [Object.new]) }.should raise_error(TypeError) obj = mock("io") obj.should_receive(:to_io).any_number_of_times.and_return(nil) - lambda { IO.select([obj]) }.should raise_error(TypeError) - lambda { IO.select(nil, [obj]) }.should raise_error(TypeError) + -> { IO.select([obj]) }.should raise_error(TypeError) + -> { IO.select(nil, [obj]) }.should raise_error(TypeError) end it "raises a TypeError if the specified timeout value is not Numeric" do - lambda { IO.select([@rd], nil, nil, Object.new) }.should raise_error(TypeError) + -> { IO.select([@rd], nil, nil, Object.new) }.should raise_error(TypeError) end it "raises TypeError if the first three arguments are not Arrays" do - lambda { IO.select(Object.new)}.should raise_error(TypeError) - lambda { IO.select(nil, Object.new)}.should raise_error(TypeError) - lambda { IO.select(nil, nil, Object.new)}.should raise_error(TypeError) + -> { IO.select(Object.new)}.should raise_error(TypeError) + -> { IO.select(nil, Object.new)}.should raise_error(TypeError) + -> { IO.select(nil, nil, Object.new)}.should raise_error(TypeError) end it "raises an ArgumentError when passed a negative timeout" do - lambda { IO.select(nil, nil, nil, -5)}.should raise_error(ArgumentError) + -> { IO.select(nil, nil, nil, -5)}.should raise_error(ArgumentError) + end + + describe "returns the available descriptors when the file descriptor" do + it "is in both read and error arrays" do + @wr.write("foobar") + result = IO.select([@rd], nil, [@rd]) + result.should == [[@rd], [], []] + end + + it "is in both write and error arrays" do + result = IO.select(nil, [@wr], [@wr]) + result.should == [[], [@wr], []] + end + + it "is in both read and write arrays" do + filename = tmp("IO_select_read_write_file") + w = File.open(filename, 'w+') + begin + IO.select([w], [w], []).should == [[w], [w], []] + ensure + w.close + rm_r filename + end + + IO.select([@wr], [@wr], []).should == [[], [@wr], []] + + @wr.write("foobar") + # CRuby on macOS returns [[@rd], [@rd], []], weird but we accept it here, probably only for pipe read-end + [ + [[@rd], [], []], + [[@rd], [@rd], []] + ].should.include? IO.select([@rd], [@rd], []) + end end end diff --git a/spec/ruby/core/io/set_encoding_by_bom_spec.rb b/spec/ruby/core/io/set_encoding_by_bom_spec.rb new file mode 100644 index 0000000000..30d5ce5a5a --- /dev/null +++ b/spec/ruby/core/io/set_encoding_by_bom_spec.rb @@ -0,0 +1,262 @@ +require_relative '../../spec_helper' + +describe "IO#set_encoding_by_bom" do + before :each do + @name = tmp('io_set_encoding_by_bom.txt') + touch(@name) + @io = new_io(@name, 'rb') + end + + after :each do + @io.close unless @io.closed? + rm_r @name + end + + it "returns nil if not readable" do + not_readable_io = new_io(@name, 'wb') + + not_readable_io.set_encoding_by_bom.should be_nil + not_readable_io.external_encoding.should == Encoding::ASCII_8BIT + ensure + not_readable_io.close + end + + it "returns the result encoding if found BOM UTF-8 sequence" do + File.binwrite(@name, "\u{FEFF}") + + @io.set_encoding_by_bom.should == Encoding::UTF_8 + @io.external_encoding.should == Encoding::UTF_8 + @io.read.b.should == "".b + @io.rewind + @io.set_encoding(Encoding::ASCII_8BIT) + + File.binwrite(@name, "\u{FEFF}abc") + + @io.set_encoding_by_bom.should == Encoding::UTF_8 + @io.external_encoding.should == Encoding::UTF_8 + @io.read.b.should == "abc".b + end + + it "returns the result encoding if found BOM UTF_16LE sequence" do + File.binwrite(@name, "\xFF\xFE") + + @io.set_encoding_by_bom.should == Encoding::UTF_16LE + @io.external_encoding.should == Encoding::UTF_16LE + @io.read.b.should == "".b + @io.rewind + @io.set_encoding(Encoding::ASCII_8BIT) + + File.binwrite(@name, "\xFF\xFEabc") + + @io.set_encoding_by_bom.should == Encoding::UTF_16LE + @io.external_encoding.should == Encoding::UTF_16LE + @io.read.b.should == "abc".b + end + + it "returns the result encoding if found BOM UTF_16BE sequence" do + File.binwrite(@name, "\xFE\xFF") + + @io.set_encoding_by_bom.should == Encoding::UTF_16BE + @io.external_encoding.should == Encoding::UTF_16BE + @io.read.b.should == "".b + @io.rewind + @io.set_encoding(Encoding::ASCII_8BIT) + + File.binwrite(@name, "\xFE\xFFabcd") + + @io.set_encoding_by_bom.should == Encoding::UTF_16BE + @io.external_encoding.should == Encoding::UTF_16BE + @io.read.b.should == "abcd".b + end + + it "returns the result encoding if found BOM UTF_32LE sequence" do + File.binwrite(@name, "\xFF\xFE\x00\x00") + + @io.set_encoding_by_bom.should == Encoding::UTF_32LE + @io.external_encoding.should == Encoding::UTF_32LE + @io.read.b.should == "".b + @io.rewind + @io.set_encoding(Encoding::ASCII_8BIT) + + File.binwrite(@name, "\xFF\xFE\x00\x00abc") + + @io.set_encoding_by_bom.should == Encoding::UTF_32LE + @io.external_encoding.should == Encoding::UTF_32LE + @io.read.b.should == "abc".b + end + + it "returns the result encoding if found BOM UTF_32BE sequence" do + File.binwrite(@name, "\x00\x00\xFE\xFF") + + @io.set_encoding_by_bom.should == Encoding::UTF_32BE + @io.external_encoding.should == Encoding::UTF_32BE + @io.read.b.should == "".b + @io.rewind + @io.set_encoding(Encoding::ASCII_8BIT) + + File.binwrite(@name, "\x00\x00\xFE\xFFabcd") + + @io.set_encoding_by_bom.should == Encoding::UTF_32BE + @io.external_encoding.should == Encoding::UTF_32BE + @io.read.b.should == "abcd".b + end + + it "returns nil if io is empty" do + @io.set_encoding_by_bom.should be_nil + @io.external_encoding.should == Encoding::ASCII_8BIT + end + + it "returns nil if UTF-8 BOM sequence is incomplete" do + File.write(@name, "\xEF") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\xEF".b + @io.rewind + + File.write(@name, "\xEFa") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\xEFa".b + @io.rewind + + File.write(@name, "\xEF\xBB") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\xEF\xBB".b + @io.rewind + + File.write(@name, "\xEF\xBBa") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\xEF\xBBa".b + end + + it "returns nil if UTF-16BE BOM sequence is incomplete" do + File.write(@name, "\xFE") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\xFE".b + @io.rewind + + File.write(@name, "\xFEa") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\xFEa".b + end + + it "returns nil if UTF-16LE/UTF-32LE BOM sequence is incomplete" do + File.write(@name, "\xFF") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\xFF".b + @io.rewind + + File.write(@name, "\xFFa") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\xFFa".b + end + + it "returns UTF-16LE if UTF-32LE BOM sequence is incomplete" do + File.write(@name, "\xFF\xFE") + + @io.set_encoding_by_bom.should == Encoding::UTF_16LE + @io.external_encoding.should == Encoding::UTF_16LE + @io.read.b.should == "".b + @io.rewind + @io.set_encoding(Encoding::ASCII_8BIT) + + File.write(@name, "\xFF\xFE\x00") + + @io.set_encoding_by_bom.should == Encoding::UTF_16LE + @io.external_encoding.should == Encoding::UTF_16LE + @io.read.b.should == "\x00".b + @io.rewind + @io.set_encoding(Encoding::ASCII_8BIT) + + File.write(@name, "\xFF\xFE\x00a") + + @io.set_encoding_by_bom.should == Encoding::UTF_16LE + @io.external_encoding.should == Encoding::UTF_16LE + @io.read.b.should == "\x00a".b + end + + it "returns nil if UTF-32BE BOM sequence is incomplete" do + File.write(@name, "\x00") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\x00".b + @io.rewind + + File.write(@name, "\x00a") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\x00a".b + @io.rewind + + File.write(@name, "\x00\x00") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\x00\x00".b + @io.rewind + + File.write(@name, "\x00\x00a") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\x00\x00a".b + @io.rewind + + File.write(@name, "\x00\x00\xFE") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\x00\x00\xFE".b + @io.rewind + + File.write(@name, "\x00\x00\xFEa") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read.b.should == "\x00\x00\xFEa".b + end + + it "returns nil if found BOM sequence not provided" do + File.write(@name, "abc") + + @io.set_encoding_by_bom.should == nil + @io.external_encoding.should == Encoding::ASCII_8BIT + @io.read(3).should == "abc".b + end + + it 'returns exception if io not in binary mode' do + not_binary_io = new_io(@name, 'r') + + -> { not_binary_io.set_encoding_by_bom }.should raise_error(ArgumentError, 'ASCII incompatible encoding needs binmode') + ensure + not_binary_io.close + end + + it 'returns exception if encoding already set' do + @io.set_encoding("utf-8") + + -> { @io.set_encoding_by_bom }.should raise_error(ArgumentError, 'encoding is set to UTF-8 already') + end + + it 'returns exception if encoding conversion is already set' do + @io.set_encoding(Encoding::UTF_8, Encoding::UTF_16BE) + + -> { @io.set_encoding_by_bom }.should raise_error(ArgumentError, 'encoding conversion is set') + end +end diff --git a/spec/ruby/core/io/set_encoding_spec.rb b/spec/ruby/core/io/set_encoding_spec.rb index 5aec6a96c3..22d9017635 100644 --- a/spec/ruby/core/io/set_encoding_spec.rb +++ b/spec/ruby/core/io/set_encoding_spec.rb @@ -1,7 +1,7 @@ require_relative '../../spec_helper' describe :io_set_encoding_write, shared: true do - it "sets the encodings to nil" do + it "sets the encodings to nil when they were set previously" do @io = new_io @name, "#{@object}:ibm437:ibm866" @io.set_encoding nil, nil @@ -9,6 +9,19 @@ describe :io_set_encoding_write, shared: true do @io.internal_encoding.should be_nil end + it "sets the encodings to nil when the IO is built with no explicit encoding" do + @io = new_io @name, @object + + # Checking our assumptions first + @io.external_encoding.should be_nil + @io.internal_encoding.should be_nil + + @io.set_encoding nil, nil + + @io.external_encoding.should be_nil + @io.internal_encoding.should be_nil + end + it "prevents the encodings from changing when Encoding defaults are changed" do @io = new_io @name, "#{@object}:utf-8:us-ascii" @io.set_encoding nil, nil @@ -38,6 +51,7 @@ describe "IO#set_encoding when passed nil, nil" do @external = Encoding.default_external @internal = Encoding.default_internal + # The defaults Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = nil @@ -113,6 +127,22 @@ describe "IO#set_encoding when passed nil, nil" do describe "with 'a+' mode" do it_behaves_like :io_set_encoding_write, nil, "a+" end + + describe "with standard IOs" do + it "correctly resets them" do + STDOUT.external_encoding.should == nil + STDOUT.internal_encoding.should == nil + + begin + STDOUT.set_encoding(Encoding::US_ASCII, Encoding::ISO_8859_1) + ensure + STDOUT.set_encoding(nil, nil) + end + + STDOUT.external_encoding.should == nil + STDOUT.internal_encoding.should == nil + end + end end describe "IO#set_encoding" do @@ -188,4 +218,21 @@ describe "IO#set_encoding" do @io.external_encoding.should == Encoding::UTF_8 @io.internal_encoding.should == Encoding::UTF_16BE end + + it "saves encoding options passed as a hash in the last argument" do + File.write(@name, "\xff") + io = File.open(@name) + io.set_encoding(Encoding::EUC_JP, Encoding::SHIFT_JIS, invalid: :replace, replace: ".") + io.read.should == "." + ensure + io.close + end + + it "raises ArgumentError when no arguments are given" do + -> { @io.set_encoding() }.should raise_error(ArgumentError) + end + + it "raises ArgumentError when too many arguments are given" do + -> { @io.set_encoding(1, 2, 3) }.should raise_error(ArgumentError) + end end diff --git a/spec/ruby/core/io/shared/binwrite.rb b/spec/ruby/core/io/shared/binwrite.rb index 1a88442a3b..e51093329b 100644 --- a/spec/ruby/core/io/shared/binwrite.rb +++ b/spec/ruby/core/io/shared/binwrite.rb @@ -21,12 +21,20 @@ describe :io_binwrite, shared: true do IO.send(@method, @filename, "abcde").should == 5 end + it "accepts options as a keyword argument" do + IO.send(@method, @filename, "hi", 0, flags: File::CREAT).should == 2 + + -> { + IO.send(@method, @filename, "hi", 0, {flags: File::CREAT}) + }.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 2..3)") + end + it "creates a file if missing" do fn = @filename + "xxx" begin - File.exist?(fn).should be_false + File.should_not.exist?(fn) IO.send(@method, fn, "test") - File.exist?(fn).should be_true + File.should.exist?(fn) ensure rm_r fn end @@ -35,9 +43,9 @@ describe :io_binwrite, shared: true do it "creates file if missing even if offset given" do fn = @filename + "xxx" begin - File.exist?(fn).should be_false + File.should_not.exist?(fn) IO.send(@method, fn, "test", 0) - File.exist?(fn).should be_true + File.should.exist?(fn) ensure rm_r fn end @@ -56,7 +64,7 @@ describe :io_binwrite, shared: true do end it "doesn't truncate and writes at the given offset after passing empty opts" do - IO.send(@method, @filename, "hello world!", 1, {}) + IO.send(@method, @filename, "hello world!", 1, **{}) File.read(@filename).should == "0hello world!34567890123456789" end @@ -67,12 +75,17 @@ describe :io_binwrite, shared: true do File.read(@filename).should == "\0\0foo" end + it "accepts a :flags option without :mode one" do + IO.send(@method, @filename, "hello, world!", flags: File::CREAT) + File.read(@filename).should == "hello, world!" + end + it "raises an error if readonly mode is specified" do - lambda { IO.send(@method, @filename, "abcde", mode: "r") }.should raise_error(IOError) + -> { IO.send(@method, @filename, "abcde", mode: "r") }.should raise_error(IOError) end it "truncates if empty :opts provided and offset skipped" do - IO.send(@method, @filename, "hello, world!", {}) + IO.send(@method, @filename, "hello, world!", **{}) File.read(@filename).should == "hello, world!" end end diff --git a/spec/ruby/core/io/shared/chars.rb b/spec/ruby/core/io/shared/chars.rb index 7f2edd2b6d..266566f221 100644 --- a/spec/ruby/core/io/shared/chars.rb +++ b/spec/ruby/core/io/shared/chars.rb @@ -46,11 +46,11 @@ describe :io_chars, shared: true do end it "raises an IOError when an enumerator created on a closed stream is accessed" do - lambda { IOSpecs.closed_io.send(@method).first }.should raise_error(IOError) + -> { IOSpecs.closed_io.send(@method).first }.should raise_error(IOError) end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.send(@method) {} }.should raise_error(IOError) + -> { IOSpecs.closed_io.send(@method) {} }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/shared/codepoints.rb b/spec/ruby/core/io/shared/codepoints.rb index a5062e7f79..6872846c1a 100644 --- a/spec/ruby/core/io/shared/codepoints.rb +++ b/spec/ruby/core/io/shared/codepoints.rb @@ -39,7 +39,7 @@ describe :io_codepoints, shared: true do it "raises an error if reading invalid sequence" do @io.pos = 60 # inside of a multibyte sequence - lambda { @enum.first }.should raise_error(ArgumentError) + -> { @enum.first }.should raise_error(ArgumentError) end it "does not change $_" do @@ -49,6 +49,6 @@ describe :io_codepoints, shared: true do end it "raises an IOError when self is not readable" do - lambda { IOSpecs.closed_io.send(@method).to_a }.should raise_error(IOError) + -> { IOSpecs.closed_io.send(@method).to_a }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/shared/each.rb b/spec/ruby/core/io/shared/each.rb index da562e03b1..0747f31b8a 100644 --- a/spec/ruby/core/io/shared/each.rb +++ b/spec/ruby/core/io/shared/each.rb @@ -33,12 +33,8 @@ describe :io_each, shared: true do $_.should == "test" end - it "returns self" do - @io.send(@method) { |l| l }.should equal(@io) - end - it "raises an IOError when self is not readable" do - lambda { IOSpecs.closed_io.send(@method) {} }.should raise_error(IOError) + -> { IOSpecs.closed_io.send(@method) {} }.should raise_error(IOError) end it "makes line count accessible via lineno" do @@ -74,9 +70,13 @@ describe :io_each, shared: true do describe "when limit is 0" do it "raises an ArgumentError" do # must pass block so Enumerator is evaluated and raises - lambda { @io.send(@method, 0){} }.should raise_error(ArgumentError) + -> { @io.send(@method, 0){} }.should raise_error(ArgumentError) end end + + it "does not accept Integers that don't fit in a C off_t" do + -> { @io.send(@method, 2**128){} }.should raise_error(RangeError) + end end describe "when passed a String containing one space as a separator" do @@ -113,6 +113,13 @@ describe :io_each, shared: true do @io.send(@method, "") { |s| ScratchPad << s } ScratchPad.recorded.should == IOSpecs.paragraphs end + + it "discards leading newlines" do + @io.readline + @io.readline + @io.send(@method, "") { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.paragraphs[1..-1] + end end describe "with both separator and limit" do @@ -152,6 +159,13 @@ describe :io_each, shared: true do @io.send(@method, "", 1024) { |s| ScratchPad << s } ScratchPad.recorded.should == IOSpecs.paragraphs end + + it "discards leading newlines" do + @io.readline + @io.readline + @io.send(@method, "", 1024) { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.paragraphs[1..-1] + end end end end @@ -161,6 +175,60 @@ describe :io_each, shared: true do @io.send(@method, chomp: true) { |s| ScratchPad << s } ScratchPad.recorded.should == IOSpecs.lines_without_newline_characters end + + it "raises exception when options passed as Hash" do + -> { + @io.send(@method, { chomp: true }) { |s| } + }.should raise_error(TypeError) + + -> { + @io.send(@method, "\n", 1, { chomp: true }) { |s| } + }.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 0..2)") + end + end + + describe "when passed chomp and a separator" do + it "yields each line without separator to the passed block" do + @io.send(@method, " ", chomp: true) { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.lines_space_separator_without_trailing_spaces + end + end + + describe "when passed chomp and empty line as a separator" do + it "yields each paragraph without trailing new line characters" do + @io.send(@method, "", 1024, chomp: true) { |s| ScratchPad << s } + ScratchPad.recorded.should == IOSpecs.paragraphs_without_trailing_new_line_characters + end + end + + describe "when passed chomp and nil as a separator" do + it "yields self's content" do + @io.pos = 100 + @io.send(@method, nil, chomp: true) { |s| ScratchPad << s } + ScratchPad.recorded.should == ["qui a linha cinco.\nHere is line six.\n"] + end + end + + describe "when passed chomp, nil as a separator, and a limit" do + it "yields each line of limit size without truncating trailing new line character" do + # 43 - is a size of the 1st paragraph in the file + @io.send(@method, nil, 43, chomp: true) { |s| ScratchPad << s } + + ScratchPad.recorded.should == [ + "Voici la ligne une.\nQui è la linea due.\n\n\n", + "Aquí está la línea tres.\n" + "Hier ist Zeile ", + "vier.\n\nEstá aqui a linha cinco.\nHere is li", + "ne six.\n" + ] + end + end + + describe "when passed too many arguments" do + it "raises ArgumentError" do + -> { + @io.send(@method, "", 1, "excess argument", chomp: true) {} + }.should raise_error(ArgumentError) + end end end @@ -168,12 +236,12 @@ describe :io_each_default_separator, shared: true do before :each do @io = IOSpecs.io_fixture "lines.txt" ScratchPad.record [] - @sep, $/ = $/, " " + suppress_warning {@sep, $/ = $/, " "} end after :each do @io.close if @io - $/ = @sep + suppress_warning {$/ = @sep} end it "uses $/ as the default line separator" do diff --git a/spec/ruby/core/io/shared/gets_ascii.rb b/spec/ruby/core/io/shared/gets_ascii.rb index 2a8fe3c9a5..2bd5470d99 100644 --- a/spec/ruby/core/io/shared/gets_ascii.rb +++ b/spec/ruby/core/io/shared/gets_ascii.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary describe :io_gets_ascii, shared: true do describe "with ASCII separator" do before :each do diff --git a/spec/ruby/core/io/shared/new.rb b/spec/ruby/core/io/shared/new.rb index f6069a4cdc..e84133493c 100644 --- a/spec/ruby/core/io/shared/new.rb +++ b/spec/ruby/core/io/shared/new.rb @@ -1,5 +1,7 @@ require_relative '../fixtures/classes' +# NOTE: should be synchronized with library/stringio/initialize_spec.rb + # This group of specs may ONLY contain specs that do successfully create # an IO instance from the file descriptor returned by #new_fd helper. describe :io_new, shared: true do @@ -18,50 +20,59 @@ describe :io_new, shared: true do rm_r @name end - it "creates an IO instance from a Fixnum argument" do + it "creates an IO instance from an Integer argument" do @io = IO.send(@method, @fd, "w") @io.should be_an_instance_of(IO) end it "creates an IO instance when STDOUT is closed" do - verbose, $VERBOSE = $VERBOSE, nil - stdout = STDOUT - stdout_file = tmp("stdout.txt") - - begin - @io = IO.send(@method, @fd, "w") - @io.should be_an_instance_of(IO) - ensure - STDOUT = stdout - $VERBOSE = verbose - rm_r stdout_file + suppress_warning do + stdout = STDOUT + stdout_file = tmp("stdout.txt") + + begin + @io = IO.send(@method, @fd, "w") + @io.should be_an_instance_of(IO) + ensure + STDOUT = stdout + rm_r stdout_file + end end end it "creates an IO instance when STDERR is closed" do - verbose, $VERBOSE = $VERBOSE, nil - stderr = STDERR - stderr_file = tmp("stderr.txt") - STDERR = new_io stderr_file - STDERR.close - - begin - @io = IO.send(@method, @fd, "w") - @io.should be_an_instance_of(IO) - ensure - STDERR = stderr - $VERBOSE = verbose - rm_r stderr_file + suppress_warning do + stderr = STDERR + stderr_file = tmp("stderr.txt") + STDERR = new_io stderr_file + STDERR.close + + begin + @io = IO.send(@method, @fd, "w") + @io.should be_an_instance_of(IO) + ensure + STDERR = stderr + rm_r stderr_file + end end end - it "calls #to_int on an object to convert to a Fixnum" do + it "calls #to_int on an object to convert to an Integer" do obj = mock("file descriptor") obj.should_receive(:to_int).and_return(@fd) @io = IO.send(@method, obj, "w") @io.should be_an_instance_of(IO) end + it "accepts options as keyword arguments" do + @io = IO.send(@method, @fd, "w", flags: File::CREAT) + @io.write("foo").should == 3 + + -> { + IO.send(@method, @fd, "w", {flags: File::CREAT}) + }.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 1..2)") + end + it "accepts a :mode option" do @io = IO.send(@method, @fd, mode: "w") @io.write("foo").should == 3 @@ -89,81 +100,81 @@ describe :io_new, shared: true do end it "uses the external encoding specified via the :external_encoding option" do - @io = IO.send(@method, @fd, 'w', {external_encoding: 'utf-8'}) + @io = IO.send(@method, @fd, 'w', external_encoding: 'utf-8') @io.external_encoding.to_s.should == 'UTF-8' end it "uses the internal encoding specified via the :internal_encoding option" do - @io = IO.send(@method, @fd, 'w', {internal_encoding: 'ibm866'}) + @io = IO.send(@method, @fd, 'w', internal_encoding: 'ibm866') @io.internal_encoding.to_s.should == 'IBM866' end it "uses the colon-separated encodings specified via the :encoding option" do - @io = IO.send(@method, @fd, 'w', {encoding: 'utf-8:ISO-8859-1'}) + @io = IO.send(@method, @fd, 'w', encoding: 'utf-8:ISO-8859-1') @io.external_encoding.to_s.should == 'UTF-8' @io.internal_encoding.to_s.should == 'ISO-8859-1' end it "uses the :encoding option as the external encoding when only one is given" do - @io = IO.send(@method, @fd, 'w', {encoding: 'ISO-8859-1'}) + @io = IO.send(@method, @fd, 'w', encoding: 'ISO-8859-1') @io.external_encoding.to_s.should == 'ISO-8859-1' end it "uses the :encoding options as the external encoding when it's an Encoding object" do - @io = IO.send(@method, @fd, 'w', {encoding: Encoding::ISO_8859_1}) + @io = IO.send(@method, @fd, 'w', encoding: Encoding::ISO_8859_1) @io.external_encoding.should == Encoding::ISO_8859_1 end it "ignores the :encoding option when the :external_encoding option is present" do - lambda { - @io = IO.send(@method, @fd, 'w', {external_encoding: 'utf-8', encoding: 'iso-8859-1:iso-8859-1'}) + -> { + @io = IO.send(@method, @fd, 'w', external_encoding: 'utf-8', encoding: 'iso-8859-1:iso-8859-1') }.should complain(/Ignoring encoding parameter/) @io.external_encoding.to_s.should == 'UTF-8' end it "ignores the :encoding option when the :internal_encoding option is present" do - lambda { - @io = IO.send(@method, @fd, 'w', {internal_encoding: 'ibm866', encoding: 'iso-8859-1:iso-8859-1'}) + -> { + @io = IO.send(@method, @fd, 'w', internal_encoding: 'ibm866', encoding: 'iso-8859-1:iso-8859-1') }.should complain(/Ignoring encoding parameter/) @io.internal_encoding.to_s.should == 'IBM866' end it "uses the encoding specified via the :mode option hash" do - @io = IO.send(@method, @fd, {mode: 'w:utf-8:ISO-8859-1'}) + @io = IO.send(@method, @fd, mode: 'w:utf-8:ISO-8859-1') @io.external_encoding.to_s.should == 'UTF-8' @io.internal_encoding.to_s.should == 'ISO-8859-1' end it "ignores the :internal_encoding option when the same as the external encoding" do - @io = IO.send(@method, @fd, 'w', {external_encoding: 'utf-8', internal_encoding: 'utf-8'}) + @io = IO.send(@method, @fd, 'w', external_encoding: 'utf-8', internal_encoding: 'utf-8') @io.external_encoding.to_s.should == 'UTF-8' @io.internal_encoding.to_s.should == '' end it "sets internal encoding to nil when passed '-'" do - @io = IO.send(@method, @fd, 'w', {external_encoding: 'utf-8', internal_encoding: '-'}) + @io = IO.send(@method, @fd, 'w', external_encoding: 'utf-8', internal_encoding: '-') @io.external_encoding.to_s.should == 'UTF-8' @io.internal_encoding.to_s.should == '' end it "sets binmode from mode string" do @io = IO.send(@method, @fd, 'wb') - @io.binmode?.should == true + @io.should.binmode? end it "does not set binmode without being asked" do @io = IO.send(@method, @fd, 'w') - @io.binmode?.should == false + @io.should_not.binmode? end it "sets binmode from :binmode option" do - @io = IO.send(@method, @fd, 'w', {binmode: true}) - @io.binmode?.should == true + @io = IO.send(@method, @fd, 'w', binmode: true) + @io.should.binmode? end it "does not set binmode from false :binmode" do - @io = IO.send(@method, @fd, 'w', {binmode: false}) - @io.binmode?.should == false + @io = IO.send(@method, @fd, 'w', binmode: false) + @io.should_not.binmode? end it "sets external encoding to binary with binmode in mode string" do @@ -173,7 +184,7 @@ describe :io_new, shared: true do # #5917 it "sets external encoding to binary with :binmode option" do - @io = IO.send(@method, @fd, 'w', {binmode: true}) + @io = IO.send(@method, @fd, 'w', binmode: true) @io.external_encoding.should == Encoding::BINARY end @@ -197,9 +208,30 @@ describe :io_new, shared: true do @io.internal_encoding.to_s.should == 'IBM866' end - it "accepts nil options" do - @io = IO.send(@method, @fd, 'w', nil) - @io.write("foo").should == 3 + it "does not use binary encoding when mode encoding is specified along with binmode: true option" do + @io = IO.send(@method, @fd, 'w:iso-8859-1', binmode: true) + @io.external_encoding.to_s.should == 'ISO-8859-1' + end + + it "does not use textmode argument when mode encoding is specified" do + @io = IO.send(@method, @fd, 'w:ascii-8bit', textmode: true) + @io.external_encoding.to_s.should == 'ASCII-8BIT' + end + + it "does not use binmode argument when external encoding is specified via the :external_encoding option" do + @io = IO.send(@method, @fd, 'w', binmode: true, external_encoding: 'iso-8859-1') + @io.external_encoding.to_s.should == 'ISO-8859-1' + end + + it "does not use textmode argument when external encoding is specified via the :external_encoding option" do + @io = IO.send(@method, @fd, 'w', textmode: true, external_encoding: 'ascii-8bit') + @io.external_encoding.to_s.should == 'ASCII-8BIT' + end + + it "raises ArgumentError for nil options" do + -> { + IO.send(@method, @fd, 'w', nil) + }.should raise_error(ArgumentError) end it "coerces mode with #to_str" do @@ -247,24 +279,24 @@ describe :io_new, shared: true do it "coerces options as third argument with #to_hash" do options = mock("options") options.should_receive(:to_hash).and_return({}) - @io = IO.send(@method, @fd, 'w', options) + @io = IO.send(@method, @fd, 'w', **options) end it "coerces options as second argument with #to_hash" do options = mock("options") options.should_receive(:to_hash).and_return({}) - @io = IO.send(@method, @fd, options) + @io = IO.send(@method, @fd, **options) end it "accepts an :autoclose option" do @io = IO.send(@method, @fd, 'w', autoclose: false) - @io.autoclose?.should == false + @io.should_not.autoclose? @io.autoclose = true end it "accepts any truthy option :autoclose" do @io = IO.send(@method, @fd, 'w', autoclose: 42) - @io.autoclose?.should == true + @io.should.autoclose? end end @@ -282,97 +314,100 @@ describe :io_new_errors, shared: true do end it "raises an Errno::EBADF if the file descriptor is not valid" do - lambda { IO.send(@method, -1, "w") }.should raise_error(Errno::EBADF) + -> { IO.send(@method, -1, "w") }.should raise_error(Errno::EBADF) end it "raises an IOError if passed a closed stream" do - lambda { IO.send(@method, IOSpecs.closed_io.fileno, 'w') }.should raise_error(IOError) + -> { IO.send(@method, IOSpecs.closed_io.fileno, 'w') }.should raise_error(IOError) end platform_is_not :windows do it "raises an Errno::EINVAL if the new mode is not compatible with the descriptor's current mode" do - lambda { IO.send(@method, @fd, "r") }.should raise_error(Errno::EINVAL) + -> { IO.send(@method, @fd, "r") }.should raise_error(Errno::EINVAL) end end it "raises ArgumentError if passed an empty mode string" do - lambda { IO.send(@method, @fd, "") }.should raise_error(ArgumentError) + -> { IO.send(@method, @fd, "") }.should raise_error(ArgumentError) end it "raises an error if passed modes two ways" do - lambda { + -> { IO.send(@method, @fd, "w", mode: "w") }.should raise_error(ArgumentError) end it "raises an error if passed encodings two ways" do - lambda { - @io = IO.send(@method, @fd, 'w:ISO-8859-1', {encoding: 'ISO-8859-1'}) + -> { + @io = IO.send(@method, @fd, 'w:ISO-8859-1', encoding: 'ISO-8859-1') }.should raise_error(ArgumentError) - lambda { - @io = IO.send(@method, @fd, 'w:ISO-8859-1', {external_encoding: 'ISO-8859-1'}) + -> { + @io = IO.send(@method, @fd, 'w:ISO-8859-1', external_encoding: 'ISO-8859-1') }.should raise_error(ArgumentError) - lambda { - @io = IO.send(@method, @fd, 'w:ISO-8859-1:UTF-8', {internal_encoding: 'ISO-8859-1'}) + -> { + @io = IO.send(@method, @fd, 'w:ISO-8859-1', internal_encoding: 'ISO-8859-1') + }.should raise_error(ArgumentError) + -> { + @io = IO.send(@method, @fd, 'w:ISO-8859-1:UTF-8', internal_encoding: 'ISO-8859-1') }.should raise_error(ArgumentError) end it "raises an error if passed matching binary/text mode two ways" do - lambda { + -> { @io = IO.send(@method, @fd, "wb", binmode: true) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, "wt", textmode: true) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, "wb", textmode: false) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, "wt", binmode: false) }.should raise_error(ArgumentError) end it "raises an error if passed conflicting binary/text mode two ways" do - lambda { + -> { @io = IO.send(@method, @fd, "wb", binmode: false) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, "wt", textmode: false) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, "wb", textmode: true) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, "wt", binmode: true) }.should raise_error(ArgumentError) end it "raises an error when trying to set both binmode and textmode" do - lambda { + -> { @io = IO.send(@method, @fd, "w", textmode: true, binmode: true) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, File::Constants::WRONLY, textmode: true, binmode: true) }.should raise_error(ArgumentError) end it "raises ArgumentError if not passed a hash or nil for options" do - lambda { + -> { @io = IO.send(@method, @fd, 'w', false) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, false, false) }.should raise_error(ArgumentError) - lambda { + -> { @io = IO.send(@method, @fd, nil, false) }.should raise_error(ArgumentError) end - it "raises TypeError if passed a hash for mode and nil for options" do - lambda { + it "raises ArgumentError if passed a hash for mode and nil for options" do + -> { @io = IO.send(@method, @fd, {mode: 'w'}, nil) - }.should raise_error(TypeError) + }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/core/io/shared/pos.rb b/spec/ruby/core/io/shared/pos.rb index fef7ab2bf7..3fdd3eb2b3 100644 --- a/spec/ruby/core/io/shared/pos.rb +++ b/spec/ruby/core/io/shared/pos.rb @@ -19,7 +19,7 @@ describe :io_pos, shared: true do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.send(@method) }.should raise_error(IOError) + -> { IOSpecs.closed_io.send(@method) }.should raise_error(IOError) end it "resets #eof?" do @@ -27,7 +27,7 @@ describe :io_pos, shared: true do io.read 1 io.read 1 io.send(@method) - io.eof?.should == false + io.should_not.eof? end end end @@ -60,13 +60,19 @@ describe :io_set_pos, shared: true do end end - it "does not accept Bignums that don't fit in a C long" do + it "raises TypeError when cannot convert implicitly argument to Integer" do File.open @fname do |io| - lambda { io.send @method, 2**128 }.should raise_error(RangeError) + -> { io.send @method, Object.new }.should raise_error(TypeError, "no implicit conversion of Object into Integer") + end + end + + it "does not accept Integers that don't fit in a C off_t" do + File.open @fname do |io| + -> { io.send @method, 2**128 }.should raise_error(RangeError) end end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.send @method, 0 }.should raise_error(IOError) + -> { IOSpecs.closed_io.send @method, 0 }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/shared/readlines.rb b/spec/ruby/core/io/shared/readlines.rb index 08d41e0a4c..6c1fa11a59 100644 --- a/spec/ruby/core/io/shared/readlines.rb +++ b/spec/ruby/core/io/shared/readlines.rb @@ -1,11 +1,11 @@ describe :io_readlines, shared: true do it "raises TypeError if the first parameter is nil" do - lambda { IO.send(@method, nil, &@object) }.should raise_error(TypeError) + -> { IO.send(@method, nil, &@object) }.should raise_error(TypeError) end it "raises an Errno::ENOENT if the file does not exist" do name = tmp("nonexistent.txt") - lambda { IO.send(@method, name, &@object) }.should raise_error(Errno::ENOENT) + -> { IO.send(@method, name, &@object) }.should raise_error(Errno::ENOENT) end it "yields a single string with entire content when the separator is nil" do @@ -54,25 +54,42 @@ describe :io_readlines_options_19, shared: true do (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator end - describe "when the object is a Fixnum" do + describe "when the object is an Integer" do before :each do @sep = $/ end after :each do - $/ = @sep + suppress_warning {$/ = @sep} end it "defaults to $/ as the separator" do - $/ = " " + suppress_warning {$/ = " "} result = IO.send(@method, @name, 10, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end - it "uses the object as a limit if it is a Fixnum" do + it "uses the object as a limit if it is an Integer" do result = IO.send(@method, @name, 10, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_limit end + + it "ignores the object as a limit if it is negative" do + result = IO.send(@method, @name, -2, &@object) + (result ? result : ScratchPad.recorded).should == IOSpecs.lines + end + + it "does not accept Integers that don't fit in a C off_t" do + -> { IO.send(@method, @name, 2**128, &@object) }.should raise_error(RangeError) + end + + ruby_bug "#18767", ""..."3.3" do + describe "when passed limit" do + it "raises ArgumentError when passed 0 as a limit" do + -> { IO.send(@method, @name, 0, &@object) }.should raise_error(ArgumentError) + end + end + end end describe "when the object is a String" do @@ -82,38 +99,40 @@ describe :io_readlines_options_19, shared: true do end it "accepts non-ASCII data as separator" do - result = IO.send(@method, @name, "\303\250".force_encoding("utf-8"), &@object) + result = IO.send(@method, @name, "\303\250".dup.force_encoding("utf-8"), &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_arbitrary_separator end end - describe "when the object is a Hash" do - it "uses the value as the options hash" do - result = IO.send(@method, @name, mode: "r", &@object) - (result ? result : ScratchPad.recorded).should == IOSpecs.lines + describe "when the object is an options Hash" do + it "raises TypeError exception" do + -> { + IO.send(@method, @name, { chomp: true }, &@object) + }.should raise_error(TypeError) end end - end - describe "when passed name, object, object" do - describe "when the first object is a Fixnum" do - it "uses the second object as an options Hash" do - lambda do - IO.send(@method, @filename, 10, mode: "w", &@object) - end.should raise_error(IOError) - end + describe "when the object is neither Integer nor String" do + it "raises TypeError exception" do + obj = mock("not io readlines limit") - it "calls #to_hash to convert the second object to a Hash" do - options = mock("io readlines options Hash") - options.should_receive(:to_hash).and_return({ mode: "w" }) - lambda do - IO.send(@method, @filename, 10, options, &@object) - end.should raise_error(IOError) + -> { + IO.send(@method, @name, obj, &@object) + }.should raise_error(TypeError) end end + end + describe "when passed name, keyword arguments" do + it "uses the keyword arguments as options" do + result = IO.send(@method, @name, mode: "r", &@object) + (result ? result : ScratchPad.recorded).should == IOSpecs.lines + end + end + + describe "when passed name, object, object" do describe "when the first object is a String" do - it "uses the second object as a limit if it is a Fixnum" do + it "uses the second object as a limit if it is an Integer" do result = IO.send(@method, @name, " ", 10, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end @@ -124,32 +143,18 @@ describe :io_readlines_options_19, shared: true do result = IO.send(@method, @name, " ", limit, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end - - it "uses the second object as an options Hash" do - lambda do - IO.send(@method, @filename, " ", mode: "w", &@object) - end.should raise_error(IOError) - end - - it "calls #to_hash to convert the second object to a Hash" do - options = mock("io readlines options Hash") - options.should_receive(:to_hash).and_return({ mode: "w" }) - lambda do - IO.send(@method, @filename, " ", options, &@object) - end.should raise_error(IOError) - end end - describe "when the first object is not a String or Fixnum" do + describe "when the first object is not a String or Integer" do it "calls #to_str to convert the object to a String" do sep = mock("io readlines separator") sep.should_receive(:to_str).at_least(1).and_return(" ") - result = IO.send(@method, @name, sep, 10, mode: "r", &@object) + result = IO.send(@method, @name, sep, 10, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end - it "uses the second object as a limit if it is a Fixnum" do - result = IO.send(@method, @name, " ", 10, mode: "r", &@object) + it "uses the second object as a limit if it is an Integer" do + result = IO.send(@method, @name, " ", 10, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end @@ -159,24 +164,57 @@ describe :io_readlines_options_19, shared: true do result = IO.send(@method, @name, " ", limit, &@object) (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end + end + + describe "when the second object is neither Integer nor String" do + it "raises TypeError exception" do + obj = mock("not io readlines limit") + + -> { + IO.send(@method, @name, " ", obj, &@object) + }.should raise_error(TypeError) + end + end - it "uses the second object as an options Hash" do - lambda do + describe "when the second object is an options Hash" do + it "raises TypeError exception" do + -> { + IO.send(@method, @name, "", { chomp: true }, &@object) + }.should raise_error(TypeError) + end + end + end + + describe "when passed name, object, keyword arguments" do + describe "when the first object is an Integer" do + it "uses the keyword arguments as options" do + -> do + IO.send(@method, @filename, 10, mode: "w", &@object) + end.should raise_error(IOError) + end + end + + describe "when the first object is a String" do + it "uses the keyword arguments as options" do + -> do IO.send(@method, @filename, " ", mode: "w", &@object) end.should raise_error(IOError) end + end + + describe "when the first object is not a String or Integer" do + it "uses the keyword arguments as options" do + sep = mock("io readlines separator") + sep.should_receive(:to_str).at_least(1).and_return(" ") - it "calls #to_hash to convert the second object to a Hash" do - options = mock("io readlines options Hash") - options.should_receive(:to_hash).and_return({ mode: "w" }) - lambda do - IO.send(@method, @filename, " ", options, &@object) + -> do + IO.send(@method, @filename, sep, mode: "w", &@object) end.should raise_error(IOError) end end end - describe "when passed name, separator, limit, options" do + describe "when passed name, separator, limit, keyword arguments" do it "calls #to_path to convert the name object" do name = mock("io name to_path") name.should_receive(:to_path).and_return(@name) @@ -198,12 +236,24 @@ describe :io_readlines_options_19, shared: true do (result ? result : ScratchPad.recorded).should == IOSpecs.lines_space_separator_limit end - it "calls #to_hash to convert the options object" do - options = mock("io readlines options Hash") - options.should_receive(:to_hash).and_return({ mode: "w" }) - lambda do - IO.send(@method, @filename, " ", 10, options, &@object) + it "uses the keyword arguments as options" do + -> do + IO.send(@method, @filename, " ", 10, mode: "w", &@object) end.should raise_error(IOError) end + + describe "when passed chomp, nil as a separator, and a limit" do + it "yields each line of limit size without truncating trailing new line character" do + # 43 - is a size of the 1st paragraph in the file + result = IO.send(@method, @name, nil, 43, chomp: true, &@object) + + (result ? result : ScratchPad.recorded).should == [ + "Voici la ligne une.\nQui è la linea due.\n\n\n", + "Aquí está la línea tres.\n" + "Hier ist Zeile ", + "vier.\n\nEstá aqui a linha cinco.\nHere is li", + "ne six.\n" + ] + end + end end end diff --git a/spec/ruby/core/io/shared/tty.rb b/spec/ruby/core/io/shared/tty.rb index 947b887f81..89ac08ec86 100644 --- a/spec/ruby/core/io/shared/tty.rb +++ b/spec/ruby/core/io/shared/tty.rb @@ -7,8 +7,7 @@ describe :io_tty, shared: true do # check to enabled tty File.open('/dev/tty') {} rescue Errno::ENXIO - # workaround for not configured environment like OS X - 1.should == 1 + skip "workaround for not configured environment like OS X" else File.open('/dev/tty') { |f| f.send(@method) }.should == true end @@ -20,6 +19,6 @@ describe :io_tty, shared: true do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.send @method }.should raise_error(IOError) + -> { IOSpecs.closed_io.send @method }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/shared/write.rb b/spec/ruby/core/io/shared/write.rb index aa6b3eedeb..964064746a 100644 --- a/spec/ruby/core/io/shared/write.rb +++ b/spec/ruby/core/io/shared/write.rb @@ -23,7 +23,7 @@ describe :io_write, shared: true do end it "checks if the file is writable if writing more than zero bytes" do - lambda { @readonly_file.send(@method, "abcde") }.should raise_error(IOError) + -> { @readonly_file.send(@method, "abcde") }.should raise_error(IOError) end it "returns the number of bytes written" do @@ -50,7 +50,7 @@ describe :io_write, shared: true do it "does not warn if called after IO#read" do @file.read(5) - lambda { @file.send(@method, "fghij") }.should_not complain + -> { @file.send(@method, "fghij") }.should_not complain end it "writes to the current position after IO#read" do @@ -66,7 +66,7 @@ describe :io_write, shared: true do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.send(@method, "hello") }.should raise_error(IOError) + -> { IOSpecs.closed_io.send(@method, "hello") }.should raise_error(IOError) end describe "on a pipe" do @@ -85,11 +85,11 @@ describe :io_write, shared: true do @r.read.should == "foo" end - # [ruby-core:90895] MJIT worker may leave fd open in a forked child. - # For instance, MJIT creates a worker before @r.close with fork(), @r.close happens, - # and the MJIT worker keeps the pipe open until the worker execve(). - # TODO: consider acquiring GVL from MJIT worker. - guard_not -> { defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? } do + # [ruby-core:90895] RJIT worker may leave fd open in a forked child. + # For instance, RJIT creates a worker before @r.close with fork(), @r.close happens, + # and the RJIT worker keeps the pipe open until the worker execve(). + # TODO: consider acquiring GVL from RJIT worker. + guard_not -> { defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled? } do it "raises Errno::EPIPE if the read end is closed and does not die from SIGPIPE" do @r.close -> { @w.send(@method, "foo") }.should raise_error(Errno::EPIPE, /Broken pipe/) @@ -97,3 +97,58 @@ describe :io_write, shared: true do end end end + +describe :io_write_transcode, shared: true do + before :each do + @transcode_filename = tmp("io_write_transcode") + end + + after :each do + rm_r @transcode_filename + end + + it "transcodes the given string when the external encoding is set and neither is BINARY" do + utf8_str = "hello" + + File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file| + file.external_encoding.should == Encoding::UTF_16BE + file.send(@method, utf8_str) + end + + result = File.binread(@transcode_filename) + expected = [0, 104, 0, 101, 0, 108, 0, 108, 0, 111] # UTF-16BE bytes for "hello" + + result.bytes.should == expected + end + + it "transcodes the given string when the external encoding is set and the string encoding is BINARY" do + str = "été".b + + File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file| + file.external_encoding.should == Encoding::UTF_16BE + -> { file.send(@method, str) }.should raise_error(Encoding::UndefinedConversionError) + end + end +end + +describe :io_write_no_transcode, shared: true do + before :each do + @transcode_filename = tmp("io_write_no_transcode") + end + + after :each do + rm_r @transcode_filename + end + + it "does not transcode the given string even when the external encoding is set" do + utf8_str = "hello" + + File.open(@transcode_filename, "w", external_encoding: Encoding::UTF_16BE) do |file| + file.external_encoding.should == Encoding::UTF_16BE + file.send(@method, utf8_str) + end + + result = File.binread(@transcode_filename) + result.bytes.should == utf8_str.bytes + end +end diff --git a/spec/ruby/core/io/stat_spec.rb b/spec/ruby/core/io/stat_spec.rb index d46d4105ca..717c45d0a3 100644 --- a/spec/ruby/core/io/stat_spec.rb +++ b/spec/ruby/core/io/stat_spec.rb @@ -3,7 +3,8 @@ require_relative 'fixtures/classes' describe "IO#stat" do before :each do - @io = IO.popen 'cat', "r+" + cmd = platform_is(:windows) ? 'rem' : 'cat' + @io = IO.popen cmd, "r+" end after :each do @@ -11,7 +12,7 @@ describe "IO#stat" do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.stat }.should raise_error(IOError) + -> { IOSpecs.closed_io.stat }.should raise_error(IOError) end it "returns a File::Stat object for the stream" do diff --git a/spec/ruby/core/io/sync_spec.rb b/spec/ruby/core/io/sync_spec.rb index c8c1c5a57e..993b7ee244 100644 --- a/spec/ruby/core/io/sync_spec.rb +++ b/spec/ruby/core/io/sync_spec.rb @@ -27,7 +27,7 @@ describe "IO#sync=" do end it "raises an IOError on closed stream" do - lambda { IOSpecs.closed_io.sync = true }.should raise_error(IOError) + -> { IOSpecs.closed_io.sync = true }.should raise_error(IOError) end end @@ -45,7 +45,7 @@ describe "IO#sync" do end it "raises an IOError on closed stream" do - lambda { IOSpecs.closed_io.sync }.should raise_error(IOError) + -> { IOSpecs.closed_io.sync }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/sysopen_spec.rb b/spec/ruby/core/io/sysopen_spec.rb index 91f70c3ca9..7ad379df3a 100644 --- a/spec/ruby/core/io/sysopen_spec.rb +++ b/spec/ruby/core/io/sysopen_spec.rb @@ -13,7 +13,7 @@ describe "IO.sysopen" do it "returns the file descriptor for a given path" do @fd = IO.sysopen(@filename, "w") - @fd.should be_kind_of(Fixnum) + @fd.should be_kind_of(Integer) @fd.should_not equal(0) end @@ -21,7 +21,7 @@ describe "IO.sysopen" do platform_is_not :windows do it "works on directories" do @fd = IO.sysopen(tmp("")) # /tmp - @fd.should be_kind_of(Fixnum) + @fd.should be_kind_of(Integer) @fd.should_not equal(0) end end @@ -33,7 +33,7 @@ describe "IO.sysopen" do end it "accepts a mode as second argument" do - lambda { @fd = IO.sysopen(@filename, "w") }.should_not raise_error + -> { @fd = IO.sysopen(@filename, "w") }.should_not raise_error @fd.should_not equal(0) end diff --git a/spec/ruby/core/io/sysread_spec.rb b/spec/ruby/core/io/sysread_spec.rb index 4062620367..d56a27b3af 100644 --- a/spec/ruby/core/io/sysread_spec.rb +++ b/spec/ruby/core/io/sysread_spec.rb @@ -6,7 +6,7 @@ describe "IO#sysread on a file" do @file_name = tmp("IO_sysread_file") + $$.to_s File.open(@file_name, "w") do |f| # write some stuff - f.write("012345678901234567890123456789") + f.write("012345678901234567890123456789\nabcdef") end @file = File.open(@file_name, "r+") end @@ -21,25 +21,25 @@ describe "IO#sysread on a file" do end it "reads the specified number of bytes from the file to the buffer" do - buf = "" # empty buffer + buf = +"" # empty buffer @file.sysread(15, buf).should == buf buf.should == "012345678901234" @file.rewind - buf = "ABCDE" # small buffer + buf = +"ABCDE" # small buffer @file.sysread(15, buf).should == buf buf.should == "012345678901234" @file.rewind - buf = "ABCDE" * 5 # large buffer + buf = +"ABCDE" * 5 # large buffer @file.sysread(15, buf).should == buf buf.should == "012345678901234" end it "coerces the second argument to string and uses it as a buffer" do - buf = "ABCDE" + buf = +"ABCDE" (obj = mock("buff")).should_receive(:to_str).any_number_of_times.and_return(buf) @file.sysread(15, obj).should == buf buf.should == "012345678901234" @@ -50,6 +50,11 @@ describe "IO#sysread on a file" do @file.sysread(5).should == "56789" end + it "raises an error when called after buffered reads" do + @file.readline + -> { @file.sysread(5) }.should raise_error(IOError) + end + it "reads normally even when called immediately after a buffered IO#read" do @file.read(15) @file.sysread(5).should == "56789" @@ -58,13 +63,13 @@ describe "IO#sysread on a file" do it "does not raise error if called after IO#read followed by IO#write" do @file.read(5) @file.write("abcde") - lambda { @file.sysread(5) }.should_not raise_error(IOError) + -> { @file.sysread(5) }.should_not raise_error(IOError) end it "does not raise error if called after IO#read followed by IO#syswrite" do @file.read(5) @file.syswrite("abcde") - lambda { @file.sysread(5) }.should_not raise_error(IOError) + -> { @file.sysread(5) }.should_not raise_error(IOError) end it "reads updated content after the flushed buffered IO#write" do @@ -77,7 +82,37 @@ describe "IO#sysread on a file" do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.sysread(5) }.should raise_error(IOError) + -> { IOSpecs.closed_io.sysread(5) }.should raise_error(IOError) + end + + it "immediately returns an empty string if the length argument is 0" do + @file.sysread(0).should == "" + end + + it "immediately returns the given buffer if the length argument is 0" do + buffer = +"existing content" + @file.sysread(0, buffer).should == buffer + buffer.should == "existing content" + end + + it "discards the existing buffer content upon successful read" do + buffer = +"existing content" + @file.sysread(11, buffer).should.equal?(buffer) + buffer.should == "01234567890" + end + + it "discards the existing buffer content upon error" do + buffer = +"existing content" + @file.seek(0, :END) + -> { @file.sysread(1, buffer) }.should raise_error(EOFError) + buffer.should be_empty + end + + it "preserves the encoding of the given buffer" do + buffer = ''.encode(Encoding::ISO_8859_1) + string = @file.sysread(10, buffer) + + buffer.encoding.should == Encoding::ISO_8859_1 end end @@ -95,4 +130,8 @@ describe "IO#sysread" do @write.syswrite "ab" @read.sysread(3).should == "ab" end + + it "raises ArgumentError when length is less than 0" do + -> { @read.sysread(-1) }.should raise_error(ArgumentError) + end end diff --git a/spec/ruby/core/io/sysseek_spec.rb b/spec/ruby/core/io/sysseek_spec.rb index 84e0a1a4ac..002f2a14eb 100644 --- a/spec/ruby/core/io/sysseek_spec.rb +++ b/spec/ruby/core/io/sysseek_spec.rb @@ -4,7 +4,7 @@ require_relative 'fixtures/classes' require_relative 'shared/pos' describe "IO#sysseek" do - it_behaves_like :io_set_pos, :seek + it_behaves_like :io_set_pos, :sysseek end describe "IO#sysseek" do @@ -23,7 +23,12 @@ describe "IO#sysseek" do it "raises an error when called after buffered reads" do @io.readline - lambda { @io.sysseek(-5, IO::SEEK_CUR) }.should raise_error(IOError) + -> { @io.sysseek(-5, IO::SEEK_CUR) }.should raise_error(IOError) + end + + it "seeks normally even when called immediately after a buffered IO#read" do + @io.read(15) + @io.sysseek(-5, IO::SEEK_CUR).should == 10 end it "moves the read position relative to the start with SEEK_SET" do @@ -36,7 +41,7 @@ describe "IO#sysseek" do # this is the safest way of checking the EOF when # sys-* methods are invoked - lambda { @io.sysread(1) }.should raise_error(EOFError) + -> { @io.sysread(1) }.should raise_error(EOFError) @io.sysseek(-25, IO::SEEK_END) @io.sysread(7).should == "cinco.\n" diff --git a/spec/ruby/core/io/syswrite_spec.rb b/spec/ruby/core/io/syswrite_spec.rb index a4dc8328aa..8bf61a27c3 100644 --- a/spec/ruby/core/io/syswrite_spec.rb +++ b/spec/ruby/core/io/syswrite_spec.rb @@ -29,15 +29,25 @@ describe "IO#syswrite on a file" do end end + it "does not modify the passed argument" do + File.open(@filename, "w") do |f| + f.set_encoding(Encoding::IBM437) + # A character whose codepoint differs between UTF-8 and IBM437 + f.syswrite("ƒ".freeze) + end + + File.binread(@filename).bytes.should == [198, 146] + end + it "warns if called immediately after a buffered IO#write" do @file.write("abcde") - lambda { @file.syswrite("fghij") }.should complain(/syswrite/) + -> { @file.syswrite("fghij") }.should complain(/syswrite/) end it "does not warn if called after IO#write with intervening IO#sysread" do @file.syswrite("abcde") @file.sysread(5) - lambda { @file.syswrite("fghij") }.should_not complain + -> { @file.syswrite("fghij") }.should_not complain end it "writes to the actual file position when called after buffered IO#read" do @@ -68,4 +78,5 @@ end describe "IO#syswrite" do it_behaves_like :io_write, :syswrite + it_behaves_like :io_write_no_transcode, :syswrite end diff --git a/spec/ruby/core/io/to_i_spec.rb b/spec/ruby/core/io/to_i_spec.rb index 7cd9e170d2..acf138c663 100644 --- a/spec/ruby/core/io/to_i_spec.rb +++ b/spec/ruby/core/io/to_i_spec.rb @@ -7,6 +7,6 @@ describe "IO#to_i" do end it "raises IOError on closed stream" do - lambda { IOSpecs.closed_io.to_i }.should raise_error(IOError) + -> { IOSpecs.closed_io.to_i }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/try_convert_spec.rb b/spec/ruby/core/io/try_convert_spec.rb index cff56ba618..a9e99de7aa 100644 --- a/spec/ruby/core/io/try_convert_spec.rb +++ b/spec/ruby/core/io/try_convert_spec.rb @@ -38,12 +38,12 @@ describe "IO.try_convert" do it "raises a TypeError if the object does not return an IO from #to_io" do obj = mock("io") obj.should_receive(:to_io).and_return("io") - lambda { IO.try_convert(obj) }.should raise_error(TypeError) + -> { IO.try_convert(obj) }.should raise_error(TypeError, "can't convert MockObject to IO (MockObject#to_io gives String)") end it "propagates an exception raised by #to_io" do obj = mock("io") obj.should_receive(:to_io).and_raise(TypeError.new) - lambda{ IO.try_convert(obj) }.should raise_error(TypeError) + ->{ IO.try_convert(obj) }.should raise_error(TypeError) end end diff --git a/spec/ruby/core/io/ungetbyte_spec.rb b/spec/ruby/core/io/ungetbyte_spec.rb index f5f9a11be1..716743a6af 100644 --- a/spec/ruby/core/io/ungetbyte_spec.rb +++ b/spec/ruby/core/io/ungetbyte_spec.rb @@ -36,38 +36,19 @@ describe "IO#ungetbyte" do @io.getbyte.should == 97 end - ruby_version_is ''...'2.6' do - it "puts back one byte for a Fixnum argument..." do - @io.ungetbyte(4095).should be_nil + it "never raises RangeError" do + for i in [4095, 0x4f7574206f6620636861722072616e67ff] do + @io.ungetbyte(i).should be_nil @io.getbyte.should == 255 end - - it "... but not for Bignum argument (eh?)" do - lambda { - @io.ungetbyte(0x4f7574206f6620636861722072616e6765) - }.should raise_error(TypeError) - end - end - - ruby_version_is '2.6'...'2.6.1' do - it "is an RangeError if the integer is not in 8bit" do - for i in [4095, 0x4f7574206f6620636861722072616e6765] do - lambda { @io.ungetbyte(i) }.should raise_error(RangeError) - end - end end - ruby_version_is '2.6.1' do - it "never raises RangeError" do - for i in [4095, 0x4f7574206f6620636861722072616e67ff] do - @io.ungetbyte(i).should be_nil - @io.getbyte.should == 255 - end - end + it "raises IOError on stream not opened for reading" do + -> { STDOUT.ungetbyte(42) }.should raise_error(IOError, "not opened for reading") end it "raises an IOError if the IO is closed" do @io.close - lambda { @io.ungetbyte(42) }.should raise_error(IOError) + -> { @io.ungetbyte(42) }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/ungetc_spec.rb b/spec/ruby/core/io/ungetc_spec.rb index 7d090d0e5d..47a4e99ebf 100644 --- a/spec/ruby/core/io/ungetc_spec.rb +++ b/spec/ruby/core/io/ungetc_spec.rb @@ -74,10 +74,10 @@ describe "IO#ungetc" do touch(@empty) File.open(@empty) { |empty| - empty.eof?.should == true + empty.should.eof? empty.getc.should == nil empty.ungetc(100) - empty.eof?.should == false + empty.should_not.eof? } end @@ -100,13 +100,12 @@ describe "IO#ungetc" do it "makes subsequent unbuffered operations to raise IOError" do @io.getc @io.ungetc(100) - lambda { @io.sysread(1) }.should raise_error(IOError) + -> { @io.sysread(1) }.should raise_error(IOError) end - it "does not affect the stream and returns nil when passed nil" do + it "raises TypeError if passed nil" do @io.getc.should == ?V - @io.ungetc(nil) - @io.getc.should == ?o + proc{@io.ungetc(nil)}.should raise_error(TypeError) end it "puts one or more characters back in the stream" do @@ -127,9 +126,13 @@ describe "IO#ungetc" do @io.ungetc(100).should be_nil end + it "raises IOError on stream not opened for reading" do + -> { STDOUT.ungetc(100) }.should raise_error(IOError, "not opened for reading") + end + it "raises IOError on closed stream" do @io.getc @io.close - lambda { @io.ungetc(100) }.should raise_error(IOError) + -> { @io.ungetc(100) }.should raise_error(IOError) end end diff --git a/spec/ruby/core/io/write_nonblock_spec.rb b/spec/ruby/core/io/write_nonblock_spec.rb index 285d1af376..5bfc690f9b 100644 --- a/spec/ruby/core/io/write_nonblock_spec.rb +++ b/spec/ruby/core/io/write_nonblock_spec.rb @@ -31,15 +31,26 @@ platform_is_not :windows do end end + it "does not modify the passed argument" do + File.open(@filename, "w") do |f| + f.set_encoding(Encoding::IBM437) + # A character whose codepoint differs between UTF-8 and IBM437 + f.write_nonblock("ƒ".freeze) + end + + File.binread(@filename).bytes.should == [198, 146] + end + it "checks if the file is writable if writing zero bytes" do - lambda { - @readonly_file.write_nonblock("") + -> { + @readonly_file.write_nonblock("") }.should raise_error(IOError) end end describe "IO#write_nonblock" do it_behaves_like :io_write, :write_nonblock + it_behaves_like :io_write_no_transcode, :write_nonblock end end @@ -54,7 +65,7 @@ describe 'IO#write_nonblock' do end it "raises an exception extending IO::WaitWritable when the write would block" do - lambda { + -> { loop { @write.write_nonblock('a' * 10_000) } }.should raise_error(IO::WaitWritable) { |e| platform_is_not :windows do @@ -68,8 +79,10 @@ describe 'IO#write_nonblock' do context "when exception option is set to false" do it "returns :wait_writable when the operation would block" do - loop { break if @write.write_nonblock("a" * 10_000, exception: false) == :wait_writable } - 1.should == 1 + loop { + break if @write.write_nonblock("a" * 10_000, exception: false) == :wait_writable + } + @write.write_nonblock("a" * 10_000, exception: false).should == :wait_writable end end @@ -77,7 +90,7 @@ describe 'IO#write_nonblock' do it 'sets the IO in nonblock mode' do require 'io/nonblock' @write.write_nonblock('a') - @write.nonblock?.should == true + @write.should.nonblock? end end end diff --git a/spec/ruby/core/io/write_spec.rb b/spec/ruby/core/io/write_spec.rb index 5fb5cc7013..e58100f846 100644 --- a/spec/ruby/core/io/write_spec.rb +++ b/spec/ruby/core/io/write_spec.rb @@ -21,11 +21,7 @@ describe "IO#write on a file" do end it "does not check if the file is writable if writing zero bytes" do - lambda { @readonly_file.write("") }.should_not raise_error - end - - it "returns a length of 0 when writing a blank string" do - @file.write('').should == 0 + -> { @readonly_file.write("") }.should_not raise_error end before :each do @@ -40,10 +36,44 @@ describe "IO#write on a file" do Encoding.default_internal = @internal end + it "returns a length of 0 when writing a blank string" do + @file.write('').should == 0 + end + + it "returns a length of 0 when writing blank strings" do + @file.write('', '', '').should == 0 + end + + it "returns a length of 0 when passed no arguments" do + @file.write().should == 0 + end + it "returns the number of bytes written" do @file.write("hellø").should == 6 end + it "does not modify the passed argument" do + File.open(@filename, "w") do |f| + f.set_encoding(Encoding::IBM437) + # A character whose codepoint differs between UTF-8 and IBM437 + f.write("ƒ".freeze) + end + + File.binread(@filename).bytes.should == [159] + end + + it "does not modify arguments when passed multiple arguments and external encoding not set" do + a, b = "a".freeze, "b".freeze + + File.open(@filename, "w") do |f| + f.write(a, b) + end + + File.binread(@filename).bytes.should == [97, 98] + a.encoding.should == Encoding::UTF_8 + b.encoding.should == Encoding::UTF_8 + end + it "uses the encoding from the given option for non-ascii encoding" do File.open(@filename, "w", encoding: Encoding::UTF_32LE) do |file| file.write("hi").should == 8 @@ -51,15 +81,32 @@ describe "IO#write on a file" do File.binread(@filename).should == "h\u0000\u0000\u0000i\u0000\u0000\u0000" end - it "uses an :open_args option" do - IO.write(@filename, 'hi', open_args: ["w", nil, {encoding: Encoding::UTF_32LE}]).should == 8 + it "uses the encoding from the given option for non-ascii encoding even if in binary mode" do + File.open(@filename, "w", encoding: Encoding::UTF_32LE, binmode: true) do |file| + file.should.binmode? + file.write("hi").should == 8 + end + File.binread(@filename).should == "h\u0000\u0000\u0000i\u0000\u0000\u0000" + + File.open(@filename, "wb", encoding: Encoding::UTF_32LE) do |file| + file.should.binmode? + file.write("hi").should == 8 + end + File.binread(@filename).should == "h\u0000\u0000\u0000i\u0000\u0000\u0000" + end + + it "uses the encoding from the given option for non-ascii encoding when multiple arguments passes" do + File.open(@filename, "w", encoding: Encoding::UTF_32LE) do |file| + file.write("h", "i").should == 8 + end + File.binread(@filename).should == "h\u0000\u0000\u0000i\u0000\u0000\u0000" end it "raises a invalid byte sequence error if invalid bytes are being written" do # pack "\xFEhi" to avoid utf-8 conflict xFEhi = ([254].pack('C*') + 'hi').force_encoding('utf-8') File.open(@filename, "w", encoding: Encoding::US_ASCII) do |file| - lambda { file.write(xFEhi) }.should raise_error(Encoding::InvalidByteSequenceError) + -> { file.write(xFEhi) }.should raise_error(Encoding::InvalidByteSequenceError) end end @@ -72,6 +119,20 @@ describe "IO#write on a file" do res = "H#{ë}ll#{ö}" File.binread(@filename).should == res.force_encoding(Encoding::BINARY) end + + platform_is_not :windows do + it "writes binary data if no encoding is given and multiple arguments passed" do + File.open(@filename, "w") do |file| + file.write("\x87".b, "ą") # 0x87 isn't a valid UTF-8 binary representation of a character + end + File.binread(@filename).bytes.should == [0x87, 0xC4, 0x85] + + File.open(@filename, "w") do |file| + file.write("\x61".encode("utf-32le"), "ą") + end + File.binread(@filename).bytes.should == [0x61, 0x00, 0x00, 0x00, 0xC4, 0x85] + end + end end describe "IO.write" do @@ -86,10 +147,44 @@ describe "IO.write" do File.read(@filename).should == "\0\0hi" end + it "requires mode to be specified in :open_args" do + -> { + IO.write(@filename, 'hi', open_args: [{encoding: Encoding::UTF_32LE, binmode: true}]) + }.should raise_error(IOError, "not opened for writing") + + IO.write(@filename, 'hi', open_args: ["w", {encoding: Encoding::UTF_32LE, binmode: true}]).should == 8 + IO.write(@filename, 'hi', open_args: [{encoding: Encoding::UTF_32LE, binmode: true, mode: "w"}]).should == 8 + end + + it "requires mode to be specified in :open_args even if flags option passed" do + -> { + IO.write(@filename, 'hi', open_args: [{encoding: Encoding::UTF_32LE, binmode: true, flags: File::CREAT}]) + }.should raise_error(IOError, "not opened for writing") + + IO.write(@filename, 'hi', open_args: ["w", {encoding: Encoding::UTF_32LE, binmode: true, flags: File::CREAT}]).should == 8 + IO.write(@filename, 'hi', open_args: [{encoding: Encoding::UTF_32LE, binmode: true, flags: File::CREAT, mode: "w"}]).should == 8 + end + it "uses the given encoding and returns the number of bytes written" do IO.write(@filename, 'hi', mode: "w", encoding: Encoding::UTF_32LE).should == 8 end + it "raises ArgumentError if encoding is specified in mode parameter and is given as :encoding option" do + -> { + IO.write(@filename, 'hi', mode: "w:UTF-16LE:UTF-16BE", encoding: Encoding::UTF_32LE) + }.should raise_error(ArgumentError, "encoding specified twice") + + -> { + IO.write(@filename, 'hi', mode: "w:UTF-16BE", encoding: Encoding::UTF_32LE) + }.should raise_error(ArgumentError, "encoding specified twice") + end + + it "writes the file with the permissions in the :perm parameter" do + rm_r @filename + IO.write(@filename, 'write :perm spec', mode: "w", perm: 0o755).should == 16 + (File.stat(@filename).mode & 0o777) == 0o755 + end + it "writes binary data if no encoding is given" do IO.write(@filename, 'Hëllö'.encode('ISO-8859-1')) xEB = [235].pack('C*') @@ -108,32 +203,46 @@ describe "IO.write" do rm_r @fifo end - it "writes correctly" do - thr = Thread.new do - IO.read(@fifo) - end - begin - string = "hi" - IO.write(@fifo, string).should == string.length - ensure - thr.join + # rb_cloexec_open() is currently missing a retry on EINTR. + # @ioquatix is looking into fixing it. Quarantined until it's done. + quarantine! do + it "writes correctly" do + thr = Thread.new do + IO.read(@fifo) + end + begin + string = "hi" + IO.write(@fifo, string).should == string.length + ensure + thr.join + end end end end + + ruby_version_is "3.3"..."4.0" do + # https://bugs.ruby-lang.org/issues/19630 + it "warns about deprecation given a path with a pipe" do + -> { + -> { + IO.write("|cat", "xxx") + }.should output_to_fd("xxx") + }.should complain(/IO process creation with a leading '\|'/) + end + end end end describe "IO#write" do it_behaves_like :io_write, :write + it_behaves_like :io_write_transcode, :write - ruby_version_is "2.5" do - it "accepts multiple arguments" do - IO.pipe do |r, w| - w.write("foo", "bar") - w.close + it "accepts multiple arguments" do + IO.pipe do |r, w| + w.write("foo", "bar") + w.close - r.read.should == "foobar" - end + r.read.should == "foobar" end end end @@ -164,3 +273,25 @@ platform_is :windows do end end end + +describe "IO#write on STDOUT" do + # https://bugs.ruby-lang.org/issues/14413 + platform_is_not :windows do + it "raises SignalException SIGPIPE if the stream is closed instead of Errno::EPIPE like other IOs" do + stderr_file = tmp("stderr") + begin + IO.popen([*ruby_exe, "-e", "loop { puts :ok }"], "r", err: stderr_file) do |io| + io.gets.should == "ok\n" + io.close + end + status = $? + status.should_not.success? + status.should.signaled? + Signal.signame(status.termsig).should == 'PIPE' + File.read(stderr_file).should.empty? + ensure + rm_r stderr_file + end + end + end +end |
