Skip to content

Commit

Permalink
Use [[ ]] consistently
Browse files Browse the repository at this point in the history
Some script used a mix of `[...]` and `[[...]]` for no apparent
reason. It would be better for maintainability to consistently use one
or the other. Since the scripts all explicitly use Bash, we may as
well use `[[...]]` throughout.
  • Loading branch information
mhucka committed Feb 23, 2025
1 parent 6553a9e commit 508720c
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 45 deletions.
6 changes: 3 additions & 3 deletions check/all
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ for arg in "$@"; do
elif [[ "${arg}" == "--apply-format-changes" ]]; then
apply_arg=( "--apply" )
elif [[ "${#rev[@]}" == 0 ]]; then
if [ "$(git cat-file -t "${arg}" 2> /dev/null)" != "commit" ]; then
if [[ "$(git cat-file -t "${arg}" 2> /dev/null)" != "commit" ]]; then
echo -e "\033[31mNo revision '${arg}'.\033[0m" >&2
exit 1
fi
Expand All @@ -61,7 +61,7 @@ done
echo "Running misc"
check/misc || errors+=( "check/misc failed" )

if [ ${only_changed} -ne 0 ]; then
if [[ ${only_changed} -ne 0 ]]; then
echo "Running incremental pylint"
check/pylint-changed-files || errors+=( "check/pylint-changed-files failed" )
else
Expand All @@ -76,7 +76,7 @@ echo "Running incremental format"
check/format-incremental "${rev[@]}" "${apply_arg[@]}" ||
errors+=( "check/format-incremental failed" )

