Skip to content

Commit 8fcf9ef

Browse files
authored
Merge pull request #2475 from dhermes/collapse-travis-config-2
Adding flexibility to how run_unit_tests determines what to run.
2 parents fbc80a4 + 7e4f576 commit 8fcf9ef

File tree

4 files changed

+139
-44
lines changed

4 files changed

+139
-44
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ install:
55
- pip install --upgrade pip tox
66

77
script:
8-
- tox -e py27
9-
- tox -e py34
8+
- python2.7 scripts/run_unit_tests.py
9+
- python3.4 scripts/run_unit_tests.py
1010
- tox -e lint
1111
- tox -e cover
12-
- tox -e isolated-cover
12+
- python scripts/run_unit_tests.py --tox-env cover
1313
- tox -e system-tests
1414
- tox -e system-tests3
1515
- scripts/update_docs.sh

CONTRIBUTING.rst

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,29 @@ Contributing
88

99
Here are some guidelines for hacking on ``google-cloud-python``.
1010

11+
Adding Features
12+
---------------
13+
14+
In order to add a feature to ``google-cloud-python``:
15+
16+
- The feature must be documented in both the API and narrative
17+
documentation (in ``docs/``).
18+
19+
- The feature must work fully on the following CPython versions: 2.7,
20+
3.4, and 3.5 on both UNIX and Windows.
21+
22+
- The feature must not add unnecessary dependencies (where
23+
"unnecessary" is of course subjective, but new dependencies should
24+
be discussed).
25+
1126
Using a Development Checkout
1227
----------------------------
1328

14-
You'll have to create a development environment to hack on ``google-cloud-python``,
15-
using a Git checkout:
29+
You'll have to create a development environment to hack on
30+
``google-cloud-python``, using a Git checkout:
1631

17-
- While logged into your GitHub account, navigate to the ``google-cloud-python`` repo
18-
on GitHub.
19-
20-
https://github.com/GoogleCloudPlatform/google-cloud-python
32+
- While logged into your GitHub account, navigate to the
33+
``google-cloud-python`` `repo`_ on GitHub.
2134

2235
- Fork and clone the ``google-cloud-python`` repository to your GitHub account by
2336
clicking the "Fork" button.
@@ -42,6 +55,8 @@ repo, from which you can submit a pull request.
4255
To work on the codebase and run the tests, we recommend using ``tox``,
4356
but you can also use a ``virtualenv`` of your own creation.
4457

58+
.. _repo: https://github.com/GoogleCloudPlatform/google-cloud-python
59+
4560
Using a custom ``virtualenv``
4661
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4762

@@ -96,6 +111,19 @@ Using ``tox``
96111
by the ``tox`` environment, so if you make changes, you'll need
97112
to again ``--recreate`` the environment.
98113

114+
- To run unit tests on a restricted set of packages::
115+
116+
$ tox -e py27 -- core datastore
117+
118+
Alternatively, you can just navigate directly to the package you are
119+
currently developing and run tests there::
120+
121+
$ export GIT_ROOT=$(pwd)
122+
$ cd ${GIT_ROOT}/core/
123+
$ tox -e py27
124+
$ cd ${GIT_ROOT}/datastore/
125+
$ tox -e py27
126+
99127
Note on Editable Installs / Develop Mode
100128
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101129

@@ -124,21 +152,6 @@ On Debian/Ubuntu::
124152

125153
$ sudo apt-get install python-dev
126154

127-
Adding Features
128-
---------------
129-
130-
In order to add a feature to ``google-cloud-python``:
131-
132-
- The feature must be documented in both the API and narrative
133-
documentation (in ``docs/``).
134-
135-
- The feature must work fully on the following CPython versions: 2.7,
136-
3.4, and 3.5 on both UNIX and Windows.
137-
138-
- The feature must not add unnecessary dependencies (where
139-
"unnecessary" is of course subjective, but new dependencies should
140-
be discussed).
141-
142155
Coding Style
143156
------------
144157

@@ -170,21 +183,25 @@ Running Tests
170183

171184
- To run all tests for ``google-cloud-python`` on a single Python version, run
172185
``py.test`` from your development virtualenv (See
173-
*Using a Development Checkout* above).
186+
`Using a Development Checkout`_ above).
187+
188+
.. _Using a Development Checkout: #using-a-development-checkout
174189

