-
Notifications
You must be signed in to change notification settings - Fork 401
Add check/all #1146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add check/all #1146
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -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 $? | ||||||||||||||||||||
Comment on lines
+45
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit - remove |
||||||||||||||||||||
|
||||||||||||||||||||
function error() { | ||||||||||||||||||||
echo >&2 "ERROR: ${*}" | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
# ~~~~ Parse the CLI arguments and gather some data ~~~~ | ||||||||||||||||||||
|
||||||||||||||||||||
declare rev | ||||||||||||||||||||
declare apply_arg | ||||||||||||||||||||
declare only_changed | ||||||||||||||||||||
Comment on lines
+55
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit - it is safer to set initial value, e.g.,
|
||||||||||||||||||||
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 | ||||||||||||||||||||
Comment on lines
+73
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This silently skips
Suggested change
|
||||||||||||||||||||
;; | ||||||||||||||||||||
*) | ||||||||||||||||||||
if [ "$(git cat-file -t "${arg}" 2> /dev/null)" != "commit" ]; then | ||||||||||||||||||||
error "No revision '${arg}'" | ||||||||||||||||||||
exit 1 | ||||||||||||||||||||
fi | ||||||||||||||||||||
rev="${arg}" | ||||||||||||||||||||
Comment on lines
+80
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will reject annotated tag wrapping a commit, because its type is "tag" (this is an issue in Cirq too). Here is a better way:
Suggested change
|
||||||||||||||||||||
;; | ||||||||||||||||||||
esac | ||||||||||||||||||||
done | ||||||||||||||||||||
|
||||||||||||||||||||
# ~~~~ Run the tests ~~~~ | ||||||||||||||||||||
|
||||||||||||||||||||
declare -a errors | ||||||||||||||||||||
declare program | ||||||||||||||||||||
Comment on lines
+91
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
function run() { | ||||||||||||||||||||
local -r program="$1" | ||||||||||||||||||||
echo "Running $program ..." | ||||||||||||||||||||
$program || errors+=( "${program} failed" ) | ||||||||||||||||||||
echo | ||||||||||||||||||||
Comment on lines
+95
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Passing the entire command line as one argument (e.g., line 102) is fragile, in case any of desired arguments contain space or a glob-like character which would get expanded. It is safer to pass the command line arguments exactly:
Suggested change
|
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if [[ -n "${only_changed}" ]]; then | ||||||||||||||||||||
run "check/format-incremental ${rev} ${apply_arg}" | ||||||||||||||||||||
run "check/pylint-changed-files ${rev}" | ||||||||||||||||||||
Comment on lines
+102
to
+103
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With
Suggested change
|
||||||||||||||||||||
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}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fail to see any code for
--no-coverage
. Either remove this paragraph or handle the option in the script. Since passing coverage is required by the CI, it seems better to just change the text and keep running pytest-and-incremental-coverage unconditionally.