Test a Julia Package as a Git Hook

Hey all, I want to try to set my git configuration such that when I commit and before I push, my git hooks fire and test my certain package. Is anyone doing this with Julia and can share how it’s done?

For certain Julia packages, I run a pre-push hook to test my package (and sometimes push to additional remotes).

In DummyPackage/.git/hooks/pre-push, I have:

#!/bin/sh
#
# To enable this hook, rename this file to "pre-push".
# Remember to make it executable with: chmod +x

red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[0;34m'
no_color='\033[0m'
reponame=$(basename `git rev-parse --show-toplevel`)


echo -e "\n${blue}Running pre-push hook\n${no_color}"
echo "Testing $reponame"
julia --project=@. test/runtests.jl

if [[ $? -ne 0 ]]; then
        echo -e "\n${red}ERROR - Tests must pass before push!\n${no_color}"
  exit 1
else
        echo -e "\n${green} Tests passed!\n${no_color}"
fi

echo -e "${green}Git hook was SUCCESSFUL!${no_color}\n"

Expand for hook execution
[DummyPackage](master)$ echo "DummyPackage" > README.md
[DummyPackage](master)$ git add README.md 
[DummyPackage](master)$ git commit -m "Added README.md"
[master 12a7b35] Added README.md
 1 file changed, 1 insertion(+), 1 deletion(-)
[DummyPackage](master)$ git push origin master 

Running pre-push hook

Testing DummyPackage
Test Summary:   | Pass  Total
DummyPackage.jl |    2      2

 Tests passed!

Git hook was SUCCESSFUL!

Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 282 bytes | 282.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
To gitlab.com:elisno/dummyproject.jl.git
   5b8808f..12a7b35  master -> master

Was this what you had in mind?

3 Likes

That helps a ton! How do I see the output of Echo and such? I have a terminal open and I see nothing.

I’m not sure.

  1. What directory are you in when trying to git push your commits?
  2. Since there’s no output, can you confirm that the tests are executed and that the code was pushed successfully to the remote (assuming that the tests pass)?
  3. Have you set the executable permission of the hook? chmod +x .git/hooks/pre-push
1 Like

The issue was that I was doing the commit via the Git extension in Atom rather than the terminal. It works when I do the commit via either but I can actually see the output when I do it via the terminal. Thanks again!

1 Like

I had the same experience with pre-commit hooks in Atom.

Initially, I thought that was a bug with the Git extension as I got no response after doing the commit. However, it was just the long runtime of the pre-commit hook.

1 Like