Skip to content

Commit

Permalink
Merge pull request #2158 from znz/relax-Dir.tmpdir-and-File.join
Browse files Browse the repository at this point in the history
Dir.mktmpdir and File.join accept Pathname
  • Loading branch information
soutaro authored Dec 23, 2024
2 parents 393e2dd + 4f85547 commit f3dc5d7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 22 deletions.
2 changes: 1 addition & 1 deletion core/file.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@ class File < IO
#
# File.join("usr", "mail", "gumby") #=> "usr/mail/gumby"
#
def self.join: (*string) -> String
def self.join: (*path) -> String

# <!--
# rdoc-file=file.c
Expand Down
4 changes: 2 additions & 2 deletions stdlib/tmpdir/0/tmpdir.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ class Dir
# FileUtils.remove_entry dir
# end
#
def self.mktmpdir: (?String | [ String, String ] | nil, ?String?, ?max_try: Integer?) -> String
| [X] (?String | [ String, String ] | nil, ?String?, ?max_try: Integer?) { (String) -> X } -> X
def self.mktmpdir: (?string | [ string, string ] | nil, ?path?, ?max_try: Integer?) -> String
| [X] (?string | [string, string ] | nil, ?path?, ?max_try: Integer?) { (String) -> X } -> X
end
85 changes: 72 additions & 13 deletions test/stdlib/Dir_tmpdir_test.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,82 @@
require_relative "test_helper"
require "tmpdir"

class Dir_tmpdirTest < StdlibTest
target Dir
class Tmpdir_Dir_SingletonTest < Test::Unit::TestCase
include TestHelper

library "tmpdir"
testing "singleton(Dir)"

def test_tmpdir
Dir.tmpdir()
assert_send_type(
"() -> String",
Dir, :mktmpdir
)
end

def test_mktmpdir
Dir.mktmpdir()
Dir.mktmpdir(["foo", "bar"])
Dir.mktmpdir("foo", Dir.tmpdir, max_try: 3)
Dir.mktmpdir(nil, nil, max_try: nil)

Dir.mktmpdir() {}
Dir.mktmpdir(["foo", "bar"]) {}
Dir.mktmpdir("foo", Dir.tmpdir, max_try: 3) {}
Dir.mktmpdir(nil, nil, max_try: nil) {}
assert_send_type(
"() -> String",
Dir, :mktmpdir
)
assert_send_type(
"() { (String) -> Integer} -> Integer",
Dir, :mktmpdir, &->(s) { s.size }
)

assert_send_type(
"(max_try: Integer) -> String",
Dir, :mktmpdir, max_try: 1
)
assert_send_type(
"(max_try: Integer) { (String) -> Integer } -> Integer",
Dir, :mktmpdir, max_try: 1, &->(s) { s.size}
)

with_string("foo") do |foo|
assert_send_type(
"(string) -> String",
Dir, :mktmpdir, foo
)
assert_send_type(
"(string) { (String) -> Integer } -> Integer",
Dir, :mktmpdir, foo, &->(x) { x.size }
)
end

assert_send_type(
"(nil) -> String",
Dir, :mktmpdir, nil
)
assert_send_type(
"(nil) { (String) -> Integer } -> Integer",
Dir, :mktmpdir, nil, &->(x) { x.size }
)

with_string("foo") do |foo|
with_string("bar") do |bar|
assert_send_type(
"([string, string]) -> String",
Dir, :mktmpdir, [foo, bar]
)
assert_send_type(
"([string, string]) { (String) -> Integer } -> Integer",
Dir, :mktmpdir, [foo, bar], &->(s) { s.size }
)
end
end

with_string("foo") do |foo|
with(Dir.tmpdir, Pathname(Dir.tmpdir)) do |bar|
assert_send_type(
"(string, path) -> String",
Dir, :mktmpdir, foo, bar
)

assert_send_type(
"(string, path) { (String) -> Integer } -> Integer",
Dir, :mktmpdir, foo, bar, &->(s) { s.size }
)
end
end
end
end
16 changes: 10 additions & 6 deletions test/stdlib/File_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,16 @@ def test_identical?
end

def test_join
assert_send_type "(String) -> String",
File, :join, "foo"
assert_send_type "(ToStr) -> String",
File, :join, ToStr.new("foo")
assert_send_type "(String, String) -> String",
File, :join, "foo", "bar"
with_path("foo") do |str|
assert_send_type(
"(path) -> String",
File, :join, str
)
assert_send_type(
"(path, path) -> String",
File, :join, str, str
)
end
end

def test_lchown
Expand Down

0 comments on commit f3dc5d7

Please sign in to comment.