Skip to content

Commit 097417c

Browse files
committed
add compatibility tests
1 parent e4a99c7 commit 097417c

4 files changed

Lines changed: 154 additions & 0 deletions

File tree

test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
test_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/tools
6+
7+
. "${test_dir}"/test_compatibility.sh

tools/test_compatibility.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
2+
. "${dir}"/test_runner.sh
3+
. "${dir}"/test_helper.sh
4+
5+
runner=$(get_test_runner "${1:-local}")
6+
7+
test_single_file_change() {
8+
run_compat_test de48add f0d09c4
9+
}
10+
11+
test_workspace_change() {
12+
run_compat_test 50efa15 60b40a3
13+
}
14+
15+
test_multiple_file_change() {
16+
run_compat_test b013593 95b4f75
17+
}
18+
19+
$runner test_single_file_change
20+
$runner test_workspace_change
21+
$runner test_multiple_file_change

tools/test_helper.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env
2+
set -eo pipefail
3+
4+
function run_compat_test() {
5+
local start_commit="$1"
6+
local end_commit="$2"
7+
8+
tmp_dir=$(mktemp -d -t ci-XXXXXXXXXX)
9+
repo_path="$tmp_dir/bazel-remote"
10+
bazel_diff="$(bazel info bazel-bin)/tools/bazel-diff/bazel-diff"
11+
bazel_differ="$(bazel info bazel-bin)/cli/bazel-differ_/bazel-differ"
12+
13+
git clone https://github.com/buchgr/bazel-remote.git "$repo_path"
14+
15+
common_opts="-w $repo_path -b $(which bazelisk)"
16+
17+
pushd "$repo_path"
18+
git checkout "$start_commit"
19+
20+
"$bazel_diff" generate-hashes $common_opts $tmp_dir/bazel-diff-starting.txt
21+
"$bazel_differ" generate-hashes $common_opts $tmp_dir/bazel-differ-starting.txt
22+
23+
git checkout "$end_commit"
24+
25+
"$bazel_diff" generate-hashes $common_opts $tmp_dir/bazel-diff-ending.txt
26+
"$bazel_differ" generate-hashes $common_opts $tmp_dir/bazel-differ-ending.txt
27+
28+
"$bazel_diff" $common_opts -sh $tmp_dir/bazel-diff-starting.txt -fh $tmp_dir/bazel-diff-ending.txt -o $tmp_dir/bazel-diff-targets.txt
29+
"$bazel_differ" diff $common_opts -s $tmp_dir/bazel-differ-starting.txt -f $tmp_dir/bazel-differ-ending.txt -o $tmp_dir/bazel-differ-targets.txt
30+
31+
cat $tmp_dir/bazel-diff-targets.txt | sort > $tmp_dir/bazel-diff-targets-sorted.txt
32+
cat $tmp_dir/bazel-differ-targets.txt | sort > $tmp_dir/bazel-differ-targets-sorted.txt
33+
34+
diff $tmp_dir/bazel-diff-targets-sorted.txt $tmp_dir/bazel-differ-targets-sorted.txt
35+
36+
popd
37+
38+
rm -rf "$tmp_dir"
39+
}

tools/test_runner.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Test runner functions for bazel-mypy-integration integration tests.
4+
# Credit: https://github.com/bazelbuild/rules_scala/blob/master/test/shell/test_runner.sh
5+
6+
NC='\033[0m'
7+
GREEN='\033[0;32m'
8+
RED='\033[0;31m'
9+
TIMOUT=60
10+
11+
run_test_ci() {
12+
# spawns the test to new process
13+
local TEST_ARG=$@
14+
local log_file=output_$$.log
15+
echo "running test $TEST_ARG"
16+
eval $TEST_ARG &>$log_file &
17+
local test_pid=$!
18+
SECONDS=0
19+
test_pulse_printer $! $TIMOUT $TEST_ARG &
20+
local pulse_printer_pid=$!
21+
local result
22+
23+
{
24+
wait $test_pid 2>/dev/null
25+
result=$?
26+
kill $pulse_printer_pid && wait $pulse_printer_pid 2>/dev/null || true
27+
} || return 1
28+
29+
DURATION=$SECONDS
30+
if [ $result -eq 0 ]; then
31+
echo -e "\n${GREEN}Test \"$TEST_ARG\" successful ($DURATION sec) $NC"
32+
else
33+
echo -e "\nLog:\n"
34+
cat $log_file
35+
echo -e "\n${RED}Test \"$TEST_ARG\" failed $NC ($DURATION sec) $NC"
36+
fi
37+
return $result
38+
}
39+
40+
test_pulse_printer() {
41+
# makes sure something is printed to stdout while test is running
42+
local test_pid=$1
43+
shift
44+
local timeout=$1 # in minutes
45+
shift
46+
local count=0
47+
48+
# clear the line
49+
echo -e "\n"
50+
51+
while [ $count -lt $timeout ]; do
52+
count=$(($count + 1))
53+
echo -ne "Still running: \"$@\"\r"
54+
sleep 60
55+
done
56+
57+
echo -e "\n${RED}Timeout (${timeout} minutes) reached. Terminating \"$@\"${NC}\n"
58+
kill -9 $test_pid
59+
}
60+
61+
run_test_local() {
62+
# runs the tests locally
63+
set +e
64+
SECONDS=0
65+
TEST_ARG=$@
66+
echo "running test $TEST_ARG"
67+
RES=$($TEST_ARG 2>&1)
68+
RESPONSE_CODE=$?
69+
DURATION=$SECONDS
70+
if [ $RESPONSE_CODE -eq 0 ]; then
71+
echo -e "${GREEN} Test \"$TEST_ARG\" successful ($DURATION sec) $NC"
72+
else
73+
echo -e "\nLog:\n"
74+
echo "$RES"
75+
echo -e "${RED} Test \"$TEST_ARG\" failed $NC ($DURATION sec) $NC"
76+
exit $RESPONSE_CODE
77+
fi
78+
}
79+
80+
get_test_runner() {
81+
test_env=$1
82+
if [[ "${test_env}" != "ci" && "${test_env}" != "local" ]]; then
83+
echo -e "${RED}test_env must be either 'local' or 'ci'"
84+
exit 1
85+
fi
86+
echo "run_test_${test_env}"
87+
}

0 commit comments

Comments
 (0)