From 6a33412bb5ed0d3ee7d3b436961141be6e4cab42 Mon Sep 17 00:00:00 2001 From: Dmitry Kakurin Date: Fri, 3 Jan 2020 19:27:05 -0800 Subject: [PATCH 1/2] Fixed Path.absname to correctly handle UNC paths on Windows absname was incorrectly converting `//host/path` to `/host/path` Added Windows-only Unit Tests for: * Path.absname for UNC path (fixed here) * Path.type for UNC path (was already correct, but not tested) --- lib/elixir/lib/path.ex | 9 +++++++-- lib/elixir/test/elixir/path_test.exs | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/elixir/lib/path.ex b/lib/elixir/lib/path.ex index 090899aa726..f2fbd02b87b 100644 --- a/lib/elixir/lib/path.ex +++ b/lib/elixir/lib/path.ex @@ -95,6 +95,8 @@ defmodule Path do absname(absname_join(name), cwd) end + @slash [?/, ?\\] + # Joins a list defp absname_join([name1, name2 | rest]), do: absname_join([absname_join(name1, name2) | rest]) @@ -110,6 +112,11 @@ defmodule Path do do_absname_join(rest, relativename, [?:, uc_letter + ?a - ?A], :win32) end + defp do_absname_join(<>, relativename, [], :win32) + when c1 in @slash and c2 in @slash do + do_absname_join(rest, relativename, '//', :win32) + end + defp do_absname_join(<>, relativename, result, :win32), do: do_absname_join(<>, relativename, result, :win32) @@ -254,8 +261,6 @@ defmodule Path do defp unix_pathtype([list | rest]) when is_list(list), do: unix_pathtype(list ++ rest) defp unix_pathtype(relative), do: {:relative, relative} - @slash [?/, ?\\] - defp win32_pathtype([list | rest]) when is_list(list), do: win32_pathtype(list ++ rest) defp win32_pathtype([char, list | rest]) when is_list(list), diff --git a/lib/elixir/test/elixir/path_test.exs b/lib/elixir/test/elixir/path_test.exs index 4ad0c99367e..d870b683a9f 100644 --- a/lib/elixir/test/elixir/path_test.exs +++ b/lib/elixir/test/elixir/path_test.exs @@ -39,6 +39,11 @@ defmodule PathTest do describe "Windows" do @describetag :windows + test "absname/1" do + assert Path.absname("//host/path") == "//host/path" + assert Path.absname("\\\\host\\path") == "//host/path" + end + test "relative/1" do assert Path.relative("C:/usr/local/bin") == "usr/local/bin" assert Path.relative("C:\\usr\\local\\bin") == "usr\\local\\bin" @@ -67,6 +72,9 @@ defmodule PathTest do assert Path.type("/usr/local/bin") == :volumerelative assert Path.type('usr/local/bin') == :relative assert Path.type("../usr/local/bin") == :relative + + assert Path.type("//host/path") == :absolute + assert Path.type("\\\\host\\path") == :absolute end test "split/1" do From 85f13b882439964fc2247887de8340e09dc6bf10 Mon Sep 17 00:00:00 2001 From: Dmitry Kakurin Date: Mon, 6 Jan 2020 12:26:16 -0800 Subject: [PATCH 2/2] Added tests for mixed slashes --- lib/elixir/test/elixir/path_test.exs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/elixir/test/elixir/path_test.exs b/lib/elixir/test/elixir/path_test.exs index d870b683a9f..6d1daafa156 100644 --- a/lib/elixir/test/elixir/path_test.exs +++ b/lib/elixir/test/elixir/path_test.exs @@ -42,6 +42,8 @@ defmodule PathTest do test "absname/1" do assert Path.absname("//host/path") == "//host/path" assert Path.absname("\\\\host\\path") == "//host/path" + assert Path.absname("\\/host\\path") == "//host/path" + assert Path.absname("/\\host\\path") == "//host/path" end test "relative/1" do @@ -75,6 +77,8 @@ defmodule PathTest do assert Path.type("//host/path") == :absolute assert Path.type("\\\\host\\path") == :absolute + assert Path.type("/\\host\\path") == :absolute + assert Path.type("\\/host\\path") == :absolute end test "split/1" do