Add error case tests for File.path

- for non-String argument
- for NUL-contained argument
- for ASCII-incompatible argument
This commit is contained in:
Nobuyoshi Nakada 2025-12-06 19:32:14 +09:00
parent aae8592650
commit f4f5f0a009
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2025-12-06 11:01:55 +00:00
2 changed files with 56 additions and 6 deletions

View File

@ -37,4 +37,45 @@ describe "File.path" do
path.should_receive(:to_path).and_return("abc")
File.path(path).should == "abc"
end
it "raises TypeError when #to_path result is not a string" do
path = mock("path")
path.should_receive(:to_path).and_return(nil)
-> { File.path(path) }.should raise_error TypeError
path = mock("path")
path.should_receive(:to_path).and_return(42)
-> { File.path(path) }.should raise_error TypeError
end
it "raises ArgumentError for string argument contains NUL character" do
-> { File.path("\0") }.should raise_error ArgumentError
-> { File.path("a\0") }.should raise_error ArgumentError
-> { File.path("a\0c") }.should raise_error ArgumentError
end
it "raises ArgumentError when #to_path result contains NUL character" do
path = mock("path")
path.should_receive(:to_path).and_return("\0")
-> { File.path(path) }.should raise_error ArgumentError
path = mock("path")
path.should_receive(:to_path).and_return("a\0")
-> { File.path(path) }.should raise_error ArgumentError
path = mock("path")
path.should_receive(:to_path).and_return("a\0c")
-> { File.path(path) }.should raise_error ArgumentError
end
it "raises Encoding::CompatibilityError for ASCII-incompatible string argument" do
path = "abc".encode(Encoding::UTF_32BE)
-> { File.path(path) }.should raise_error Encoding::CompatibilityError
end
it "raises Encoding::CompatibilityError when #to_path result is ASCII-incompatible" do
path = mock("path")
path.should_receive(:to_path).and_return("abc".encode(Encoding::UTF_32BE))
-> { File.path(path) }.should raise_error Encoding::CompatibilityError
end
end

View File

@ -204,12 +204,21 @@ class TestFileExhaustive < Test::Unit::TestCase
end
conv_error = ->(method, msg = "converting with #{method}") {
o = Struct.new(method).new(42)
assert_raise(TypeError, msg) {File.path(o)}
o = Struct.new(method).new("abc".encode(Encoding::UTF_32BE))
assert_raise(Encoding::CompatibilityError, msg) {File.path(o)}
o = Struct.new(method).new("\0")
assert_raise(ArgumentError, msg) {File.path(o)}
test = ->(&new) do
o = new.(42)
assert_raise(TypeError, msg) {File.path(o)}
o = new.("abc".encode(Encoding::UTF_32BE))
assert_raise(Encoding::CompatibilityError, msg) {File.path(o)}
["\0", "a\0", "a\0c"].each do |path|
o = new.(path)
assert_raise(ArgumentError, msg) {File.path(o)}
end
end
test.call(&:itself)
test.call(&Struct.new(method).method(:new))
}
conv_error[:to_path]