blob: 84470f5adfe0d3f0f54b5551efc94bd1bd100725 [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
Tres Seaverfbf447c2021-06-16 13:45:41 -040023
24BLACK_VERSION = "black==19.10b0"
25BLACK_PATHS = ["docs", "google", "tests", "noxfile.py", "setup.py"]
26# Black and flake8 clash on the syntax for ignoring flake8's F401 in this file.
27BLACK_EXCLUDES = ["--exclude", "^/google/api_core/operations_v1/__init__.py"]
28
29DEFAULT_PYTHON_VERSION = "3.7"
Bu Sun Kimc5fee892021-01-13 14:46:03 -070030CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
Lidi Zhenga82f2892020-05-26 17:30:51 -070031
Bu Sun Kimc5fee892021-01-13 14:46:03 -070032_MINIMAL_ASYNCIO_SUPPORT_PYTHON_VERSION = [3, 6]
Lidi Zhenga82f2892020-05-26 17:30:51 -070033
Tres Seaverfbf447c2021-06-16 13:45:41 -040034
Lidi Zhenga82f2892020-05-26 17:30:51 -070035def _greater_or_equal_than_36(version_string):
Tres Seaverfdbed0f2020-12-10 15:19:02 -050036 tokens = version_string.split(".")
Lidi Zhenga82f2892020-05-26 17:30:51 -070037 for i, token in enumerate(tokens):
38 try:
39 tokens[i] = int(token)
40 except ValueError:
41 pass
42 return tokens >= [3, 6]
43
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070044
Tres Seaverfbf447c2021-06-16 13:45:41 -040045@nox.session(python=DEFAULT_PYTHON_VERSION)
46def lint(session):
47 """Run linters.
48
49 Returns a failure if the linters find linting errors or sufficiently
50 serious code quality issues.
51 """
52 session.install("flake8", "flake8-import-order", BLACK_VERSION)
53 session.install(".")
54 session.run(
55 "black", "--check", *BLACK_EXCLUDES, *BLACK_PATHS,
56 )
57 session.run("flake8", "google", "tests")
58
59
60@nox.session(python=DEFAULT_PYTHON_VERSION)
61def blacken(session):
62 """Run black.
63
64 Format code to uniform standard.
65 """
66 session.install(BLACK_VERSION)
67 session.run("black", *BLACK_EXCLUDES, *BLACK_PATHS)
68
69
Danny Hermesfd1d18f2017-11-01 21:47:55 -070070def default(session):
71 """Default unit test session.
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070072
Danny Hermesfd1d18f2017-11-01 21:47:55 -070073 This is intended to be run **without** an interpreter set, so
74 that the current ``python`` (on the ``PATH``) or the version of
75 Python corresponding to the ``nox`` binary the ``PATH`` can
76 run the tests.
77 """
Bu Sun Kimc5fee892021-01-13 14:46:03 -070078 constraints_path = str(
79 CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt"
80 )
81
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070082 # Install all test dependencies, then install this package in-place.
Tres Seaverffa528e2021-08-12 17:42:46 -040083 session.install("mock", "pytest", "pytest-cov")
84 session.install("-e", ".[grpc]", "-c", constraints_path)
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070085
Lidi Zhenga82f2892020-05-26 17:30:51 -070086 pytest_args = [
87 "python",
88 "-m",
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080089 "py.test",
90 "--quiet",
91 "--cov=google.api_core",
92 "--cov=tests.unit",
93 "--cov-append",
94 "--cov-config=.coveragerc",
95 "--cov-report=",
Tres Seaver0c2c5562020-02-25 13:59:11 -050096 "--cov-fail-under=0",
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080097 os.path.join("tests", "unit"),
Lidi Zhenga82f2892020-05-26 17:30:51 -070098 ]
99 pytest_args.extend(session.posargs)
100
Bu Sun Kimff6ef1b2021-08-03 11:47:13 -0600101 # Inject AsyncIO content and proto-plus, if version >= 3.6.
102 # proto-plus is needed for a field mask test in test_protobuf_helpers.py
Lidi Zhenga82f2892020-05-26 17:30:51 -0700103 if _greater_or_equal_than_36(session.python):
Bu Sun Kimff6ef1b2021-08-03 11:47:13 -0600104 session.install("asyncmock", "pytest-asyncio", "proto-plus")
Lidi Zhenga82f2892020-05-26 17:30:51 -0700105
106 pytest_args.append("--cov=tests.asyncio")
107 pytest_args.append(os.path.join("tests", "asyncio"))
108 session.run(*pytest_args)
109 else:
110 # Run py.test against the unit tests.
111 session.run(*pytest_args)
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700112
113
Tres Seavera30f0042021-08-03 13:59:25 -0400114@nox.session(python=["3.6", "3.7", "3.8", "3.9"])
Bu Sun Kim9f45e3c2018-10-10 11:04:44 -0700115def unit(session):
Danny Hermesfd1d18f2017-11-01 21:47:55 -0700116 """Run the unit test suite."""
Danny Hermesfd1d18f2017-11-01 21:47:55 -0700117 default(session)
118
119
Tres Seavera30f0042021-08-03 13:59:25 -0400120@nox.session(python=["3.6", "3.7", "3.8", "3.9"])
Bu Sun Kim9f45e3c2018-10-10 11:04:44 -0700121def unit_grpc_gcp(session):
Weiran Fang0a5c85c2018-07-27 11:30:48 -0700122 """Run the unit test suite with grpcio-gcp installed."""
Bu Sun Kimc5fee892021-01-13 14:46:03 -0700123 constraints_path = str(
124 CURRENT_DIRECTORY / "testing" / f"constraints-{session.python}.txt"
125 )
Weiran Fang0a5c85c2018-07-27 11:30:48 -0700126 # Install grpcio-gcp
Tres Seaverffa528e2021-08-12 17:42:46 -0400127 session.install("-e", ".[grpcgcp]", "-c", constraints_path)
Weiran Fang0a5c85c2018-07-27 11:30:48 -0700128
129 default(session)
130
131
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800132@nox.session(python="3.6")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700133def lint_setup_py(session):
134 """Verify that setup.py is valid (including RST check)."""
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700135
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800136 session.install("docutils", "Pygments")
137 session.run("python", "setup.py", "check", "--restructuredtext", "--strict")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700138
139
Rebecca Chen0fce6372018-09-27 10:45:58 -0700140# No 3.7 because pytype supports up to 3.6 only.
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800141@nox.session(python="3.6")
Rebecca Chen0fce6372018-09-27 10:45:58 -0700142def pytype(session):
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800143 """Run type-checking."""
Tres Seaverffa528e2021-08-12 17:42:46 -0400144 session.install(".[grpc, grpcgcp]", "pytype >= 2019.3.21")
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800145 session.run("pytype")
Rebecca Chen0fce6372018-09-27 10:45:58 -0700146
147
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800148@nox.session(python="3.6")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700149def cover(session):
150 """Run the final coverage report.
151
152 This outputs the coverage report aggregating coverage from the unit
153 test runs (not system test runs), and then erases coverage data.
154 """
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800155 session.install("coverage", "pytest-cov")
156 session.run("coverage", "report", "--show-missing", "--fail-under=100")
157 session.run("coverage", "erase")
Bu Sun Kimbee4b072019-06-25 12:44:16 -0700158
159
Dan Lee877fabc2021-07-07 11:40:46 -0400160@nox.session(python="3.8")
Bu Sun Kimbee4b072019-06-25 12:44:16 -0700161def docs(session):
162 """Build the docs for this library."""
163
Tres Seaverffa528e2021-08-12 17:42:46 -0400164 session.install("-e", ".[grpc, grpcgcp]")
Dan Lee877fabc2021-07-07 11:40:46 -0400165 session.install("sphinx==4.0.1", "alabaster", "recommonmark")
Bu Sun Kimbee4b072019-06-25 12:44:16 -0700166
167 shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True)
168 session.run(
169 "sphinx-build",
170 "-W", # warnings as errors
171 "-T", # show full traceback on exception
172 "-N", # no colors
173 "-b",
174 "html",
175 "-d",
176 os.path.join("docs", "_build", "doctrees", ""),
177 os.path.join("docs", ""),
178 os.path.join("docs", "_build", "html", ""),
Tres Seaver0c2c5562020-02-25 13:59:11 -0500179 )
Tres Seaveraaffc892020-12-02 14:31:03 -0500180
181
Dan Lee877fabc2021-07-07 11:40:46 -0400182@nox.session(python="3.8")
Tres Seaveraaffc892020-12-02 14:31:03 -0500183def docfx(session):
184 """Build the docfx yaml files for this library."""
185
186 session.install("-e", ".")
Dan Lee877fabc2021-07-07 11:40:46 -0400187 session.install(
188 "sphinx==4.0.1", "alabaster", "recommonmark", "gcp-sphinx-docfx-yaml"
189 )
Tres Seaveraaffc892020-12-02 14:31:03 -0500190
191 shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True)
192 session.run(
193 "sphinx-build",
194 "-T", # show full traceback on exception
195 "-N", # no colors
196 "-D",
197 (
198 "extensions=sphinx.ext.autodoc,"
199 "sphinx.ext.autosummary,"
200 "docfx_yaml.extension,"
201 "sphinx.ext.intersphinx,"
202 "sphinx.ext.coverage,"
203 "sphinx.ext.napoleon,"
204 "sphinx.ext.todo,"
205 "sphinx.ext.viewcode,"
206 "recommonmark"
207 ),
208 "-b",
209 "html",
210 "-d",
211 os.path.join("docs", "_build", "doctrees", ""),
212 os.path.join("docs", ""),
213 os.path.join("docs", "_build", "html", ""),
214 )