From a8c121a482ef51637c3349c4155d4bc4b7f3b60a Mon Sep 17 00:00:00 2001 From: mhucka Date: Fri, 26 Sep 2025 20:38:17 +0000 Subject: [PATCH 01/11] Add check/all This is adapted from, but heavily modified from, Cirq's check/all. --- check/all | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100755 check/all diff --git a/check/all b/check/all new file mode 100755 index 000000000..afc13be77 --- /dev/null +++ b/check/all @@ -0,0 +1,125 @@ +#!/usr/bin/env bash +# +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Summary: run multiple checks on the OpenFermion code base. +# This is modeled in part on Cirq's check/all script. + +declare -r usage="\ +Usage: ${0##*/} [--help] [--only-changed-files] [--apply-format-changes] [BASE_REV] + +If the first argument given on the command line is the option --help or -h, +this program prints usage information and then exits. + +With no arguments, this runs multiple checks on the code base. These tests +are based on the programs in the checks/ subdirectory. + +If the option --no-coverage is specified, check/pytest will be run instead of +check/pytest-and-incremental-coverage. + +If --apply-format-changes is specified, the flag --apply will be passed to +check/format-incremental to apply the format changes suggested by the +formatter. + +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). If given, +the argument BASE_REV is passed on to tests that can use it, such as +check/pytest-and-incremental-coverage." + +set -eo pipefail -o errtrace +shopt -s inherit_errexit + +# Get the working directory to the repo root. +thisdir=$(dirname "${BASH_SOURCE[0]:?}") || exit $? +repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel) || exit $? +cd "${repo_dir}" || exit $? + +function error() { + echo >&2 "ERROR: ${*}" +} + +# ~~~~ Parse the CLI arguments and gather some data ~~~~ + +declare rev +declare apply_arg +declare only_changed +for arg in "$@"; do + case "${arg}" in + -h | --help) + echo "${usage}" + exit 0 + ;; + --apply-format-changes) + apply_arg="--apply" + shift + ;; + --only-changed-files) + only_changed="true" + shift + ;; + -*) + if [[ -n "${rev}" ]]; then + error "Invalid arguments." + echo "${usage}" + exit 1 + fi + ;; + *) + if [ "$(git cat-file -t "${arg}" 2> /dev/null)" != "commit" ]; then + error "No revision '${arg}'" + exit 1 + fi + rev="${arg}" + ;; + esac +done + +# ~~~~ Run the tests ~~~~ + +declare -a errors +declare program + +function run() { + local -r program="$1" + echo "Running $program ..." + $program || errors+=( "${program} failed" ) + echo +} + +if [[ -n "${only_changed}" ]]; then + run "check/format-incremental ${rev} ${apply_arg}" + run "check/pylint-changed-files ${rev}" +else + run "check/format-incremental ${rev} ${apply_arg} --all" + run "check/pylint" +fi +run "check/mypy" +run "check/pytest-and-incremental-coverage ${rev}" +run "check/shellcheck" + +# ~~~~ Summarize the results and exit with a status code ~~~~ + +declare exit_code=0 +echo +echo "Done." +if [[ "${#errors[@]}" == 0 ]]; then + echo "All checks passed." +else + error "Some checks failed." + printf " %s\n" "${errors[@]}" + exit_code=1 +fi + +exit "${exit_code}" From 3a368b688f7c933448d0cd4ffa3c42a3a8ae3cd0 Mon Sep 17 00:00:00 2001 From: mhucka Date: Mon, 13 Oct 2025 21:53:01 +0000 Subject: [PATCH 02/11] Remove description of non-existent `--no-coverage` option --- check/all | 3 --- 1 file changed, 3 deletions(-) diff --git a/check/all b/check/all index afc13be77..0ef5fe03d 100755 --- a/check/all +++ b/check/all @@ -26,9 +26,6 @@ this program prints usage information and then exits. With no arguments, this runs multiple checks on the code base. These tests are based on the programs in the checks/ subdirectory. -If the option --no-coverage is specified, check/pytest will be run instead of -check/pytest-and-incremental-coverage. - If --apply-format-changes is specified, the flag --apply will be passed to check/format-incremental to apply the format changes suggested by the formatter. From cc75a884a3b28d12dab46cc3e6ba5208d04c834f Mon Sep 17 00:00:00 2001 From: mhucka Date: Mon, 13 Oct 2025 21:54:54 +0000 Subject: [PATCH 03/11] Remove unnecessary exit statements --- check/all | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/check/all b/check/all index 0ef5fe03d..73db60db6 100755 --- a/check/all +++ b/check/all @@ -39,9 +39,9 @@ set -eo pipefail -o errtrace shopt -s inherit_errexit # Get the working directory to the repo root. -thisdir=$(dirname "${BASH_SOURCE[0]:?}") || exit $? -repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel) || exit $? -cd "${repo_dir}" || exit $? +thisdir=$(dirname "${BASH_SOURCE[0]:?}") +repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel) +cd "${repo_dir}" function error() { echo >&2 "ERROR: ${*}" From 373b4f8e6131e0cfe59d9b45e65383c0be15cfad Mon Sep 17 00:00:00 2001 From: mhucka Date: Tue, 14 Oct 2025 02:04:00 +0000 Subject: [PATCH 04/11] Ignore `*,cover` files In more recent versions of Pytest coverage, the extension is named with a comma (`,cover`) and not a period. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ff9450490..f50859de4 100644 --- a/.gitignore +++ b/.gitignore @@ -67,6 +67,7 @@ htmlcov/ nosetests.xml coverage.xml *.cover +*,cover .hypothesis/ # Sphinx documentation From bfe8785d94b86e986f6aff1ce48f3ace5fdb2903 Mon Sep 17 00:00:00 2001 From: mhucka Date: Tue, 14 Oct 2025 04:22:19 +0000 Subject: [PATCH 05/11] Add check/all to scripts checked by check/shellcheck --- check/shellcheck | 3 +++ 1 file changed, 3 insertions(+) diff --git a/check/shellcheck b/check/shellcheck index 58a2215ff..f5d36bfdf 100755 --- a/check/shellcheck +++ b/check/shellcheck @@ -54,10 +54,13 @@ IFS=$'\n' read -r -d "" -a our_shell_scripts < <( declare -a required_shell_scripts required_shell_scripts=( # items below must be sorted + check/all check/format-incremental check/mypy check/pylint + check/pylint-changed-files check/pytest + check/pytest-and-incremental-coverage ) scripts_not_found=$(comm -13 \ From 3cd8297aba8dc8b7b87e49aa1cf2083a8fe33e53 Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Sat, 22 Nov 2025 15:24:54 -0800 Subject: [PATCH 06/11] Apply suggestion from @pavoljuhas Co-authored-by: Pavol Juhas --- check/all | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/check/all b/check/all index 73db60db6..95a304ab7 100755 --- a/check/all +++ b/check/all @@ -67,11 +67,9 @@ for arg in "$@"; do shift ;; -*) - if [[ -n "${rev}" ]]; then - error "Invalid arguments." - echo "${usage}" - exit 1 - fi + error "Invalid option '${arg}'" + error "See '$0 --help' for the list of supported options." + exit 1 ;; *) if [ "$(git cat-file -t "${arg}" 2> /dev/null)" != "commit" ]; then From 442b21d58cc71b81482d0f98c5e7c9bdb5aba4e5 Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Sat, 22 Nov 2025 15:25:28 -0800 Subject: [PATCH 07/11] Apply suggestion from @pavoljuhas Co-authored-by: Pavol Juhas --- check/all | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/check/all b/check/all index 95a304ab7..d52903644 100755 --- a/check/all +++ b/check/all @@ -72,11 +72,10 @@ for arg in "$@"; do exit 1 ;; *) - if [ "$(git cat-file -t "${arg}" 2> /dev/null)" != "commit" ]; then + if ! rev=( "$(git rev-parse --verify --end-of-options "${arg}^{commit}")" ); then error "No revision '${arg}'" exit 1 fi - rev="${arg}" ;; esac done From 45dfe78c588c993891739bbdea595e1e1a84e433 Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Sat, 22 Nov 2025 15:26:11 -0800 Subject: [PATCH 08/11] Apply suggestion from @pavoljuhas Co-authored-by: Pavol Juhas --- check/all | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/check/all b/check/all index d52903644..07361ec76 100755 --- a/check/all +++ b/check/all @@ -82,8 +82,7 @@ done # ~~~~ Run the tests ~~~~ -declare -a errors -declare program +declare -a errors=() function run() { local -r program="$1" From b202803a49a535132c5985e83b51b71bfa19c78a Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Sat, 22 Nov 2025 15:26:41 -0800 Subject: [PATCH 09/11] Apply suggestion from @pavoljuhas Co-authored-by: Pavol Juhas --- check/all | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/check/all b/check/all index 07361ec76..3ec862232 100755 --- a/check/all +++ b/check/all @@ -85,9 +85,8 @@ done declare -a errors=() function run() { - local -r program="$1" - echo "Running $program ..." - $program || errors+=( "${program} failed" ) + echo "Running $* ..." + "$@" || errors+=( "$* failed" ) echo } From 650881b08ba23335fc95722503a5dad2a2eb373c Mon Sep 17 00:00:00 2001 From: Michael Hucka Date: Sat, 22 Nov 2025 15:28:04 -0800 Subject: [PATCH 10/11] Apply suggestion from @pavoljuhas Co-authored-by: Pavol Juhas --- check/all | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/check/all b/check/all index 3ec862232..719f33c35 100755 --- a/check/all +++ b/check/all @@ -91,8 +91,8 @@ function run() { } if [[ -n "${only_changed}" ]]; then - run "check/format-incremental ${rev} ${apply_arg}" - run "check/pylint-changed-files ${rev}" + run check/format-incremental "${rev[@]}" "${apply_arg[@]}" + run check/pylint-changed-files "${rev[@]}" else run "check/format-incremental ${rev} ${apply_arg} --all" run "check/pylint" From 5433e1f9b9ec5707a3cbf095de881ec6df682478 Mon Sep 17 00:00:00 2001 From: mhucka Date: Sat, 22 Nov 2025 23:34:24 +0000 Subject: [PATCH 11/11] Address flaws pointed out in code review --- check/all | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/check/all b/check/all index 719f33c35..76a03c436 100755 --- a/check/all +++ b/check/all @@ -14,8 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Summary: run multiple checks on the OpenFermion code base. -# This is modeled in part on Cirq's check/all script. +# This script is modeled in part on Cirq's check/all script. declare -r usage="\ Usage: ${0##*/} [--help] [--only-changed-files] [--apply-format-changes] [BASE_REV] @@ -49,9 +48,9 @@ function error() { # ~~~~ Parse the CLI arguments and gather some data ~~~~ -declare rev -declare apply_arg -declare only_changed +declare -a rev=() +declare -a apply_arg=() +declare only_changed="" for arg in "$@"; do case "${arg}" in -h | --help) @@ -59,7 +58,7 @@ for arg in "$@"; do exit 0 ;; --apply-format-changes) - apply_arg="--apply" + apply_arg=( "--apply" ) shift ;; --only-changed-files) @@ -94,12 +93,12 @@ if [[ -n "${only_changed}" ]]; then run check/format-incremental "${rev[@]}" "${apply_arg[@]}" run check/pylint-changed-files "${rev[@]}" else - run "check/format-incremental ${rev} ${apply_arg} --all" - run "check/pylint" + run check/format-incremental "${rev[@]}" "${apply_arg[@]}" --all + run check/pylint "${rev[@]}" fi -run "check/mypy" -run "check/pytest-and-incremental-coverage ${rev}" -run "check/shellcheck" +run check/mypy +run check/pytest-and-incremental-coverage "${rev[@]}" +run check/shellcheck # ~~~~ Summarize the results and exit with a status code ~~~~