This repository has been archived by the owner on Oct 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |