Skip to content

Commit afb2c13

Browse files
authored
Merge branch 'main' into mh-revised-fix-481
2 parents c6f44b6 + 4d66bf3 commit afb2c13

123 files changed

Lines changed: 454 additions & 391 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ htmlcov/
6767
nosetests.xml
6868
coverage.xml
6969
*.cover
70+
*,cover
7071
.hypothesis/
7172

7273
# Sphinx documentation

check/all

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Copyright 2025 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# This script is modeled in part on Cirq's check/all script.
18+
19+
declare -r usage="\
20+
Usage: ${0##*/} [--help] [--only-changed-files] [--apply-format-changes] [BASE_REV]
21+
22+
If the first argument given on the command line is the option --help or -h,
23+
this program prints usage information and then exits.
24+
25+
With no arguments, this runs multiple checks on the code base. These tests
26+
are based on the programs in the checks/ subdirectory.
27+
28+
If --apply-format-changes is specified, the flag --apply will be passed to
29+
check/format-incremental to apply the format changes suggested by the
30+
formatter.
31+
32+
You can specify a base git revision to compare against (i.e., to use when
33+
determining whether or not a file is considered to have changed). If given,
34+
the argument BASE_REV is passed on to tests that can use it, such as
35+
check/pytest-and-incremental-coverage."
36+
37+
set -eo pipefail -o errtrace
38+
shopt -s inherit_errexit
39+
40+
# Get the working directory to the repo root.
41+
thisdir=$(dirname "${BASH_SOURCE[0]:?}")
42+
repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel)
43+
cd "${repo_dir}"
44+
45+
function error() {
46+
echo >&2 "ERROR: ${*}"
47+
}
48+
49+
# ~~~~ Parse the CLI arguments and gather some data ~~~~
50+
51+
declare -a rev=()
52+
declare -a apply_arg=()
53+
declare only_changed=""
54+
for arg in "$@"; do
55+
case "${arg}" in
56+
-h | --help)
57+
echo "${usage}"
58+
exit 0
59+
;;
60+
--apply-format-changes)
61+
apply_arg=( "--apply" )
62+
shift
63+
;;
64+
--only-changed-files)
65+
only_changed="true"
66+
shift
67+
;;
68+
-*)
69+
error "Invalid option '${arg}'"
70+
error "See '$0 --help' for the list of supported options."
71+
exit 1
72+
;;
73+
*)
74+
if ! rev=( "$(git rev-parse --verify --end-of-options "${arg}^{commit}")" ); then
75+
error "No revision '${arg}'"
76+
exit 1
77+
fi
78+
;;
79+
esac
80+
done
81+
82+
# ~~~~ Run the tests ~~~~
83+
84+
declare -a errors=()
85+
86+
function run() {
87+
echo "Running $* ..."
88+
"$@" || errors+=( "$* failed" )
89+
echo
90+
}
91+
92+
if [[ -n "${only_changed}" ]]; then
93+
run check/format-incremental "${rev[@]}" "${apply_arg[@]}"
94+
run check/pylint-changed-files "${rev[@]}"
95+
else
96+
run check/format-incremental "${rev[@]}" "${apply_arg[@]}" --all
97+
run check/pylint "${rev[@]}"
98+
fi
99+
run check/mypy
100+
run check/pytest-and-incremental-coverage "${rev[@]}"
101+
run check/shellcheck
102+
103+
# ~~~~ Summarize the results and exit with a status code ~~~~
104+
105+
declare exit_code=0
106+
echo
107+
echo "Done."
108+
if [[ "${#errors[@]}" == 0 ]]; then
109+
echo "All checks passed."
110+
else
111+
error "Some checks failed."
112+
printf " %s\n" "${errors[@]}"
113+
exit_code=1
114+
fi
115+
116+
exit "${exit_code}"

