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

Commit

Permalink
Merge 14439a8 into e947cd1
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten committed Nov 3, 2019
2 parents e947cd1 + 14439a8 commit 66d046e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/xgit/util/file_utils.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Xgit.Util.FileUtils do
@moduledoc false

# Internal utility for recursively listing the contents of a directory.

import Xgit.Util.ForceCoverage

@doc ~S"""
Recursively list the files of a directory.
Directories are scanned, but their paths are not reported as part of the result.
"""
@spec recursive_files!(path :: Path.t()) :: [Path.t()]
def recursive_files!(path \\ ".") do
cond do
File.regular?(path) ->
cover [path]

File.dir?(path) ->
path
|> File.ls!()
|> Enum.map(&Path.join(path, &1))
|> Enum.map(&recursive_files!/1)
|> Enum.concat()

true ->
cover []
end
end
end
54 changes: 54 additions & 0 deletions test/xgit/util/file_utils_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
defmodule Xgit.Util.FileUtilsTest do
use ExUnit.Case, async: true

import Xgit.Test.TempDirTestCase

alias Xgit.Util.FileUtils

describe "recursive_files!/1" do
test "empty dir" do
%{tmp_dir: tmp} = tmp_dir!()
assert [] = FileUtils.recursive_files!(tmp)
end

test "dir doesn't exist" do
%{tmp_dir: tmp} = tmp_dir!()
foo_path = Path.join(tmp, "foo")
assert [] = FileUtils.recursive_files!(foo_path)
end

test "one file" do
%{tmp_dir: tmp} = tmp_dir!()
foo_path = Path.join(tmp, "foo")
File.write!(foo_path, "foo")
assert [^foo_path] = FileUtils.recursive_files!(tmp)
end

test "one file, nested" do
%{tmp_dir: tmp} = tmp_dir!()
bar_dir_path = Path.join(tmp, "bar")
File.mkdir_p!(bar_dir_path)
foo_path = Path.join(bar_dir_path, "foo")
File.write!(foo_path, "foo")
assert [^foo_path] = FileUtils.recursive_files!(tmp)
end

test "three file" do
%{tmp_dir: tmp} = tmp_dir!()

foo_path = Path.join(tmp, "foo")
File.write!(foo_path, "foo")

bar_path = Path.join(tmp, "bar")
File.write!(bar_path, "bar")

blah_path = Path.join(tmp, "blah")
File.write!(blah_path, "blah")

assert [^bar_path, ^blah_path, ^foo_path] =
tmp
|> FileUtils.recursive_files!()
|> Enum.sort()
end
end
end

0 comments on commit 66d046e

Please sign in to comment.