#!/bin/bash

# This is the git pre-commit hook.    It is executed when you run "git commit",
# and it looks at all the python files in a git repository.   If any of them have
# a "test" function, it executes it.
# 
# If the test method fails, the commit is aborted.

PYTHONPATH=".:lib:$PYTHONPATH"
export PYTHONPATH

for d in tests test . lib bin
do
  if test -d $d
  then
  for f in $d/*.py
  do
    q=$(basename "$f" .py)
    if egrep '^def[[:space:]]{1,}test\(' "$f"
    then
      echo "Testing module ${f}.test"
      if ! python -c "import $q; $q.test()"
      then
        echo "Fail on ${f}.test"
        exit 1
      fi
    fi
    if egrep '^def[[:space:]]{1,}_test\(' "$f"
    then
      echo "Testing module ${f}._test"
      if ! python -c "import $q; $q._test()"
      then
        echo "Fail on ${f}._test"
        exit 1
      fi
    fi
  done
  fi
done
