Skip to content

Commit

Permalink
Always use bash arrays for changed files and directories (quantumlib#…
Browse files Browse the repository at this point in the history
…4703)

Store changed paths in bash arrays by splitting diff output at newlines.
Avoid scalars that need to be split and may perform wildcard expansion.

Ref: https://www.baeldung.com/linux/reading-output-into-array#using-theread-command
  • Loading branch information
pavoljuhas authored and rht committed May 1, 2023
1 parent 98cc80c commit bae54a5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
9 changes: 5 additions & 4 deletions check/pylint-changed-files
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ else
rev="${base}"
fi

changed=$(git diff --name-only ${rev} -- \
typeset -a changed
IFS=$'\n' read -r -d '' -a changed < \
<(git diff --name-only ${rev} -- \
| grep -E "^(cirq|dev_tools|examples).*.py$"
)

num_changed=$(echo -e "${changed[@]}" | wc -w)
num_changed=${#changed[@]}

# Run it.
echo "Found ${num_changed} lintable files associated with changes." >&2
if [ "${num_changed}" -eq 0 ]; then
exit 0
fi
env PYTHONPATH=dev_tools pylint --rcfile=dev_tools/conf/.pylintrc ${changed[@]}

env PYTHONPATH=dev_tools pylint --rcfile=dev_tools/conf/.pylintrc "${changed[@]}"
8 changes: 5 additions & 3 deletions check/pytest-changed-files
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ fi
echo "Comparing against revision '${rev}'." >&2

# Get the _test version of changed python files.
changed=$(git diff --name-only ${rev} -- \
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/" \
Expand All @@ -65,7 +67,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=$(echo -e "${changed[@]}" | wc -w)
num_changed=${#changed[@]}

# Run it.
echo "Found ${num_changed} test files associated with changes." >&2
Expand All @@ -75,4 +77,4 @@ fi

source dev_tools/pypath

pytest ${rest} ${changed[@]}
pytest ${rest} "${changed[@]}"
8 changes: 6 additions & 2 deletions check/pytest-changed-files-and-incremental-coverage
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,19 @@ else
fi

# Find involved files.
cov_changed_python_file_dirs=$(git diff --name-only "${rev}" -- \
typeset -a cov_changed_python_file_dirs
IFS=$'\n' read -r -d '' -a cov_changed_python_file_dirs < \
<(git diff --name-only "${rev}" -- \
| grep "\.py$" \
| grep -v "_pb2\.py$" \
| xargs -I {} dirname {} \
| perl -ne 'chomp(); if (-e $_) {print "--cov=$_\n"}' \
| sort \
| uniq \
)
changed_python_tests=$(git diff --name-only "${rev}" -- \
typeset -a changed_python_tests
IFS=$'\n' read -r -d '' -a changed_python_tests < \
<(git diff --name-only "${rev}" -- \
| grep "\.py$" \
| sed -e "s/\.py$/_test.py/" \
| sed -e "s/_test_test\.py$/_test.py/" \
Expand Down

0 comments on commit bae54a5

Please sign in to comment.