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 | |
Bu Sun Kim | c5fee89 | 2021-01-13 14:46:03 -0700 | [diff] [blame] | 32 | _MINIMAL_ASYNCIO_SUPPORT_PYTHON_VERSION = [3, 6] |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 33 | |
Tres Seaver | fbf447c | 2021-06-16 13:45:41 -0400 | [diff] [blame^] | 34 | |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 35 | def _greater_or_equal_than_36(version_string): |
Tres Seaver | fdbed0f | 2020-12-10 15:19:02 -0500 | [diff] [blame] | 36 | tokens = version_string.split(".") |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 37 | for i, token in enumerate(tokens): |
| 38 | try: |
| 39 | tokens[i] = int(token) |
| 40 | except ValueError: |
| 41 | pass |
| 42 | return tokens >= [3, 6] |
| 43 | |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 44 | |
Tres Seaver | fbf447c | 2021-06-16 13:45:41 -0400 | [diff] [blame^] | 45 | @nox.session(python=DEFAULT_PYTHON_VERSION) |
| 46 | def lint(session): |
| 47 | """Run linters. |
| 48 | |
| 49 | Returns a failure if the linters find linting errors or sufficiently |
| 50 | serious code quality issues. |
| 51 | """ |
| 52 | session.install("flake8", "flake8-import-order", BLACK_VERSION) |
| 53 | session.install(".") |
| 54 | session.run( |
| 55 | "black", "--check", *BLACK_EXCLUDES, *BLACK_PATHS, |
| 56 | ) |
| 57 | session.run("flake8", "google", "tests") |
| 58 | |
| 59 | |
| 60 | @nox.session(python=DEFAULT_PYTHON_VERSION) |
| 61 | def blacken(session): |
| 62 | """Run black. |
| 63 | |
| 64 | Format code to uniform standard. |
| 65 | """ |
| 66 | session.install(BLACK_VERSION) |
| 67 | session.run("black", *BLACK_EXCLUDES, *BLACK_PATHS) |
| 68 | |
| 69 | |
Danny Hermes | fd1d18f | 2017-11-01 21:47:55 -0700 | [diff] [blame] | 70 | def default(session): |
| 71 | """Default unit test session. |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 72 | |
Danny Hermes | fd1d18f | 2017-11-01 21:47:55 -0700 | [diff] [blame] | 73 | This is intended to be run **without** an interpreter set, so |
| 74 | that the current ``python`` (on the ``PATH``) or the version of |
| 75 | Python corresponding to the ``nox`` binary the ``PATH`` can |
| 76 | run the tests. |
| 77 | """ |
Bu Sun Kim | c5fee89 | 2021-01-13 14:46:03 -0700 | [diff] [blame] | 78 | constraints_path = str( |
| 79 | CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt" |
| 80 | ) |
| 81 | |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 82 | # Install all test dependencies, then install this package in-place. |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 83 | session.install("mock", "pytest", "pytest-cov", "grpcio >= 1.0.2") |
Bu Sun Kim | c5fee89 | 2021-01-13 14:46:03 -0700 | [diff] [blame] | 84 | session.install("-e", ".", "-c", constraints_path) |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 85 | |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 86 | pytest_args = [ |
| 87 | "python", |
| 88 | "-m", |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 89 | "py.test", |
| 90 | "--quiet", |
| 91 | "--cov=google.api_core", |
| 92 | "--cov=tests.unit", |
| 93 | "--cov-append", |
| 94 | "--cov-config=.coveragerc", |
| 95 | "--cov-report=", |
Tres Seaver | 0c2c556 | 2020-02-25 13:59:11 -0500 | [diff] [blame] | 96 | "--cov-fail-under=0", |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 97 | os.path.join("tests", "unit"), |
Lidi Zheng | a82f289 | 2020-05-26 17:30:51 -0700 | [diff] [blame] | 98 | ] |
| 99 | pytest_args.extend(session.posargs) |
| 100 | |
| 101 | # Inject AsyncIO content, if version >= 3.6. |
| 102 | if _greater_or_equal_than_36(session.python): |
| 103 | session.install("asyncmock", "pytest-asyncio") |
| 104 | |
| 105 | pytest_args.append("--cov=tests.asyncio") |
| 106 | pytest_args.append(os.path.join("tests", "asyncio")) |
| 107 | session.run(*pytest_args) |
| 108 | else: |
| 109 | # Run py.test against the unit tests. |
| 110 | session.run(*pytest_args) |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 111 | |
| 112 | |
Tres Seaver | 9ac3708 | 2020-12-14 12:44:10 -0500 | [diff] [blame] | 113 | @nox.session(python=["2.7", "3.6", "3.7", "3.8", "3.9"]) |
Bu Sun Kim | 9f45e3c | 2018-10-10 11:04:44 -0700 | [diff] [blame] | 114 | def unit(session): |
Danny Hermes | fd1d18f | 2017-11-01 21:47:55 -0700 | [diff] [blame] | 115 | """Run the unit test suite.""" |
Danny Hermes | fd1d18f | 2017-11-01 21:47:55 -0700 | [diff] [blame] | 116 | default(session) |
| 117 | |
| 118 | |
Tres Seaver | 9ac3708 | 2020-12-14 12:44:10 -0500 | [diff] [blame] | 119 | @nox.session(python=["2.7", "3.6", "3.7", "3.8", "3.9"]) |
Bu Sun Kim | 9f45e3c | 2018-10-10 11:04:44 -0700 | [diff] [blame] | 120 | def unit_grpc_gcp(session): |
Weiran Fang | 0a5c85c | 2018-07-27 11:30:48 -0700 | [diff] [blame] | 121 | """Run the unit test suite with grpcio-gcp installed.""" |
Bu Sun Kim | c5fee89 | 2021-01-13 14:46:03 -0700 | [diff] [blame] | 122 | constraints_path = str( |
| 123 | CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt" |
| 124 | ) |
Weiran Fang | 0a5c85c | 2018-07-27 11:30:48 -0700 | [diff] [blame] | 125 | # Install grpcio-gcp |
Bu Sun Kim | c5fee89 | 2021-01-13 14:46:03 -0700 | [diff] [blame] | 126 | session.install("grpcio-gcp", "-c", constraints_path) |
Weiran Fang | 0a5c85c | 2018-07-27 11:30:48 -0700 | [diff] [blame] | 127 | |
| 128 | default(session) |
| 129 | |
| 130 | |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 131 | @nox.session(python="3.6") |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 132 | def lint_setup_py(session): |
| 133 | """Verify that setup.py is valid (including RST check).""" |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 134 | |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 135 | session.install("docutils", "Pygments") |
| 136 | session.run("python", "setup.py", "check", "--restructuredtext", "--strict") |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 137 | |
| 138 | |
Rebecca Chen | 0fce637 | 2018-09-27 10:45:58 -0700 | [diff] [blame] | 139 | # No 2.7 due to https://github.com/google/importlab/issues/26. |
| 140 | # No 3.7 because pytype supports up to 3.6 only. |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 141 | @nox.session(python="3.6") |
Rebecca Chen | 0fce637 | 2018-09-27 10:45:58 -0700 | [diff] [blame] | 142 | def pytype(session): |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 143 | """Run type-checking.""" |
| 144 | session.install( |
Christopher Wilcox | 2576a1b | 2019-03-27 10:58:39 -0700 | [diff] [blame] | 145 | ".", "grpcio >= 1.8.2", "grpcio-gcp >= 0.2.2", "pytype >= 2019.3.21" |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 146 | ) |
| 147 | session.run("pytype") |
Rebecca Chen | 0fce637 | 2018-09-27 10:45:58 -0700 | [diff] [blame] | 148 | |
| 149 | |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 150 | @nox.session(python="3.6") |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 151 | def cover(session): |
| 152 | """Run the final coverage report. |
| 153 | |
| 154 | This outputs the coverage report aggregating coverage from the unit |
| 155 | test runs (not system test runs), and then erases coverage data. |
| 156 | """ |
Christopher Wilcox | 6f4070d | 2018-11-29 11:02:52 -0800 | [diff] [blame] | 157 | session.install("coverage", "pytest-cov") |
| 158 | session.run("coverage", "report", "--show-missing", "--fail-under=100") |
| 159 | session.run("coverage", "erase") |
Bu Sun Kim | bee4b07 | 2019-06-25 12:44:16 -0700 | [diff] [blame] | 160 | |
| 161 | |
| 162 | @nox.session(python="3.7") |
| 163 | def docs(session): |
| 164 | """Build the docs for this library.""" |
| 165 | |
| 166 | session.install(".", "grpcio >= 1.8.2", "grpcio-gcp >= 0.2.2") |
| 167 | session.install("-e", ".") |
arithmetic1728 | 748c935 | 2020-04-14 15:46:26 -0700 | [diff] [blame] | 168 | session.install("sphinx < 3.0", "alabaster", "recommonmark") |
Bu Sun Kim | bee4b07 | 2019-06-25 12:44:16 -0700 | [diff] [blame] | 169 | |
| 170 | shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True) |
| 171 | session.run( |
| 172 | "sphinx-build", |
| 173 | "-W", # warnings as errors |
| 174 | "-T", # show full traceback on exception |
| 175 | "-N", # no colors |
| 176 | "-b", |
| 177 | "html", |
| 178 | "-d", |
| 179 | os.path.join("docs", "_build", "doctrees", ""), |
| 180 | os.path.join("docs", ""), |
| 181 | os.path.join("docs", "_build", "html", ""), |
Tres Seaver | 0c2c556 | 2020-02-25 13:59:11 -0500 | [diff] [blame] | 182 | ) |
Tres Seaver | aaffc89 | 2020-12-02 14:31:03 -0500 | [diff] [blame] | 183 | |
| 184 | |
| 185 | @nox.session(python="3.7") |
| 186 | def docfx(session): |
| 187 | """Build the docfx yaml files for this library.""" |
| 188 | |
| 189 | session.install("-e", ".") |
| 190 | # sphinx-docfx-yaml supports up to sphinx version 1.5.5. |
| 191 | # https://github.com/docascode/sphinx-docfx-yaml/issues/97 |
| 192 | session.install("sphinx==1.5.5", "alabaster", "recommonmark", "sphinx-docfx-yaml") |
| 193 | |
| 194 | shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True) |
| 195 | session.run( |
| 196 | "sphinx-build", |
| 197 | "-T", # show full traceback on exception |
| 198 | "-N", # no colors |
| 199 | "-D", |
| 200 | ( |
| 201 | "extensions=sphinx.ext.autodoc," |
| 202 | "sphinx.ext.autosummary," |
| 203 | "docfx_yaml.extension," |
| 204 | "sphinx.ext.intersphinx," |
| 205 | "sphinx.ext.coverage," |
| 206 | "sphinx.ext.napoleon," |
| 207 | "sphinx.ext.todo," |
| 208 | "sphinx.ext.viewcode," |
| 209 | "recommonmark" |
| 210 | ), |
| 211 | "-b", |
| 212 | "html", |
| 213 | "-d", |
| 214 | os.path.join("docs", "_build", "doctrees", ""), |
| 215 | os.path.join("docs", ""), |
| 216 | os.path.join("docs", "_build", "html", ""), |
| 217 | ) |