Skip to content

Commit 9c90955

Browse files
author
José Valim
committed
Merge pull request #2370 from keithseahus/master
Fix Path.absname("/") and Path.expand("/") to return the absolute path "/".
2 parents 86a8a6e + 61e626b commit 9c90955

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/elixir/lib/path.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ defmodule Path do
6262
case type(path) do
6363
:relative -> join(relative_to, path)
6464
:absolute ->
65-
if :binary.last(path) == ?/ do
66-
binary_part(path, 0, byte_size(path) - 1)
67-
else
68-
path
65+
cond do
66+
:binary.first(path) == ?/ and size(path) == 1 ->
67+
path
68+
:binary.last(path) == ?/ ->
69+
binary_part(path, 0, byte_size(path) - 1)
70+
true ->
71+
path
6972
end
7073
:volumerelative ->
7174
relative_to = IO.chardata_to_string(relative_to)
@@ -138,6 +141,7 @@ defmodule Path do
138141
139142
## Unix examples
140143
144+
Path.type("/") #=> :absolute
141145
Path.type("/usr/local/bin") #=> :absolute
142146
Path.type("usr/local/bin") #=> :relative
143147
Path.type("../usr/local/bin") #=> :relative

lib/elixir/test/elixir/path_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ defmodule PathTest do
6565
end
6666

6767
test :absname do
68+
assert (Path.absname("/") |> strip_drive_letter_if_windows) == "/"
69+
assert (Path.absname("/foo") |> strip_drive_letter_if_windows) == "/foo"
6870
assert (Path.absname("/foo/bar") |> strip_drive_letter_if_windows) == "/foo/bar"
6971
assert (Path.absname("/foo/bar/") |> strip_drive_letter_if_windows) == "/foo/bar"
7072
assert (Path.absname("/foo/bar/../bar") |> strip_drive_letter_if_windows) == "/foo/bar/../bar"
@@ -92,6 +94,8 @@ defmodule PathTest do
9294
end
9395

9496
test :expand_path do
97+
assert (Path.expand("/") |> strip_drive_letter_if_windows) == "/"
98+
assert (Path.expand("/foo") |> strip_drive_letter_if_windows) == "/foo"
9599
assert (Path.expand("/foo/bar") |> strip_drive_letter_if_windows) == "/foo/bar"
96100
assert (Path.expand("/foo/bar/") |> strip_drive_letter_if_windows) == "/foo/bar"
97101
assert (Path.expand("/foo/bar/.") |> strip_drive_letter_if_windows)== "/foo/bar"

0 commit comments

Comments
 (0)