Skip to content

Commit 056f9e3

Browse files
committed
Add regression tests for install module
These new tests will ensure the git hooks installation is working from any folder reference within the project (e.g. from root folder, deps path...)
1 parent 0ed257b commit 056f9e3

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/mix/tasks/git_hooks/install.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ defmodule Mix.Tasks.GitHooks.Install do
4545
Printer.info("Installing git hooks...")
4646

4747
mix_path = Config.mix_path()
48-
project_path = Application.get_env(:git_hooks, :project_path, "")
48+
project_path = Application.get_env(:git_hooks, :project_path, GitPath.resolve_app_path())
4949

5050
ensure_hooks_folder_exists()
5151
clean_missing_hooks()

test/mix/tasks/git_hooks/install_test.exs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,43 @@ defmodule Mix.Tasks.InstallTest do
4444

4545
Application.delete_env(:git_hooks, :project_path)
4646
end
47+
48+
test "installs git hooks when run from the project root", %{tmp_dir: project_path} do
49+
put_git_hook_config(
50+
[:pre_commit, :pre_push],
51+
tasks: {:cmd, "check"}
52+
)
53+
54+
hooks_file = Install.run(["--dry-run", "--quiet"])
55+
56+
assert hooks_file == [
57+
pre_commit: expect_hook_template("pre_commit", project_path),
58+
pre_push: expect_hook_template("pre_push", project_path)
59+
]
60+
end
61+
62+
test "installs git hooks when run from the dependency directory", %{tmp_dir: project_path} do
63+
# Simulate being in the dependency directory
64+
deps_git_hooks_dir = Path.join([project_path, "deps", "git_hooks"])
65+
File.mkdir_p!(deps_git_hooks_dir)
66+
67+
File.cd!(deps_git_hooks_dir, fn ->
68+
# Need to reset the config cache because Application env might be cached
69+
Application.delete_env(:git_hooks, :project_path)
70+
71+
put_git_hook_config(
72+
[:pre_commit, :pre_push],
73+
tasks: {:cmd, "check"}
74+
)
75+
76+
hooks_file = Install.run(["--dry-run", "--quiet"])
77+
78+
assert hooks_file == [
79+
pre_commit: expect_hook_template("pre_commit", project_path),
80+
pre_push: expect_hook_template("pre_push", project_path)
81+
]
82+
end)
83+
end
4784
end
4885

4986
#

0 commit comments

Comments
 (0)