Skip to content

Instantly share code, notes, and snippets.

@lfalmeida
Created August 7, 2016 13:01
Show Gist options
  • Save lfalmeida/6d34228b16215b44b2d2b0ebbf65e09d to your computer and use it in GitHub Desktop.
Save lfalmeida/6d34228b16215b44b2d2b0ebbf65e09d to your computer and use it in GitHub Desktop.
Script para hook pre-receive que executa os testes unitários e rejeita em caso de falha
#!/bin/bash
printMessage () {
if [ $# -eq 0 ]; then
echo "Usage: printMessage MESSAGE [printMessage_CHARACTER]"
return 1
fi
echo ""
echo ""
printf -v _hr "%*s" 80 && echo -en ${_hr// /${2--}} && echo -e "\r\033[2C$1"
echo ""
}
while read oldrev newrev refname
do
# Executar este script somente para o branch master
if [[ $refname = "refs/heads/master" ]] ; then
printMessage "[ Preparing to run phpunit for $newrev ... ]"
# Como este é um repositório bare, precisamos colocar os arquivos em algum lugar
# neste caso, o diretório /tmp
git archive $newrev | tar -x -C /tmp
printMessage "[ Executando testes para $newrev ... ]"
# This part is the actual code which is used to run our tests
# In my case, the phpunit testsuite resides in the tests directory, so go there
cd /var/www/html/projeto
# executando a bateria de testes
./vendor/bin/phpunit # > /dev/null
# $? esta variável armazena o retorno do último comando executado
rc=$?
if [[ $rc != 0 ]] ; then
# Um código de retorno diferente de zero significa que os testes falharam
printMessage "[ Os testes falharam na rev $newrev - ***PUSH REJEITADO*** ]"
printMessage "[ Execute os testes localmente e confirme que todos os testes estejam passando ]"
exit $rc
fi
fi
done
# Se tudo der ok, finalizamos com retorno de sucesso
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment