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