175190
- To run the full set of ``google-cloud-python`` tests on all platforms, install
176-
``tox`` (https://testrun.org/tox/) into a system Python. The ``tox`` console
177-
script will be installed into the scripts location for that Python. While
178-
``cd``'ed to the ``google-cloud-python`` checkout root directory (it contains
179-
``tox.ini``), invoke the ``tox`` console script. This will read the
180-
``tox.ini`` file and execute the tests on multiple Python versions and
181-
platforms; while it runs, it creates a virtualenv for each version/platform
182-
combination. For example::
191+
``tox`` (https://tox.readthedocs.io/en/latest/) into a system Python. The
192+
``tox`` console script will be installed into the scripts location for that
193+
Python. While ``cd``'-ed to the ``google-cloud-python`` checkout root
194+
directory (it contains ``tox.ini``), invoke the ``tox`` console script.
195+
This will read the ``tox.ini`` file and execute the tests on multiple
196+
Python versions and platforms; while it runs, it creates a ``virtualenv`` for
197+
each version/platform combination. For example::
183198

184199
$ sudo --set-home /usr/bin/pip install tox
185200
$ cd ${HOME}/hack-on-google-cloud-python/
186201
$ /usr/bin/tox
187202

203+
.. _Using a Development Checkout: #using-a-development-checkout
204+
188205
Running System Tests
189206
--------------------
190207

scripts/run_unit_tests.py

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@
3535
'scripts',
3636
'system_tests',
3737
)
38-
ENV_REMAP = {
39-
'isolated-cover': 'cover',
38+
TOX_ENV_VAR = 'TOXENV'
39+
ACCEPTED_VERSIONS = {
40+
(2, 7): 'py27',
41+
(3, 4): 'py34',
42+
(3, 5): 'py35',
4043
}
44+
UNSET_SENTINEL = object() # Sentinel for argparser
4145

4246

4347
def check_output(*args):
@@ -79,6 +83,47 @@ def get_package_directories():
7983
return result
8084

8185

86+
def verify_packages(subset, all_packages):
87+
"""Verify that a subset of packages are among all packages.
88+
89+
:type subset: list
90+
:param subset: List of a subset of package names.
91+
92+
:type all_packages: list
93+
:param all_packages: List of all package names.
94+
95+
:raises: :class:`~exceptions.ValueError` if there are unknown packages
96+
in ``subset``
97+
"""
98+
left_out = set(subset) - set(all_packages)
99+
if left_out:
100+
raise ValueError('Unknown packages',
101+
sorted(left_out))
102+
103+
104+
def get_test_packages():
105+
"""Get a list of packages which need tests run.
106+
107+
Filters the package list in the following order:
108+
109+
* Check command line for packages passed in as positional arguments
110+
* Just use all packages
111+
112+
:rtype: list
113+
:returns: A list of all package directories where tests
114+
need be run.
115+
"""
116+
all_packages = get_package_directories()
117+
118+
parser = get_parser()
119+
args = parser.parse_args()
120+
if args.packages is not UNSET_SENTINEL:
121+
verify_packages(args.packages, all_packages)
122+
return sorted(args.packages)
123+
else:
124+
return all_packages
125+
126+
82127
def run_package(package, tox_env):
83128
"""Run tox environment for a given package.
84129
@@ -111,28 +156,60 @@ def get_parser():
111156
description = 'Run tox environment(s) in all sub-packages.'
112157
parser = argparse.ArgumentParser(description=description)
113158
parser.add_argument(
114-
'--tox-env-dir', dest='tox_env_dir',
115-
help='The current tox environment directory.')
159+
'--tox-env', dest='tox_env',
160+
help='The tox environment(s) to run in sub-packages.')
161+
packages_help = 'Optional list of sub-packages to be tested.'
162+
parser.add_argument('packages', nargs='*',
163+
default=UNSET_SENTINEL, help=packages_help)
116164
return parser
117165

118166

167+
def get_tox_env_from_version():
168+
"""Get ``tox`` environment from the current Python version.
169+
170+
:rtype: str
171+
:returns: The current ``tox`` environment to be used, e.g. ``"py27"``.
172+
:raises: :class:`EnvironmentError` if the first two options
173+
don't yield any value and the current version of
174+
Python is not in ``ACCEPTED_VERSIONS``.
175+
"""
176+
version_info = sys.version_info[:2]
177+
try:
178+
return ACCEPTED_VERSIONS[version_info]
179+
except KeyError:
180+
raise EnvironmentError(
181+
'Invalid Python version', version_info,
182+
'Accepted versions are',
183+
sorted(ACCEPTED_VERSIONS.keys()))
184+
185+
119186
def get_tox_env():
120187
"""Get the environment to be used with ``tox``.
121188
189+
Tries to infer the ``tox`` environment in the following order
190+
191+
* From the ``--tox-env`` command line flag
192+
* From the ``TOXENV`` environment variable
193+
* From the version of the current running Python
194+
122195
:rtype: str
123196
:returns: The current ``tox`` environment to be used, e.g. ``"py27"``.
124197
"""
125198
parser = get_parser()
126199
args = parser.parse_args()
127-
env_dir = args.tox_env_dir
128-
_, tox_env = os.path.split(env_dir)
129-
tox_env = ENV_REMAP.get(tox_env, tox_env)
200+
if args.tox_env is not None:
201+
tox_env = args.tox_env
202+
elif TOX_ENV_VAR in os.environ:
203+
tox_env = os.environ[TOX_ENV_VAR]
204+
else:
205+
tox_env = get_tox_env_from_version()
206+
130207
return tox_env
131208

132209

133210
def main():
134211
"""Run all the unit tests that need to be run."""
135-
packages_to_run = get_package_directories()
212+
packages_to_run = get_test_packages()
136213
if not packages_to_run:
137214
print('No tests to run.')
138215
return
@@ -150,7 +227,7 @@ def main():
150227
msg_parts.append('- ' + package)
151228
msg = '\n'.join(msg_parts)
152229
print(msg, file=sys.stderr)
153-
sys.exit(len(failed_packages))
230+
sys.exit(1)
154231

155232

156233
if __name__ == '__main__':

tox.ini

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tox]
22
envlist =
3-
cover,docs,lint
3+
py27,py34,py35,cover,docs,lint
44

55
[testing]
66
deps =
@@ -114,7 +114,7 @@ covercmd =
114114

115115
[testenv]
116116
commands =
117-
python {toxinidir}/scripts/run_unit_tests.py --tox-env-dir {envdir}
117+
python {toxinidir}/scripts/run_unit_tests.py {posargs}
118118
deps =
119119
skip_install =
120120
py27: True
@@ -123,7 +123,8 @@ skip_install =
123123
isolated-cover: True
124124

125125
[testenv:isolated-cover]
126-
commands = {[testenv]commands}
126+
commands =
127+
python {toxinidir}/scripts/run_unit_tests.py {posargs} --tox-env cover
127128

128129
[testenv:cover]
129130
basepython =

0 commit comments

Comments
 (0)