1717import os
1818from pathlib import Path
1919import sys
20+ from typing import Callable , Dict , List , Optional
2021
2122import nox
2223
3940 # You can opt out from the test for specific Python versions.
4041 'ignored_versions' : ["2.7" ],
4142
43+ # Old samples are opted out of enforcing Python type hints
44+ # All new samples should feature them
45+ 'enforce_type_hints' : False ,
46+
4247 # An envvar key for determining the project id to use. Change it
4348 # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4449 # build specific Cloud project. You can also use your own string
6469TEST_CONFIG .update (TEST_CONFIG_OVERRIDE )
6570
6671
67- def get_pytest_env_vars ():
72+ def get_pytest_env_vars () -> Dict [ str , str ] :
6873 """Returns a dict for pytest invocation."""
6974 ret = {}
7075
@@ -93,7 +98,7 @@ def get_pytest_env_vars():
9398#
9499
95100
96- def _determine_local_import_names (start_dir ) :
101+ def _determine_local_import_names (start_dir : str ) -> List [ str ] :
97102 """Determines all import names that should be considered "local".
98103
99104 This is used when running the linter to insure that import order is
@@ -131,8 +136,11 @@ def _determine_local_import_names(start_dir):
131136
132137
133138@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" )
136144
137145 local_names = _determine_local_import_names ("." )
138146 args = FLAKE8_COMMON_ARGS + [
@@ -141,8 +149,18 @@ def lint(session):
141149 "."
142150 ]
143151 session .run ("flake8" , * args )
152+ #
153+ # Black
154+ #
144155
145156
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+
146164#
147165# Sample Tests
148166#
@@ -151,7 +169,7 @@ def lint(session):
151169PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml" ]
152170
153171
154- def _session_tests (session , post_install = None ):
172+ def _session_tests (session : nox . sessions . Session , post_install : Callable = None ) -> None :
155173 """Runs py.test for a particular project."""
156174 if os .path .exists ("requirements.txt" ):
157175 session .install ("-r" , "requirements.txt" )
@@ -177,7 +195,7 @@ def _session_tests(session, post_install=None):
177195
178196
179197@nox .session (python = ALL_VERSIONS )
180- def py (session ) :
198+ def py (session : nox . sessions . Session ) -> None :
181199 """Runs py.test for a sample using the specified version of Python."""
182200 if session .python in TESTED_VERSIONS :
183201 _session_tests (session )
@@ -192,7 +210,7 @@ def py(session):
192210#
193211
194212
195- def _get_repo_root ():
213+ def _get_repo_root () -> Optional [ str ] :
196214 """ Returns the root folder of the project. """
197215 # Get root of this repository. Assume we don't have directories nested deeper than 10 items.
198216 p = Path (os .getcwd ())
@@ -201,6 +219,11 @@ def _get_repo_root():
201219 break
202220 if Path (p / ".git" ).exists ():
203221 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 )
204227 p = p .parent
205228 raise Exception ("Unable to detect repository root." )
206229
@@ -210,7 +233,7 @@ def _get_repo_root():
210233
211234@nox .session
212235@nox .parametrize ("path" , GENERATED_READMES )
213- def readmegen (session , path ) :
236+ def readmegen (session : nox . sessions . Session , path : str ) -> None :
214237 """(Re-)generates the readme for a sample."""
215238 session .install ("jinja2" , "pyyaml" )
216239 dir_ = os .path .dirname (path )
0 commit comments