diff options
Diffstat (limited to 'Lib/packaging/tests/support.py')
| -rw-r--r-- | Lib/packaging/tests/support.py | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/Lib/packaging/tests/support.py b/Lib/packaging/tests/support.py index 66890c9..441efc0 100644 --- a/Lib/packaging/tests/support.py +++ b/Lib/packaging/tests/support.py @@ -41,6 +41,9 @@ import tempfile import sysconfig from packaging.dist import Distribution +from packaging.util import resolve_name +from packaging.command import set_command, _COMMANDS + from packaging.tests import unittest from test.support import requires_zlib, unlink @@ -49,9 +52,10 @@ __all__ = [ # TestCase mixins 'LoggingCatcher', 'TempdirManager', 'EnvironRestorer', # mocks - 'DummyCommand', 'TestDistribution', + 'DummyCommand', 'TestDistribution', 'Inputs', # 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', ] @@ -247,7 +251,7 @@ class DummyCommand: Useful for mocking one dependency command in the tests for another command, see e.g. the dummy build command in test_build_scripts. """ - # XXX does not work with dist.get_reinitialized_command, which typechecks + # XXX does not work with dist.reinitialize_command, which typechecks # and wants a finalized attribute def __init__(self, **kwargs): @@ -270,6 +274,22 @@ class TestDistribution(Distribution): return self._config_files +class Inputs: + """Fakes user inputs.""" + # TODO document usage + # TODO use context manager or something for auto cleanup + + def __init__(self, *answers): + self.answers = answers + self.index = 0 + + def __call__(self, prompt=''): + try: + return self.answers[self.index] + finally: + self.index += 1 + + def create_distribution(configfiles=()): """Prepares a distribution with given config files parsed.""" d = TestDistribution() @@ -280,6 +300,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): |
