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