mirror of
https://github.com/pypa/pip
synced 2023-12-13 21:30:23 +01:00
refactor tests with diff_state
This commit is contained in:
parent
fc6c5821c3
commit
d9d32ae25b
|
@ -40,6 +40,44 @@ def write_file(filename, text):
|
|||
def get_env():
|
||||
return env
|
||||
|
||||
# FIXME ScriptTest does something similar, but only within a single
|
||||
# ProcResult; this generalizes it so states can be compared across
|
||||
# multiple commands. Maybe should be rolled into ScriptTest?
|
||||
def diff_states(start, end, ignore_keys=None):
|
||||
"""
|
||||
Differences two "filesystem states" as represented by dictionaries
|
||||
of FoundFile and FoundDir objects.
|
||||
|
||||
Returns a dictionary with following keys:
|
||||
|
||||
``deleted``
|
||||
Dictionary of files/directories found only in the start state.
|
||||
|
||||
``created``
|
||||
Dictionary of files/directories found only in the end state.
|
||||
|
||||
``updated``
|
||||
Dictionary of files whose size has changed (FIXME not entirely
|
||||
reliable, but comparing contents is not possible because
|
||||
FoundFile.bytes is lazy, and comparing mtime doesn't help if
|
||||
we want to know if a file has been returned to its earlier
|
||||
state).
|
||||
|
||||
Ignores mtime and other file attributes; only presence/absence and
|
||||
size are considered.
|
||||
|
||||
"""
|
||||
ignore_keys = ignore_keys and set(ignore_keys) or set()
|
||||
start_keys = set(start.keys())
|
||||
end_keys = set(end.keys())
|
||||
deleted = dict([(k, start[k]) for k in start_keys.difference(end_keys).difference(ignore_keys)])
|
||||
created = dict([(k, end[k]) for k in end_keys.difference(start_keys).difference(ignore_keys)])
|
||||
updated = {}
|
||||
for k in start_keys.intersection(end_keys).difference(ignore_keys):
|
||||
if (start[k].size != end[k].size):
|
||||
updated[k] = end[k]
|
||||
return dict(deleted=deleted, created=created, updated=updated)
|
||||
|
||||
import optparse
|
||||
parser = optparse.OptionParser(usage='%prog [OPTIONS] [TEST_FILE...]')
|
||||
parser.add_option('--first', action='store_true',
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
Basic setup::
|
||||
|
||||
>>> from __main__ import here, reset_env, run_pip, pyversion, lib_py, get_env
|
||||
>>> try:
|
||||
... s = set()
|
||||
... except NameError:
|
||||
... from sets import Set as set
|
||||
>>> from __main__ import here, reset_env, run_pip, pyversion, lib_py, get_env, diff_states
|
||||
>>> easy_install_pth = lib_py + 'site-packages/easy-install.pth'
|
||||
|
||||
Simple install and uninstall::
|
||||
|
@ -13,10 +9,8 @@ Simple install and uninstall::
|
|||
>>> result = run_pip('install', 'INITools==0.2', expect_error=True)
|
||||
>>> assert (lib_py + 'site-packages/initools') in result.files_created, sorted(result.files_created.keys())
|
||||
>>> result2 = run_pip('uninstall', 'INITools', '-y', expect_error=True)
|
||||
>>> diff = set(result.files_before.keys()).symmetric_difference(set(result2.files_after.keys()))
|
||||
>>> try: diff.remove('build')
|
||||
... except KeyError: pass
|
||||
>>> assert(not len(diff)), diff
|
||||
>>> diff_states(result.files_before, result2.files_after, ignore_keys=['build']).values()
|
||||
[{}, {}, {}]
|
||||
|
||||
Uninstall an easy_installed package::
|
||||
|
||||
|
@ -25,11 +19,8 @@ Uninstall an easy_installed package::
|
|||
>>> result = env.run('%s/bin/easy_install' % env.base_path, 'INITools')
|
||||
>>> assert('INITools' in result.files_updated[easy_install_pth].bytes), result.files_after[easy-install_pth].bytes
|
||||
>>> result2 = run_pip('uninstall', 'INITools', '-y', expect_error=True)
|
||||
>>> diff = set(result.files_before.keys()).symmetric_difference(set(result2.files_after.keys()))
|
||||
>>> try: diff.remove('build')
|
||||
... except KeyError: pass
|
||||
>>> assert(not len(diff)), diff
|
||||
>>> assert(result.files_before[easy_install_pth].bytes == result2.files_after[easy_install_pth].bytes), result2.files_after[easy_install_pth].bytes
|
||||
>>> diff_states(result.files_before, result2.files_after, ignore_keys=['build']).values()
|
||||
[{}, {}, {}]
|
||||
|
||||
Uninstall a package with more files (script entry points, etc)::
|
||||
|
||||
|
@ -37,22 +28,16 @@ Uninstall a package with more files (script entry points, etc)::
|
|||
>>> result = run_pip('install', 'virtualenv', expect_error=True)
|
||||
>>> assert ('bin/virtualenv') in result.files_created, sorted(result.files_created.keys())
|
||||
>>> result2 = run_pip('uninstall', 'virtualenv', '-y', expect_error=True)
|
||||
>>> diff = set(result.files_before.keys()).symmetric_difference(set(result2.files_after.keys()))
|
||||
>>> try: diff.remove('build'); diff.remove(lib_py + 'site-packages/support-files')
|
||||
... except KeyError: pass
|
||||
>>> assert(not len(diff)), diff
|
||||
>>> diff_states(result.files_before, result2.files_after,
|
||||
... ignore_keys=['build', lib_py + 'site-packages/support-files']).values()
|
||||
[{}, {}, {}]
|
||||
|
||||
Uninstall an editable installation from svn::
|
||||
|
||||
>>> reset_env()
|
||||
>>> result = run_pip('install', '-e', 'svn+http://svn.colorstudy.com/INITools/trunk#egg=initools-dev', expect_error=True)
|
||||
>>> egg_link = result.files_created[lib_py + 'site-packages/INITools.egg-link']
|
||||
>>> # FIXME: I don't understand why there's a trailing . here:
|
||||
>>> egg_link.bytes
|
||||
'.../test-scratch/src/initools\n.'
|
||||
>>> result2 = run_pip('uninstall', '-y', 'initools', expect_error=True)
|
||||
>>> diff = set(result.files_before.keys()).symmetric_difference(set(result2.files_after.keys()))
|
||||
>>> try: diff.remove('build')
|
||||
... except KeyError: pass
|
||||
>>> assert(not len(diff)), diff
|
||||
>>> assert(result.files_before[easy_install_pth].bytes == result2.files_after[easy_install_pth].bytes), result2.files_after[easy_install_pth].bytes
|
||||
>>> diff_states(result.files_before, result2.files_after).values()
|
||||
[{}, {}, {}]
|
||||
|
||||
|
|
Loading…
Reference in a new issue