|
2 | 2 | import os
|
3 | 3 | import time
|
4 | 4 |
|
5 |
| -from mock import patch |
| 5 | +import pytest |
| 6 | +from mock import Mock, patch |
6 | 7 |
|
7 | 8 | from pip._internal.cli.base_command import Command
|
| 9 | +from pip._internal.cli.status_codes import SUCCESS |
| 10 | +from pip._internal.utils import temp_dir |
8 | 11 | from pip._internal.utils.logging import BrokenStdoutLoggingError
|
| 12 | +from pip._internal.utils.temp_dir import TempDirectory |
9 | 13 |
|
10 | 14 |
|
11 | 15 | class FakeCommand(Command):
|
@@ -145,3 +149,65 @@ def test_unicode_messages(self, tmpdir):
|
145 | 149 | cmd = FakeCommandWithUnicode()
|
146 | 150 | log_path = tmpdir.joinpath('log')
|
147 | 151 | cmd.main(['fake_unicode', '--log', log_path])
|
| 152 | + |
| 153 | + |
| 154 | +@pytest.mark.no_auto_tempdir_manager |
| 155 | +def test_base_command_provides_tempdir_helpers(): |
| 156 | + assert temp_dir._tempdir_manager is None |
| 157 | + assert temp_dir._tempdir_registry is None |
| 158 | + |
| 159 | + def assert_helpers_set(options, args): |
| 160 | + assert temp_dir._tempdir_manager is not None |
| 161 | + assert temp_dir._tempdir_registry is not None |
| 162 | + |
| 163 | + c = Command("fake", "fake") |
| 164 | + c.run = Mock(side_effect=assert_helpers_set) |
| 165 | + assert c.main(["fake"]) == SUCCESS |
| 166 | + c.run.assert_called_once() |
| 167 | + |
| 168 | + |
| 169 | +not_deleted = "not_deleted" |
| 170 | + |
| 171 | + |
| 172 | +@pytest.mark.parametrize("kind,exists", [ |
| 173 | + (not_deleted, True), ("deleted", False) |
| 174 | +]) |
| 175 | +@pytest.mark.no_auto_tempdir_manager |
| 176 | +def test_base_command_global_tempdir_cleanup(kind, exists): |
| 177 | + assert temp_dir._tempdir_manager is None |
| 178 | + assert temp_dir._tempdir_registry is None |
| 179 | + |
| 180 | + class Holder(object): |
| 181 | + value = None |
| 182 | + |
| 183 | + def create_temp_dirs(options, args): |
| 184 | + c.tempdir_registry.set_delete(not_deleted, False) |
| 185 | + Holder.value = TempDirectory(kind=kind, globally_managed=True).path |
| 186 | + |
| 187 | + c = Command("fake", "fake") |
| 188 | + c.run = Mock(side_effect=create_temp_dirs) |
| 189 | + assert c.main(["fake"]) == SUCCESS |
| 190 | + c.run.assert_called_once() |
| 191 | + assert os.path.exists(Holder.value) == exists |
| 192 | + |
| 193 | + |
| 194 | +@pytest.mark.parametrize("kind,exists", [ |
| 195 | + (not_deleted, True), ("deleted", False) |
| 196 | +]) |
| 197 | +@pytest.mark.no_auto_tempdir_manager |
| 198 | +def test_base_command_local_tempdir_cleanup(kind, exists): |
| 199 | + assert temp_dir._tempdir_manager is None |
| 200 | + assert temp_dir._tempdir_registry is None |
| 201 | + |
| 202 | + def create_temp_dirs(options, args): |
| 203 | + c.tempdir_registry.set_delete(not_deleted, False) |
| 204 | + |
| 205 | + with TempDirectory(kind=kind) as d: |
| 206 | + path = d.path |
| 207 | + assert os.path.exists(path) |
| 208 | + assert os.path.exists(path) == exists |
| 209 | + |
| 210 | + c = Command("fake", "fake") |
| 211 | + c.run = Mock(side_effect=create_temp_dirs) |
| 212 | + assert c.main(["fake"]) == SUCCESS |
| 213 | + c.run.assert_called_once() |
0 commit comments