From 381324b41e12d4147e460df7e29ad9c50294d852 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Fri, 28 Mar 2025 11:36:44 +0900 Subject: [PATCH 1/4] Add tests for IO.read and IO.write --- test/stdlib/IO_test.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/stdlib/IO_test.rb b/test/stdlib/IO_test.rb index 3a2f0b8dd..7bc92e46f 100644 --- a/test/stdlib/IO_test.rb +++ b/test/stdlib/IO_test.rb @@ -35,6 +35,31 @@ def test_binwrite end end + def test_read + assert_send_type "(String) -> String", + IO, :read, File.expand_path(__FILE__) + assert_send_type "(String, Integer) -> String", + IO, :read, File.expand_path(__FILE__), 3 + assert_send_type "(String, Integer, Integer) -> String", + IO, :read, File.expand_path(__FILE__), 3, 0 + end + + def test_write + Dir.mktmpdir do |dir| + filename = File.join(dir, "some_file") + content = "foo" + + assert_send_type "(String, String) -> Integer", + IO, :write, filename, content + assert_send_type "(String, String, Integer) -> Integer", + IO, :write, filename, content, 0 + assert_send_type "(String, String, mode: String) -> Integer", + IO, :write, filename, content, mode: "a" + assert_send_type "(String, String, Integer, mode: String) -> Integer", + IO, :write, filename, content, 0, mode: "a" + end + end + def test_open Dir.mktmpdir do |dir| assert_send_type "(Integer) -> IO", From f14d1f3636bfb1ae1929caeb335c9176d89e8b6d Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Fri, 28 Mar 2025 11:37:31 +0900 Subject: [PATCH 2/4] Add tests for Pathname argument for IO.binread, IO.binwrite, IO.read and IO.write --- test/stdlib/IO_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/stdlib/IO_test.rb b/test/stdlib/IO_test.rb index 7bc92e46f..08febf01c 100644 --- a/test/stdlib/IO_test.rb +++ b/test/stdlib/IO_test.rb @@ -11,6 +11,8 @@ class IOSingletonTest < Test::Unit::TestCase def test_binread assert_send_type "(String) -> String", IO, :binread, File.expand_path(__FILE__) + assert_send_type "(Pathname) -> String", + IO, :binread, Pathname(File.expand_path(__FILE__)) assert_send_type "(String, Integer) -> String", IO, :binread, File.expand_path(__FILE__), 3 assert_send_type "(String, Integer, Integer) -> String", @@ -26,6 +28,8 @@ def test_binwrite assert_send_type "(String, String) -> Integer", IO, :binwrite, filename, content + assert_send_type "(Pathname, String) -> Integer", + IO, :binwrite, Pathname(filename), content assert_send_type "(String, String, Integer) -> Integer", IO, :binwrite, filename, content, 0 assert_send_type "(String, String, mode: String) -> Integer", @@ -38,6 +42,8 @@ def test_binwrite def test_read assert_send_type "(String) -> String", IO, :read, File.expand_path(__FILE__) + assert_send_type "(Pathname) -> String", + IO, :read, Pathname(File.expand_path(__FILE__)) assert_send_type "(String, Integer) -> String", IO, :read, File.expand_path(__FILE__), 3 assert_send_type "(String, Integer, Integer) -> String", @@ -51,6 +57,8 @@ def test_write assert_send_type "(String, String) -> Integer", IO, :write, filename, content + assert_send_type "(Pathname, String) -> Integer", + IO, :write, Pathname(filename), content assert_send_type "(String, String, Integer) -> Integer", IO, :write, filename, content, 0 assert_send_type "(String, String, mode: String) -> Integer", From 8729c8b7dc68d43963699a230535925dcb4555f4 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 1 Apr 2025 17:09:51 +0900 Subject: [PATCH 3/4] make IO.binread, IO.binwrite, IO.read and IO.write accept _ToPath --- core/io.rbs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/io.rbs b/core/io.rbs index 0284fecde..887f056de 100644 --- a/core/io.rbs +++ b/core/io.rbs @@ -2278,7 +2278,7 @@ class IO < Object # potential security vulnerabilities if called with untrusted input; see # [Command Injection](rdoc-ref:command_injection.rdoc). # - def self.binread: (String name, ?Integer? length, ?Integer offset) -> String + def self.binread: (_ToPath | string name, ?Integer? length, ?Integer offset) -> String #