Luke Sneeringer | acb6e3e | 2017-10-31 08:57:09 -0700 | [diff] [blame] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from __future__ import absolute_import |
| 16 | import os |
Bu Sun Kim | c5fee89 | 2021-01-13 14:46:03 -0700 | [diff] [blame] | 17 | import pathlib |
Bu Sun Kim | bee4b07 | 2019-06-25 12:44:16 -0700 | [diff] [blame] | 18 | import shutil |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 19 | |
Rebecca Chen | 0fce637 | 2018-09-27 10:45:58 -0700 | [diff] [blame] | 20 | # https://github.com/google/importlab/issues/25 |
| 21 | import nox # pytype: disable=import-error |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 22 | |
Tres Seaver | fbf447c | 2021-06-16 13:45:41 -0400 | [diff] [blame] | 23 | |
| 24 | BLACK_VERSION = "black==19.10b0" |
| 25 | BLACK_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"] |
| 26 | # Black and flake8 clash on the syntax for ignoring flake8's F401 in this file. |
| 27 | BLACK_EXCLUDES = ["--exclude", "^/google/api_core/operations_v1/__init__.py"] |
| 28 | |
| 29 | DEFAULT_PYTHON_VERSION = "3.7" |
Bu Sun Kim | c5fee89 | 2021-01-13 14:46:03 -0700 | [diff] [blame] | 30 | CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute() |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 31 | |
Tres Seaver | 1db493c | 2021-08-30 13:45:35 -0400 | [diff] [blame] | 32 | # 'docfx' is excluded since it only needs to run in 'docs-presubmit' |
| 33 | nox.options.sessions = [ |
| 34 | "unit", |
| 35 | "unit_grpc_gcp", |
Tres Seaver | 09cf285 | 2021-10-19 12:58:59 -0400 | [diff] [blame] | 36 | "unit_wo_grpc", |
Tres Seaver | 1db493c | 2021-08-30 13:45:35 -0400 | [diff] [blame] | 37 | "cover", |
| 38 | "pytype", |
Tres Seaver | 0023ee1 | 2021-10-13 17:54:33 -0400 | [diff] [blame] | 39 | "mypy", |
Tres Seaver | 1db493c | 2021-08-30 13:45:35 -0400 | [diff] [blame] | 40 | "lint", |
| 41 | "lint_setup_py", |
| 42 | "blacken", |
| 43 | "docs", |
| 44 | ] |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 45 | |
Tres Seaver | fbf447c | 2021-06-16 13:45:41 -0400 | [diff] [blame] | 46 | |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 47 | def _greater_or_equal_than_36(version_string): |
Tres Seaver | fdbed0f | 2020-12-10 15:19:02 -0500 | [diff] [blame] | 48 | tokens = version_string.split(".") |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 49 | for i, token in enumerate(tokens): |
| 50 | try: |
| 51 | tokens[i] = int(token) |
| 52 | except ValueError: |
| 53 | pass |
| 54 | return tokens >= [3, 6] |
| 55 | |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 56 | |
Tres Seaver | fbf447c | 2021-06-16 13:45:41 -0400 | [diff] [blame] | 57 | @nox.session(python=DEFAULT_PYTHON_VERSION) |
| 58 | def lint(session): |
| 59 | """Run linters. |
| 60 | |
| 61 | Returns a failure if the linters find linting errors or sufficiently |
| 62 | serious code quality issues. |
| 63 | """ |
| 64 | session.install("flake8", "flake8-import-order", BLACK_VERSION) |
| 65 | session.install(".") |
| 66 | session.run( |
| 67 | "black", "--check", *BLACK_EXCLUDES, *BLACK_PATHS, |
| 68 | ) |
| 69 | session.run("flake8", "google", "tests") |
| 70 | |
| 71 | |
| 72 | @nox.session(python=DEFAULT_PYTHON_VERSION) |
| 73 | def blacken(session): |
| 74 | """Run black. |
| 75 | |
| 76 | Format code to uniform standard. |
| 77 | """ |
| 78 | session.install(BLACK_VERSION) |
| 79 | session.run("black", *BLACK_EXCLUDES, *BLACK_PATHS) |
| 80 | |
| 81 | |
Tres Seaver | 09cf285 | 2021-10-19 12:58:59 -0400 | [diff] [blame] | 82 | def default(session, install_grpc=True): |
Danny Hermes | fd1d18f | 2017-11-01 21:47:55 -0700 | [diff] [blame] | 83 | """Default unit test session. |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 84 | |
Danny Hermes | fd1d18f | 2017-11-01 21:47:55 -0700 | [diff] [blame] | 85 | This is intended to be run **without** an interpreter set, so |
| 86 | that the current ``python`` (on the ``PATH``) or the version of |
| 87 | Python corresponding to the ``nox`` binary the ``PATH`` can |
| 88 | run the tests. |
| 89 | """ |
Bu Sun Kim | c5fee89 | 2021-01-13 14:46:03 -0700 | [diff] [blame] | 90 | constraints_path = str( |
| 91 | CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt" |
| 92 | ) |
| 93 | |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 94 | # Install all test dependencies, then install this package in-place. |
Tres Seaver | ffa528e | 2021-08-12 17:42:46 -0400 | [diff] [blame] | 95 | session.install("mock", "pytest", "pytest-cov") |
Tres Seaver | 09cf285 | 2021-10-19 12:58:59 -0400 | [diff] [blame] | 96 | if install_grpc: |
| 97 | session.install("-e", ".[grpc]", "-c", constraints_path) |
| 98 | else: |
| 99 | session.install("-e", ".", "-c", constraints_path) |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 100 | |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 101 | pytest_args = [ |
| 102 | "python", |
| 103 | "-m", |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 104 | "py.test", |
| 105 | "--quiet", |
| 106 | "--cov=google.api_core", |
| 107 | "--cov=tests.unit", |
| 108 | "--cov-append", |
| 109 | "--cov-config=.coveragerc", |
| 110 | "--cov-report=", |
Tres Seaver | 0c2c556 | 2020-02-25 13:59:11 -0500 | [diff] [blame] | 111 | "--cov-fail-under=0", |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 112 | os.path.join("tests", "unit"), |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 113 | ] |
| 114 | pytest_args.extend(session.posargs) |
| 115 | |
Bu Sun Kim | ff6ef1b | 2021-08-03 11:47:13 -0600 | [diff] [blame] | 116 | # Inject AsyncIO content and proto-plus, if version >= 3.6. |
| 117 | # proto-plus is needed for a field mask test in test_protobuf_helpers.py |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 118 | if _greater_or_equal_than_36(session.python): |
Bu Sun Kim | ff6ef1b | 2021-08-03 11:47:13 -0600 | [diff] [blame] | 119 | session.install("asyncmock", "pytest-asyncio", "proto-plus") |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 120 | |
| 121 | pytest_args.append("--cov=tests.asyncio") |
| 122 | pytest_args.append(os.path.join("tests", "asyncio")) |
| 123 | session.run(*pytest_args) |
| 124 | else: |
| 125 | # Run py.test against the unit tests. |
| 126 | session.run(*pytest_args) |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 127 | |
| 128 | |
Tres Seaver | a422a5d | 2021-10-05 16:54:45 -0400 | [diff] [blame] | 129 | @nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10"]) |
Bu Sun Kim | 9f45e3c | 2018-10-10 11:04:44 -0700 | [diff] [blame] | 130 | def unit(session): |
Danny Hermes | fd1d18f | 2017-11-01 21:47:55 -0700 | [diff] [blame] | 131 | """Run the unit test suite.""" |
Danny Hermes | fd1d18f | 2017-11-01 21:47:55 -0700 | [diff] [blame] | 132 | default(session) |
| 133 | |
| 134 | |
Tres Seaver | a30f004 | 2021-08-03 13:59:25 -0400 | [diff] [blame] | 135 | @nox.session(python=["3.6", "3.7", "3.8", "3.9"]) |
Bu Sun Kim | 9f45e3c | 2018-10-10 11:04:44 -0700 | [diff] [blame] | 136 | def unit_grpc_gcp(session): |
Weiran Fang | 0a5c85c | 2018-07-27 11:30:48 -0700 | [diff] [blame] | 137 | """Run the unit test suite with grpcio-gcp installed.""" |
Bu Sun Kim | c5fee89 | 2021-01-13 14:46:03 -0700 | [diff] [blame] | 138 | constraints_path = str( |
| 139 | CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt" |
| 140 | ) |
Weiran Fang | 0a5c85c | 2018-07-27 11:30:48 -0700 | [diff] [blame] | 141 | # Install grpcio-gcp |
Tres Seaver | ffa528e | 2021-08-12 17:42:46 -0400 | [diff] [blame] | 142 | session.install("-e", ".[grpcgcp]", "-c", constraints_path) |
Weiran Fang | 0a5c85c | 2018-07-27 11:30:48 -0700 | [diff] [blame] | 143 | |
| 144 | default(session) |
| 145 | |
| 146 | |
Tres Seaver | 09cf285 | 2021-10-19 12:58:59 -0400 | [diff] [blame] | 147 | @nox.session(python=["3.6", "3.10"]) |
| 148 | def unit_wo_grpc(session): |
| 149 | """Run the unit test suite w/o grpcio installed""" |
| 150 | default(session, install_grpc=False) |
| 151 | |
| 152 | |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 153 | @nox.session(python="3.6") |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 154 | def lint_setup_py(session): |
| 155 | """Verify that setup.py is valid (including RST check).""" |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 156 | |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 157 | session.install("docutils", "Pygments") |
| 158 | session.run("python", "setup.py", "check", "--restructuredtext", "--strict") |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 159 | |
| 160 | |
Rebecca Chen | 0fce637 | 2018-09-27 10:45:58 -0700 | [diff] [blame] | 161 | # No 3.7 because pytype supports up to 3.6 only. |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 162 | @nox.session(python="3.6") |
Rebecca Chen | 0fce637 | 2018-09-27 10:45:58 -0700 | [diff] [blame] | 163 | def pytype(session): |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 164 | """Run type-checking.""" |
Tres Seaver | ffa528e | 2021-08-12 17:42:46 -0400 | [diff] [blame] | 165 | session.install(".[grpc, grpcgcp]", "pytype >= 2019.3.21") |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 166 | session.run("pytype") |
Rebecca Chen | 0fce637 | 2018-09-27 10:45:58 -0700 | [diff] [blame] | 167 | |
| 168 | |
Tres Seaver | 0023ee1 | 2021-10-13 17:54:33 -0400 | [diff] [blame] | 169 | @nox.session(python=DEFAULT_PYTHON_VERSION) |
| 170 | def mypy(session): |
| 171 | """Run type-checking.""" |
| 172 | session.install(".[grpc, grpcgcp]", "mypy") |
Tres Seaver | bc0abe4 | 2021-11-18 14:58:20 -0500 | [diff] [blame] | 173 | session.install( |
| 174 | "types-setuptools", "types-requests", "types-protobuf", "types-mock" |
| 175 | ) |
Tres Seaver | 0023ee1 | 2021-10-13 17:54:33 -0400 | [diff] [blame] | 176 | session.run("mypy", "google", "tests") |
| 177 | |
| 178 | |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 179 | @nox.session(python="3.6") |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 180 | def cover(session): |
| 181 | """Run the final coverage report. |
| 182 | |
| 183 | This outputs the coverage report aggregating coverage from the unit |
| 184 | test runs (not system test runs), and then erases coverage data. |
| 185 | """ |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 186 | session.install("coverage", "pytest-cov") |
| 187 | session.run("coverage", "report", "--show-missing", "--fail-under=100") |
| 188 | session.run("coverage", "erase") |
Bu Sun Kim | bee4b07 | 2019-06-25 12:44:16 -0700 | [diff] [blame] | 189 | |
| 190 | |
Dan Lee | 877fabc | 2021-07-07 11:40:46 -0400 | [diff] [blame] | 191 | @nox.session(python="3.8") |
Bu Sun Kim | bee4b07 | 2019-06-25 12:44:16 -0700 | [diff] [blame] | 192 | def docs(session): |
| 193 | """Build the docs for this library.""" |
| 194 | |
Tres Seaver | ffa528e | 2021-08-12 17:42:46 -0400 | [diff] [blame] | 195 | session.install("-e", ".[grpc, grpcgcp]") |
Dan Lee | 877fabc | 2021-07-07 11:40:46 -0400 | [diff] [blame] | 196 | session.install("sphinx==4.0.1", "alabaster", "recommonmark") |
Bu Sun Kim | bee4b07 | 2019-06-25 12:44:16 -0700 | [diff] [blame] | 197 | |
| 198 | shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True) |
| 199 | session.run( |
| 200 | "sphinx-build", |
| 201 | "-W", # warnings as errors |
| 202 | "-T", # show full traceback on exception |
| 203 | "-N", # no colors |
| 204 | "-b", |
| 205 | "html", |
| 206 | "-d", |
| 207 | os.path.join("docs", "_build", "doctrees", ""), |
| 208 | os.path.join("docs", ""), |
| 209 | os.path.join("docs", "_build", "html", ""), |
Tres Seaver | 0c2c556 | 2020-02-25 13:59:11 -0500 | [diff] [blame] | 210 | ) |
Tres Seaver | aaffc89 | 2020-12-02 14:31:03 -0500 | [diff] [blame] | 211 | |
| 212 | |
Dan Lee | 877fabc | 2021-07-07 11:40:46 -0400 | [diff] [blame] | 213 | @nox.session(python="3.8") |
Tres Seaver | aaffc89 | 2020-12-02 14:31:03 -0500 | [diff] [blame] | 214 | def docfx(session): |
| 215 | """Build the docfx yaml files for this library.""" |
| 216 | |
| 217 | session.install("-e", ".") |
Dan Lee | 877fabc | 2021-07-07 11:40:46 -0400 | [diff] [blame] | 218 | session.install( |
| 219 | "sphinx==4.0.1", "alabaster", "recommonmark", "gcp-sphinx-docfx-yaml" |
| 220 | ) |
Tres Seaver | aaffc89 | 2020-12-02 14:31:03 -0500 | [diff] [blame] | 221 | |
| 222 | shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True) |
| 223 | session.run( |
| 224 | "sphinx-build", |
| 225 | "-T", # show full traceback on exception |
| 226 | "-N", # no colors |
| 227 | "-D", |
| 228 | ( |
| 229 | "extensions=sphinx.ext.autodoc," |
| 230 | "sphinx.ext.autosummary," |
| 231 | "docfx_yaml.extension," |
| 232 | "sphinx.ext.intersphinx," |
| 233 | "sphinx.ext.coverage," |
| 234 | "sphinx.ext.napoleon," |
| 235 | "sphinx.ext.todo," |
| 236 | "sphinx.ext.viewcode," |
| 237 | "recommonmark" |
| 238 | ), |
| 239 | "-b", |
| 240 | "html", |
| 241 | "-d", |
| 242 | os.path.join("docs", "_build", "doctrees", ""), |
| 243 | os.path.join("docs", ""), |
| 244 | os.path.join("docs", "_build", "html", ""), |
| 245 | ) |