blob: 6a8ccdae22c9a05fc6fbeffc56ca4a9d0eacae11 [file] [log] [blame]
release-please[bot]fd2965c2021-08-11 17:00:21 +00001# Copyright 2019 Google LLC
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
15from __future__ import print_function
16
17import os
18from pathlib import Path
19import sys
20from typing import Callable, Dict, List, Optional
21
22import nox
23
24
25# WARNING - WARNING - WARNING - WARNING - WARNING
26# WARNING - WARNING - WARNING - WARNING - WARNING
27# DO NOT EDIT THIS FILE EVER!
28# WARNING - WARNING - WARNING - WARNING - WARNING
29# WARNING - WARNING - WARNING - WARNING - WARNING
30
31BLACK_VERSION = "black==19.10b0"
32
33# Copy `noxfile_config.py` to your directory and modify it instead.
34
35# `TEST_CONFIG` dict is a configuration hook that allows users to
36# modify the test configurations. The values here should be in sync
37# with `noxfile_config.py`. Users will copy `noxfile_config.py` into
38# their directory and modify it.
39
40TEST_CONFIG = {
41 # You can opt out from the test for specific Python versions.
42 'ignored_versions': ["2.7"],
43
44 # Old samples are opted out of enforcing Python type hints
45 # All new samples should feature them
46 'enforce_type_hints': False,
47
48 # An envvar key for determining the project id to use. Change it
49 # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
50 # build specific Cloud project. You can also use your own string
51 # to use your own Cloud project.
52 'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
53 # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
54 # If you need to use a specific version of pip,
55 # change pip_version_override to the string representation
56 # of the version number, for example, "20.2.4"
57 "pip_version_override": None,
58 # A dictionary you want to inject into your test. Don't put any
59 # secrets here. These values will override predefined values.
60 'envs': {},
61}
62
63
64try:
65 # Ensure we can import noxfile_config in the project's directory.
66 sys.path.append('.')
67 from noxfile_config import TEST_CONFIG_OVERRIDE
68except ImportError as e:
69 print("No user noxfile_config found: detail: {}".format(e))
70 TEST_CONFIG_OVERRIDE = {}
71
72# Update the TEST_CONFIG with the user supplied values.
73TEST_CONFIG.update(TEST_CONFIG_OVERRIDE)
74
75
76def get_pytest_env_vars() -> Dict[str, str]:
77 """Returns a dict for pytest invocation."""
78 ret = {}
79
80 # Override the GCLOUD_PROJECT and the alias.
81 env_key = TEST_CONFIG['gcloud_project_env']
82 # This should error out if not set.
83 ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
84
85 # Apply user supplied envs.
86 ret.update(TEST_CONFIG['envs'])
87 return ret
88
89
90# DO NOT EDIT - automatically generated.
91# All versions used to tested samples.
92ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8", "3.9"]
93
94# Any default versions that should be ignored.
95IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
96
97TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
98
99INSTALL_LIBRARY_FROM_SOURCE = bool(os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False))
100#
101# Style Checks
102#
103
104
105def _determine_local_import_names(start_dir: str) -> List[str]:
106 """Determines all import names that should be considered "local".
107
108 This is used when running the linter to insure that import order is
109 properly checked.
110 """
111 file_ext_pairs = [os.path.splitext(path) for path in os.listdir(start_dir)]
112 return [
113 basename
114 for basename, extension in file_ext_pairs
115 if extension == ".py"
116 or os.path.isdir(os.path.join(start_dir, basename))
117 and basename not in ("__pycache__")
118 ]
119
120
121# Linting with flake8.
122#
123# We ignore the following rules:
124# E203: whitespace before ‘:’
125# E266: too many leading ‘#’ for block comment
126# E501: line too long
127# I202: Additional newline in a section of imports
128#
129# We also need to specify the rules which are ignored by default:
130# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121']
131FLAKE8_COMMON_ARGS = [
132 "--show-source",
133 "--builtin=gettext",
134 "--max-complexity=20",
135 "--import-order-style=google",
136 "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py",
137 "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202",
138 "--max-line-length=88",
139]
140
141
142@nox.session
143def lint(session: nox.sessions.Session) -> None:
144 if not TEST_CONFIG['enforce_type_hints']:
145 session.install("flake8", "flake8-import-order")
146 else:
147 session.install("flake8", "flake8-import-order", "flake8-annotations")
148
149 local_names = _determine_local_import_names(".")
150 args = FLAKE8_COMMON_ARGS + [
151 "--application-import-names",
152 ",".join(local_names),
153 "."
154 ]
155 session.run("flake8", *args)
156#
157# Black
158#
159
160
161@nox.session
162def blacken(session: nox.sessions.Session) -> None:
163 session.install(BLACK_VERSION)
164 python_files = [path for path in os.listdir(".") if path.endswith(".py")]
165
166 session.run("black", *python_files)
167
168#
169# Sample Tests
170#
171
172
173PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]
174
175
176def _session_tests(session: nox.sessions.Session, post_install: Callable = None) -> None:
177 if TEST_CONFIG["pip_version_override"]:
178 pip_version = TEST_CONFIG["pip_version_override"]
179 session.install(f"pip=={pip_version}")
180 """Runs py.test for a particular project."""
181 if os.path.exists("requirements.txt"):
182 if os.path.exists("constraints.txt"):
183 session.install("-r", "requirements.txt", "-c", "constraints.txt")
184 else:
185 session.install("-r", "requirements.txt")
186
187 if os.path.exists("requirements-test.txt"):
188 if os.path.exists("constraints-test.txt"):
189 session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt")
190 else:
191 session.install("-r", "requirements-test.txt")
192
193 if INSTALL_LIBRARY_FROM_SOURCE:
194 session.install("-e", _get_repo_root())
195
196 if post_install:
197 post_install(session)
198
199 session.run(
200 "pytest",
201 *(PYTEST_COMMON_ARGS + session.posargs),
202 # Pytest will return 5 when no tests are collected. This can happen
203 # on travis where slow and flaky tests are excluded.
204 # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
205 success_codes=[0, 5],
206 env=get_pytest_env_vars()
207 )
208
209
210@nox.session(python=ALL_VERSIONS)
211def py(session: nox.sessions.Session) -> None:
212 """Runs py.test for a sample using the specified version of Python."""
213 if session.python in TESTED_VERSIONS:
214 _session_tests(session)
215 else:
216 session.skip("SKIPPED: {} tests are disabled for this sample.".format(
217 session.python
218 ))
219
220
221#
222# Readmegen
223#
224
225
226def _get_repo_root() -> Optional[str]:
227 """ Returns the root folder of the project. """
228 # Get root of this repository. Assume we don't have directories nested deeper than 10 items.
229 p = Path(os.getcwd())
230 for i in range(10):
231 if p is None:
232 break
233 if Path(p / ".git").exists():
234 return str(p)
235 # .git is not available in repos cloned via Cloud Build
236 # setup.py is always in the library's root, so use that instead
237 # https://github.com/googleapis/synthtool/issues/792
238 if Path(p / "setup.py").exists():
239 return str(p)
240 p = p.parent
241 raise Exception("Unable to detect repository root.")
242
243
244GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")])
245
246
247@nox.session
248@nox.parametrize("path", GENERATED_READMES)
249def readmegen(session: nox.sessions.Session, path: str) -> None:
250 """(Re-)generates the readme for a sample."""
251 session.install("jinja2", "pyyaml")
252 dir_ = os.path.dirname(path)
253
254 if os.path.exists(os.path.join(dir_, "requirements.txt")):
255 session.install("-r", os.path.join(dir_, "requirements.txt"))
256
257 in_file = os.path.join(dir_, "README.rst.in")
258 session.run(
259 "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file
260 )