12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ """
16
+ Noxfile used with nox-automation to run tests across all samples.
17
+
18
+ Use nox -l to see all possible sessions.
19
+
20
+ In general, you'll want to run:
21
+
22
+ nox -s lint
23
+ # or
24
+ nox -s list -- /path/to/sample/dir
25
+
26
+ And:
27
+
28
+ nox -s tests -- /path/to/sample/dir
29
+
30
+ """
31
+
15
32
import fnmatch
16
33
import itertools
17
34
import os
20
37
21
38
import nox
22
39
40
+ # Location of our common testing utilities. This isn't published to PyPI.
23
41
REPO_TOOLS_REQ = \
24
42
'git+https://github.com/GoogleCloudPlatform/python-repo-tools.git'
25
43
44
+ # Arguments used for every invocation of py.test.
26
45
COMMON_PYTEST_ARGS = [
27
46
'-x' , '--no-success-flaky-report' , '--cov' , '--cov-config' ,
28
47
'.coveragerc' , '--cov-append' , '--cov-report=' ]
29
48
49
+ # Blacklists of samples to ingnore.
30
50
# Bigtable and Speech are disabled because they use gRPC, which does not yet
31
51
# support Python 3. See: https://github.com/grpc/grpc/issues/282
32
52
TESTS_BLACKLIST = set ((
38
58
39
59
40
60
def list_files (folder , pattern ):
61
+ """Lists all files below the given folder that match the pattern."""
41
62
for root , folders , files in os .walk (folder ):
42
63
for filename in files :
43
64
if fnmatch .fnmatch (filename , pattern ):
44
65
yield os .path .join (root , filename )
45
66
46
67
47
68
def collect_sample_dirs (start_dir , blacklist = set ()):
48
- """Recursively collects a list of dirs that contain tests."""
69
+ """Recursively collects a list of dirs that contain tests.
70
+
71
+ This works by listing the contents of directories and finding
72
+ directories that have `*_test.py` files.
73
+ """
49
74
# Collect all the directories that have tests in them.
50
75
for parent , subdirs , files in os .walk (start_dir ):
51
76
if any (f for f in files if f [- 8 :] == '_test.py' ):
@@ -61,6 +86,8 @@ def collect_sample_dirs(start_dir, blacklist=set()):
61
86
62
87
63
88
def get_changed_files ():
89
+ """Uses travis environment variables to determine which files
90
+ have changed for this pull request / push."""
64
91
# Debug info
65
92
print ('TRAVIS_PULL_REQUEST: {}' .format (
66
93
os .environ .get ('TRAVIS_PULL_REQUEST' )))
@@ -85,6 +112,8 @@ def get_changed_files():
85
112
86
113
87
114
def filter_samples (sample_dirs , changed_files ):
115
+ """Filers the list of sample directories to only include directories that
116
+ contain changed files."""
88
117
result = []
89
118
for sample_dir in sample_dirs :
90
119
if sample_dir .startswith ('./' ):
@@ -97,6 +126,7 @@ def filter_samples(sample_dirs, changed_files):
97
126
98
127
99
128
def setup_appengine (session ):
129
+ """Installs the App Engine SDK."""
100
130
# Install the app engine sdk and setup import paths.
101
131
gae_root = os .environ .get ('GAE_ROOT' , tempfile .gettempdir ())
102
132
session .env ['PYTHONPATH' ] = os .path .join (gae_root , 'google_appengine' )
@@ -111,6 +141,22 @@ def setup_appengine(session):
111
141
def run_tests_in_sesssion (
112
142
session , interpreter , use_appengine = False , skip_flaky = False ,
113
143
changed_only = False , sample_directories = None ):
144
+ """This is the main function for executing tests.
145
+
146
+ It:
147
+ 1. Install the common testing utilities.
148
+ 2. Installs the test requirements for the current interpreter.
149
+ 3. Determines which pytest arguments to use. skip_flaky causes extra
150
+ arguments to be passed that will skip tests marked flaky.
151
+ 4. If posargs are specified, it will use that as the list of samples to
152
+ test.
153
+ 5. If posargs is not specified, it will gather the list of samples by
154
+ walking the repository tree.
155
+ 6. If changed_only was specified, it'll use Travis environment variables
156
+ to figure out which samples should be tested based on which files
157
+ were changed.
158
+ 7. For each sample directory, it runs py.test.
159
+ """
114
160
session .interpreter = interpreter
115
161
session .install (REPO_TOOLS_REQ )
116
162
session .install ('-r' , 'requirements-{}-dev.txt' .format (interpreter ))
@@ -156,10 +202,12 @@ def run_tests_in_sesssion(
156
202
157
203
@nox .parametrize ('interpreter' , ['python2.7' , 'python3.4' ])
158
204
def session_tests (session , interpreter ):
205
+ """Runs tests"""
159
206
run_tests_in_sesssion (session , interpreter )
160
207
161
208
162
209
def session_gae (session ):
210
+ """Runs test for GAE Standard samples."""
163
211
run_tests_in_sesssion (
164
212
session , 'python2.7' , use_appengine = True ,
165
213
sample_directories = collect_sample_dirs (
@@ -168,6 +216,8 @@ def session_gae(session):
168
216
169
217
170
218
def session_grpc (session ):
219
+ """Runs tests for samples that need grpc."""
220
+ # TODO: Remove this when grpc supports Python 3.
171
221
run_tests_in_sesssion (
172
222
session ,
173
223
'python2.7' ,
@@ -192,6 +242,7 @@ def session_travis(session, subsession):
192
242
193
243
194
244
def session_lint (session ):
245
+ """Lints each sample."""
195
246
session .install ('flake8' , 'flake8-import-order' )
196
247
session .run (
197
248
'flake8' , '--builtin=gettext' , '--max-complexity=10' ,
@@ -202,6 +253,7 @@ def session_lint(session):
202
253
203
254
204
255
def session_reqcheck (session ):
256
+ """Checks for out of date requirements."""
205
257
session .install (REPO_TOOLS_REQ )
206
258
207
259
if 'update' in session .posargs :
0 commit comments