14
14
15
15
from __future__ import print_function
16
16
17
+ import glob
17
18
import os
18
19
from pathlib import Path
19
20
import sys
38
39
39
40
TEST_CONFIG = {
40
41
# You can opt out from the test for specific Python versions.
41
- 'ignored_versions' : ["2.7" ],
42
-
42
+ "ignored_versions" : ["2.7" ],
43
43
# Old samples are opted out of enforcing Python type hints
44
44
# All new samples should feature them
45
- 'enforce_type_hints' : False ,
46
-
45
+ "enforce_type_hints" : False ,
47
46
# An envvar key for determining the project id to use. Change it
48
47
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
49
48
# build specific Cloud project. You can also use your own string
50
49
# to use your own Cloud project.
51
- ' gcloud_project_env' : ' GOOGLE_CLOUD_PROJECT' ,
50
+ " gcloud_project_env" : " GOOGLE_CLOUD_PROJECT" ,
52
51
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
53
52
# If you need to use a specific version of pip,
54
53
# change pip_version_override to the string representation
55
54
# of the version number, for example, "20.2.4"
56
55
"pip_version_override" : None ,
57
56
# A dictionary you want to inject into your test. Don't put any
58
57
# secrets here. These values will override predefined values.
59
- ' envs' : {},
58
+ " envs" : {},
60
59
}
61
60
62
61
63
62
try :
64
63
# Ensure we can import noxfile_config in the project's directory.
65
- sys .path .append ('.' )
64
+ sys .path .append ("." )
66
65
from noxfile_config import TEST_CONFIG_OVERRIDE
67
66
except ImportError as e :
68
67
print ("No user noxfile_config found: detail: {}" .format (e ))
@@ -77,13 +76,13 @@ def get_pytest_env_vars() -> Dict[str, str]:
77
76
ret = {}
78
77
79
78
# Override the GCLOUD_PROJECT and the alias.
80
- env_key = TEST_CONFIG [' gcloud_project_env' ]
79
+ env_key = TEST_CONFIG [" gcloud_project_env" ]
81
80
# This should error out if not set.
82
- ret [' GOOGLE_CLOUD_PROJECT' ] = os .environ [env_key ]
83
- ret [' GCLOUD_PROJECT' ] = os .environ [env_key ] # deprecated
81
+ ret [" GOOGLE_CLOUD_PROJECT" ] = os .environ [env_key ]
82
+ ret [" GCLOUD_PROJECT" ] = os .environ [env_key ] # deprecated
84
83
85
84
# Apply user supplied envs.
86
- ret .update (TEST_CONFIG [' envs' ])
85
+ ret .update (TEST_CONFIG [" envs" ])
87
86
return ret
88
87
89
88
@@ -92,7 +91,7 @@ def get_pytest_env_vars() -> Dict[str, str]:
92
91
ALL_VERSIONS = ["2.7" , "3.6" , "3.7" , "3.8" , "3.9" , "3.10" ]
93
92
94
93
# Any default versions that should be ignored.
95
- IGNORED_VERSIONS = TEST_CONFIG [' ignored_versions' ]
94
+ IGNORED_VERSIONS = TEST_CONFIG [" ignored_versions" ]
96
95
97
96
TESTED_VERSIONS = sorted ([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS ])
98
97
@@ -145,7 +144,7 @@ def _determine_local_import_names(start_dir: str) -> List[str]:
145
144
146
145
@nox .session
147
146
def lint (session : nox .sessions .Session ) -> None :
148
- if not TEST_CONFIG [' enforce_type_hints' ]:
147
+ if not TEST_CONFIG [" enforce_type_hints" ]:
149
148
session .install ("flake8" , "flake8-import-order" )
150
149
else :
151
150
session .install ("flake8" , "flake8-import-order" , "flake8-annotations" )
@@ -154,7 +153,7 @@ def lint(session: nox.sessions.Session) -> None:
154
153
args = FLAKE8_COMMON_ARGS + [
155
154
"--application-import-names" ,
156
155
"," .join (local_names ),
157
- "."
156
+ "." ,
158
157
]
159
158
session .run ("flake8" , * args )
160
159
@@ -163,6 +162,7 @@ def lint(session: nox.sessions.Session) -> None:
163
162
# Black
164
163
#
165
164
165
+
166
166
@nox .session
167
167
def blacken (session : nox .sessions .Session ) -> None :
168
168
session .install ("black" )
@@ -179,38 +179,47 @@ def blacken(session: nox.sessions.Session) -> None:
179
179
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml" ]
180
180
181
181
182
- def _session_tests (session : nox .sessions .Session , post_install : Callable = None ) -> None :
183
- if TEST_CONFIG ["pip_version_override" ]:
184
- pip_version = TEST_CONFIG ["pip_version_override" ]
185
- session .install (f"pip=={ pip_version } " )
186
- """Runs py.test for a particular project."""
187
- if os .path .exists ("requirements.txt" ):
188
- if os .path .exists ("constraints.txt" ):
189
- session .install ("-r" , "requirements.txt" , "-c" , "constraints.txt" )
190
- else :
191
- session .install ("-r" , "requirements.txt" )
192
-
193
- if os .path .exists ("requirements-test.txt" ):
194
- if os .path .exists ("constraints-test.txt" ):
195
- session .install ("-r" , "requirements-test.txt" , "-c" , "constraints-test.txt" )
196
- else :
197
- session .install ("-r" , "requirements-test.txt" )
198
-
199
- if INSTALL_LIBRARY_FROM_SOURCE :
200
- session .install ("-e" , _get_repo_root ())
201
-
202
- if post_install :
203
- post_install (session )
204
-
205
- session .run (
206
- "pytest" ,
207
- * (PYTEST_COMMON_ARGS + session .posargs ),
208
- # Pytest will return 5 when no tests are collected. This can happen
209
- # on travis where slow and flaky tests are excluded.
210
- # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
211
- success_codes = [0 , 5 ],
212
- env = get_pytest_env_vars ()
213
- )
182
+ def _session_tests (
183
+ session : nox .sessions .Session , post_install : Callable = None
184
+ ) -> None :
185
+ # check for presence of tests
186
+ test_list = glob .glob ("*_test.py" ) + glob .glob ("test_*.py" )
187
+ if len (test_list ) == 0 :
188
+ print ("No tests found, skipping directory." )
189
+ else :
190
+ if TEST_CONFIG ["pip_version_override" ]:
191
+ pip_version = TEST_CONFIG ["pip_version_override" ]
192
+ session .install (f"pip=={ pip_version } " )
193
+ """Runs py.test for a particular project."""
194
+ if os .path .exists ("requirements.txt" ):
195
+ if os .path .exists ("constraints.txt" ):
196
+ session .install ("-r" , "requirements.txt" , "-c" , "constraints.txt" )
197
+ else :
198
+ session .install ("-r" , "requirements.txt" )
199
+
200
+ if os .path .exists ("requirements-test.txt" ):
201
+ if os .path .exists ("constraints-test.txt" ):
202
+ session .install (
203
+ "-r" , "requirements-test.txt" , "-c" , "constraints-test.txt"
204
+ )
205
+ else :
206
+ session .install ("-r" , "requirements-test.txt" )
207
+
208
+ if INSTALL_LIBRARY_FROM_SOURCE :
209
+ session .install ("-e" , _get_repo_root ())
210
+
211
+ if post_install :
212
+ post_install (session )
213
+
214
+ session .run (
215
+ "pytest" ,
216
+ * (PYTEST_COMMON_ARGS + session .posargs ),
217
+ # Pytest will return 5 when no tests are collected. This can happen
218
+ # on travis where slow and flaky tests are excluded.
219
+ # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
220
+ success_codes = [0 , 5 ],
221
+ env = get_pytest_env_vars (),
222
+ )
214
223
215
224
216
225
@nox .session (python = ALL_VERSIONS )
@@ -219,9 +228,9 @@ def py(session: nox.sessions.Session) -> None:
219
228
if session .python in TESTED_VERSIONS :
220
229
_session_tests (session )
221
230
else :
222
- session .skip ("SKIPPED: {} tests are disabled for this sample." . format (
223
- session .python
224
- ))
231
+ session .skip (
232
+ "SKIPPED: {} tests are disabled for this sample." . format ( session .python )
233
+ )
225
234
226
235
227
236
#
@@ -230,7 +239,7 @@ def py(session: nox.sessions.Session) -> None:
230
239
231
240
232
241
def _get_repo_root () -> Optional [str ]:
233
- """ Returns the root folder of the project. """
242
+ """Returns the root folder of the project."""
234
243
# Get root of this repository.
235
244
# Assume we don't have directories nested deeper than 10 items.
236
245
p = Path (os .getcwd ())
0 commit comments