From 20226eca636873e1cd0418c244976d6bea11d62f Mon Sep 17 00:00:00 2001 From: Bruno Goncalves Date: Mar 29 2018 11:29:48 +0000 Subject: python syntax test --- diff --git a/ci/Jenkinsfile b/ci/Jenkinsfile index 0666c7d..16e6667 100644 --- a/ci/Jenkinsfile +++ b/ci/Jenkinsfile @@ -94,6 +94,16 @@ pipeline { ''' } } + stage('Tests') { + steps { + sh ''' + set -efux + # Install test dependencies + yum install -y gcc python-devel redhat-rpm-config python-virtualenv + ./tests/python-syntax.sh + ''' + } + } stage('SRPM') { steps { sh ''' diff --git a/tests/python-syntax.sh b/tests/python-syntax.sh new file mode 100755 index 0000000..470a299 --- /dev/null +++ b/tests/python-syntax.sh @@ -0,0 +1,80 @@ +#!/usr/bin/bash + +which virtualenv > /dev/null 2>&1 +if [[ $? -ne 0 ]]; then + echo "Please install python virtualenv package before running the test" + exit 1 +fi + +function run() { + # Function to show the command line it will execute, + # otherwise it only shows command output + if [[ -z $1 ]]; then + echo "run() requires command as argument" + return 1 + fi + echo "Running: $1" + eval $1 + return $? +} + +test_path=$(readlink -f $0) +tests_dir=$(dirname $test_path) +# Use the directory above tests to search for python scripts +source_dir=$(dirname $tests_dir) + +echo "Searching for python scripts under ${source_dir}..." +all_files=$(find $source_dir -type f -not -path "*.git*") +python_files="" +# Check what files are python scripts +for my_file in $all_files; do + file -i ${my_file} | grep -q x-python + if [[ $? -eq 0 || ${my_file: -3} == ".py" ]]; then + python_files+="${my_file} " + fi +done + +error=0 + +virt_env_dir=$(mktemp -d) +if [[ -z $virt_env_dir ]]; then + echo "FAIL: Could not create tmp directory" + exit 1; +fi + +virtualenv ${virt_env_dir} +source ${virt_env_dir}/bin/activate --system-site-packages + +pip install inspektor +if [[ $? -ne 0 ]]; then + deactivate + echo "FAIL: Could not install inspektor" + rm -rf ${virt_env_dir} + exit 1 +fi + +plint_enable="W0611,W0612,W0622" +# Disabling E0611, because pylint has problems importing distutils modules on virtualenv +plint_disable="W,R,C,E1002,E1101,E1103,E1120,F0401,I0011,E0611" + +for pyfile in $python_files; do + run "inspekt indent ${pyfile}" + error=$((error + $?)) + run "inspekt lint --enable $plint_enable --disable $plint_disable ${pyfile}" + error=$((error + $?)) + run "pycodestyle --max-line-length=120 ${pyfile}" + error=$((error + $?)) +done + +deactivate + +if [[ -z "$KEEP_VIRT_ENV" ]]; then + rm -rf ${virt_env_dir} +fi + +if [[ ${error} -ne 0 ]]; then + echo "FAIL: problem with python syntax" + exit 1 +fi + +exit 0