blob: 650cef28a5efefd5fa2a188fde73d54e75baca79 [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 Kimc5fee892021-01-13 14:46:03 -070017import pathlib
Bu Sun Kimbee4b072019-06-25 12:44:16 -070018import shutil
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070019
Rebecca Chen0fce6372018-09-27 10:45:58 -070020# https://github.com/google/importlab/issues/25
21import nox # pytype: disable=import-error
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070022
Bu Sun Kimc5fee892021-01-13 14:46:03 -070023CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
Lidi Zhenga82f2892020-05-26 17:30:51 -070024
Bu Sun Kimc5fee892021-01-13 14:46:03 -070025_MINIMAL_ASYNCIO_SUPPORT_PYTHON_VERSION = [3, 6]
Lidi Zhenga82f2892020-05-26 17:30:51 -070026
27def _greater_or_equal_than_36(version_string):
Tres Seaverfdbed0f2020-12-10 15:19:02 -050028 tokens = version_string.split(".")
Lidi Zhenga82f2892020-05-26 17:30:51 -070029 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 Parrott77fb0f22017-10-18 12:52:35 -070036
Danny Hermesfd1d18f2017-11-01 21:47:55 -070037def default(session):
38 """Default unit test session.
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070039
Danny Hermesfd1d18f2017-11-01 21:47:55 -070040 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 Kimc5fee892021-01-13 14:46:03 -070045 constraints_path = str(
46 CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt"
47 )
48
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070049 # Install all test dependencies, then install this package in-place.
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080050 session.install("mock", "pytest", "pytest-cov", "grpcio >= 1.0.2")
Bu Sun Kimc5fee892021-01-13 14:46:03 -070051 session.install("-e", ".", "-c", constraints_path)
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070052
Lidi Zhenga82f2892020-05-26 17:30:51 -070053 pytest_args = [
54 "python",
55 "-m",
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080056 "py.test",
57 "--quiet",
58 "--cov=google.api_core",
59 "--cov=tests.unit",
60 "--cov-append",
61 "--cov-config=.coveragerc",
62 "--cov-report=",
Tres Seaver0c2c5562020-02-25 13:59:11 -050063 "--cov-fail-under=0",
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080064 os.path.join("tests", "unit"),
Lidi Zhenga82f2892020-05-26 17:30:51 -070065 ]
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 Parrott77fb0f22017-10-18 12:52:35 -070078
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(session):
Danny Hermesfd1d18f2017-11-01 21:47:55 -070082 """Run the unit test suite."""
Danny Hermesfd1d18f2017-11-01 21:47:55 -070083 default(session)
84
85
Tres Seaver9ac37082020-12-14 12:44:10 -050086@nox.session(python=["2.7", "3.6", "3.7", "3.8", "3.9"])
Bu Sun Kim9f45e3c2018-10-10 11:04:44 -070087def unit_grpc_gcp(session):
Weiran Fang0a5c85c2018-07-27 11:30:48 -070088 """Run the unit test suite with grpcio-gcp installed."""
Bu Sun Kimc5fee892021-01-13 14:46:03 -070089 constraints_path = str(
90 CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt"
91 )
Weiran Fang0a5c85c2018-07-27 11:30:48 -070092 # Install grpcio-gcp
Bu Sun Kimc5fee892021-01-13 14:46:03 -070093 session.install("grpcio-gcp", "-c", constraints_path)
Weiran Fang0a5c85c2018-07-27 11:30:48 -070094
95 default(session)
96
97
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080098@nox.session(python="3.6")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070099def 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 Wilcox6f4070d2018-11-29 11:02:52 -0800105 session.install("flake8", "flake8-import-order")
106 session.install(".")
107 session.run("flake8", "google", "tests")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700108
109
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800110@nox.session(python="3.6")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700111def lint_setup_py(session):
112 """Verify that setup.py is valid (including RST check)."""
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700113
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800114 session.install("docutils", "Pygments")
115 session.run("python", "setup.py", "check", "--restructuredtext", "--strict")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700116
117
Rebecca Chen0fce6372018-09-27 10:45:58 -0700118# 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 Wilcox6f4070d2018-11-29 11:02:52 -0800120@nox.session(python="3.6")
Rebecca Chen0fce6372018-09-27 10:45:58 -0700121def pytype(session):
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800122 """Run type-checking."""
123 session.install(
Christopher Wilcox2576a1b2019-03-27 10:58:39 -0700124 ".", "grpcio >= 1.8.2", "grpcio-gcp >= 0.2.2", "pytype >= 2019.3.21"
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800125 )
126 session.run("pytype")
Rebecca Chen0fce6372018-09-27 10:45:58 -0700127
128
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800129@nox.session(python="3.6")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700130def 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 Wilcox6f4070d2018-11-29 11:02:52 -0800136 session.install("coverage", "pytest-cov")
137 session.run("coverage", "report", "--show-missing", "--fail-under=100")
138 session.run("coverage", "erase")
Bu Sun Kimbee4b072019-06-25 12:44:16 -0700139
140
141@nox.session(python="3.7")
142def 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", ".")
arithmetic1728748c9352020-04-14 15:46:26 -0700147 session.install("sphinx < 3.0", "alabaster", "recommonmark")
Bu Sun Kimbee4b072019-06-25 12:44:16 -0700148
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 Seaver0c2c5562020-02-25 13:59:11 -0500161 )
Tres Seaveraaffc892020-12-02 14:31:03 -0500162
163
164@nox.session(python="3.7")
165def 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 )