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
39
40
# You can opt out from the test for specific Python versions.
40
41
'ignored_versions' : ["2.7" ],
41
42
43
+ # Old samples are opted out of enforcing Python type hints
44
+ # All new samples should feature them
45
+ 'enforce_type_hints' : False ,
46
+
42
47
# An envvar key for determining the project id to use. Change it
43
48
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
44
49
# build specific Cloud project. You can also use your own string
64
69
TEST_CONFIG .update (TEST_CONFIG_OVERRIDE )
65
70
66
71
67
- def get_pytest_env_vars ():
72
+ def get_pytest_env_vars () -> Dict [ str , str ] :
68
73
"""Returns a dict for pytest invocation."""
69
74
ret = {}
70
75
@@ -93,7 +98,7 @@ def get_pytest_env_vars():
93
98
#
94
99
95
100
96
- def _determine_local_import_names (start_dir ) :
101
+ def _determine_local_import_names (start_dir : str ) -> List [ str ] :
97
102
"""Determines all import names that should be considered "local".
98
103
99
104
This is used when running the linter to insure that import order is
@@ -131,8 +136,11 @@ def _determine_local_import_names(start_dir):
131
136
132
137
133
138
@nox .session
134
- def lint (session ):
135
- session .install ("flake8" , "flake8-import-order" )
139
+ def lint (session : nox .sessions .Session ) -> None :
140
+ if not TEST_CONFIG ['enforce_type_hints' ]:
141
+ session .install ("flake8" , "flake8-import-order" )
142
+ else :
143
+ session .install ("flake8" , "flake8-import-order" , "flake8-annotations" )
136
144
137
145
local_names = _determine_local_import_names ("." )
138
146
args = FLAKE8_COMMON_ARGS + [
@@ -141,8 +149,18 @@ def lint(session):
141
149
"."
142
150
]
143
151
session .run ("flake8" , * args )
152
+ #
153
+ # Black
154
+ #
144
155
145
156
157
+ @nox .session
158
+ def blacken (session : nox .sessions .Session ) -> None :
159
+ session .install ("black" )
160
+ python_files = [path for path in os .listdir ("." ) if path .endswith (".py" )]
161
+
162
+ session .run ("black" , * python_files )
163
+
146
164
#
147
165
# Sample Tests
148
166
#
@@ -151,7 +169,7 @@ def lint(session):
151
169
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml" ]
152
170
153
171
154
- def _session_tests (session , post_install = None ):
172
+ def _session_tests (session : nox . sessions . Session , post_install : Callable = None ) -> None :
155
173
"""Runs py.test for a particular project."""
156
174
if os .path .exists ("requirements.txt" ):
157
175
session .install ("-r" , "requirements.txt" )
@@ -177,7 +195,7 @@ def _session_tests(session, post_install=None):
177
195
178
196
179
197
@nox .session (python = ALL_VERSIONS )
180
- def py (session ) :
198
+ def py (session : nox . sessions . Session ) -> None :
181
199
"""Runs py.test for a sample using the specified version of Python."""
182
200
if session .python in TESTED_VERSIONS :
183
201
_session_tests (session )
@@ -192,7 +210,7 @@ def py(session):
192
210
#
193
211
194
212
195
- def _get_repo_root ():
213
+ def _get_repo_root () -> Optional [ str ] :
196
214
""" Returns the root folder of the project. """
197
215
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
198
216
p = Path (os .getcwd ())
@@ -201,6 +219,11 @@ def _get_repo_root():
201
219
break
202
220
if Path (p / ".git" ).exists ():
203
221
return str (p )
222
+ # .git is not available in repos cloned via Cloud Build
223
+ # setup.py is always in the library's root, so use that instead
224
+ # https://github.com/googleapis/synthtool/issues/792
225
+ if Path (p / "setup.py" ).exists ():
226
+ return str (p )
204
227
p = p .parent
205
228
raise Exception ("Unable to detect repository root." )
206
229
@@ -210,7 +233,7 @@ def _get_repo_root():
210
233
211
234
@nox .session
212
235
@nox .parametrize ("path" , GENERATED_READMES )
213
- def readmegen (session , path ) :
236
+ def readmegen (session : nox . sessions . Session , path : str ) -> None :
214
237
"""(Re-)generates the readme for a sample."""
215
238
session .install ("jinja2" , "pyyaml" )
216
239
dir_ = os .path .dirname (path )
0 commit comments