Skip to content

Commit

Permalink
[GR-18163] Fix specs on Ruby 2.7 and address comments
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/3626
  • Loading branch information
eregon committed Jan 26, 2023
2 parents f911355 + 5ac6ce0 commit ff00639
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 43 deletions.
14 changes: 7 additions & 7 deletions lib/truffle/pathname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -806,26 +806,26 @@ class Pathname # * IO *
#
# This method has existed since 1.8.1.
#
def each_line(*args, **kw, &block) # :yield: line
IO.foreach(@path, *args, **kw, &block)
def each_line(...) # :yield: line
IO.foreach(@path, ...)
end

# See <tt>IO.read</tt>. Returns all data from the file, or the first +N+ bytes
# if specified.
def read(*args, **kw) IO.read(@path, *args, **kw) end
def read(...) IO.read(@path, ...) end

# See <tt>IO.binread</tt>. Returns all the bytes from the file, or the first +N+
# if specified.
def binread(*args) IO.binread(@path, *args) end

# See <tt>IO.write</tt>. Returns the number of bytes written to the file.
def write(*args, **kw) IO.write(@path, *args, **kw) end
def write(...) IO.write(@path, ...) end

# See <tt>IO.binwrite</tt>. Returns the number of bytes written to the file.
def binwrite(*args) IO.binwrite(@path, *args) end

# See <tt>IO.readlines</tt>. Returns all the lines from the file.
def readlines(*args, **kw) IO.readlines(@path, *args, **kw) end
def readlines(...) IO.readlines(@path, ...) end

# See <tt>IO.sysopen</tt>.
def sysopen(*args) IO.sysopen(@path, *args) end
Expand Down Expand Up @@ -870,8 +870,8 @@ def ftype() File.ftype(@path) end
def make_link(old) File.link(old, @path) end

# See <tt>File.open</tt>. Opens the file for reading or writing.
def open(*args, **kw, &block) # :yield: file
File.open(@path, *args, **kw, &block)
def open(...) # :yield: file
File.open(@path, ...)
end

# See <tt>File.readlink</tt>. Read symbolic link.
Expand Down
37 changes: 31 additions & 6 deletions spec/ruby/core/file/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@

-> { @fh.puts("test") }.should raise_error(IOError)
@fh.read.should == ""
File.should.exist?(@file)
end

it "returns a new read-only File when mode is not specified but flags option is present" do
@fh = File.new(@file, flags: File::CREAT)

-> { @fh.puts("test") }.should raise_error(IOError)
@fh.read.should == ""
File.should.exist?(@file)
end

it "creates a new file when use File::EXCL mode" do
Expand Down Expand Up @@ -132,6 +134,26 @@
File.should.exist?(@file)
end

it "returns a new read-only File when use File::RDONLY|File::CREAT mode" do
@fh = File.new(@file, File::RDONLY|File::CREAT)
@fh.should be_kind_of(File)
File.should.exist?(@file)

# it's read-only
-> { @fh.puts("test") }.should raise_error(IOError)
@fh.read.should == ""
end

it "returns a new read-only File when use File::CREAT mode" do
@fh = File.new(@file, File::CREAT)
@fh.should be_kind_of(File)
File.should.exist?(@file)

# it's read-only
-> { @fh.puts("test") }.should raise_error(IOError)
@fh.read.should == ""
end

it "coerces filename using to_str" do
name = mock("file")
name.should_receive(:to_str).and_return(@file)
Expand All @@ -146,13 +168,16 @@
File.should.exist?(@file)
end

it "accepts options as a keyword argument" do
@fh = File.new(@file, 'w', 0755, flags: @flags)
@fh.should be_kind_of(File)
ruby_version_is "3.0" do
it "accepts options as a keyword argument" do
@fh = File.new(@file, 'w', 0755, flags: @flags)
@fh.should be_kind_of(File)
@fh.close

-> {
@fh = File.new(@file, 'w', 0755, {flags: @flags})
}.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 1..3)")
-> {
@fh = File.new(@file, 'w', 0755, {flags: @flags})
}.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 1..3)")
end
end

it "bitwise-ORs mode and flags option" do
Expand Down
14 changes: 8 additions & 6 deletions spec/ruby/core/file/open_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -565,13 +565,15 @@
File.open(@file, 'wb+') {|f| f.external_encoding.should == Encoding::BINARY}
end

it "accepts options as a keyword argument" do
@fh = File.open(@file, 'w', 0755, flags: File::CREAT)
@fh.should be_an_instance_of(File)
ruby_version_is "3.0" do
it "accepts options as a keyword argument" do
@fh = File.open(@file, 'w', 0755, flags: File::CREAT)
@fh.should be_an_instance_of(File)

-> {
File.open(@file, 'w', 0755, {flags: File::CREAT})
}.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 1..3)")
-> {
File.open(@file, 'w', 0755, {flags: File::CREAT})
}.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 1..3)")
end
end

it "uses the second argument as an options Hash" do
Expand Down
16 changes: 9 additions & 7 deletions spec/ruby/core/io/initialize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@
@io.fileno.should == fd
end

it "accepts options as keyword arguments" do
fd = new_fd @name, "w:utf-8"
ruby_version_is "3.0" do
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)
@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)")
-> {
@io.send(:initialize, fd, "w", {flags: File::CREAT})
}.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 1..2)")
end
end

it "raises a TypeError when passed an IO" do
Expand Down
13 changes: 8 additions & 5 deletions spec/ruby/core/io/read_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
IO.read(p)
end

it "accepts options as keyword arguments" do
IO.read(@fname, 3, 0, mode: "r+").should == @contents[0, 3]
ruby_version_is "3.0" do
# 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/)
-> {
IO.read(@fname, 3, 0, {mode: "r+"})
}.should raise_error(ArgumentError, /wrong number of arguments/)
end
end

it "accepts an empty options Hash" do
Expand Down
14 changes: 8 additions & 6 deletions spec/ruby/core/io/shared/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@
@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
ruby_version_is "3.0" do
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)")
-> {
IO.send(@method, @fd, "w", {flags: File::CREAT})
}.should raise_error(ArgumentError, "wrong number of arguments (given 3, expected 1..2)")
end
end

it "accepts a :mode option" do
Expand Down
14 changes: 8 additions & 6 deletions spec/ruby/core/kernel/open_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@
-> { open }.should raise_error(ArgumentError)
end

it "accepts options as keyword arguments" do
@file = open(@name, "r", 0666, flags: File::CREAT)
@file.should be_kind_of(File)
ruby_version_is "3.0" do
it "accepts options as keyword arguments" do
@file = open(@name, "r", 0666, flags: File::CREAT)
@file.should be_kind_of(File)

-> {
open(@name, "r", 0666, {flags: File::CREAT})
}.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 1..3)")
-> {
open(@name, "r", 0666, {flags: File::CREAT})
}.should raise_error(ArgumentError, "wrong number of arguments (given 4, expected 1..3)")
end
end

describe "when given an object that responds to to_open" do
Expand Down

0 comments on commit ff00639

Please sign in to comment.