Skip to content

Commit

Permalink
Better handle file names starting with ~ in Path#expand (#7768)
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot authored and RX14 committed May 21, 2019
1 parent ee0df4e commit 927de59
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 4 additions & 0 deletions spec/std/path_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ describe Path do
it_expands_path("~", "/", "\\", base: "/tmp/gumby/ddd", env_home: "/")
it_expands_path("~/a", "/a", "\\a", base: "/tmp/gumby/ddd", env_home: "/")
end

describe "ignores name starting with ~" do
it_expands_path("~foo.txt", "/current/~foo.txt", "\\current\\~foo.txt", base: "/current", env_home: "/")
end
end

describe "#<=>" do
Expand Down
12 changes: 4 additions & 8 deletions src/path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -564,14 +564,10 @@ struct Path

name = @name

if name.starts_with?('~')
home = home.to_kind(@kind).normalize

if name.size == 1
name = home.to_s
else
name = home.join(name.byte_slice(2, name.bytesize - 2)).to_s
end
if name == "~"
name = home.to_kind(@kind).normalize.to_s
elsif name.starts_with?("~/")
name = home.to_kind(@kind).normalize.join(name.byte_slice(2, name.bytesize - 2)).to_s
end

unless new_instance(name).absolute?
Expand Down

0 comments on commit 927de59

Please sign in to comment.