-
Notifications
You must be signed in to change notification settings - Fork 1k
/
pytest-changed-files
executable file
·81 lines (74 loc) · 2.71 KB
/
pytest-changed-files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env bash
################################################################################
# Runs pytest on files that have been modified.
#
# Usage:
# check/pytest-changed-files [BASE_REVISION] [--pytest-flags]
#
# This tool is not clever; it does not understand dependencies between all
# files. If "x.py" or "x_test.py" have been modified, it will include
# "x_test.py" in the arguments given to pytest. That's all it does.
#
# You can specify a base git revision to compare against (i.e. to use when
# determining whether or not a file is considered to have "changed"). For
# example, you can compare against 'origin/main' or 'HEAD~1'.
#
# If you don't specify a base revision, the following defaults will be tried, in
# order, until one exists:
#
# 1. upstream/main
# 2. origin/main
# 3. main
#
# If none exists, the script fails.
#
# You can pass flags such as "-v" into pytest by placing them after the
# comparison id.
################################################################################
# Get the working directory to the repo root.
thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
cd "${topdir}" || exit $?
# Figure out which branch to compare against.
rest=( "$@" )
if [ -n "$1" ] && [[ $1 != -* ]]; then
if ! git rev-parse --verify --quiet --no-revs "$1^{commit}"; then
echo -e "\033[31mNo revision '$1'.\033[0m" >&2
exit 1
fi
rev=$1
rest=( "${@:2}" )
elif [ "$(git cat-file -t upstream/main 2> /dev/null)" == "commit" ]; then
rev=upstream/main
elif [ "$(git cat-file -t origin/main 2> /dev/null)" == "commit" ]; then
rev=origin/main
elif [ "$(git cat-file -t main 2> /dev/null)" == "commit" ]; then
rev=main
else
echo -e "\033[31mNo default revision found to compare against. Argument #1 must be what to diff against (e.g. 'origin/main' or 'HEAD~1').\033[0m" >&2
exit 1
fi
echo "Comparing against revision '${rev}'." >&2
# Get the _test version of changed python files.
typeset -a changed
IFS=$'\n' read -r -d '' -a changed < \
<(git diff --name-only "${rev}" -- \
| grep "\.py$" \
| sed -e "s/\.py$/_test.py/" \
| sed -e "s/_test_test\.py$/_test.py/" \
| perl -ne 'chomp(); if (-e $_) {print "$_\n"}' \
| sort \
| uniq \
)
if git diff --name-only "${rev}" -- | grep "__init__\.py$" > /dev/null; then
# Include global API tests when an __init__ file is touched.
changed+=('cirq-core/cirq/protocols/json_serialization_test.py')
fi
num_changed=${#changed[@]}
# Run it.
echo "Found ${num_changed} test files associated with changes." >&2
if [ "${num_changed}" -eq 0 ]; then
exit 0
fi
source dev_tools/pypath || exit $?
pytest "${rest[@]}" "${changed[@]}"