Skip to content

Commit

Permalink
Declare variables more consistently
Browse files Browse the repository at this point in the history
This adds declarations to more variables used in the shell scripts.
  • Loading branch information
mhucka committed Feb 23, 2025
1 parent 714f932 commit 6553a9e
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 30 deletions.
9 changes: 5 additions & 4 deletions check/all
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
cd "${topdir}" || exit $?

errors=()
declare -a errors

# Parse arguments.
apply_arg=( )
only_changed=0
rev=( )
declare -a apply_arg
declare -a rev
declare only_changed=0
for arg in "$@"; do
if [[ "${arg}" == "--only-changed-files" ]]; then
only_changed=1
Expand Down Expand Up @@ -89,6 +89,7 @@ fi
echo "Running doctest"
check/doctest || errors+=( "check/doctest failed" )

declare -i result
echo
if [[ "${#errors[@]}" == 0 ]]; then
echo "All checks passed."
Expand Down
10 changes: 5 additions & 5 deletions check/format-incremental
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $?
topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
cd "${topdir}" || exit $?


# Parse arguments.
only_print=1
only_changed=1
rev=""
declare -i only_print=1
declare -i only_changed=1
declare rev=""
for arg in "$@"; do
if [[ "${arg}" == "--apply" ]]; then
only_print=0
Expand Down Expand Up @@ -94,11 +93,12 @@ if (( ${#format_files[@]} == 0 )); then
exit 0
fi

declare BLACKVERSION
BLACKVERSION="$(black --version)"

echo "Running the black formatter... (version: $BLACKVERSION)"

args=("--color")
declare -a args=("--color")
if (( only_print == 1 )); then
args+=("--check" "--diff")
fi
Expand Down
2 changes: 1 addition & 1 deletion check/mypy
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ read -r -a CIRQ_PACKAGES < \

echo -e -n "\033[31m"
mypy --config-file=dev_tools/conf/$CONFIG_FILE "$@" "${CIRQ_PACKAGES[@]}" dev_tools examples
result=$?
declare -i result=$?
echo -e -n "\033[0m"

exit ${result}
4 changes: 2 additions & 2 deletions check/nbformat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#
################################################################################

only_print=1
declare -i only_print=1
for arg in "$@"; do
if [[ "${arg}" == "--apply" ]]; then
only_print=0
Expand All @@ -39,7 +39,7 @@ my_nbfmt() {
python3 -m tensorflow_docs.tools.nbfmt --indent=1 "$@"
}

exit_code=0
declare -i exit_code=0

# Check if notebooks are already formatted and store output for later use
if output=$(my_nbfmt --test docs 2>&1); then
Expand Down
5 changes: 3 additions & 2 deletions check/pylint-changed-files
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ 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
declare base
base="$(git merge-base "${rev}" HEAD)"
if [ "$(git rev-parse "${rev}")" == "${base}" ]; then
if [[ "$(git rev-parse "${rev}")" == "${base}" ]]; then
echo -e "Comparing against revision '${rev}'." >&2
else
echo -e "Comparing against revision '${rev}' (merge base ${base})." >&2
Expand All @@ -60,7 +61,7 @@ IFS=$'\n' read -r -d '' -a changed < \
| grep -E "^(cirq|dev_tools|examples).*.py$"
)

num_changed=${#changed[@]}
declare -i num_changed=${#changed[@]}

# Run it.
echo "Found ${num_changed} lintable files associated with changes." >&2
Expand Down
4 changes: 2 additions & 2 deletions check/pytest
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ cd "${topdir}" || exit $?

# Run in parallel by default. Pass the `-n0` option for a single-process run.
# (the last `-n` option wins)
PYTEST_ARGS=( "-n=auto" "$@" )
declare -a PYTEST_ARGS=( "-n=auto" "$@" )

source dev_tools/pypath || exit $?

pytest "${PYTEST_ARGS[@]}"
RESULT=$?
declare -i RESULT=$?

if [ "$RESULT" = 5 ]; then
echo "[exit 5] No tests collected, but ignored."
Expand Down
15 changes: 8 additions & 7 deletions check/pytest-and-incremental-coverage
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
cd "$( dirname "${BASH_SOURCE[0]}" )" || exit 1
cd "$(git rev-parse --show-toplevel)" || exit 1

pytest_args=()
analyze_cov=1
baserev=""
declare -a pytest_args
declare -i analyze_cov=1
declare baserev=""

if [[ "$1" == [^-]* ]]; then
baserev="$1"
Expand All @@ -44,6 +44,7 @@ for arg in "$@"; do
done

# Figure out which revision to compare against.
declare rev=""
if [ -n "${baserev}" ]; then
if ! git rev-parse --verify --quiet --no-revs "${baserev}^{commit}"; then
echo -e "\033[31mNo revision '${baserev}'.\033[0m" >&2
Expand All @@ -60,8 +61,9 @@ 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
declare base
base="$(git merge-base "${rev}" HEAD)"
if [ "$(git rev-parse "${rev}")" == "${base}" ]; then
if [[ "$(git rev-parse "${rev}")" == "${base}" ]]; then
echo -e "Comparing against revision '${rev}'." >&2
else
echo -e "Comparing against revision '${rev}' (merge base ${base})." >&2
Expand All @@ -72,11 +74,10 @@ source dev_tools/pypath || exit $?

# Run tests while producing coverage files.
check/pytest --cov "${pytest_args[@]}"
pytest_result=$?

declare -i pytest_result=$?

# assume successful cover_result in case coverage is not run
cover_result=0
declare -i cover_result=0
if (( analyze_cov )); then
# Convert to .py,cover files.
coverage annotate
Expand Down
5 changes: 3 additions & 2 deletions check/pytest-changed-files
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
cd "${topdir}" || exit $?

# Figure out which branch to compare against.
rest=( "$@" )
declare -a rest=( "$@" )
declare rev=""
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
Expand Down Expand Up @@ -68,7 +69,7 @@ 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[@]}
declare -i num_changed=${#changed[@]}

# Run it.
echo "Found ${num_changed} test files associated with changes." >&2
Expand Down
10 changes: 6 additions & 4 deletions check/pytest-changed-files-and-incremental-coverage
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cd "$( dirname "${BASH_SOURCE[0]}" )" || exit 1
cd "$(git rev-parse --show-toplevel)" || exit 1

# Figure out which revision to compare against.
declare rev=""
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
Expand All @@ -49,8 +50,9 @@ 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
declare base
base="$(git merge-base "${rev}" HEAD)"
if [ "$(git rev-parse "${rev}")" == "${base}" ]; then
if [[ "$(git rev-parse "${rev}")" == "${base}" ]]; then
echo -e "Comparing against revision '${rev}'." >&2
else
echo -e "Comparing against revision '${rev}' (merge base ${base})." >&2
Expand Down Expand Up @@ -93,17 +95,17 @@ source dev_tools/pypath || exit $?
check/pytest "${changed_python_tests[@]}" \
"${cov_changed_python_file_dirs[@]}" \
--cov-report=annotate
pytest_result=$?
declare -i pytest_result=$?

# Analyze coverage files.
python dev_tools/check_incremental_coverage_annotations.py "${rev}"
cover_result=$?
declare -i cover_result=$?

# Clean up generated coverage files.
git clean --force --quiet -x -- "*.py,cover"

# Report result.
if [ "${pytest_result}" -ne "0" ] || [ "${cover_result}" -ne "0" ]; then
if (( pytest_result || cover_result )); then
exit 1
fi
exit 0
3 changes: 2 additions & 1 deletion check/shellcheck
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
cd "${topdir}" || exit $?

# Process command line arguments
opt_dry_run=0
declare -i opt_dry_run=0
declare -a shellcheck_options
declare -a our_shell_scripts

Expand Down Expand Up @@ -50,6 +50,7 @@ required_shell_scripts=(
dev_tools/pypath
)

declare scripts_not_found=""
scripts_not_found="$(comm -13 \
<(printf "%s\n" "${our_shell_scripts[@]}") \
<(printf "%s\n" "${required_shell_scripts[@]}") )"
Expand Down
1 change: 1 addition & 0 deletions check/ts-build-current
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
check/ts-build

# Find the changed bundle.js files, if any
declare untracked
untracked=$(git status --porcelain 2>/dev/null | grep "cirq-web/cirq_ts/dist/" | cut -d " " -f 3)

if [[ -n "$untracked" ]]; then
Expand Down

0 comments on commit 6553a9e

Please sign in to comment.