-
Notifications
You must be signed in to change notification settings - Fork 3k
/
test_uninstall.py
241 lines (193 loc) · 9.6 KB
/
test_uninstall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from __future__ import with_statement
import textwrap
import sys
from os.path import join, abspath, normpath
from tempfile import mkdtemp
from mock import patch
from tests.test_pip import here, reset_env, run_pip, assert_all_changes, write_file, pyversion
from tests.local_repos import local_repo, local_checkout
from pip.util import rmtree
def test_simple_uninstall():
"""
Test simple install and uninstall.
"""
env = reset_env()
result = run_pip('install', 'INITools==0.2')
assert join(env.site_packages, 'initools') in result.files_created, sorted(result.files_created.keys())
#the import forces the generation of __pycache__ if the version of python supports it
env.run('python', '-c', "import initools")
result2 = run_pip('uninstall', 'INITools', '-y')
assert_all_changes(result, result2, [env.venv/'build', 'cache'])
def test_uninstall_with_scripts():
"""
Uninstall an easy_installed package with scripts.
"""
env = reset_env()
result = env.run('easy_install', 'PyLogo', expect_stderr=True)
easy_install_pth = env.site_packages/ 'easy-install.pth'
pylogo = sys.platform == 'win32' and 'pylogo' or 'PyLogo'
assert(pylogo in result.files_updated[easy_install_pth].bytes)
result2 = run_pip('uninstall', 'pylogo', '-y', expect_error=True)
assert_all_changes(result, result2, [env.venv/'build', 'cache'])
def test_uninstall_easy_install_after_import():
"""
Uninstall an easy_installed package after it's been imported
"""
env = reset_env()
result = env.run('easy_install', 'INITools==0.2', expect_stderr=True)
#the import forces the generation of __pycache__ if the version of python supports it
env.run('python', '-c', "import initools")
result2 = run_pip('uninstall', 'INITools', '-y')
assert_all_changes(result, result2, [env.venv/'build', 'cache'])
def test_uninstall_namespace_package():
"""
Uninstall a distribution with a namespace package without clobbering
the namespace and everything in it.
"""
env = reset_env()
result = run_pip('install', 'pd.requires==0.0.3', expect_error=True)
assert join(env.site_packages, 'pd') in result.files_created, sorted(result.files_created.keys())
result2 = run_pip('uninstall', 'pd.find', '-y', expect_error=True)
assert join(env.site_packages, 'pd') not in result2.files_deleted, sorted(result2.files_deleted.keys())
assert join(env.site_packages, 'pd', 'find') in result2.files_deleted, sorted(result2.files_deleted.keys())
def test_uninstall_overlapping_package():
"""
Uninstalling a distribution that adds modules to a pre-existing package
should only remove those added modules, not the rest of the existing
package.
See: GitHub issue #355 (pip uninstall removes things it didn't install)
"""
parent_pkg = abspath(join(here, 'packages', 'parent-0.1.tar.gz'))
child_pkg = abspath(join(here, 'packages', 'child-0.1.tar.gz'))
env = reset_env()
result1 = run_pip('install', parent_pkg, expect_error=False)
assert join(env.site_packages, 'parent') in result1.files_created, sorted(result1.files_created.keys())
result2 = run_pip('install', child_pkg, expect_error=False)
assert join(env.site_packages, 'child') in result2.files_created, sorted(result2.files_created.keys())
assert normpath(join(env.site_packages, 'parent/plugins/child_plugin.py')) in result2.files_created, sorted(result2.files_created.keys())
#the import forces the generation of __pycache__ if the version of python supports it
env.run('python', '-c', "import parent.plugins.child_plugin, child")
result3 = run_pip('uninstall', '-y', 'child', expect_error=False)
assert join(env.site_packages, 'child') in result3.files_deleted, sorted(result3.files_created.keys())
assert normpath(join(env.site_packages, 'parent/plugins/child_plugin.py')) in result3.files_deleted, sorted(result3.files_deleted.keys())
assert join(env.site_packages, 'parent') not in result3.files_deleted, sorted(result3.files_deleted.keys())
# Additional check: uninstalling 'child' should return things to the
# previous state, without unintended side effects.
assert_all_changes(result2, result3, [])
def test_uninstall_console_scripts():
"""
Test uninstalling a package with more files (console_script entry points, extra directories).
"""
env = reset_env()
args = ['install']
args.append('discover')
result = run_pip(*args, **{"expect_error": True})
assert env.bin/'discover'+env.exe in result.files_created, sorted(result.files_created.keys())
result2 = run_pip('uninstall', 'discover', '-y', expect_error=True)
assert_all_changes(result, result2, [env.venv/'build', 'cache'])
def test_uninstall_easy_installed_console_scripts():
"""
Test uninstalling package with console_scripts that is easy_installed.
"""
env = reset_env()
args = ['easy_install']
args.append('discover')
result = env.run(*args, **{"expect_stderr": True})
assert env.bin/'discover'+env.exe in result.files_created, sorted(result.files_created.keys())
result2 = run_pip('uninstall', 'discover', '-y')
assert_all_changes(result, result2, [env.venv/'build', 'cache'])
def test_uninstall_editable_from_svn():
"""
Test uninstalling an editable installation from svn.
"""
env = reset_env()
result = run_pip('install', '-e', '%s#egg=initools-dev' %
local_checkout('svn+http://svn.colorstudy.com/INITools/trunk'))
result.assert_installed('INITools')
result2 = run_pip('uninstall', '-y', 'initools')
assert (env.venv/'src'/'initools' in result2.files_after), 'oh noes, pip deleted my sources!'
assert_all_changes(result, result2, [env.venv/'src', env.venv/'build'])
def test_uninstall_editable_with_source_outside_venv():
"""
Test uninstalling editable install from existing source outside the venv.
"""
try:
temp = mkdtemp()
tmpdir = join(temp, 'virtualenv')
_test_uninstall_editable_with_source_outside_venv(tmpdir)
finally:
rmtree(temp)
def _test_uninstall_editable_with_source_outside_venv(tmpdir):
env = reset_env()
result = env.run('git', 'clone', local_repo('git+git://github.com/pypa/virtualenv'), tmpdir)
result2 = run_pip('install', '-e', tmpdir)
assert (join(env.site_packages, 'virtualenv.egg-link') in result2.files_created), list(result2.files_created.keys())
result3 = run_pip('uninstall', '-y', 'virtualenv', expect_error=True)
assert_all_changes(result, result3, [env.venv/'build'])
def test_uninstall_from_reqs_file():
"""
Test uninstall from a requirements file.
"""
env = reset_env()
write_file('test-req.txt', textwrap.dedent("""\
-e %s#egg=initools-dev
# and something else to test out:
PyLogo<0.4
""" % local_checkout('svn+http://svn.colorstudy.com/INITools/trunk')))
result = run_pip('install', '-r', 'test-req.txt')
write_file('test-req.txt', textwrap.dedent("""\
# -f, -i, and --extra-index-url should all be ignored by uninstall
-f http://www.example.com
-i http://www.example.com
--extra-index-url http://www.example.com
-e %s#egg=initools-dev
# and something else to test out:
PyLogo<0.4
""" % local_checkout('svn+http://svn.colorstudy.com/INITools/trunk')))
result2 = run_pip('uninstall', '-r', 'test-req.txt', '-y')
assert_all_changes(
result, result2, [env.venv/'build', env.venv/'src', env.scratch/'test-req.txt'])
def test_uninstall_as_egg():
"""
Test uninstall package installed as egg.
"""
env = reset_env()
to_install = abspath(join(here, 'packages', 'FSPkg'))
result = run_pip('install', to_install, '--egg', expect_error=False)
fspkg_folder = env.site_packages/'fspkg'
egg_folder = env.site_packages/'FSPkg-0.1dev-py%s.egg' % pyversion
assert fspkg_folder not in result.files_created, str(result.stdout)
assert egg_folder in result.files_created, str(result)
result2 = run_pip('uninstall', 'FSPkg', '-y', expect_error=True)
assert_all_changes(result, result2, [env.venv/'build', 'cache'])
@patch('pip.req.logger')
def test_uninstallpathset_no_paths(mock_logger):
"""
Test UninstallPathSet logs notification when there are no paths to uninstall
"""
from pip.req import UninstallPathSet
from pkg_resources import get_distribution
test_dist = get_distribution('pip')
# ensure that the distribution is "local"
with patch("pip.req.dist_is_local") as mock_dist_is_local:
mock_dist_is_local.return_value = True
uninstall_set = UninstallPathSet(test_dist)
uninstall_set.remove() #with no files added to set
mock_logger.notify.assert_any_call("Can't uninstall 'pip'. No files were found to uninstall.")
@patch('pip.req.logger')
def test_uninstallpathset_non_local(mock_logger):
"""
Test UninstallPathSet logs notification and returns (with no exception) when dist is non-local
"""
from pip.req import UninstallPathSet
from pkg_resources import get_distribution
test_dist = get_distribution('pip')
test_dist.location = "/NON_LOCAL"
# ensure that the distribution is "non-local"
# setting location isn't enough, due to egg-link file checking for
# develop-installs
with patch("pip.req.dist_is_local") as mock_dist_is_local:
mock_dist_is_local.return_value = False
uninstall_set = UninstallPathSet(test_dist)
uninstall_set.remove() #with no files added to set; which is the case when trying to remove non-local dists
mock_logger.notify.assert_any_call("Not uninstalling pip at /NON_LOCAL, outside environment %s" % sys.prefix)