From 9a850e0c00262f8f7c2c6270af142ccd5666e215 Mon Sep 17 00:00:00 2001 From: Jack Wilsdon Date: Tue, 14 Jun 2016 17:51:54 +0100 Subject: [PATCH] Add custom setup.py test command --- .travis.yml | 5 ++-- setup.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index eefaa7b..a0e4775 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,10 +28,9 @@ matrix: # To install dependencies, tell tox to do everything but actually running the # test. install: - - travis_retry pip install tox - - travis_retry tox --notest + - travis_retry python setup.py test -a "--notest" -script: tox +script: python setup.py test # Report coverage to codecov.io. before_install: diff --git a/setup.py b/setup.py index c556e96..3f489c3 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,63 @@ -from setuptools import setup +from __future__ import division, absolute_import, print_function + import os +from os import path +import sys +from setuptools.dist import Distribution +from setuptools import setup, Command + + +class CustomDistribution(Distribution): + def __init__(self, *args, **kwargs): + self.sdist_requires = None + Distribution.__init__(self, *args, **kwargs) + + def get_finalized_command(self, command, create=1): + cmd_obj = self.get_command_obj(command, create) + cmd_obj.ensure_finalized() + return cmd_obj + + def export_live_eggs(self, env=False): + """Adds all of the eggs in the current environment to PYTHONPATH.""" + path_eggs = [p for p in sys.path if p.endswith('.egg')] + + command = self.get_finalized_command("egg_info") + egg_base = path.abspath(command.egg_base) + + unique_path_eggs = set(path_eggs + [egg_base]) + + os.environ['PYTHONPATH'] = ':'.join(unique_path_eggs) + + +class test(Command): # noqa: ignore=N801 + """Command to run tox.""" + + description = "run tox tests" + + user_options = [('tox-args=', 'a', "Arguments to pass to tox")] + + def initialize_options(self): + self.tox_args = '' + + def finalize_options(self): + pass + + def run(self): + # Install test dependencies if needed. + if self.distribution.tests_require: + self.distribution.fetch_build_eggs(self.distribution.tests_require) + + # Add eggs to PYTHONPATH. We need to do this to ensure our eggs are + # seen by Tox. + self.distribution.export_live_eggs() + + shlex = __import__('shlex') + tox = __import__('tox') + + parsed_args = shlex.split(self.tox_args) + result = tox.cmdline(args=parsed_args) + + sys.exit(result) def _read(fn): @@ -17,10 +75,10 @@ def _read(fn): license='MIT', platforms='ALL', long_description=_read("README.rst"), - install_requires=[ - 'pyyaml' - ], + install_requires=['pyyaml'], + tests_require=['tox'], py_modules=['confuse'], + cmdclass={'test': test}, classifiers=[ 'Intended Audience :: Developers', 'License :: OSI Approved :: MIT License', @@ -30,4 +88,5 @@ def _read(fn): 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', ], + distclass=CustomDistribution )