blob: 1ac44504916809374b170ab3e0651d9cf37d6c7f [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
Lidi Zhenga82f2892020-05-26 17:30:51 -070018import sys
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
Lidi Zhenga82f2892020-05-26 17:30:51 -070023_MINIMAL_ASYNCIO_SUPPORT_PYTHON_VERSION = [3, 6]
24
25
26def _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 Parrott77fb0f22017-10-18 12:52:35 -070035
Danny Hermesfd1d18f2017-11-01 21:47:55 -070036def default(session):
37 """Default unit test session.
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070038
Danny Hermesfd1d18f2017-11-01 21:47:55 -070039 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 Parrott77fb0f22017-10-18 12:52:35 -070044 # Install all test dependencies, then install this package in-place.
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080045 session.install("mock", "pytest", "pytest-cov", "grpcio >= 1.0.2")
46 session.install("-e", ".")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070047
Lidi Zhenga82f2892020-05-26 17:30:51 -070048 pytest_args = [
49 "python",
50 "-m",
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080051 "py.test",
52 "--quiet",
53 "--cov=google.api_core",
54 "--cov=tests.unit",
55 "--cov-append",
56 "--cov-config=.coveragerc",
57 "--cov-report=",
Tres Seaver0c2c5562020-02-25 13:59:11 -050058 "--cov-fail-under=0",
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080059 os.path.join("tests", "unit"),
Lidi Zhenga82f2892020-05-26 17:30:51 -070060 ]
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 Parrott77fb0f22017-10-18 12:52:35 -070073
74
Bu Sun Kime72202e2020-02-19 17:58:47 -080075@nox.session(python=["2.7", "3.5", "3.6", "3.7", "3.8"])
Bu Sun Kim9f45e3c2018-10-10 11:04:44 -070076def unit(session):
Danny Hermesfd1d18f2017-11-01 21:47:55 -070077 """Run the unit test suite."""
Danny Hermesfd1d18f2017-11-01 21:47:55 -070078 default(session)
79
80
Bu Sun Kime72202e2020-02-19 17:58:47 -080081@nox.session(python=["2.7", "3.5", "3.6", "3.7", "3.8"])
Bu Sun Kim9f45e3c2018-10-10 11:04:44 -070082def unit_grpc_gcp(session):
Weiran Fang0a5c85c2018-07-27 11:30:48 -070083 """Run the unit test suite with grpcio-gcp installed."""
84
Weiran Fang0a5c85c2018-07-27 11:30:48 -070085 # Install grpcio-gcp
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080086 session.install("grpcio-gcp")
Weiran Fang0a5c85c2018-07-27 11:30:48 -070087
88 default(session)
89
90
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080091@nox.session(python="3.6")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070092def 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 Wilcox6f4070d2018-11-29 11:02:52 -080098 session.install("flake8", "flake8-import-order")
99 session.install(".")
100 session.run("flake8", "google", "tests")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700101
102
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800103@nox.session(python="3.6")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700104def lint_setup_py(session):
105 """Verify that setup.py is valid (including RST check)."""
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700106
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800107 session.install("docutils", "Pygments")
108 session.run("python", "setup.py", "check", "--restructuredtext", "--strict")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700109
110
Rebecca Chen0fce6372018-09-27 10:45:58 -0700111# 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 Wilcox6f4070d2018-11-29 11:02:52 -0800113@nox.session(python="3.6")
Rebecca Chen0fce6372018-09-27 10:45:58 -0700114def pytype(session):
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800115 """Run type-checking."""
116 session.install(
Christopher Wilcox2576a1b2019-03-27 10:58:39 -0700117 ".", "grpcio >= 1.8.2", "grpcio-gcp >= 0.2.2", "pytype >= 2019.3.21"
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800118 )
119 session.run("pytype")
Rebecca Chen0fce6372018-09-27 10:45:58 -0700120
121
Christopher Wilcox6f4070d2018-11-29 11:02:52 -0800122@nox.session(python="3.6")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -0700123def 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 Wilcox6f4070d2018-11-29 11:02:52 -0800129 session.install("coverage", "pytest-cov")
130 session.run("coverage", "report", "--show-missing", "--fail-under=100")
131 session.run("coverage", "erase")
Bu Sun Kimbee4b072019-06-25 12:44:16 -0700132
133
134@nox.session(python="3.7")
135def 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", ".")
arithmetic1728748c9352020-04-14 15:46:26 -0700140 session.install("sphinx < 3.0", "alabaster", "recommonmark")
Bu Sun Kimbee4b072019-06-25 12:44:16 -0700141
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 Seaver0c2c5562020-02-25 13:59:11 -0500154 )
Tres Seaveraaffc892020-12-02 14:31:03 -0500155
156
157@nox.session(python="3.7")
158def docfx(session):
159 """Build the docfx yaml files for this library."""
160
161 session.install("-e", ".")
162 # sphinx-docfx-yaml supports up to sphinx version 1.5.5.
163 # https://github.com/docascode/sphinx-docfx-yaml/issues/97
164 session.install("sphinx==1.5.5", "alabaster", "recommonmark", "sphinx-docfx-yaml")
165
166 shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True)
167 session.run(
168 "sphinx-build",
169 "-T", # show full traceback on exception
170 "-N", # no colors
171 "-D",
172 (
173 "extensions=sphinx.ext.autodoc,"
174 "sphinx.ext.autosummary,"
175 "docfx_yaml.extension,"
176 "sphinx.ext.intersphinx,"
177 "sphinx.ext.coverage,"
178 "sphinx.ext.napoleon,"
179 "sphinx.ext.todo,"
180 "sphinx.ext.viewcode,"
181 "recommonmark"
182 ),
183 "-b",
184 "html",
185 "-d",
186 os.path.join("docs", "_build", "doctrees", ""),
187 os.path.join("docs", ""),
188 os.path.join("docs", "_build", "html", ""),
189 )