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
+
41
44
# Old samples are opted out of enforcing Python type hints
42
45
# All new samples should feature them
43
- "enforce_type_hints" : False ,
46
+ 'enforce_type_hints' : False ,
47
+
44
48
# An envvar key for determining the project id to use. Change it
45
49
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
46
50
# build specific Cloud project. You can also use your own string
47
51
# to use your own Cloud project.
48
- " gcloud_project_env" : " GOOGLE_CLOUD_PROJECT" ,
52
+ ' gcloud_project_env' : ' GOOGLE_CLOUD_PROJECT' ,
49
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 ,
50
58
# A dictionary you want to inject into your test. Don't put any
51
59
# secrets here. These values will override predefined values.
52
- " envs" : {},
60
+ ' envs' : {},
53
61
}
54
62
55
63
56
64
try :
57
65
# Ensure we can import noxfile_config in the project's directory.
58
- sys .path .append ("." )
66
+ sys .path .append ('.' )
59
67
from noxfile_config import TEST_CONFIG_OVERRIDE
60
68
except ImportError as e :
61
69
print ("No user noxfile_config found: detail: {}" .format (e ))
65
73
TEST_CONFIG .update (TEST_CONFIG_OVERRIDE )
66
74
67
75
68
- def get_pytest_env_vars ():
76
+ def get_pytest_env_vars () -> Dict [ str , str ] :
69
77
"""Returns a dict for pytest invocation."""
70
78
ret = {}
71
79
72
80
# Override the GCLOUD_PROJECT and the alias.
73
- env_key = TEST_CONFIG [" gcloud_project_env" ]
81
+ env_key = TEST_CONFIG [' gcloud_project_env' ]
74
82
# This should error out if not set.
75
- ret [" GOOGLE_CLOUD_PROJECT" ] = os .environ [env_key ]
83
+ ret [' GOOGLE_CLOUD_PROJECT' ] = os .environ [env_key ]
76
84
77
85
# Apply user supplied envs.
78
- ret .update (TEST_CONFIG [" envs" ])
86
+ ret .update (TEST_CONFIG [' envs' ])
79
87
return ret
80
88
81
89
82
90
# DO NOT EDIT - automatically generated.
83
- # All versions used to tested samples.
84
- 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 " ]
85
93
86
94
# Any default versions that should be ignored.
87
- IGNORED_VERSIONS = TEST_CONFIG [" ignored_versions" ]
95
+ IGNORED_VERSIONS = TEST_CONFIG [' ignored_versions' ]
88
96
89
97
TESTED_VERSIONS = sorted ([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS ])
90
98
91
- 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" )
92
100
#
93
101
# Style Checks
94
102
#
95
103
96
104
97
- def _determine_local_import_names (start_dir ) :
105
+ def _determine_local_import_names (start_dir : str ) -> List [ str ] :
98
106
"""Determines all import names that should be considered "local".
99
107
100
108
This is used when running the linter to insure that import order is
@@ -132,8 +140,8 @@ def _determine_local_import_names(start_dir):
132
140
133
141
134
142
@nox .session
135
- def lint (session ) :
136
- if not TEST_CONFIG [" enforce_type_hints" ]:
143
+ def lint (session : nox . sessions . Session ) -> None :
144
+ if not TEST_CONFIG [' enforce_type_hints' ]:
137
145
session .install ("flake8" , "flake8-import-order" )
138
146
else :
139
147
session .install ("flake8" , "flake8-import-order" , "flake8-annotations" )
@@ -142,24 +150,21 @@ def lint(session):
142
150
args = FLAKE8_COMMON_ARGS + [
143
151
"--application-import-names" ,
144
152
"," .join (local_names ),
145
- "." ,
153
+ "."
146
154
]
147
155
session .run ("flake8" , * args )
148
-
149
-
150
156
#
151
157
# Black
152
158
#
153
159
154
160
155
161
@nox .session
156
- def blacken (session ) :
157
- session .install ("black" )
162
+ def blacken (session : nox . sessions . Session ) -> None :
163
+ session .install (BLACK_VERSION )
158
164
python_files = [path for path in os .listdir ("." ) if path .endswith (".py" )]
159
165
160
166
session .run ("black" , * python_files )
161
167
162
-
163
168
#
164
169
# Sample Tests
165
170
#
@@ -168,13 +173,22 @@ def blacken(session):
168
173
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml" ]
169
174
170
175
171
- 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 } " )
172
180
"""Runs py.test for a particular project."""
173
181
if os .path .exists ("requirements.txt" ):
174
- 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" )
175
186
176
187
if os .path .exists ("requirements-test.txt" ):
177
- 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" )
178
192
179
193
if INSTALL_LIBRARY_FROM_SOURCE :
180
194
session .install ("-e" , _get_repo_root ())
@@ -194,22 +208,22 @@ def _session_tests(session, post_install=None):
194
208
195
209
196
210
@nox .session (python = ALL_VERSIONS )
197
- def py (session ) :
211
+ def py (session : nox . sessions . Session ) -> None :
198
212
"""Runs py.test for a sample using the specified version of Python."""
199
213
if session .python in TESTED_VERSIONS :
200
214
_session_tests (session )
201
215
else :
202
- session .skip (
203
- "SKIPPED: {} tests are disabled for this sample." . format ( session .python )
204
- )
216
+ session .skip ("SKIPPED: {} tests are disabled for this sample." . format (
217
+ session .python
218
+ ))
205
219
206
220
207
221
#
208
222
# Readmegen
209
223
#
210
224
211
225
212
- def _get_repo_root ():
226
+ def _get_repo_root () -> Optional [ str ] :
213
227
""" Returns the root folder of the project. """
214
228
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
215
229
p = Path (os .getcwd ())
@@ -232,7 +246,7 @@ def _get_repo_root():
232
246
233
247
@nox .session
234
248
@nox .parametrize ("path" , GENERATED_READMES )
235
- def readmegen (session , path ) :
249
+ def readmegen (session : nox . sessions . Session , path : str ) -> None :
236
250
"""(Re-)generates the readme for a sample."""
237
251
session .install ("jinja2" , "pyyaml" )
238
252
dir_ = os .path .dirname (path )
0 commit comments