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