Skip to content

Commit

Permalink
Fix Path.new receiving Path as first argument (#8753)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Feb 8, 2020
1 parent 147386e commit e9662d2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
21 changes: 21 additions & 0 deletions spec/std/path_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ describe Path do
end

it { Path.new.to_s.should eq "" }

it "joins components" do
Path.new("foo", "bar").should eq Path.new("foo").join("bar")
Path.new(Path.new("foo"), "bar").should eq Path.new("foo", "bar")
Path.new(Path.posix("foo"), "bar").should eq Path.new("foo", "bar")
Path.new(Path.windows("foo"), "bar").should eq Path.new("foo", "bar")
end
end

describe ".posix" do
Expand All @@ -80,6 +87,13 @@ describe Path do
end

it { Path.posix.to_s.should eq "" }

it "joins components" do
Path.posix("foo", "bar").should eq Path.posix("foo").join("bar")
Path.posix(Path.new("foo"), "bar").should eq Path.posix("foo", "bar")
Path.posix(Path.posix("foo"), "bar").should eq Path.posix("foo", "bar")
Path.posix(Path.windows("foo"), "bar").should eq Path.posix("foo", "bar")
end
end

describe ".windows" do
Expand All @@ -94,6 +108,13 @@ describe Path do
end

it { Path.windows.to_s.should eq "" }

it "joins components" do
Path.windows("foo", "bar").should eq Path.windows("foo").join("bar")
Path.windows(Path.new("foo"), "bar").should eq Path.windows("foo", "bar")
Path.windows(Path.posix("foo"), "bar").should eq Path.windows("foo", "bar")
Path.windows(Path.windows("foo"), "bar").should eq Path.windows("foo", "bar")
end
end

it ".[]" do
Expand Down
30 changes: 26 additions & 4 deletions src/path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,17 @@ struct Path
end

# :ditto:
def self.new(name : String, *parts) : Path
def self.new(path : Path) : Path
path.to_native
end

# :ditto:
def self.new(name : String | Path, *parts : String | Path) : Path
new(name).join(*parts)
end

# :ditto:
def self.[](name : String, *parts) : Path
def self.[](name : String | Path, *parts) : Path
new(name, *parts)
end

Expand All @@ -123,7 +128,12 @@ struct Path
end

# :ditto:
def self.posix(name : String, *parts) : Path
def self.posix(path : Path) : Path
path.to_posix
end

# :ditto:
def self.posix(name : String | Path, *parts : String | Path) : Path
posix(name).join(parts)
end

Expand All @@ -138,7 +148,12 @@ struct Path
end

# :ditto:
def self.windows(name : String, *parts) : Path
def self.windows(path : Path) : Path
path.to_windows
end

# :ditto:
def self.windows(name : String | Path, *parts : String | Path) : Path
windows(name).join(parts)
end

Expand Down Expand Up @@ -528,6 +543,11 @@ struct Path
@name.byte_at?(1) === ':' && @name.char_at(0).ascii_letter?
end

# Converts this path to a native path.
def to_native : Path
to_kind(Kind.native)
end

# Converts this path to a Windows path.
#
# ```
Expand Down Expand Up @@ -761,6 +781,8 @@ struct Path
# ```
def join(parts : Enumerable) : Path
if parts.is_a?(Indexable)
return self if parts.empty?

# If it's just a single part we can avoid one allocation of String.build
return join(parts.first) if parts.size == 1

Expand Down

0 comments on commit e9662d2

Please sign in to comment.