From 7b2775d010321c1ec89bf12d4d78d6db0098e4fc Mon Sep 17 00:00:00 2001 From: Eric Scouten Date: Sun, 3 Nov 2019 10:34:24 -0800 Subject: [PATCH 1/2] Implement Xgit.Util.FileUtils.recursive_files!/1. --- lib/xgit/util/file_utils.ex | 27 +++++++++++++++ test/xgit/util/file_utils_test.exs | 54 ++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 lib/xgit/util/file_utils.ex create mode 100644 test/xgit/util/file_utils_test.exs diff --git a/lib/xgit/util/file_utils.ex b/lib/xgit/util/file_utils.ex new file mode 100644 index 0000000..16a96ae --- /dev/null +++ b/lib/xgit/util/file_utils.ex @@ -0,0 +1,27 @@ +defmodule Xgit.Util.FileUtils do + @moduledoc false + + # Internal utility for recursively listing the contents of a directory. + + @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) -> + [path] + + File.dir?(path) -> + File.ls!(path) + |> Enum.map(&Path.join(path, &1)) + |> Enum.map(&recursive_files!/1) + |> Enum.concat() + + true -> + [] + end + end +end diff --git a/test/xgit/util/file_utils_test.exs b/test/xgit/util/file_utils_test.exs new file mode 100644 index 0000000..d007fdb --- /dev/null +++ b/test/xgit/util/file_utils_test.exs @@ -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 From 8c31b03ec23389227c13e0b9a7fd52128eb9a6ab Mon Sep 17 00:00:00 2001 From: Eric Scouten Date: Sun, 3 Nov 2019 10:36:19 -0800 Subject: [PATCH 2/2] Add cover macro. --- lib/xgit/util/file_utils.ex | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/xgit/util/file_utils.ex b/lib/xgit/util/file_utils.ex index 16a96ae..59c9edb 100644 --- a/lib/xgit/util/file_utils.ex +++ b/lib/xgit/util/file_utils.ex @@ -3,6 +3,8 @@ defmodule Xgit.Util.FileUtils do # Internal utility for recursively listing the contents of a directory. + import Xgit.Util.ForceCoverage + @doc ~S""" Recursively list the files of a directory. @@ -12,7 +14,7 @@ defmodule Xgit.Util.FileUtils do def recursive_files!(path \\ ".") do cond do File.regular?(path) -> - [path] + cover [path] File.dir?(path) -> File.ls!(path) @@ -21,7 +23,7 @@ defmodule Xgit.Util.FileUtils do |> Enum.concat() true -> - [] + cover [] end end end