Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Commit

Permalink
Implement Xgit.Core.FilePath.ensure_trailing_separator/1. (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten committed Sep 3, 2019
1 parent 5b072e0 commit 81d5af8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/xgit/core/file_path.ex
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,19 @@ defmodule Xgit.Core.FilePath do
defp to_lower(b) when b >= ?A and b <= ?Z, do: cover(b + 32)
defp to_lower(b), do: cover(b)

@doc ~S"""
Ensure that a trailing `/` is present.
**Exception:** If the path is empty, it will be returned as-is.
"""
@spec ensure_trailing_separator(path :: t) :: t
def ensure_trailing_separator([]), do: cover([])

def ensure_trailing_separator(path) when is_list(path) do
# We strip trailing `/` because there might be more than one.
strip_trailing_separator(path) ++ '/'
end

@doc ~S"""
Remove trailing `/` if present.
"""
Expand Down
18 changes: 18 additions & 0 deletions test/xgit/core/file_path_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,24 @@ defmodule Xgit.Core.FilePathTest do
end
end

describe "ensure_trailing_separator/1" do
test "empty list" do
assert ensure_trailing_separator([]) == []
end

test "without trailing /" do
assert ensure_trailing_separator('abc') == 'abc/'
assert ensure_trailing_separator('/abc') == '/abc/'
assert ensure_trailing_separator('foo/b') == 'foo/b/'
end

test "with trailing /" do
assert ensure_trailing_separator('/') == '/'
assert ensure_trailing_separator('abc/') == 'abc/'
assert ensure_trailing_separator('foo/bar//') == 'foo/bar/'
end
end

describe "strip_trailing_separator/1" do
test "empty list" do
assert strip_trailing_separator([]) == []
Expand Down

0 comments on commit 81d5af8

Please sign in to comment.