2008-10-16 00:02:57 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import os, sys
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
pyversion = sys.version[:3]
|
|
|
|
lib_py = 'lib/python%s/' % pyversion
|
|
|
|
here = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
base_path = os.path.join(here, 'test-scratch')
|
2009-10-07 23:25:17 +02:00
|
|
|
download_cache = os.path.join(here, 'test-cache')
|
|
|
|
if not os.path.exists(download_cache):
|
|
|
|
os.makedirs(download_cache)
|
2008-10-16 00:02:57 +02:00
|
|
|
|
|
|
|
from scripttest import TestFileEnvironment
|
|
|
|
|
|
|
|
if 'PYTHONPATH' in os.environ:
|
|
|
|
del os.environ['PYTHONPATH']
|
|
|
|
|
2009-10-07 23:25:17 +02:00
|
|
|
try:
|
|
|
|
any
|
|
|
|
except NameError:
|
|
|
|
def any(seq):
|
|
|
|
for item in seq:
|
|
|
|
if item:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2009-10-24 15:24:08 +02:00
|
|
|
def clear_environ(environ):
|
|
|
|
return dict(((k, v) for k, v in environ.iteritems()
|
|
|
|
if not k.lower().startswith('pip_')))
|
|
|
|
|
2010-02-21 23:47:49 +01:00
|
|
|
env = None
|
2009-10-24 15:24:08 +02:00
|
|
|
def reset_env(environ=None):
|
2008-10-16 00:02:57 +02:00
|
|
|
global env
|
2009-10-24 15:24:08 +02:00
|
|
|
if not environ:
|
|
|
|
environ = os.environ.copy()
|
|
|
|
environ = clear_environ(environ)
|
|
|
|
environ['PIP_DOWNLOAD_CACHE'] = download_cache
|
2009-10-07 23:25:17 +02:00
|
|
|
environ['PIP_NO_INPUT'] = '1'
|
2010-02-24 11:24:54 +01:00
|
|
|
environ['PYTHONPATH'] = os.path.abspath(os.path.join(__file__, os.path.pardir, os.path.pardir))
|
2009-10-07 23:25:17 +02:00
|
|
|
env = TestFileEnvironment(base_path, ignore_hidden=False, environ=environ)
|
2008-12-17 19:46:48 +01:00
|
|
|
env.run(sys.executable, '-m', 'virtualenv', '--no-site-packages', env.base_path)
|
2009-10-20 17:36:53 +02:00
|
|
|
# make sure we have current setuptools to avoid svn incompatibilities
|
|
|
|
env.run('%s/bin/easy_install' % env.base_path, 'setuptools==0.6c11')
|
2009-11-20 08:06:01 +01:00
|
|
|
# Uninstall (kind of) pip, so PYTHONPATH can take effect:
|
|
|
|
env.run('%s/bin/easy_install' % env.base_path, '-m', 'pip')
|
2008-10-16 00:02:57 +02:00
|
|
|
env.run('mkdir', 'src')
|
|
|
|
|
2008-10-16 00:24:00 +02:00
|
|
|
def run_pip(*args, **kw):
|
2009-11-20 08:06:01 +01:00
|
|
|
args = (sys.executable, '-c', 'import pip; pip.main()', '-E', env.base_path) + args
|
2008-10-16 00:02:57 +02:00
|
|
|
#print >> sys.__stdout__, 'running', ' '.join(args)
|
2010-02-24 11:24:54 +01:00
|
|
|
if getattr(options, 'show_error', False):
|
2008-10-16 00:02:57 +02:00
|
|
|
kw['expect_error'] = True
|
|
|
|
result = env.run(*args, **kw)
|
2010-02-24 11:24:54 +01:00
|
|
|
if getattr(options, 'show_error', False) and result.returncode:
|
2008-10-16 00:02:57 +02:00
|
|
|
print result
|
|
|
|
return result
|
|
|
|
|
|
|
|
def write_file(filename, text):
|
|
|
|
f = open(os.path.join(base_path, filename), 'w')
|
|
|
|
f.write(text)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def get_env():
|
|
|
|
return env
|
|
|
|
|
2009-04-01 00:17:08 +02:00
|
|
|
# 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?
|
2009-04-06 19:59:20 +02:00
|
|
|
def diff_states(start, end, ignore=None):
|
2009-04-01 00:17:08 +02:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
2009-04-06 19:59:20 +02:00
|
|
|
ignore = ignore or []
|
|
|
|
start_keys = set([k for k in start.keys()
|
2009-09-06 19:16:02 +02:00
|
|
|
if not any([k.startswith(i) for i in ignore])])
|
2009-04-06 19:59:20 +02:00
|
|
|
end_keys = set([k for k in end.keys()
|
2009-09-06 19:16:02 +02:00
|
|
|
if not any([k.startswith(i) for i in ignore])])
|
2009-04-06 19:59:20 +02:00
|
|
|
deleted = dict([(k, start[k]) for k in start_keys.difference(end_keys)])
|
|
|
|
created = dict([(k, end[k]) for k in end_keys.difference(start_keys)])
|
2009-04-01 00:17:08 +02:00
|
|
|
updated = {}
|
2009-04-06 19:59:20 +02:00
|
|
|
for k in start_keys.intersection(end_keys):
|
2009-04-01 00:17:08 +02:00
|
|
|
if (start[k].size != end[k].size):
|
|
|
|
updated[k] = end[k]
|
|
|
|
return dict(deleted=deleted, created=created, updated=updated)
|
|
|
|
|
2008-10-16 00:02:57 +02:00
|
|
|
import optparse
|
|
|
|
parser = optparse.OptionParser(usage='%prog [OPTIONS] [TEST_FILE...]')
|
|
|
|
parser.add_option('--first', action='store_true',
|
|
|
|
help='Show only first failure')
|
|
|
|
parser.add_option('--diff', action='store_true',
|
|
|
|
help='Show diffs in doctest failures')
|
|
|
|
parser.add_option('--show-error', action='store_true',
|
2008-10-16 00:24:00 +02:00
|
|
|
help='Show the errors (use expect_error=True in run_pip)')
|
2008-10-16 00:02:57 +02:00
|
|
|
parser.add_option('-v', action='store_true',
|
|
|
|
help='Be verbose')
|
|
|
|
|
2010-02-21 23:47:49 +01:00
|
|
|
options = None
|
2008-10-16 00:02:57 +02:00
|
|
|
def main():
|
|
|
|
global options
|
|
|
|
options, args = parser.parse_args()
|
|
|
|
reset_env()
|
|
|
|
if not args:
|
2009-10-25 00:23:10 +02:00
|
|
|
args = ['test_basic.txt', 'test_requirements.txt', 'test_freeze.txt', 'test_proxy.txt', 'test_uninstall.txt', 'test_upgrade.txt', 'test_config.txt']
|
2008-10-16 00:02:57 +02:00
|
|
|
optionflags = doctest.ELLIPSIS
|
|
|
|
if options.first:
|
|
|
|
optionflags |= doctest.REPORT_ONLY_FIRST_FAILURE
|
|
|
|
if options.diff:
|
|
|
|
optionflags |= doctest.REPORT_UDIFF
|
|
|
|
for filename in args:
|
|
|
|
if not filename.endswith('.txt'):
|
|
|
|
filename += '.txt'
|
|
|
|
if not filename.startswith('test_'):
|
|
|
|
filename = 'test_' + filename
|
|
|
|
## FIXME: test for filename existance
|
|
|
|
failures, successes = doctest.testfile(filename, optionflags=optionflags)
|
|
|
|
if options.first and failures:
|
|
|
|
break
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|