forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnox.py
More file actions
129 lines (101 loc) · 4.61 KB
/
nox.py
File metadata and controls
129 lines (101 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import fnmatch
import os
import tempfile
import nox
REPO_TOOLS_REQ =\
'git+https://github.com/GoogleCloudPlatform/python-repo-tools.git'
COMMON_PYTEST_ARGS = [
'-x', '--no-success-flaky-report', '--cov', '--cov-config',
'.coveragerc', '--cov-append', '--cov-report=']
SESSION_TESTS_BLACKLIST = set(('appengine', 'testing'))
def session_lint(session):
session.install('flake8', 'flake8-import-order')
session.run(
'flake8', '--builtin=gettext', '--max-complexity=10',
'--import-order-style=google',
'--exclude',
'container_engine/django_tutorial/polls/migrations/*,.nox,.cache,env',
*(session.posargs or ['.']))
def list_files(folder, pattern):
for root, folders, files in os.walk(folder):
for filename in files:
if fnmatch.fnmatch(filename, pattern):
yield os.path.join(root, filename)
def session_reqcheck(session):
session.install(REPO_TOOLS_REQ)
if 'update' in session.posargs:
command = 'update-requirements'
else:
command = 'check-requirements'
for reqfile in list_files('.', 'requirements*.txt'):
session.run('gcprepotools', command, reqfile)
def collect_sample_dirs(start_dir, blacklist=set()):
"""Recursively collects a list of dirs that contain tests."""
# Collect all the directories that have tests in them.
for parent, subdirs, files in os.walk(start_dir):
if any(f for f in files if f[-8:] == '_test.py'):
# Don't recurse further, since py.test will do that.
del subdirs[:]
# This dir has tests in it. yield it.
yield parent
else:
# Filter out dirs we don't want to recurse into
subdirs[:] = [s for s in subdirs
if s[0].isalpha() and s not in blacklist]
@nox.parametrize('interpreter', ['python2.7', 'python3.4'])
def session_tests(session, interpreter, extra_pytest_args=None):
session.interpreter = interpreter
session.install(REPO_TOOLS_REQ)
session.install('-r', 'requirements-{}-dev.txt'.format(interpreter))
# extra_pytest_args can be send by another session calling this session,
# see session_travis.
pytest_args = COMMON_PYTEST_ARGS + (extra_pytest_args or [])
# session.posargs is any leftover arguments from the command line, which
# allows users to run a particular test instead of all of them.
for sample in (session.posargs or
collect_sample_dirs('.', SESSION_TESTS_BLACKLIST)):
# Install additional dependencies if they exist
dirname = sample if os.path.isdir(sample) else os.path.dirname(sample)
for reqfile in list_files(dirname, 'requirements*.txt'):
session.install('-r', reqfile)
session.run(
'py.test', sample,
*pytest_args,
success_codes=[0, 5]) # Treat no test collected as success.
def session_gae(session, extra_pytest_args=None):
session.interpreter = 'python2.7'
session.install(REPO_TOOLS_REQ)
session.install('-r', 'requirements-python2.7-dev.txt')
# Install the app engine sdk and setup import paths.
gae_root = os.environ.get('GAE_ROOT', tempfile.gettempdir())
session.env['PYTHONPATH'] = os.path.join(gae_root, 'google_appengine')
session.run('gcprepotools', 'download-appengine-sdk', gae_root)
# Create a lib directory to prevent the GAE vendor library from
# complaining.
if not os.path.exists('lib'):
os.makedirs('lib')
pytest_args = COMMON_PYTEST_ARGS + (extra_pytest_args or [])
for sample in (session.posargs or collect_sample_dirs('appengine')):
session.run(
'py.test', sample,
*pytest_args,
success_codes=[0, 5]) # Treat no test collected as success.
def session_travis(session):
"""On travis, just run with python3.4 and don't run slow or flaky tests."""
session_tests(
session, 'python3.4', extra_pytest_args=['-m not slow and not flaky'])
session_gae(
session, extra_pytest_args=['-m not slow and not flaky'])