blob: 86758e1f1630694d856603cda24cf8bd2329d348 [file] [log] [blame]
Luke Sneeringeracb6e3e2017-10-31 08:57:09 -07001# Copyright 2016 Google LLC
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -07002#
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
15from __future__ import absolute_import
16import os
Bu Sun Kimbee4b072019-06-25 12:44:16 -070017import shutil
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070018
Rebecca Chen0fce6372018-09-27 10:45:58 -070019# https://github.com/google/importlab/issues/25
20import nox # pytype: disable=import-error
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070021
Lidi Zhenga82f2892020-05-26 17:30:51 -070022_MINIMAL_ASYNCIO_SUPPORT_PYTHON_VERSION = [3, 6]
23
24
25def _greater_or_equal_than_36(version_string):
Tres Seaverfdbed0f2020-12-10 15:19:02 -050026 tokens = version_string.split(".")
Lidi Zhenga82f2892020-05-26 17:30:51 -070027 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 Parrott77fb0f22017-10-18 12:52:35 -070034
Danny Hermesfd1d18f2017-11-01 21:47:55 -070035def default(session):
36 """Default unit test session.
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070037
Danny Hermesfd1d18f2017-11-01 21:47:55 -070038 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 Parrott77fb0f22017-10-18 12:52:35 -070043 # Install all test dependencies, then install this package in-place.
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080044 session.install("mock", "pytest", "pytest-cov", "grpcio >= 1.0.2")
45 session.install("-e", ".")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070046
Lidi Zhenga82f2892020-05-26 17:30:51 -070047 pytest_args = [
48 "python",
49 "-m",
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080050 "py.test",
51 "--quiet",
52 "--cov=google.api_core",
53 "--cov=tests.unit",
54 "--cov-append",
55 "--cov-config=.coveragerc",
56 "--cov-report=",
Tres Seaver0c2c5562020-02-25 13:59:11 -050057 "--cov-fail-under=0",
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080058 os.path.join("tests", "unit"),
Lidi Zhenga82f2892020-05-26 17:30:51 -070059 ]
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 Parrott77fb0f22017-10-18 12:52:35 -070072
73
Tres Seaver9ac37082020-12-14 12:44:10 -050074@nox.session(python=["2.7", "3.6", "3.7", "3.8", "3.9"])
Bu Sun Kim9f45e3c2018-10-10 11:04:44 -070075def unit(session):
Danny Hermesfd1d18f2017-11-01 21:47:55 -070076 """Run the unit test suite."""
Danny Hermesfd1d18f2017-11-01 21:47:55 -070077 default(session)
78
79
Tres Seaver9ac37082020-12-14 12:44:10 -050080@nox.session(python=["2.7", "3.6", "3.7", "3.8", "3.9"])
Bu Sun Kim9f45e3c2018-10-10 11:04:44 -070081def unit_grpc_gcp(session):
Weiran Fang0a5c85c2018-07-27 11:30:48 -070082 """Run the unit test suite with grpcio-gcp installed."""
83
Weiran Fang0a5c85c2018-07-27 11:30:48 -070084 # Install grpcio-gcp
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080085 session.install("grpcio-gcp")
Weiran Fang0a5c85c2018-07-27 11:30:48 -070086
87 default(session)
88
89
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080090@nox.session(python="3.6")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070091def 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 Wilcox6f4070d2018-11-29 11:02:52 -080097 session.install("flake8", "flake8-import-order")
98 session.install(".")
99 session.run("flake8", "google", "tests")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700100
101
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800102@nox.session(python="3.6")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700103def lint_setup_py(session):
104 """Verify that setup.py is valid (including RST check)."""
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700105
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800106 session.install("docutils", "Pygments")
107 session.run("python", "setup.py", "check", "--restructuredtext", "--strict")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700108
109
Rebecca Chen0fce6372018-09-27 10:45:58 -0700110# 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 Wilcox6f4070d2018-11-29 11:02:52 -0800112@nox.session(python="3.6")
Rebecca Chen0fce6372018-09-27 10:45:58 -0700113def pytype(session):
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800114 """Run type-checking."""
115 session.install(
Christopher Wilcox2576a1b2019-03-27 10:58:39 -0700116 ".", "grpcio >= 1.8.2", "grpcio-gcp >= 0.2.2", "pytype >= 2019.3.21"
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800117 )
118 session.run("pytype")
Rebecca Chen0fce6372018-09-27 10:45:58 -0700119
120
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800121@nox.session(python="3.6")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700122def 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 Wilcox6f4070d2018-11-29 11:02:52 -0800128 session.install("coverage", "pytest-cov")
129 session.run("coverage", "report", "--show-missing", "--fail-under=100")
130 session.run("coverage", "erase")
Bu Sun Kimbee4b072019-06-25 12:44:16 -0700131
132
133@nox.session(python="3.7")
134def 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", ".")
arithmetic1728748c9352020-04-14 15:46:26 -0700139 session.install("sphinx < 3.0", "alabaster", "recommonmark")
Bu Sun Kimbee4b072019-06-25 12:44:16 -0700140
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 Seaver0c2c5562020-02-25 13:59:11 -0500153 )
Tres Seaveraaffc892020-12-02 14:31:03 -0500154
155
156@nox.session(python="3.7")
157def 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 )