summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author?ric Araujo <merwok@netwok.org>2011-11-12 04:59:36 +0100
committer?ric Araujo <merwok@netwok.org>2011-11-12 04:59:36 +0100
commit37cc8079333cd92ae07a969acbcd8978c14b776a (patch)
treec7e659a4fa7776961a8d9f5aab37db2d245273ea
parent118a6f15c7f87e80fec94b21385079d595614c22 (diff)
downloaddisutils2-37cc8079333cd92ae07a969acbcd8978c14b776a.tar.gz
Make sure tests that register custom commands also clear them
-rw-r--r--distutils2/tests/support.py15
-rw-r--r--distutils2/tests/test_config.py24
-rw-r--r--distutils2/tests/test_dist.py26
3 files changed, 39 insertions, 26 deletions
diff --git a/distutils2/tests/support.py b/distutils2/tests/support.py
index 2d18022..11a98c1 100644
--- a/distutils2/tests/support.py
+++ b/distutils2/tests/support.py
@@ -49,6 +49,9 @@ except ImportError:
zlib = None
from distutils2.dist import Distribution
+from distutils2.util import resolve_name
+from distutils2.command import set_command, _COMMANDS
+
from distutils2.tests import unittest
from distutils2._backport import sysconfig
@@ -59,7 +62,8 @@ __all__ = [
# mocks
'DummyCommand', 'TestDistribution',
# misc. functions and decorators
- 'fake_dec', 'create_distribution', 'copy_xxmodule_c', 'fixup_build_ext',
+ 'fake_dec', 'create_distribution', 'use_command',
+ 'copy_xxmodule_c', 'fixup_build_ext',
# imported from this module for backport purposes
'unittest', 'requires_zlib', 'skip_2to3_optimize', 'skip_unless_symlink',
]
@@ -291,6 +295,15 @@ def create_distribution(configfiles=()):
return d
+def use_command(testcase, fullname):
+ """Register command at *fullname* for the duration of a test."""
+ set_command(fullname)
+ # XXX maybe set_command should return the class object
+ name = resolve_name(fullname).get_command_name()
+ # XXX maybe we need a public API to remove commands
+ testcase.addCleanup(_COMMANDS.__delitem__, name)
+
+
def fake_dec(*args, **kw):
"""Fake decorator"""
def _wrap(func):
diff --git a/distutils2/tests/test_config.py b/distutils2/tests/test_config.py
index 16fbda4..987740b 100644
--- a/distutils2/tests/test_config.py
+++ b/distutils2/tests/test_config.py
@@ -2,7 +2,6 @@
"""Tests for distutils2.config."""
import os
import sys
-from StringIO import StringIO
from distutils2 import command
from distutils2.dist import Distribution
@@ -184,13 +183,14 @@ class FooBarBazTest(object):
def __init__(self, dist):
self.distribution = dist
+ self._record = []
@classmethod
def get_command_name(cls):
return 'foo'
def run(self):
- self.distribution.foo_was_here = True
+ self._record.append('foo has run')
def nothing(self):
pass
@@ -210,21 +210,11 @@ class ConfigTestCase(support.TempdirManager,
def setUp(self):
super(ConfigTestCase, self).setUp()
- self.addCleanup(setattr, sys, 'stdout', sys.stdout)
- self.addCleanup(setattr, sys, 'stderr', sys.stderr)
- sys.stdout = StringIO()
- sys.stderr = StringIO()
-
- self.addCleanup(os.chdir, os.getcwd())
tempdir = self.mkdtemp()
self.working_dir = os.getcwd()
os.chdir(tempdir)
self.tempdir = tempdir
- def tearDown(self):
- os.chdir(self.working_dir)
- super(ConfigTestCase, self).tearDown()
-
def write_setup(self, kwargs=None):
opts = {'description-file': 'README', 'extra-files': '',
'setup-hooks': 'distutils2.tests.test_config.version_hook'}
@@ -379,7 +369,7 @@ class ConfigTestCase(support.TempdirManager,
self.assertIn('hooks', sys.modules)
def test_missing_setup_hook_warns(self):
- self.write_setup({'setup-hooks': 'this.does._not.exist'})
+ self.write_setup({'setup-hooks': 'does._not.exist'})
self.write_file('README', 'yeah')
self.get_dist()
logs = self.get_logs()
@@ -495,10 +485,12 @@ class ConfigTestCase(support.TempdirManager,
self.write_file((pkg, '__init__.py'), '#')
# try to run the install command to see if foo is called
+ self.addCleanup(command._COMMANDS.__delitem__, 'foo')
dist = self.get_dist()
- self.assertIn('foo', command.get_command_names())
- self.assertEqual('FooBarBazTest',
- dist.get_command_obj('foo').__class__.__name__)
+ dist.run_command('install_dist')
+ cmd = dist.get_command_obj('foo')
+ self.assertEqual(cmd.__class__.__name__, 'FooBarBazTest')
+ self.assertEqual(cmd._record, ['foo has run'])
def test_suite():
diff --git a/distutils2/tests/test_dist.py b/distutils2/tests/test_dist.py
index 0dc44f3..968ce97 100644
--- a/distutils2/tests/test_dist.py
+++ b/distutils2/tests/test_dist.py
@@ -6,27 +6,32 @@ import textwrap
import distutils2.dist
from distutils2.dist import Distribution
-from distutils2.command import set_command
from distutils2.command.cmd import Command
from distutils2.errors import PackagingModuleError, PackagingOptionError
from distutils2.tests import captured_stdout
from distutils2.tests import support, unittest
-from distutils2.tests.support import create_distribution
+from distutils2.tests.support import create_distribution, use_command
from distutils2.tests.support import unload
class test_dist(Command):
- """Sample distutils2 extension command."""
+ """Custom command used for testing."""
user_options = [
- ("sample-option=", "S", "help text"),
+ ('sample-option=', 'S',
+ "help text"),
]
def initialize_options(self):
self.sample_option = None
+ self._record = []
def finalize_options(self):
- pass
+ if self.sample_option is None:
+ self.sample_option = 'default value'
+
+ def run(self):
+ self._record.append('test_dist has run')
class DistributionTestCase(support.TempdirManager,
@@ -38,6 +43,8 @@ class DistributionTestCase(support.TempdirManager,
def setUp(self):
super(DistributionTestCase, self).setUp()
+ # XXX this is ugly, we should fix the functions to accept args
+ # (defaulting to sys.argv)
self.argv = sys.argv, sys.argv[:]
del sys.argv[1:]
@@ -181,7 +188,8 @@ class DistributionTestCase(support.TempdirManager,
self.write_file((temp_home, "config2.cfg"),
'[test_dist]\npre-hook.b = type')
- set_command('distutils2.tests.test_dist.test_dist')
+ use_command(self, 'distutils2.tests.test_dist.test_dist')
+
dist = create_distribution(config_files)
cmd = dist.get_command_obj("test_dist")
self.assertEqual(cmd.pre_hook, {"a": 'type', "b": 'type'})
@@ -209,7 +217,7 @@ class DistributionTestCase(support.TempdirManager,
record.append('post-%s' % cmd.get_command_name())
'''))
- set_command('distutils2.tests.test_dist.test_dist')
+ use_command(self, 'distutils2.tests.test_dist.test_dist')
d = create_distribution([config_file])
cmd = d.get_command_obj("test_dist")
@@ -236,7 +244,7 @@ class DistributionTestCase(support.TempdirManager,
[test_dist]
pre-hook.test = nonexistent.dotted.name'''))
- set_command('distutils2.tests.test_dist.test_dist')
+ use_command(self, 'distutils2.tests.test_dist.test_dist')
d = create_distribution([config_file])
cmd = d.get_command_obj("test_dist")
cmd.ensure_finalized()
@@ -251,7 +259,7 @@ class DistributionTestCase(support.TempdirManager,
[test_dist]
pre-hook.test = distutils2.tests.test_dist.__doc__'''))
- set_command('distutils2.tests.test_dist.test_dist')
+ use_command(self, 'distutils2.tests.test_dist.test_dist')
d = create_distribution([config_file])
cmd = d.get_command_obj("test_dist")
cmd.ensure_finalized()