|
| 1 | +from distutils import core, dist |
| 2 | +from distutils.command import clean |
| 3 | +import atexit |
| 4 | +import os.path |
| 5 | +import shutil |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | + |
| 9 | +if sys.version_info >= (2, 7): |
| 10 | + import unittest |
| 11 | +else: # noinspection PyPackageRequirements,PyUnresolvedReferences |
| 12 | + import unittest2 as unittest |
| 13 | + |
| 14 | +from setupext import janitor |
| 15 | + |
| 16 | + |
| 17 | +def run_setup(*command_line): |
| 18 | + """ |
| 19 | + Run the setup command with `command_line`. |
| 20 | +
|
| 21 | + :param command_line: the command line arguments to pass |
| 22 | + as the simulated command line |
| 23 | +
|
| 24 | + This function runs :func:`distutils.core.setup` after it |
| 25 | + configures an environment that mimics passing the specified |
| 26 | + command line arguments. The ``distutils`` internals are |
| 27 | + replaced with a :class:`~distutils.dist.Distribution` |
| 28 | + instance that will only execute the clean command. Other |
| 29 | + commands can be passed freely to simulate command line usage |
| 30 | + patterns. |
| 31 | +
|
| 32 | + """ |
| 33 | + class FakeDistribution(dist.Distribution): |
| 34 | + |
| 35 | + def __init__(self, *args, **kwargs): |
| 36 | + """Enable verbose output to make tests easier to debug.""" |
| 37 | + dist.Distribution.__init__(self, *args, **kwargs) |
| 38 | + self.verbose = 3 |
| 39 | + |
| 40 | + def run_command(self, command): |
| 41 | + """Only run the clean command.""" |
| 42 | + if command == 'clean': |
| 43 | + dist.Distribution.run_command(self, command) |
| 44 | + |
| 45 | + def parse_config_files(self, filenames=None): |
| 46 | + """Skip processing of configuration files.""" |
| 47 | + pass |
| 48 | + |
| 49 | + core.setup( |
| 50 | + distclass=FakeDistribution, |
| 51 | + script_name='testsetup.py', |
| 52 | + script_args=command_line, |
| 53 | + cmdclass={'clean': janitor.CleanCommand}, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +class CommandOptionTests(unittest.TestCase): |
| 58 | + |
| 59 | + def test_that_distutils_options_are_present(self): |
| 60 | + defined_options = set(t[0] for t in janitor.CleanCommand.user_options) |
| 61 | + superclass_options = set(t[0] for t in clean.clean.user_options) |
| 62 | + self.assertTrue(defined_options.issuperset(superclass_options)) |
| 63 | + |
| 64 | + def test_that_janitor_user_options_are_not_clean_options(self): |
| 65 | + self.assertIsNot( |
| 66 | + janitor.CleanCommand.user_options, clean.clean.user_options) |
| 67 | + |
| 68 | + def test_that_janitor_defines_dist_command(self): |
| 69 | + self.assertIn( |
| 70 | + ('dist', 'd', 'remove distribution directory'), |
| 71 | + janitor.CleanCommand.user_options) |
| 72 | + |
| 73 | + |
| 74 | +class DirectoryCleanupTests(unittest.TestCase): |
| 75 | + temp_dir = tempfile.mkdtemp() |
| 76 | + |
| 77 | + @classmethod |
| 78 | + def setUpClass(cls): |
| 79 | + super(DirectoryCleanupTests, cls).setUpClass() |
| 80 | + atexit.register(shutil.rmtree, cls.temp_dir) |
| 81 | + |
| 82 | + @classmethod |
| 83 | + def create_directory(cls, dir_name): |
| 84 | + return tempfile.mkdtemp(dir=cls.temp_dir, prefix=dir_name) |
| 85 | + |
| 86 | + def assert_path_does_not_exist(self, full_path): |
| 87 | + if os.path.exists(full_path): |
| 88 | + raise AssertionError('{0} should not exist'.format(full_path)) |
| 89 | + |
| 90 | + def assert_path_exists(self, full_path): |
| 91 | + if not os.path.exists(full_path): |
| 92 | + raise AssertionError('{0} should exist'.format(full_path)) |
| 93 | + |
| 94 | + def test_that_dist_directory_is_removed_for_sdist(self): |
| 95 | + dist_dir = self.create_directory('dist-dir') |
| 96 | + run_setup( |
| 97 | + 'sdist', '--dist-dir={0}'.format(dist_dir), |
| 98 | + 'clean', '--dist', |
| 99 | + ) |
| 100 | + self.assert_path_does_not_exist(dist_dir) |
| 101 | + |
| 102 | + def test_that_dist_directory_is_removed_for_bdist_dumb(self): |
| 103 | + dist_dir = self.create_directory('dist-dir') |
| 104 | + run_setup( |
| 105 | + 'bdist_dumb', '--dist-dir={0}'.format(dist_dir), |
| 106 | + 'clean', '--dist', |
| 107 | + ) |
| 108 | + self.assert_path_does_not_exist(dist_dir) |
| 109 | + |
| 110 | + def test_that_multiple_dist_directories_with_be_removed(self): |
| 111 | + sdist_dir = self.create_directory('sdist-dir') |
| 112 | + bdist_dir = self.create_directory('bdist_dumb') |
| 113 | + run_setup( |
| 114 | + 'sdist', '--dist-dir={0}'.format(sdist_dir), |
| 115 | + 'bdist_dumb', '--dist-dir={0}'.format(bdist_dir), |
| 116 | + 'clean', '--dist', |
| 117 | + ) |
| 118 | + self.assert_path_does_not_exist(sdist_dir) |
| 119 | + self.assert_path_does_not_exist(bdist_dir) |
| 120 | + |
| 121 | + def test_that_directories_are_not_removed_without_parameter(self): |
| 122 | + sdist_dir = self.create_directory('sdist-dir') |
| 123 | + bdist_dir = self.create_directory('bdist_dumb') |
| 124 | + run_setup( |
| 125 | + 'sdist', '--dist-dir={0}'.format(sdist_dir), |
| 126 | + 'bdist_dumb', '--dist-dir={0}'.format(bdist_dir), |
| 127 | + 'clean', |
| 128 | + ) |
| 129 | + self.assert_path_exists(sdist_dir) |
| 130 | + self.assert_path_exists(bdist_dir) |
0 commit comments