Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Path.new receiving Path as first argument #8753

Merged
Merged
Show file tree
Hide file tree
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
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