# -*- coding: UTF-8 -*- """develop tests """ import os import shutil import site import sys import tempfile import pytest from setuptools.compat import StringIO, PY2 from setuptools.command.test import test from setuptools.dist import Distribution from .textwrap import DALS SETUP_PY = DALS(""" from setuptools import setup setup(name='foo', packages=['name', 'name.space', 'name.space.tests'], namespace_packages=['name'], test_suite='name.space.tests.test_suite', ) """) NS_INIT = DALS(""" # -*- coding: Latin-1 -*- # Söme Arbiträry Ünicode to test Issüé 310 try: __import__('pkg_resources').declare_namespace(__name__) except ImportError: from pkgutil import extend_path __path__ = extend_path(__path__, __name__) """) # Make sure this is Latin-1 binary, before writing: if PY2: NS_INIT = NS_INIT.decode('UTF-8') NS_INIT = NS_INIT.encode('Latin-1') TEST_PY = DALS(""" import unittest class TestTest(unittest.TestCase): def test_test(self): print "Foo" # Should fail under Python 3 unless 2to3 is used test_suite = unittest.makeSuite(TestTest) """) @pytest.mark.skipif('hasattr(sys, "real_prefix")') @pytest.mark.usefixture('useroverride') class TestTestTest: def setup_method(self, method): # Directory structure self.dir = tempfile.mkdtemp() os.mkdir(os.path.join(self.dir, 'name')) os.mkdir(os.path.join(self.dir, 'name', 'space')) os.mkdir(os.path.join(self.dir, 'name', 'space', 'tests')) # setup.py setup = os.path.join(self.dir, 'setup.py') with open(setup, 'wt') as f: f.write(SETUP_PY) self.old_cwd = os.getcwd() # name/__init__.py init = os.path.join(self.dir, 'name', '__init__.py') with open(init, 'wb') as f: f.write(NS_INIT) # name/space/__init__.py init = os.path.join(self.dir, 'name', 'space', '__init__.py') with open(init, 'wt') as f: f.write('#empty\n') # name/space/tests/__init__.py init = os.path.join(self.dir, 'name', 'space', 'tests', '__init__.py') with open(init, 'wt') as f: f.write(TEST_PY) os.chdir(self.dir) def teardown_method(self, method): os.chdir(self.old_cwd) shutil.rmtree(self.dir) def test_test(self): dist = Distribution(dict( name='foo', packages=['name', 'name.space', 'name.space.tests'], namespace_packages=['name'], test_suite='name.space.tests.test_suite', use_2to3=True, )) dist.script_name = 'setup.py' cmd = test(dist) cmd.user = 1 cmd.ensure_finalized() cmd.install_dir = site.USER_SITE cmd.user = 1 old_stdout = sys.stdout sys.stdout = StringIO() try: try: # try/except/finally doesn't work in Python 2.4, so we need nested try-statements. cmd.run() except SystemExit: # The test runner calls sys.exit, stop that making an error. pass finally: sys.stdout = old_stdout