Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rubinius/rubinius
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: e401fc7a1265
Choose a base ref
...
head repository: rubinius/rubinius
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 391db8d72bc9
Choose a head ref
  • 4 commits
  • 4 files changed
  • 1 contributor

Commits on Feb 5, 2016

  1. Moved ARGF.read encoding specs to shared spec file

    Yorick Peterse committed Feb 5, 2016
    Copy the full SHA
    cb7ee92 View commit details
  2. ARGF.read should use ASCII 8BIT, not US_ASCII

    Yorick Peterse committed Feb 5, 2016
    Copy the full SHA
    05f2a7e View commit details
  3. Added specs for ARGF.read_nonblock

    Yorick Peterse committed Feb 5, 2016
    Copy the full SHA
    68ab57c View commit details
  4. Added ARGF.read_nonblock

    Fixes #3594
    Yorick Peterse committed Feb 5, 2016
    Copy the full SHA
    391db8d View commit details
Showing with 121 additions and 24 deletions.
  1. +23 −0 core/argf.rb
  2. +74 −0 spec/ruby/core/argf/read_nonblock_spec.rb
  3. +1 −24 spec/ruby/core/argf/read_spec.rb
  4. +23 −0 spec/ruby/core/argf/shared/read.rb
23 changes: 23 additions & 0 deletions core/argf.rb
Original file line number Diff line number Diff line change
@@ -363,6 +363,29 @@ def read(bytes=nil, output=nil)
output
end

def read_nonblock(maxlen, output = nil, exception: true)
output ||= default_value

unless advance!
output.clear
raise EOFError, "ARGF at end"
end

begin
out = @stream.read_nonblock(maxlen, output, exception: exception)

return out if out == :wait_readable
rescue EOFError => e
raise e if @use_stdin_only

@stream.close
@advance = true
advance! or raise e
end

return output
end

#
# Read next line of text.
#
74 changes: 74 additions & 0 deletions spec/ruby/core/argf/read_nonblock_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../shared/read', __FILE__)

describe 'ARGF.read_nonblock' do
it_behaves_like :argf_read, :read_nonblock

before do
@file1_name = fixture(__FILE__, 'file1.txt')
@file2_name = fixture(__FILE__, 'file2.txt')

@file1 = File.read(@file1_name)
@file2 = File.read(@file2_name)

@chunk1 = File.read(@file1_name, 4)
@chunk2 = File.read(@file2_name, 4)
end

after do
ARGF.close unless ARGF.closed?
end

it 'reads up to the given amount of bytes' do
argv [@file1_name] do
ARGF.read_nonblock(4).should == @chunk1
end
end

describe 'when using multiple files' do
it 'reads up to the given amount of bytes from the first file' do
argv [@file1_name, @file2_name] do
ARGF.read_nonblock(4).should == @chunk1
end
end

it 'returns an empty String when reading after having read the first file in its entirety' do
argv [@file1_name, @file2_name] do
ARGF.read_nonblock(File.size(@file1_name)).should == @file1
ARGF.read_nonblock(4).should == ''
end
end
end

describe 'when ARGV is empty' do
it 'raises EOFError' do
proc { argv [] { ARGF.read_nonblock(4) } }.should raise_error(EOFError)
end
end

it 'reads up to the given bytes from STDIN' do
stdin = ruby_exe('print ARGF.read_nonblock(4)', :args => "< #{@file1_name}")

stdin.should == @chunk1
end

it 'reads up to the given bytes from a file when a file and STDIN are present' do
stdin = ruby_exe("print ARGF.read_nonblock(4)", :args => "#{@file1_name} - < #{@file2_name}")

stdin.should == @chunk1
end

it 'raises IO::EAGAINWaitReadable when STDIN is empty' do
input = 'ARGF.read_nonblock(4) rescue print $!.class'
stdin = ruby_exe(input, escape: true)

stdin.should == 'IO::EAGAINWaitReadable'
end

it 'returns :wait_readable when the :exception is set to false' do
input = 'p ARGF.read_nonblock(4, nil, exception: false)'
stdin = ruby_exe(input, escape: true)

stdin.strip.should == ':wait_readable'
end
end
25 changes: 1 addition & 24 deletions spec/ruby/core/argf/read_spec.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
it_behaves_like :argf_read, :read

before :each do

@file1_name = fixture __FILE__, "file1.txt"
@file2_name = fixture __FILE__, "file2.txt"
@stdin_name = fixture __FILE__, "stdin.txt"
@@ -66,27 +66,4 @@
ARGF.read.should == @file1 + @file1
end
end

with_feature :encoding do

before :each do
@external = Encoding.default_external
@internal = Encoding.default_internal

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = nil
end

after :each do
Encoding.default_external = @external
Encoding.default_internal = @internal
end

it "reads the contents of the file with default encoding" do
Encoding.default_external = Encoding::US_ASCII
argv [@file1_name, @file2_name] do
ARGF.read.encoding.should == Encoding::US_ASCII
end
end
end
end
23 changes: 23 additions & 0 deletions spec/ruby/core/argf/shared/read.rb
Original file line number Diff line number Diff line change
@@ -59,4 +59,27 @@
end
end
end

with_feature :encoding do
before :each do
@external = Encoding.default_external
@internal = Encoding.default_internal

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = nil
end

after :each do
Encoding.default_external = @external
Encoding.default_internal = @internal
end

it "reads the contents of the file with default encoding" do
Encoding.default_external = Encoding::ASCII_8BIT

argv [@file1_name, @file2_name] do
ARGF.send(@method, 4).encoding.should == Encoding::ASCII_8BIT
end
end
end
end