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
2 changes: 2 additions & 0 deletions lib/pathname_builtin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ class Pathname
#
def initialize(path)
@path = File.path(path).dup
rescue TypeError => e
raise e.class, "Pathname.new requires a String, #to_path or #to_str", cause: nil
end

#
Expand Down
13 changes: 13 additions & 0 deletions test/pathname/test_pathname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,13 @@ def test_initialize
p2 = Pathname.new(p1)
assert_equal(p1, p2)

obj = Object.new
assert_raise_with_message(TypeError, /#to_path or #to_str/) { Pathname.new(obj) }

obj = Object.new
def obj.to_path; "a/path"; end
assert_equal("a/path", Pathname.new(obj).to_s)

obj = Object.new
def obj.to_str; "a/b"; end
assert_equal("a/b", Pathname.new(obj).to_s)
Expand All @@ -495,6 +502,12 @@ def test_initialize_nul
assert_raise(ArgumentError) { Pathname.new("a\0") }
end

def test_initialize_encoding
omit "https://github.com/jruby/jruby/issues/9120" if RUBY_ENGINE == "jruby"
omit "https://github.com/truffleruby/truffleruby/issues/4047" if RUBY_ENGINE == "truffleruby"
assert_raise(Encoding::CompatibilityError) { Pathname.new("a".encode(Encoding::UTF_32BE)) }
end

def test_global_constructor
p = Pathname.new('a')
assert_equal(p, Pathname('a'))
Expand Down