check/shellcheck

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ IFS=$'\n' read -r -d "" -a our_shell_scripts < <(
5454
declare -a required_shell_scripts
5555
required_shell_scripts=(
5656
# items below must be sorted
57+
check/all
5758
check/format-incremental
5859
check/mypy
5960
check/pylint
61+
check/pylint-changed-files
6062
check/pytest
63+
check/pytest-and-incremental-coverage
6164
)
6265

6366
scripts_not_found=$(comm -13 \

dev_tools/env_tools.py

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ def get_unhidden_ungenerated_python_files(directory: str) -> Iterable[str]:
4040
yield os.path.join(dirpath, filename)
4141

4242

43-
def create_virtual_env(venv_path: str, requirements_paths: Iterable[str],
44-
python_path: str, verbose: bool) -> None:
43+
def create_virtual_env(
44+
venv_path: str, requirements_paths: Iterable[str], python_path: str, verbose: bool
45+
) -> None:
4546
"""Creates a new virtual environment and then installs dependencies.
4647
4748
Args:
@@ -50,30 +51,24 @@ def create_virtual_env(venv_path: str, requirements_paths: Iterable[str],
5051
python_path: The python binary to use.
5152
verbose: When set, more progress output is produced.
5253
"""
53-
shell_tools.run_cmd('virtualenv',
54-
None if verbose else '--quiet',
55-
'-p',
56-
python_path,
57-
venv_path,
58-
out=sys.stderr)
54+
shell_tools.run_cmd(
55+
'virtualenv', None if verbose else '--quiet', '-p', python_path, venv_path, out=sys.stderr
56+
)
5957
pip_path = os.path.join(venv_path, 'bin', 'pip')
6058
for req_path in requirements_paths:
61-
shell_tools.run_cmd(pip_path,
62-
'install',
63-
None if verbose else '--quiet',
64-
'-r',
65-
req_path,
66-
out=sys.stderr)
59+
shell_tools.run_cmd(
60+
pip_path, 'install', None if verbose else '--quiet', '-r', req_path, out=sys.stderr
61+
)
6762

6863

6964
def prepare_temporary_test_environment(
70-
destination_directory: str,
71-
repository: GithubRepository,
72-
pull_request_number: Optional[int],
73-
verbose: bool,
74-
env_name: str = '.test_virtualenv',
75-
python_path: str = sys.executable,
76-
commit_ids_known_callback: Callable[[PreparedEnv], None] = None
65+
destination_directory: str,
66+
repository: GithubRepository,
67+
pull_request_number: Optional[int],
68+
verbose: bool,
69+
env_name: str = '.test_virtualenv',
70+
python_path: str = sys.executable,
71+
commit_ids_known_callback: Callable[[PreparedEnv], None] = None,
7772
) -> PreparedEnv:
7873
"""Prepares a temporary test environment at the (existing empty) directory.
7974
@@ -101,10 +96,12 @@ def prepare_temporary_test_environment(
10196
destination_directory=destination_directory,
10297
repository=repository,
10398
pull_request_number=pull_request_number,
104-
verbose=verbose)
99+
verbose=verbose,
100+
)
105101
else:
106102
env = git_env_tools.fetch_local_files(
107-
destination_directory=destination_directory, verbose=verbose)
103+
destination_directory=destination_directory, verbose=verbose
104+
)
108105

109106
if commit_ids_known_callback is not None:
110107
commit_ids_known_callback(env)
@@ -113,18 +110,19 @@ def prepare_temporary_test_environment(
113110
base_path = cast(str, env.destination_directory)
114111
env_path = os.path.join(base_path, env_name)
115112
req_path = os.path.join(base_path, 'tutorial-requirements.txt')
116-
dev_req_path = os.path.join(base_path, 'dev_tools', 'conf',
117-
'pip-list-dev-tools.txt')
118-
contrib_req_path = os.path.join(base_path, 'cirq', 'contrib',
119-
'contrib-tutorial-requirements.txt')
113+
dev_req_path = os.path.join(base_path, 'dev_tools', 'conf', 'pip-list-dev-tools.txt')
114+
contrib_req_path = os.path.join(
115+
base_path, 'cirq', 'contrib', 'contrib-tutorial-requirements.txt'
116+
)
120117
rev_paths = [req_path, dev_req_path, contrib_req_path]
121-
create_virtual_env(venv_path=env_path,
122-
python_path=python_path,
123-
requirements_paths=rev_paths,
124-
verbose=verbose)
125-
126-
return PreparedEnv(github_repo=env.repository,
127-
actual_commit_id=env.actual_commit_id,
128-
compare_commit_id=env.compare_commit_id,
129-
destination_directory=env.destination_directory,
130-
virtual_env_path=env_path)
118+
create_virtual_env(
119+
venv_path=env_path, python_path=python_path, requirements_paths=rev_paths, verbose=verbose
120+
)
121+
122+
return PreparedEnv(
123+
github_repo=env.repository,
124+
actual_commit_id=env.actual_commit_id,
125+
compare_commit_id=env.compare_commit_id,
126+
destination_directory=env.destination_directory,
127+
virtual_env_path=env_path,
128+
)

dev_tools/github_repository.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
class GithubRepository:
1919
"""Details how to access a repository on github."""
2020

21-
def __init__(self, organization: str, name: str,
22-
access_token: Optional[str]) -> None:
21+
def __init__(self, organization: str, name: str, access_token: Optional[str]) -> None:
2322
"""
2423
Args:
2524
organization: The github organization the repository is under.

0 commit comments

Comments
 (0)