Skip to content

Commit

Permalink
Add regression tests for install module
Browse files Browse the repository at this point in the history
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...)
  • Loading branch information
qgadrian committed Nov 29, 2024
1 parent 7553c44 commit 39bf7e0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mix/tasks/git_hooks/install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule Mix.Tasks.GitHooks.Install do
Printer.info("Installing git hooks...")

mix_path = Config.mix_path()
project_path = Application.get_env(:git_hooks, :project_path, "")
project_path = Application.get_env(:git_hooks, :project_path, GitPath.resolve_app_path())

ensure_hooks_folder_exists()
clean_missing_hooks()
Expand Down
37 changes: 37 additions & 0 deletions test/mix/tasks/git_hooks/install_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,43 @@ defmodule Mix.Tasks.InstallTest do

Application.delete_env(:git_hooks, :project_path)
end

test "installs git hooks when run from the project root", %{tmp_dir: project_path} do
put_git_hook_config(
[:pre_commit, :pre_push],
tasks: {:cmd, "check"}
)

hooks_file = Install.run(["--dry-run", "--quiet"])

assert hooks_file == [
pre_commit: expect_hook_template("pre_commit", project_path),
pre_push: expect_hook_template("pre_push", project_path)
]
end

test "installs git hooks when run from the dependency directory", %{tmp_dir: project_path} do
# Simulate being in the dependency directory
deps_git_hooks_dir = Path.join([project_path, "deps", "git_hooks"])
File.mkdir_p!(deps_git_hooks_dir)

File.cd!(deps_git_hooks_dir, fn ->
# Need to reset the config cache because Application env might be cached
Application.delete_env(:git_hooks, :project_path)

put_git_hook_config(
[:pre_commit, :pre_push],
tasks: {:cmd, "check"}
)

hooks_file = Install.run(["--dry-run", "--quiet"])

assert hooks_file == [
pre_commit: expect_hook_template("pre_commit", project_path),
pre_push: expect_hook_template("pre_push", project_path)
]
end)
end
end

#
Expand Down

0 comments on commit 39bf7e0

Please sign in to comment.