1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
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+
1532import fnmatch
1633import itertools
1734import os
2037
2138import nox
2239
40+ # Location of our common testing utilities. This isn't published to PyPI.
2341REPO_TOOLS_REQ = \
2442 'git+https://github.com/GoogleCloudPlatform/python-repo-tools.git'
2543
44+ # Arguments used for every invocation of py.test.
2645COMMON_PYTEST_ARGS = [
2746 '-x' , '--no-success-flaky-report' , '--cov' , '--cov-config' ,
2847 '.coveragerc' , '--cov-append' , '--cov-report=' ]
2948
49+ # Blacklists of samples to ingnore.
3050# Bigtable and Speech are disabled because they use gRPC, which does not yet
3151# support Python 3. See: https://github.com/grpc/grpc/issues/282
3252TESTS_BLACKLIST = set ((
3858
3959
4060def list_files (folder , pattern ):
61+ """Lists all files below the given folder that match the pattern."""
4162 for root , folders , files in os .walk (folder ):
4263 for filename in files :
4364 if fnmatch .fnmatch (filename , pattern ):
4465 yield os .path .join (root , filename )
4566
4667
4768def 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+ """
4974 # Collect all the directories that have tests in them.
5075 for parent , subdirs , files in os .walk (start_dir ):
5176 if any (f for f in files if f [- 8 :] == '_test.py' ):
@@ -61,6 +86,8 @@ def collect_sample_dirs(start_dir, blacklist=set()):
6186
6287
6388def get_changed_files ():
89+ """Uses travis environment variables to determine which files
90+ have changed for this pull request / push."""
6491 # Debug info
6592 print ('TRAVIS_PULL_REQUEST: {}' .format (
6693 os .environ .get ('TRAVIS_PULL_REQUEST' )))
@@ -85,6 +112,8 @@ def get_changed_files():
85112
86113
87114def filter_samples (sample_dirs , changed_files ):
115+ """Filers the list of sample directories to only include directories that
116+ contain changed files."""
88117 result = []
89118 for sample_dir in sample_dirs :
90119 if sample_dir .startswith ('./' ):
@@ -97,6 +126,7 @@ def filter_samples(sample_dirs, changed_files):
97126
98127
99128def setup_appengine (session ):
129+ """Installs the App Engine SDK."""
100130 # Install the app engine sdk and setup import paths.
101131 gae_root = os .environ .get ('GAE_ROOT' , tempfile .gettempdir ())
102132 session .env ['PYTHONPATH' ] = os .path .join (gae_root , 'google_appengine' )
@@ -111,6 +141,22 @@ def setup_appengine(session):
111141def run_tests_in_sesssion (
112142 session , interpreter , use_appengine = False , skip_flaky = False ,
113143 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+ """
114160 session .interpreter = interpreter
115161 session .install (REPO_TOOLS_REQ )
116162 session .install ('-r' , 'requirements-{}-dev.txt' .format (interpreter ))
@@ -156,10 +202,12 @@ def run_tests_in_sesssion(
156202
157203@nox .parametrize ('interpreter' , ['python2.7' , 'python3.4' ])
158204def session_tests (session , interpreter ):
205+ """Runs tests"""
159206 run_tests_in_sesssion (session , interpreter )
160207
161208
162209def session_gae (session ):
210+ """Runs test for GAE Standard samples."""
163211 run_tests_in_sesssion (
164212 session , 'python2.7' , use_appengine = True ,
165213 sample_directories = collect_sample_dirs (
@@ -168,6 +216,8 @@ def session_gae(session):
168216
169217
170218def session_grpc (session ):
219+ """Runs tests for samples that need grpc."""
220+ # TODO: Remove this when grpc supports Python 3.
171221 run_tests_in_sesssion (
172222 session ,
173223 'python2.7' ,
@@ -192,6 +242,7 @@ def session_travis(session, subsession):
192242
193243
194244def session_lint (session ):
245+ """Lints each sample."""
195246 session .install ('flake8' , 'flake8-import-order' )
196247 session .run (
197248 'flake8' , '--builtin=gettext' , '--max-complexity=10' ,
@@ -202,6 +253,7 @@ def session_lint(session):
202253
203254
204255def session_reqcheck (session ):
256+ """Checks for out of date requirements."""
205257 session .install (REPO_TOOLS_REQ )
206258
207259 if 'update' in session .posargs :
0 commit comments