Skip to content

Commit

Permalink
Merge pull request #381 from GoogleCloudPlatform/travis-run-changed
Browse files Browse the repository at this point in the history
Travis - Only run tests that have changed
  • Loading branch information
jerjou committed Oct 25, 2016
2 parents b757c53 + 6c20087 commit ffb6968
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion travis.sh
Expand Up @@ -23,8 +23,51 @@ SKIP_TESTS=false
if [ -z "$GOOGLE_APPLICATION_CREDENTIALS" ] ; then
SKIP_TESTS=true
fi

# Finds the closest parent dir that encompasses all changed files, and has a
# pom.xml
travis_changed_files_parent() {
[ -z "$TRAVIS_PULL_REQUEST" ] && return 0 # If we're not in a PR, forget it

(
set +e

changed="$(git diff --name-only "$TRAVIS_COMMIT" "$TRAVIS_BRANCH")"
if [ $? -ne 0 ]; then
# Fall back to git head
changed="$(git diff --name-only "$(git rev-parse HEAD)" "$TRAVIS_BRANCH")"
[ $? -ne 0 ] && return 0 # Give up. Just run everything.
fi

# Find the common prefix
prefix="$(echo "$changed" | \
# N: Do this for a pair of lines
# s: capture the beginning of a line, that's followed by a new line
# starting with that capture group. IOW - two lines that start with the
# same zero-or-more characters. Replace it with just the capture group
# (ie the common prefix).
# D: Delete the first line of the pair, leaving the second line for the
# next pass.
sed -e 'N;s/^\(.*\).*\n\1.*$/\1\n\1/;D')"

while [ ! -z "$prefix" ] && [ ! -r "$prefix/pom.xml" ] && [ "${prefix%/*}" != "$prefix" ]; do
prefix="${prefix%/*}"
done

[ -r "$prefix/pom.xml" ] || return 0

echo "$prefix"
)
}

common_travis_dir="$(travis_changed_files_parent)"

[ -z "$common_travis_dir" ] || pushd "$common_travis_dir"

mvn --batch-mode clean verify -DskipTests=$SKIP_TESTS | egrep -v "(^\[INFO\] Download|^\[INFO\].*skipping)"

[ -z "$common_travis_dir" ] || popd

# Check that all shell scripts in this repo (including this one) pass the
# Shell Check linter.
shellcheck ./**/*.sh
Expand All @@ -44,7 +87,9 @@ test_localhost() {
appengine/datastore/indexes-perfect
)
for testdir in "${devserver_tests[@]}" ; do
./java-repo-tools/scripts/test-localhost.sh appengine "${testdir}"
if [ -z "$common_travis_dir" ] || [[ $testdir = $common_travis_dir* ]]; then
./java-repo-tools/scripts/test-localhost.sh appengine "${testdir}"
fi
done

# newplugin_std_tests=(
Expand Down

0 comments on commit ffb6968

Please sign in to comment.