summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/readlines_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/io/readlines_spec.rb')
-rw-r--r--spec/ruby/core/io/readlines_spec.rb135
1 files changed, 91 insertions, 44 deletions
diff --git a/spec/ruby/core/io/readlines_spec.rb b/spec/ruby/core/io/readlines_spec.rb
index 22ba844b52..640e253200 100644
--- a/spec/ruby/core/io/readlines_spec.rb
+++ b/spec/ruby/core/io/readlines_spec.rb
@@ -1,32 +1,32 @@
# -*- encoding: utf-8 -*-
-require File.expand_path('../../../spec_helper', __FILE__)
-require File.expand_path('../fixtures/classes', __FILE__)
-require File.expand_path('../shared/readlines', __FILE__)
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
+require_relative 'shared/readlines'
describe "IO#readlines" do
before :each do
@io = IOSpecs.io_fixture "lines.txt"
- @orig_exteenc = Encoding.default_external
+ @orig_extenc = Encoding.default_external
Encoding.default_external = Encoding::UTF_8
end
after :each do
@io.close unless @io.closed?
- Encoding.default_external = @orig_exteenc
+ Encoding.default_external = @orig_extenc
end
it "raises an IOError if the stream is closed" do
@io.close
- lambda { @io.readlines }.should raise_error(IOError)
+ -> { @io.readlines }.should.raise(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
@@ -37,12 +37,12 @@ describe "IO#readlines" do
describe "when passed no arguments" do
it "updates self's position" do
@io.readlines
- @io.pos.should eql(137)
+ @io.pos.should.eql?(137)
end
it "updates self's lineno based on the number of lines read" do
@io.readlines
- @io.lineno.should eql(9)
+ @io.lineno.should.eql?(9)
end
it "does not change $_" do
@@ -81,12 +81,12 @@ describe "IO#readlines" do
it "updates self's lineno based on the number of lines read" do
@io.readlines("r")
- @io.lineno.should eql(5)
+ @io.lineno.should.eql?(5)
end
it "updates self's position based on the number of characters read" do
@io.readlines("r")
- @io.pos.should eql(137)
+ @io.pos.should.eql?(137)
end
it "does not change $_" 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(ArgumentError)
end
- with_feature :fork 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(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(TypeError)
+
+ -> {
+ @io.readlines("\n", 1, { chomp: true })
+ }.should.raise(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,15 +143,15 @@ describe "IO#readlines" do
end
it "raises an IOError if the stream is opened for append only" do
- lambda do
- File.open(@name, fmode("a:utf-8")) { |f| f.readlines }
- end.should raise_error(IOError)
+ -> do
+ File.open(@name, "a:utf-8") { |f| f.readlines }
+ end.should.raise(IOError)
end
it "raises an IOError if the stream is opened for write only" do
- lambda do
- File.open(@name, fmode("w:utf-8")) { |f| f.readlines }
- end.should raise_error(IOError)
+ -> do
+ File.open(@name, "w:utf-8") { |f| f.readlines }
+ end.should.raise(IOError)
end
end
@@ -169,6 +174,48 @@ 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
+
+ # 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
+
it_behaves_like :io_readlines, :readlines
it_behaves_like :io_readlines_options_19, :readlines
end
@@ -184,27 +231,27 @@ 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
Encoding.default_external = Encoding::UTF_8
lines = IO.readlines(@name)
- lines.all? { |s| s.encoding == Encoding::UTF_8 }.should be_true
+ lines.all? { |s| s.encoding == Encoding::UTF_8 }.should == true
end
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
+ lines.all? { |s| s.encoding == Encoding::UTF_16 }.should == true
end
- it "ignores the default internal encoding if the external encoding is ASCII-8BIT" do
- Encoding.default_external = Encoding::ASCII_8BIT
+ it "ignores the default internal encoding if the external encoding is BINARY" do
+ Encoding.default_external = Encoding::BINARY
Encoding.default_internal = Encoding::UTF_8
lines = IO.readlines(@name)
- lines.all? { |s| s.encoding == Encoding::ASCII_8BIT }.should be_true
+ lines.all? { |s| s.encoding == Encoding::BINARY }.should == true
end
end