if [ ${only_changed} -ne 0 ]; then
if [[ ${only_changed} -ne 0 ]]; then
echo "Running pytest and incremental coverage on changed files"
check/pytest-changed-files-and-incremental-coverage "${rev[@]}" ||
errors+=( "check/pytest-changed-files-and-incremental-coverage failed" )
Expand Down
12 changes: 6 additions & 6 deletions check/format-incremental
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ for arg in "$@"; do
only_print=0
elif [[ "${arg}" == "--all" ]]; then
only_changed=0
elif [ -z "${rev}" ]; then
elif [[ -z "${rev}" ]]; then
if ! git rev-parse --verify --quiet --no-revs "${arg}^{commit}"; then
echo -e "\033[31mNo revision '${arg}'.\033[0m" >&2
exit 1
Expand All @@ -59,20 +59,20 @@ done
declare -a format_files
if (( only_changed == 1 )); then
# Figure out which branch to compare against.
if [ -z "${rev}" ]; then
if [ "$(git cat-file -t upstream/main 2> /dev/null)" == "commit" ]; then
if [[ -z "${rev}" ]]; then
if [[ "$(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
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
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
fi
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
2 changes: 1 addition & 1 deletion check/misc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cd "${topdir}" || exit $?
# Check for non-contrib references to contrib.
results=$(grep -Rl "\bcirq\.contrib\b" cirq-core | grep -v "cirq/contrib" | grep -v "__")
RESULT=$?
if [ $RESULT -eq 0 ]; then
if [[ $RESULT -eq 0 ]]; then
echo -e "\033[31m'cirq.contrib' mentioned in non-contrib files:\033[0m"
echo "${results}"
echo
Expand Down
10 changes: 5 additions & 5 deletions check/pylint-changed-files
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $?
cd "${topdir}" || exit $?

# Figure out which revision to compare against.
if [ -n "$1" ] && [[ $1 != -* ]]; then
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
elif [ "$(git cat-file -t upstream/main 2> /dev/null)" == "commit" ]; then
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
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
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
Expand All @@ -65,7 +65,7 @@ declare -i num_changed=${#changed[@]}

# Run it.
echo "Found ${num_changed} lintable files associated with changes." >&2
if [ "${num_changed}" -eq 0 ]; then
if [[ "${num_changed}" -eq 0 ]]; then
exit 0
fi
env PYTHONPATH=dev_tools pylint --jobs=0 --rcfile=dev_tools/conf/.pylintrc "${changed[@]}"
2 changes: 1 addition & 1 deletion check/pytest
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ source dev_tools/pypath || exit $?
pytest "${PYTEST_ARGS[@]}"
declare -i RESULT=$?

if [ "$RESULT" = 5 ]; then
if [[ "$RESULT" = 5 ]]; then
echo "[exit 5] No tests collected, but ignored."
exit 0
fi
Expand Down
8 changes: 4 additions & 4 deletions check/pytest-and-incremental-coverage
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ done

# Figure out which revision to compare against.
declare rev=""
if [ -n "${baserev}" ]; then
if [[ -n "${baserev}" ]]; then
if ! git rev-parse --verify --quiet --no-revs "${baserev}^{commit}"; then
echo -e "\033[31mNo revision '${baserev}'.\033[0m" >&2
exit 1
fi
rev="${baserev}"
elif [ "$(git cat-file -t upstream/main 2> /dev/null)" == "commit" ]; then
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
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
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
Expand Down
10 changes: 5 additions & 5 deletions check/pytest-changed-files
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ cd "${topdir}" || exit $?
# Figure out which branch to compare against.
declare -a rest=( "$@" )
declare rev=""
if [ -n "$1" ] && [[ $1 != -* ]]; then
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
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
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
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
Expand All @@ -73,7 +73,7 @@ declare -i num_changed=${#changed[@]}

# Run it.
echo "Found ${num_changed} test files associated with changes." >&2
if [ "${num_changed}" -eq 0 ]; then
if [[ "${num_changed}" -eq 0 ]]; then
exit 0
fi

Expand Down
10 changes: 5 additions & 5 deletions check/pytest-changed-files-and-incremental-coverage
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ cd "$(git rev-parse --show-toplevel)" || exit 1

# Figure out which revision to compare against.
declare rev=""
if [ -n "$1" ] && [[ $1 != -* ]]; then
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
elif [ "$(git cat-file -t upstream/main 2> /dev/null)" == "commit" ]; then
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
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
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
Expand Down Expand Up @@ -84,7 +84,7 @@ if git diff --name-only "${rev}" -- | grep "__init__\.py$" > /dev/null; then
# Include global API tests when an __init__ file is touched.
changed_python_tests+=('cirq-core/cirq/protocols/json_serialization_test.py')
fi
if [ "${#changed_python_tests[@]}" -eq 0 ]; then
if [[ "${#changed_python_tests[@]}" -eq 0 ]]; then
echo -e "\033[33mNo changed files with associated python tests.\033[0m" >&2
exit 0
fi
Expand Down
6 changes: 3 additions & 3 deletions dev_tools/packaging/produce-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
set -e
trap "{ echo -e '\033[31mFAILED\033[0m'; }" ERR

if [ -z "${1}" ]; then
if [[ -z "${1}" ]]; then
echo -e "\033[31mNo output directory given.\033[0m"
exit 1
fi
Expand All @@ -49,15 +49,15 @@ repo_dir=$(git rev-parse --show-toplevel)
cd "${repo_dir}"

# Make a clean copy of HEAD, without files ignored by git (but potentially kept by setup.py).
if [ -n "$(git status --short)" ]; then
if [[ -n "$(git status --short)" ]]; then
echo -e "\033[31mWARNING: You have uncommitted changes. They won't be included in the package.\033[0m"
fi
tmp_git_dir=$(mktemp -d "/tmp/produce-package-git.XXXXXXXXXXXXXXXX")
trap '{ rm -rf "${tmp_git_dir}"; }' EXIT
echo "Creating pristine repository clone at ${tmp_git_dir}"
git clone --shared --quiet "${repo_dir}" "${tmp_git_dir}"
cd "${tmp_git_dir}"
if [ -n "${SPECIFIED_VERSION}" ]; then
if [[ -n "${SPECIFIED_VERSION}" ]]; then
CURRENT_VERSION=$(set -e; my_dev_tools_modules print_version)
my_dev_tools_modules replace_version --old="${CURRENT_VERSION}" --new="${SPECIFIED_VERSION}"
fi
Expand Down
16 changes: 8 additions & 8 deletions dev_tools/packaging/publish-dev-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ trap "{ echo -e '\033[31mFAILED\033[0m'; }" ERR
EXPECTED_VERSION=$1
PROD_SWITCH=$2

if [ -z "${EXPECTED_VERSION}" ]; then
if [[ -z "${EXPECTED_VERSION}" ]]; then
echo -e "\033[31mFirst argument must be the expected version.\033[0m"
exit 1
fi
Expand All @@ -63,34 +63,34 @@ if [[ "${EXPECTED_VERSION}" != *dev* ]]; then
exit 1
fi
ACTUAL_VERSION_LINE=$(tail -n 1 "${PROJECT_NAME}/_version.py")
if [ "${ACTUAL_VERSION_LINE}" != '__version__ = "'"${EXPECTED_VERSION}"'"' ]; then
if [[ "${ACTUAL_VERSION_LINE}" != '__version__ = "'"${EXPECTED_VERSION}"'"' ]]; then
echo -e "\033[31mExpected version (${EXPECTED_VERSION}) didn't match the one in ${PROJECT_NAME}/_version.py (${ACTUAL_VERSION_LINE}).\033[0m"
exit 1
fi

if [ -z "${PROD_SWITCH}" ] || [ "${PROD_SWITCH}" = "--test" ]; then
if [[ -z "${PROD_SWITCH}" || "${PROD_SWITCH}" = "--test" ]]; then
PYPI_REPOSITORY_FLAG=( "--repository-url=https://test.pypi.org/legacy/" )
PYPI_REPO_NAME="TEST"
USERNAME="${TEST_TWINE_USERNAME}"
PASSWORD="${TEST_TWINE_PASSWORD}"
if [ -z "${USERNAME}" ]; then
if [[ -z "${USERNAME}" ]]; then
echo -e "\033[31mTEST_TWINE_USERNAME environment variable must be set.\033[0m"
exit 1
fi
if [ -z "${PASSWORD}" ]; then
if [[ -z "${PASSWORD}" ]]; then
echo -e "\033[31mTEST_TWINE_PASSWORD environment variable must be set.\033[0m"
exit 1
fi
elif [ "${PROD_SWITCH}" = "--prod" ]; then
elif [[ "${PROD_SWITCH}" = "--prod" ]]; then
PYPI_REPOSITORY_FLAG=( )
PYPI_REPO_NAME="PROD"
USERNAME="${PROD_TWINE_USERNAME}"
PASSWORD="${PROD_TWINE_PASSWORD}"
if [ -z "${USERNAME}" ]; then
if [[ -z "${USERNAME}" ]]; then
echo -e "\033[31mPROD_TWINE_USERNAME environment variable must be set.\033[0m"
exit 1
fi
if [ -z "${PASSWORD}" ]; then
if [[ -z "${PASSWORD}" ]]; then
echo -e "\033[31mPROD_TWINE_PASSWORD environment variable must be set.\033[0m"
exit 1
fi
Expand Down
8 changes: 4 additions & 4 deletions dev_tools/packaging/verify-published-package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ PROJECT_NAME=cirq
PROJECT_VERSION=$1
PROD_SWITCH=$2

if [ -z "${PROJECT_VERSION}" ]; then
if [[ -z "${PROJECT_VERSION}" ]]; then
echo -e "\033[31mFirst argument must be the package version to test.\033[0m"
exit 1
fi

declare -a PIP_FLAGS

if [ "${PROD_SWITCH}" = "--test" ]; then
if [[ "${PROD_SWITCH}" = "--test" ]]; then
PIP_FLAGS=("--index-url=https://test.pypi.org/simple/")
PYPI_REPO_NAME="TEST"
PYPI_PROJECT_NAME="cirq"
elif [ "${PROD_SWITCH}" = "--prod" ]; then
elif [[ "${PROD_SWITCH}" = "--prod" ]]; then
PIP_FLAGS=()
PYPI_REPO_NAME="PROD"
PYPI_PROJECT_NAME="cirq"
Expand All @@ -67,7 +67,7 @@ cd "${tmp_dir}"
trap '{ rm -rf "${tmp_dir}"; }' EXIT

# Test installation from published package
PYTHON_VERSION=python3
PYTHON_VERSION=python3

# Prepare.
CONTRIB_DEPS_FILE="${REPO_ROOT}/cirq-core/cirq/contrib/requirements.txt"
Expand Down

0 comments on commit 508720c

Please sign in to comment.