Skip to content
Open
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
29 changes: 29 additions & 0 deletions core/file/dirname_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ def object.to_int; 2; end
File.dirname("foo/../").should == "foo"
end

it "rejects strings encoded with non ASCII-compatible encodings" do
Encoding.list.reject(&:ascii_compatible?).reject(&:dummy?).each do |enc|
path = "/foo/bar".encode(enc)
-> {
File.dirname(path)
}.should raise_error(Encoding::CompatibilityError)
end
end

it "works with all ASCII-compatible encodings" do
Encoding.list.select(&:ascii_compatible?).each do |enc|
File.dirname("/foo/bar".encode(enc)).should == "/foo".encode(enc)
end
end

it "handles Shift JIS 0x5C (\\) as second byte of a multi-byte sequence" do
# dir/fileソname.txt
path = "dir/file\x83\x5cname.txt".b.force_encoding(Encoding::SHIFT_JIS)
path.valid_encoding?.should be_true
File.dirname(path).should == "dir"
end

platform_is_not :windows do
it "ignores repeated leading / (edge cases on non-windows)" do
File.dirname("/////foo/bar/").should == "/foo"
Expand All @@ -98,6 +120,13 @@ def object.to_int; 2; end
File.dirname("//foo//").should == "//foo"
File.dirname('/////').should == '//'
end

it "handles Shift JIS 0x5C (\\) as second byte of a multi-byte sequence (windows)" do
# dir/fileソname.txt
path = "dir\\file\x83\x5cname.txt".b.force_encoding(Encoding::SHIFT_JIS)
path.valid_encoding?.should be_true
File.dirname(path).should == "dir"
end
end

it "accepts an object that has a #to_path method" do
Expand Down