Skip to content

Commit 80bf069

Browse files
authored
add self-test for calc_release_version.py (#1330)
1 parent 3c7951b commit 80bf069

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

build/calc_release_version.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@
2222
current Git commit.
2323
"""
2424

25+
# XXX NOTE XXX NOTE XXX NOTE XXX
26+
# After modifying this script it is advisable to execute the self-test.
27+
#
28+
# This is done by starting in the directory containing this script and then
29+
# executing a separate self-test script, like this:
30+
#
31+
# $ bash ./calc_release_version_selftest.sh
32+
#
33+
# The self-test script will emit diagnostic output. If tracing of the execution
34+
# of each command is desired, then add the -x option to the bash invocation.
35+
# XXX NOTE XXX NOTE XXX NOTE XXX
36+
2537
import datetime
2638
import re
2739
import subprocess
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/bin/bash
2+
# calc_release_version_selftest.sh is used to test output of calc_release_version.py.
3+
# run with:
4+
# cd etc
5+
# ./calc_release_version_selftest.sh
6+
7+
set -o errexit
8+
set -o pipefail
9+
10+
function assert_eq () {
11+
a="$1"
12+
b="$2"
13+
if [[ "$a" != "$b" ]]; then
14+
echo "Assertion failed: $a != $b"
15+
# Print caller
16+
caller
17+
exit 1
18+
fi
19+
}
20+
21+
SAVED_REF=$(git rev-parse HEAD)
22+
23+
function cleanup () {
24+
[[ -e calc_release_version_test.py ]] && rm calc_release_version_test.py
25+
git checkout $SAVED_REF --quiet
26+
}
27+
28+
trap cleanup EXIT
29+
30+
: ${PYTHON_INTERP:=python}
31+
if [[ -z $(command -v "${PYTHON_INTERP}") ]]; then
32+
echo "Python interpreter '${PYTHON_INTERP}' is not valid."
33+
echo "Set the PYTHON_INTERP environment variable to a valid interpreter."
34+
exit 1
35+
fi
36+
37+
# copy calc_release_version.py to a separate file not tracked by git so it does not change on `git checkout`
38+
cp calc_release_version.py calc_release_version_test.py
39+
40+
echo "Test a tagged commit ... begin"
41+
{
42+
git checkout 1.23.4 --quiet
43+
got=$("${PYTHON_INTERP}" calc_release_version_test.py)
44+
assert_eq "$got" "1.23.4"
45+
got=$("${PYTHON_INTERP}" calc_release_version_test.py -p)
46+
assert_eq "$got" "1.23.3"
47+
git checkout - --quiet
48+
}
49+
echo "Test a tagged commit ... end"
50+
51+
DATE=$(date +%Y%m%d)
52+
echo "Test an untagged commit ... begin"
53+
{
54+
# 42a818429d6d586a6abf22367ac6fea1e9ce3f2c is commit before 1.23.4
55+
git checkout 42a818429d6d586a6abf22367ac6fea1e9ce3f2c --quiet
56+
got=$("${PYTHON_INTERP}" calc_release_version_test.py)
57+
assert_eq "$got" "1.23.4-$DATE+git42a818429d"
58+
got=$("${PYTHON_INTERP}" calc_release_version_test.py -p)
59+
assert_eq "$got" "1.23.4"
60+
git checkout - --quiet
61+
}
62+
echo "Test an untagged commit ... end"
63+
64+
echo "Test next minor version ... begin"
65+
{
66+
CURRENT_SHORTREF=$(git rev-parse --revs-only --short=10 HEAD)
67+
got=$("${PYTHON_INTERP}" calc_release_version_test.py --next-minor)
68+
# XXX NOTE XXX NOTE XXX
69+
# If you find yourself looking at this line because the assertion below
70+
# failed, then it is probably because a new major/minor release was made.
71+
# Update the expected output to represent the correct next version.
72+
# XXX NOTE XXX NOTE XXX
73+
assert_eq "$got" "1.25.0-$DATE+git$CURRENT_SHORTREF"
74+
got=$("${PYTHON_INTERP}" calc_release_version_test.py --next-minor -p)
75+
# XXX NOTE XXX NOTE XXX
76+
# If you find yourself looking at this line because the assertion below
77+
# failed, then it is probably because a new major/minor release was made.
78+
# Update the expected output to represent the correct next version.
79+
# XXX NOTE XXX NOTE XXX
80+
assert_eq "$got" "1.24.1"
81+
}
82+
echo "Test next minor version ... end"
83+
84+
echo "All tests passed"

0 commit comments

Comments
 (0)