4949# Libraries that only work on Python 2.7
5050PY27_ONLY_LIBRARIES = ['mysql-python' ]
5151
52+ # Whether we're running on Travis CI
53+ ON_TRAVIS = os .environ .get ('TRAVIS' , False )
54+
5255
5356def list_files (folder , pattern ):
5457 """Lists all files below the given folder that match the pattern."""
@@ -136,21 +139,14 @@ def setup_appengine(session):
136139
137140def run_tests_in_sesssion (
138141 session , interpreter , sample_directories , use_appengine = True ,
139- skip_flaky = False , changed_only = False ):
142+ skip_flaky = False ):
140143 """This is the main function for executing tests.
141144
142145 It:
143146 1. Install the common testing utilities.
144147 2. Installs the test requirements.
145148 3. Determines which pytest arguments to use. skip_flaky causes extra
146149 arguments to be passed that will skip tests marked flaky.
147- 4. If posargs are specified, it will use that as the list of samples to
148- test.
149- 5. If posargs is not specified, it will gather the list of samples by
150- walking the repository tree.
151- 6. If changed_only was specified, it'll use Travis environment variables
152- to figure out which samples should be tested based on which files
153- were changed.
154150 7. For each sample directory, it runs py.test.
155151 """
156152 session .interpreter = interpreter
@@ -165,13 +161,6 @@ def run_tests_in_sesssion(
165161 if skip_flaky :
166162 pytest_args .append ('-m not slow and not flaky' )
167163
168- if changed_only :
169- changed_files = get_changed_files ()
170- sample_directories = filter_samples (
171- sample_directories , changed_files )
172- print ('Running tests on a subset of samples: ' )
173- print ('\n ' .join (sample_directories ))
174-
175164 for sample in sample_directories :
176165 # Ignore lib and env directories
177166 ignore_args = [
@@ -211,28 +200,62 @@ def session_gae(session):
211200def session_travis (session , subsession ):
212201 """On travis, just run with python3.4 and don't run slow or flaky tests."""
213202 if subsession == 'tests' :
203+ interpreter = 'python3.4'
214204 sample_directories = collect_sample_dirs (
215205 '.' , set ('./appengine/standard' ))
216- run_tests_in_sesssion (
217- session , 'python3.4' , sample_directories ,
218- skip_flaky = True , changed_only = True )
219- else :
206+ elif subsession == 'gae' :
207+ interpreter = 'python2.7'
220208 sample_directories = collect_sample_dirs ('appengine/standard' )
221- run_tests_in_sesssion (
222- session , 'python2.7' , sample_directories ,
223- skip_flaky = True , changed_only = True )
209+
210+ changed_files = get_changed_files ()
211+ sample_directories = filter_samples (
212+ sample_directories , changed_files )
213+
214+ if not sample_directories :
215+ print ('No samples changed.' )
216+ return
217+
218+ print ('Running tests on a subset of samples: ' )
219+ print ('\n ' .join (sample_directories ))
220+
221+ run_tests_in_sesssion (
222+ session , interpreter , sample_directories ,
223+ skip_flaky = True , changed_only = True )
224224
225225
226226def session_lint (session ):
227227 """Lints each sample."""
228+ sample_directories = session .posargs
229+ if not sample_directories :
230+ sample_directories = collect_sample_dirs ('.' )
231+
232+ # On travis, on lint changed samples.
233+ if ON_TRAVIS :
234+ changed_files = get_changed_files ()
235+ sample_directories = filter_samples (
236+ sample_directories , changed_files )
237+
228238 session .install ('flake8' , 'flake8-import-order' )
229- session .run (
230- 'flake8' , '--builtin=gettext' , '--max-complexity=15' ,
231- '--import-order-style=google' ,
232- '--exclude' ,
233- 'container_engine/django_tutorial/polls/migrations/*,.nox,.cache,env,'
234- 'lib' ,
235- * (session .posargs or ['.' ]))
239+
240+ for sample_directory in sample_directories :
241+ # Determine local import names
242+ local_names = [
243+ basename
244+ for basename , extension
245+ in [os .path .splitext (path ) for path
246+ in os .listdir (sample_directory )]
247+ if extension == '.py' or os .path .isdir (
248+ os .path .join (sample_directory , basename ))]
249+
250+ session .run (
251+ 'flake8' ,
252+ '--show-source' ,
253+ '--builtin' , 'gettext' ,
254+ '--max-complexity' , '15' ,
255+ '--import-order-style' , 'google' ,
256+ '--exclude' , '.nox,.cache,env,lib' ,
257+ '--application-import-names' , ',' .join (local_names ),
258+ sample_directory )
236259
237260
238261def session_reqcheck (session ):
0 commit comments