Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test git hooks

Since "testing" is a common use for a Git hook, my question is hard to search for.

I'm writing a fairly involved git post-receive hook and want to know what the best way to test it is. Currently my process is:

  • make changes to post-receive in a dummy "remote" repo
  • make a change to a dummy local repo
  • commit change in dummy local repo
  • push change to dummy remote repo

Is there any easier way to test this? Ideally it would look like:

  • make change(s) to post-receive in a dummy repo
  • issue "magic" command to test post-receive

Perhaps I can "reissue" a previous push or have the remote repo act as though it just received a push with a specific hash?

like image 797
Errol Avatar asked Jul 16 '12 20:07

Errol


People also ask

How do you test a pre received hook?

A pre-receive hook, like a pre-push hook, must read its standard input, one line at a time, checking each group of parameters supplied on that line. If the hook exits with a nonzero return, the entire push is rejected (from the server's side).

How do I run a test in commit?

Invoke the Commit feature using Ctrl + K on Windows/Linux and ⌘ + K on macOS, then select the Commit options and check the Run Tests option, then select which test configuration you wish to run. Note: You must have the Use non-modal commit interface activated before you can use this feature.


3 Answers

Write a hook that just records its arguments/environment and dumps that to a file. Then you can just re-invoke the real hook at your leisure with the same environment/arguments and it will act as though you just re-issued the exact same push.

like image 145
Lily Ballard Avatar answered Sep 28 '22 10:09

Lily Ballard


Answer this four-years-old question.

If you'd like to test hook, you need to test in local environment first, I give the detail commands for following up, use post-receive as sample:

$ mkdir /tmp/hook_test
$ cd /tmp/hook_test

# set local git repo, where you put hooks in it.
$ git clone --bare https://github.com/git/git.git

# set develop environment which is cloned from the new created repo. 
$ git clone git.git repo 
    
# copy and rename the hook you need test to "post-receive"
$ cd git.git/hooks
$ cp ~/post-receive-test post-receive

# suppose the hook script is bash script.
# edit "post-receive" and add "set -x" to second line in it to active debug

$ cd /tmp/hook_test/repo
# emulate a hook trigger, do some changes, "git add" and "git commit" it 

$ git push
 
# Now you should see the script "post-receive" runs automatically with debug details.

You should be free to run git push, that the updates are only pushed to local repo /tmp/hook_test/git.git

like image 32
BMW Avatar answered Sep 28 '22 09:09

BMW


My approach is to dial the HEAD at the remote repo back one commit, and then push again:

ssh <repo> 'cd /<repo_path>; git update-ref refs/heads/master HEAD^' && git push origin master
like image 25
dirksen Avatar answered Sep 28 '22 09:09

dirksen