17
17
import os
18
18
from pathlib import Path
19
19
import sys
20
+ from typing import Callable , Dict , List , Optional
20
21
21
22
import nox
22
23
27
28
# WARNING - WARNING - WARNING - WARNING - WARNING
28
29
# WARNING - WARNING - WARNING - WARNING - WARNING
29
30
30
- # Copy `noxfile_config.py` to your directory and modify it instead.
31
+ BLACK_VERSION = "black==19.10b0"
31
32
33
+ # Copy `noxfile_config.py` to your directory and modify it instead.
32
34
33
35
# `TEST_CONFIG` dict is a configuration hook that allows users to
34
36
# modify the test configurations. The values here should be in sync
37
39
38
40
TEST_CONFIG = {
39
41
# You can opt out from the test for specific Python versions.
40
- 'ignored_versions' : ["2.7" ],
42
+ 'ignored_versions' : [],
43
+
44
+ # Old samples are opted out of enforcing Python type hints
45
+ # All new samples should feature them
46
+ 'enforce_type_hints' : False ,
41
47
42
48
# An envvar key for determining the project id to use. Change it
43
49
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
44
50
# build specific Cloud project. You can also use your own string
45
51
# to use your own Cloud project.
46
52
'gcloud_project_env' : 'GOOGLE_CLOUD_PROJECT' ,
47
53
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
48
-
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 ,
49
58
# A dictionary you want to inject into your test. Don't put any
50
59
# secrets here. These values will override predefined values.
51
60
'envs' : {},
64
73
TEST_CONFIG .update (TEST_CONFIG_OVERRIDE )
65
74
66
75
67
- def get_pytest_env_vars ():
76
+ def get_pytest_env_vars () -> Dict [ str , str ] :
68
77
"""Returns a dict for pytest invocation."""
69
78
ret = {}
70
79
@@ -79,21 +88,21 @@ def get_pytest_env_vars():
79
88
80
89
81
90
# DO NOT EDIT - automatically generated.
82
- # All versions used to tested samples.
83
- ALL_VERSIONS = ["2.7 " , "3.6 " , "3.7 " , "3.8 " ]
91
+ # All versions used to test samples.
92
+ ALL_VERSIONS = ["3.6 " , "3.7 " , "3.8 " , "3.9 " ]
84
93
85
94
# Any default versions that should be ignored.
86
95
IGNORED_VERSIONS = TEST_CONFIG ['ignored_versions' ]
87
96
88
97
TESTED_VERSIONS = sorted ([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS ])
89
98
90
- INSTALL_LIBRARY_FROM_SOURCE = bool ( os .environ .get ("INSTALL_LIBRARY_FROM_SOURCE" , False ))
99
+ INSTALL_LIBRARY_FROM_SOURCE = os .environ .get ("INSTALL_LIBRARY_FROM_SOURCE" , False ) in ( "True" , "true" )
91
100
#
92
101
# Style Checks
93
102
#
94
103
95
104
96
- def _determine_local_import_names (start_dir ) :
105
+ def _determine_local_import_names (start_dir : str ) -> List [ str ] :
97
106
"""Determines all import names that should be considered "local".
98
107
99
108
This is used when running the linter to insure that import order is
@@ -131,8 +140,11 @@ def _determine_local_import_names(start_dir):
131
140
132
141
133
142
@nox .session
134
- def lint (session ):
135
- session .install ("flake8" , "flake8-import-order" )
143
+ def 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" )
136
148
137
149
local_names = _determine_local_import_names ("." )
138
150
args = FLAKE8_COMMON_ARGS + [
@@ -141,7 +153,17 @@ def lint(session):
141
153
"."
142
154
]
143
155
session .run ("flake8" , * args )
156
+ #
157
+ # Black
158
+ #
159
+
144
160
161
+ @nox .session
162
+ def 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 )
145
167
146
168
#
147
169
# Sample Tests
@@ -151,13 +173,22 @@ def lint(session):
151
173
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml" ]
152
174
153
175
154
- def _session_tests (session , post_install = None ):
176
+ def _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 } " )
155
180
"""Runs py.test for a particular project."""
156
181
if os .path .exists ("requirements.txt" ):
157
- session .install ("-r" , "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" )
158
186
159
187
if os .path .exists ("requirements-test.txt" ):
160
- session .install ("-r" , "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" )
161
192
162
193
if INSTALL_LIBRARY_FROM_SOURCE :
163
194
session .install ("-e" , _get_repo_root ())
@@ -177,7 +208,7 @@ def _session_tests(session, post_install=None):
177
208
178
209
179
210
@nox .session (python = ALL_VERSIONS )
180
- def py (session ) :
211
+ def py (session : nox . sessions . Session ) -> None :
181
212
"""Runs py.test for a sample using the specified version of Python."""
182
213
if session .python in TESTED_VERSIONS :
183
214
_session_tests (session )
@@ -192,7 +223,7 @@ def py(session):
192
223
#
193
224
194
225
195
- def _get_repo_root ():
226
+ def _get_repo_root () -> Optional [ str ] :
196
227
""" Returns the root folder of the project. """
197
228
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
198
229
p = Path (os .getcwd ())
@@ -201,6 +232,11 @@ def _get_repo_root():
201
232
break
202
233
if Path (p / ".git" ).exists ():
203
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 )
204
240
p = p .parent
205
241
raise Exception ("Unable to detect repository root." )
206
242
@@ -210,7 +246,7 @@ def _get_repo_root():
210
246
211
247
@nox .session
212
248
@nox .parametrize ("path" , GENERATED_READMES )
213
- def readmegen (session , path ) :
249
+ def readmegen (session : nox . sessions . Session , path : str ) -> None :
214
250
"""(Re-)generates the readme for a sample."""
215
251
session .install ("jinja2" , "pyyaml" )
216
252
dir_ = os .path .dirname (path )
0 commit comments