Skip to content

Commit

Permalink
Use parallel pytest in the CI checks (quantumlib#5006)
Browse files Browse the repository at this point in the history
Allow parallel execution of check/pytest scripts in GitHub CI workflows.
  • Loading branch information
pavoljuhas authored and rht committed May 1, 2023
1 parent 72e0b49 commit e93c2e3
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 42 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
- name: Install changed files test dependencies
run: dev_tools/conf/pip-install-minimal-for-pytest-changed-files.sh
- name: Changed files test
run: check/pytest-changed-files
run: check/pytest-changed-files -n auto
lint:
name: Lint check
runs-on: ubuntu-20.04
Expand Down Expand Up @@ -168,7 +168,7 @@ jobs:
- name: Run Quil dependencies
run: docker-compose -f cirq-rigetti/docker-compose.test.yaml up -d
- name: Pytest check
run: check/pytest --ignore=cirq-core/cirq/contrib --actually-quiet --rigetti-integration
run: check/pytest -n auto --ignore=cirq-core/cirq/contrib --actually-quiet --rigetti-integration
- name: Stop Quil dependencies
run: docker-compose -f cirq-rigetti/docker-compose.test.yaml down
build_docs:
Expand Down Expand Up @@ -221,7 +221,7 @@ jobs:
- name: Run Quil dependencies
run: docker-compose -f cirq-rigetti/docker-compose.test.yaml up -d
- name: Coverage check
run: check/pytest-and-incremental-coverage --actually-quiet
run: check/pytest-and-incremental-coverage -n auto
- name: Stop Quil dependencies
run: docker-compose -f cirq-rigetti/docker-compose.test.yaml down
windows:
Expand All @@ -245,7 +245,7 @@ jobs:
- name: Pytest Windows
run: |
source dev_tools/pypath
check/pytest --ignore=cirq-core/cirq/contrib --actually-quiet
check/pytest -n auto --ignore=cirq-core/cirq/contrib --actually-quiet
shell: bash
macos:
name: Pytest MacOS
Expand All @@ -266,7 +266,7 @@ jobs:
- name: Install requirements
run: pip install --upgrade --upgrade-strategy eager -r dev_tools/requirements/no-contrib.env.txt
- name: Pytest check
run: check/pytest --ignore=cirq-core/cirq/contrib
run: check/pytest -n auto --ignore=cirq-core/cirq/contrib
notebooks-stable:
name: Changed Notebooks Isolated Test against Cirq stable
env:
Expand Down
53 changes: 39 additions & 14 deletions check/pytest-and-incremental-coverage
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Finds changed uncovered lines.
#
# Usage:
# check/pytest-and-incremental-coverage [BASE_REVISION]
# check/pytest-and-incremental-coverage [BASE_REVISION] [PYTEST_ARGS]
#
# You can specify a base git revision to compare against (i.e. to use when
# determining whether or not a line is considered to have "changed"). To make
Expand All @@ -26,13 +26,30 @@
cd "$( dirname "${BASH_SOURCE[0]}" )" || exit 1
cd "$(git rev-parse --show-toplevel)" || exit 1

PYTEST_ARGS=()
ANALYZE_COV=1
BASEREV=""

if [[ "$1" == [^-]* ]]; then
BASEREV="$1"
shift
fi

for arg in "$@"; do
if [[ "${arg}" == "--no-analyze" ]]; then
ANALYZE_COV=0
else
PYTEST_ARGS+=("${arg}")
fi
done

# Figure out which revision to compare against.
if [ ! -z "$1" ] && [[ $1 != -* ]]; then
if [ "$(git cat-file -t $1 2> /dev/null)" != "commit" ]; then
echo -e "\033[31mNo revision '$1'.\033[0m" >&2
if [ -n "${BASEREV}" ]; then
if [ "$(git cat-file -t "${BASEREV}" 2> /dev/null)" != "commit" ]; then
echo -e "\033[31mNo revision '${BASEREV}'.\033[0m" >&2
exit 1
fi
rev=$1
rev="${BASEREV}"
elif [ "$(git cat-file -t upstream/master 2> /dev/null)" == "commit" ]; then
rev=upstream/master
elif [ "$(git cat-file -t origin/master 2> /dev/null)" == "commit" ]; then
Expand All @@ -57,19 +74,27 @@ source dev_tools/pypath
check/pytest --actually-quiet \
--rigetti-integration \
--cov \
--cov-report=annotate \
--cov-config=dev_tools/conf/.coveragerc
--cov-config=dev_tools/conf/.coveragerc \
"${PYTEST_ARGS[@]}"
pytest_result=$?

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

# Clean up generated coverage files.
find . | grep "\.py,cover$" | xargs rm -f
# assume succesful cover_result in case coverage is not run
cover_result=0
if (( $ANALYZE_COV )); then
# Convert to .py,cover files.
coverage annotate

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

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

# Report result.
if [ "${pytest_result}" -ne "0" ] || [ "${cover_result}" -ne "0" ]; then
exit 1
if (( ${pytest_result} || ${cover_result} )); then
exit 1
fi
exit 0
2 changes: 1 addition & 1 deletion check/pytest-changed-files-and-incremental-coverage
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ python dev_tools/check_incremental_coverage_annotations.py "${rev}"
cover_result=$?

# Clean up generated coverage files.
find . | grep "\.py,cover$" | xargs rm -f
git clean --force --quiet -x -- "*.py,cover"

# Report result.
if [ "${pytest_result}" -ne "0" ] || [ "${cover_result}" -ne "0" ]; then
Expand Down
68 changes: 46 additions & 22 deletions dev_tools/bash_scripts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,11 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
assert result.exit_code == 0
assert result.out == (
'INTERCEPTED check/pytest '
'--actually-quiet --rigetti-integration --cov --cov-report=annotate '
'--actually-quiet --rigetti-integration --cov '
'--cov-config=dev_tools/conf/.coveragerc\n'
'The annotate command will be removed in a future version.\n'
'Get in touch if you still use it: ned@nedbatchelder.com\n'
'No data to report.\n'
'INTERCEPTED '
'python dev_tools/check_incremental_coverage_annotations.py HEAD\n'
)
Expand All @@ -375,10 +378,13 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
assert result.exit_code == 0
assert result.out == (
'INTERCEPTED check/pytest '
'--actually-quiet --rigetti-integration --cov --cov-report=annotate '
'--actually-quiet --rigetti-integration --cov '
'--cov-config=dev_tools/conf/.coveragerc\n'
'INTERCEPTED python '
'dev_tools/check_incremental_coverage_annotations.py master\n'
'The annotate command will be removed in a future version.\n'
'Get in touch if you still use it: ned@nedbatchelder.com\n'
'No data to report.\n'
'INTERCEPTED '
'python dev_tools/check_incremental_coverage_annotations.py master\n'
)
assert result.err == "Comparing against revision 'master'.\n"

Expand All @@ -391,10 +397,13 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
assert result.exit_code == 0
assert result.out == (
'INTERCEPTED check/pytest '
'--actually-quiet --rigetti-integration --cov --cov-report=annotate '
'--actually-quiet --rigetti-integration --cov '
'--cov-config=dev_tools/conf/.coveragerc\n'
'INTERCEPTED python '
'dev_tools/check_incremental_coverage_annotations.py origin/master\n'
'The annotate command will be removed in a future version.\n'
'Get in touch if you still use it: ned@nedbatchelder.com\n'
'No data to report.\n'
'INTERCEPTED '
'python dev_tools/check_incremental_coverage_annotations.py origin/master\n'
)
assert result.err == "Comparing against revision 'origin/master'.\n"

Expand All @@ -407,10 +416,13 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
assert result.exit_code == 0
assert result.out == (
'INTERCEPTED check/pytest '
'--actually-quiet --rigetti-integration --cov --cov-report=annotate '
'--actually-quiet --rigetti-integration --cov '
'--cov-config=dev_tools/conf/.coveragerc\n'
'INTERCEPTED python '
'dev_tools/check_incremental_coverage_annotations.py upstream/master\n'
'The annotate command will be removed in a future version.\n'
'Get in touch if you still use it: ned@nedbatchelder.com\n'
'No data to report.\n'
'INTERCEPTED '
'python dev_tools/check_incremental_coverage_annotations.py upstream/master\n'
)
assert result.err == "Comparing against revision 'upstream/master'.\n"

Expand All @@ -423,10 +435,13 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
assert result.exit_code == 0
assert result.out == (
'INTERCEPTED check/pytest '
'--actually-quiet --rigetti-integration --cov --cov-report=annotate '
'--actually-quiet --rigetti-integration --cov '
'--cov-config=dev_tools/conf/.coveragerc\n'
'INTERCEPTED python '
'dev_tools/check_incremental_coverage_annotations.py upstream/master\n'
'The annotate command will be removed in a future version.\n'
'Get in touch if you still use it: ned@nedbatchelder.com\n'
'No data to report.\n'
'INTERCEPTED '
'python dev_tools/check_incremental_coverage_annotations.py upstream/master\n'
)
assert result.err == "Comparing against revision 'upstream/master'.\n"

Expand All @@ -451,10 +466,13 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
assert result.exit_code == 0
assert result.out == (
'INTERCEPTED check/pytest '
'--actually-quiet --rigetti-integration --cov --cov-report=annotate '
'--actually-quiet --rigetti-integration --cov '
'--cov-config=dev_tools/conf/.coveragerc\n'
'INTERCEPTED python '
'dev_tools/check_incremental_coverage_annotations.py HEAD\n'
'The annotate command will be removed in a future version.\n'
'Get in touch if you still use it: ned@nedbatchelder.com\n'
'No data to report.\n'
'INTERCEPTED '
'python dev_tools/check_incremental_coverage_annotations.py HEAD\n'
)
assert result.err == "Comparing against revision 'HEAD'.\n"

Expand All @@ -467,10 +485,13 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
assert result.exit_code == 0
assert result.out == (
'INTERCEPTED check/pytest '
'--actually-quiet --rigetti-integration --cov --cov-report=annotate '
'--actually-quiet --rigetti-integration --cov '
'--cov-config=dev_tools/conf/.coveragerc\n'
'INTERCEPTED python '
'dev_tools/check_incremental_coverage_annotations.py master\n'
'The annotate command will be removed in a future version.\n'
'Get in touch if you still use it: ned@nedbatchelder.com\n'
'No data to report.\n'
'INTERCEPTED '
'python dev_tools/check_incremental_coverage_annotations.py master\n'
)
assert result.err == "Comparing against revision 'master'.\n"

Expand All @@ -490,10 +511,13 @@ def test_pytest_and_incremental_coverage_branch_selection(tmpdir_factory):
assert result.exit_code == 0
assert result.out.startswith(
'INTERCEPTED check/pytest '
'--actually-quiet --rigetti-integration --cov --cov-report=annotate '
'--actually-quiet --rigetti-integration --cov '
'--cov-config=dev_tools/conf/.coveragerc\n'
'INTERCEPTED python '
'dev_tools/check_incremental_coverage_annotations.py '
'The annotate command will be removed in a future version.\n'
'Get in touch if you still use it: ned@nedbatchelder.com\n'
'No data to report.\n'
'INTERCEPTED '
'python dev_tools/check_incremental_coverage_annotations.py '
)
assert result.err.startswith("Comparing against revision 'master' (merge base ")

Expand Down

0 comments on commit e93c2e3

Please sign in to comment.