Skip to content

Commit f4f5f0a

Browse files
committed
Add error case tests for File.path
- for non-String argument - for NUL-contained argument - for ASCII-incompatible argument
1 parent aae8592 commit f4f5f0a

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

spec/ruby/core/file/path_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,45 @@
3737
path.should_receive(:to_path).and_return("abc")
3838
File.path(path).should == "abc"
3939
end
40+
41+
it "raises TypeError when #to_path result is not a string" do
42+
path = mock("path")
43+
path.should_receive(:to_path).and_return(nil)
44+
-> { File.path(path) }.should raise_error TypeError
45+
46+
path = mock("path")
47+
path.should_receive(:to_path).and_return(42)
48+
-> { File.path(path) }.should raise_error TypeError
49+
end
50+
51+
it "raises ArgumentError for string argument contains NUL character" do
52+
-> { File.path("\0") }.should raise_error ArgumentError
53+
-> { File.path("a\0") }.should raise_error ArgumentError
54+
-> { File.path("a\0c") }.should raise_error ArgumentError
55+
end
56+
57+
it "raises ArgumentError when #to_path result contains NUL character" do
58+
path = mock("path")
59+
path.should_receive(:to_path).and_return("\0")
60+
-> { File.path(path) }.should raise_error ArgumentError
61+
62+
path = mock("path")
63+
path.should_receive(:to_path).and_return("a\0")
64+
-> { File.path(path) }.should raise_error ArgumentError
65+
66+
path = mock("path")
67+
path.should_receive(:to_path).and_return("a\0c")
68+
-> { File.path(path) }.should raise_error ArgumentError
69+
end
70+
71+
it "raises Encoding::CompatibilityError for ASCII-incompatible string argument" do
72+
path = "abc".encode(Encoding::UTF_32BE)
73+
-> { File.path(path) }.should raise_error Encoding::CompatibilityError
74+
end
75+
76+
it "raises Encoding::CompatibilityError when #to_path result is ASCII-incompatible" do
77+
path = mock("path")
78+
path.should_receive(:to_path).and_return("abc".encode(Encoding::UTF_32BE))
79+
-> { File.path(path) }.should raise_error Encoding::CompatibilityError
80+
end
4081
end

test/ruby/test_file_exhaustive.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,21 @@ def test_path
204204
end
205205

206206
conv_error = ->(method, msg = "converting with #{method}") {
207-
o = Struct.new(method).new(42)
208-
assert_raise(TypeError, msg) {File.path(o)}
209-
o = Struct.new(method).new("abc".encode(Encoding::UTF_32BE))
210-
assert_raise(Encoding::CompatibilityError, msg) {File.path(o)}
211-
o = Struct.new(method).new("\0")
212-
assert_raise(ArgumentError, msg) {File.path(o)}
207+
test = ->(&new) do
208+
o = new.(42)
209+
assert_raise(TypeError, msg) {File.path(o)}
210+
211+
o = new.("abc".encode(Encoding::UTF_32BE))
212+
assert_raise(Encoding::CompatibilityError, msg) {File.path(o)}
213+
214+
["\0", "a\0", "a\0c"].each do |path|
215+
o = new.(path)
216+
assert_raise(ArgumentError, msg) {File.path(o)}
217+
end
218+
end
219+
220+
test.call(&:itself)
221+
test.call(&Struct.new(method).method(:new))
213222
}
214223

215224
conv_error[:to_path]

0 commit comments

Comments
 (0)