Skip to content
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
12 changes: 12 additions & 0 deletions spec/std/path_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ describe Path do
assert_paths("C:/foo", "C:", "C:/", &.parent)
assert_paths("C:\\foo", ".", "C:\\", &.parent)
assert_paths("/foo/C:/bar", "/foo/C:", "/foo/C:", &.parent)
assert_paths("//some/share", "//some", "//some/share", &.parent)
assert_paths("//some/share/", "//some", "//some/share/", &.parent)
assert_paths("//some/share/a", "//some/share", "//some/share/", &.parent)
assert_paths("//some/share/a/", "//some/share", "//some/share/", &.parent)
end

describe "#parents" do
Expand Down Expand Up @@ -237,6 +241,10 @@ describe Path do
assert_paths("C:\\folder", ["."], ["C:\\"], &.parents)
assert_paths("C:\\\\folder", ["."], ["C:\\\\"], &.parents)
assert_paths("C:\\.", ["."], ["C:\\"], &.parents)
assert_paths("//some/share", ["//", "//some"], [] of String, &.parents)
assert_paths("//some/share/", ["//", "//some"], [] of String, &.parents)
assert_paths("//some/share/a", ["//", "//some", "//some/share"], ["//some/share/"], &.parents)
assert_paths("//some/share/a/", ["//", "//some", "//some/share"], ["//some/share/"], &.parents)
end

describe "#dirname" do
Expand All @@ -256,6 +264,10 @@ describe Path do
assert_paths_raw("C:", ".", "C:", &.dirname)
assert_paths_raw("C:/", ".", "C:/", &.dirname)
assert_paths_raw("C:\\", ".", "C:\\", &.dirname)
assert_paths_raw("//some/share", "//some", "//some/share", &.dirname)
assert_paths_raw("//some/share/", "//some", "//some/share/", &.dirname)
assert_paths_raw("//some/share/a", "//some/share", "//some/share/", &.dirname)
assert_paths_raw("//some/share/a/", "//some/share", "//some/share/", &.dirname)
end

describe "#basename" do
Expand Down
10 changes: 5 additions & 5 deletions src/path.cr
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ struct Path
when 1 # Path has no parent (ex. "hello/", "C:/", "crystal")
return anchor.to_s if windows? && windows_drive?
"."
else # Path has a parent (ex. "a/a", "/home/user//", "C://Users/mmm")
return String.new(slice[0, 1]) if pos == -1
if windows? && pos == 1 && slice.unsafe_fetch(pos) === ':' && (anchor = self.anchor)
return anchor.to_s
else # Path has a parent (ex. "a/a", "/home/user//", "C://Users/mmm", "\\wsl.localhost\Debian")
if windows? && (anchor = self.anchor) && pos < anchor.to_s.bytesize
anchor.to_s
else
@name.byte_slice(0, {pos, 0}.max + 1)
end
String.new(slice[0, pos + 1])
end
end

Expand Down
Loading