Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions internal/vm/io_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,54 @@ func (vm *VM) registerIOClassMethods(cIO, cFile *RClass) {
def("binwrite", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value {
return vm.ioWriteFile(args)
})
// IO.copy_stream(src, dst, copy_length = nil, src_offset = nil): copy bytes from
// src to dst, each of which may be a path String or an IO. Returns the number
// of bytes copied. src_offset is honoured for a path but rejected for a
// (non-fd) IO such as StringIO, matching MRI.
def("copy_stream", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value {
if len(args) < 2 {
raise("ArgumentError", "wrong number of arguments (given %d, expected 2..4)", len(args))
}
length, hasLen := -1, false
if len(args) >= 3 && !object.IsNil(args[2]) {
length, hasLen = int(intArg(args[2])), true
}
srcOffset, hasOff := -1, false
if len(args) >= 4 && !object.IsNil(args[3]) {
srcOffset, hasOff = int(intArg(args[3])), true
}
var data *object.String
if p, ok := args[0].(*object.String); ok {
ra := []object.Value{p}
if hasLen {
ra = append(ra, object.IntValue(int64(length)))
if hasOff {
ra = append(ra, object.IntValue(int64(srcOffset)))
}
}
data = vm.ioReadFile(ra, true).(*object.String)
} else {
if hasOff {
raise("ArgumentError", "cannot specify src_offset for non-IO")
}
var ra []object.Value
if hasLen {
ra = append(ra, object.IntValue(int64(length)))
}
r := vm.send(args[0], "read", ra, nil)
if s, ok := r.(*object.String); ok {
data = s
} else {
data = object.NewString("")
}
}
if p, ok := args[1].(*object.String); ok {
vm.ioWriteFile([]object.Value{p, data})
} else {
vm.send(args[1], "write", []object.Value{data}, nil)
}
return object.IntValue(int64(len(data.Bytes())))
})
def("foreach", func(vm *VM, _ object.Value, args []object.Value, blk *Proc) object.Value {
pos, _ := splitIOOpts(args)
if len(pos) == 0 {
Expand Down
29 changes: 29 additions & 0 deletions internal/vm/io_class_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,32 @@ func TestIOClassHelpers(t *testing.T) {
t.Error("modeBase wrong")
}
}

// TestIOCopyStream covers IO.copy_stream across path and StringIO sources and
// destinations, the copy_length and src_offset arguments, and the
// src_offset-for-non-IO ArgumentError. Paths use Dir.mktmpdir so the test is
// cross-platform. Asserted against MRI Ruby 4.0.6.
func TestIOCopyStream(t *testing.T) {
pre := `require "tmpdir"; require "stringio"; `
cases := []struct{ src, want string }{
// StringIO → StringIO, with and without a copy length.
{pre + `a = StringIO.new("abcdef"); b = StringIO.new; n = IO.copy_stream(a, b); p [n, b.string]`, "[6, \"abcdef\"]\n"},
{pre + `a = StringIO.new("abcdef"); b = StringIO.new; n = IO.copy_stream(a, b, 3); p [n, b.string]`, "[3, \"abc\"]\n"},
// path → path, path → StringIO, StringIO → path, and length+offset on a path.
{pre + `Dir.mktmpdir { |d| s = File.join(d, "s"); t = File.join(d, "t"); File.write(s, "hello"); n = IO.copy_stream(s, t); p [n, File.read(t)] }`, "[5, \"hello\"]\n"},
{pre + `Dir.mktmpdir { |d| s = File.join(d, "s"); File.write(s, "world"); b = StringIO.new; n = IO.copy_stream(s, b); p [n, b.string] }`, "[5, \"world\"]\n"},
{pre + `Dir.mktmpdir { |d| t = File.join(d, "t"); a = StringIO.new("data"); n = IO.copy_stream(a, t); p [n, File.read(t)] }`, "[4, \"data\"]\n"},
{pre + `Dir.mktmpdir { |d| s = File.join(d, "s"); t = File.join(d, "t"); File.write(s, "abcdef"); n = IO.copy_stream(s, t, 3, 2); p [n, File.read(t)] }`, "[3, \"cde\"]\n"},
// A length read past EOF copies nothing.
{pre + `a = StringIO.new(""); b = StringIO.new; n = IO.copy_stream(a, b, 5); p [n, b.string]`, "[0, \"\"]\n"},
// src_offset is rejected for a non-IO (StringIO) source.
{pre + `a = StringIO.new("abcdef"); b = StringIO.new; begin; IO.copy_stream(a, b, 3, 2); rescue ArgumentError => e; p e.message; end`, "\"cannot specify src_offset for non-IO\"\n"},
// Fewer than two arguments is an ArgumentError.
{pre + `begin; IO.copy_stream(StringIO.new("x")); rescue ArgumentError => e; p e.class; end`, "ArgumentError\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want)
}
}
}
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16080
16095