Skip to content

Commit 6c20087

Browse files
author
Jerjou Cheng
committed
Travis - Only run tests that have changed
1 parent b757c53 commit 6c20087

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

travis.sh

+46-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,51 @@ SKIP_TESTS=false
2323
if [ -z "$GOOGLE_APPLICATION_CREDENTIALS" ] ; then
2424
SKIP_TESTS=true
2525
fi
26+
27+
# Finds the closest parent dir that encompasses all changed files, and has a
28+
# pom.xml
29+
travis_changed_files_parent() {
30+
[ -z "$TRAVIS_PULL_REQUEST" ] && return 0 # If we're not in a PR, forget it
31+
32+
(
33+
set +e
34+
35+
changed="$(git diff --name-only "$TRAVIS_COMMIT" "$TRAVIS_BRANCH")"
36+
if [ $? -ne 0 ]; then
37+
# Fall back to git head
38+
changed="$(git diff --name-only "$(git rev-parse HEAD)" "$TRAVIS_BRANCH")"
39+
[ $? -ne 0 ] && return 0 # Give up. Just run everything.
40+
fi
41+
42+
# Find the common prefix
43+
prefix="$(echo "$changed" | \
44+
# N: Do this for a pair of lines
45+
# s: capture the beginning of a line, that's followed by a new line
46+
# starting with that capture group. IOW - two lines that start with the
47+
# same zero-or-more characters. Replace it with just the capture group
48+
# (ie the common prefix).
49+
# D: Delete the first line of the pair, leaving the second line for the
50+
# next pass.
51+
sed -e 'N;s/^\(.*\).*\n\1.*$/\1\n\1/;D')"
52+
53+
while [ ! -z "$prefix" ] && [ ! -r "$prefix/pom.xml" ] && [ "${prefix%/*}" != "$prefix" ]; do
54+
prefix="${prefix%/*}"
55+
done
56+
57+
[ -r "$prefix/pom.xml" ] || return 0
58+
59+
echo "$prefix"
60+
)
61+
}
62+
63+
common_travis_dir="$(travis_changed_files_parent)"
64+
65+
[ -z "$common_travis_dir" ] || pushd "$common_travis_dir"
66+
2667
mvn --batch-mode clean verify -DskipTests=$SKIP_TESTS | egrep -v "(^\[INFO\] Download|^\[INFO\].*skipping)"
2768

69+
[ -z "$common_travis_dir" ] || popd
70+
2871
# Check that all shell scripts in this repo (including this one) pass the
2972
# Shell Check linter.
3073
shellcheck ./**/*.sh
@@ -44,7 +87,9 @@ test_localhost() {
4487
appengine/datastore/indexes-perfect
4588
)
4689
for testdir in "${devserver_tests[@]}" ; do
47-
./java-repo-tools/scripts/test-localhost.sh appengine "${testdir}"
90+
if [ -z "$common_travis_dir" ] || [[ $testdir = $common_travis_dir* ]]; then
91+
./java-repo-tools/scripts/test-localhost.sh appengine "${testdir}"
92+
fi
4893
done
4994

5095
# newplugin_std_tests=(

0 commit comments

Comments
 (0)