Search code examples
bashjenkinsnose

Conditional nosetests from bash


I'm attempting to only run tests in files that have changed between git commits as part of our integration testing in Jenkins.

The code below works, unless the filters remove all file results. Then ALL tests get run by nose. I would want no tests run in that case.

if [ $GIT_PREVIOUS_SUCCESSFUL_COMMIT != $GIT_COMMIT ]; then
    git diff --name-only --diff-filter=d $GIT_PREVIOUS_SUCCESSFUL_COMMIT $GIT_COMMIT | grep .py | xargs nosetests -vv
fi

Any suggestions?


Solution

  • I think I've got it. Needed to split the initial results and check if the list is empty before calling nosetests.

    if [ $GIT_PREVIOUS_SUCCESSFUL_COMMIT != $GIT_COMMIT ]; then
        TESTFILES=$(git diff --name-only --diff-filter=d $GIT_PREVIOUS_SUCCESSFUL_COMMIT $GIT_COMMIT | grep .py)
        if [ -z "$TESTFILES" ]; then
            echo "No test files found"
        else
            nosetests -vv $TESTFILES
        fi
    fi