summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/api_tests.txt116
-rw-r--r--tests/install_test.py75
-rw-r--r--tests/manual_test.py28
-rw-r--r--tests/test_ez_setup.py (renamed from tests/test_distribute_setup.py)18
-rw-r--r--tests/test_pkg_resources.py115
5 files changed, 186 insertions, 166 deletions
diff --git a/tests/api_tests.txt b/tests/api_tests.txt
index 65443e09..86ca245d 100644
--- a/tests/api_tests.txt
+++ b/tests/api_tests.txt
@@ -119,7 +119,7 @@ editing are also a Distribution. (And, with a little attention to the
directory names used, and including some additional metadata, such a
"development distribution" can be made pluggable as well.)
- >>> from pkg_resources import WorkingSet, VersionConflict
+ >>> from pkg_resources import WorkingSet
A working set's entries are the sys.path entries that correspond to the active
distributions. By default, the working set's entries are the items on
@@ -170,8 +170,8 @@ You can append a path entry to a working set using ``add_entry()``::
>>> ws.entries
['http://example.com/something']
>>> ws.add_entry(pkg_resources.__file__)
- >>> ws.entries == ['http://example.com/something', pkg_resources.__file__]
- True
+ >>> ws.entries
+ ['http://example.com/something', '...pkg_resources.py...']
Multiple additions result in multiple entries, even if the entry is already in
the working set (because ``sys.path`` can contain the same entry more than
@@ -210,9 +210,12 @@ working set triggers a ``pkg_resources.VersionConflict`` error:
>>> try:
... ws.find(Requirement.parse("Bar==1.0"))
- ... except VersionConflict:
- ... print('ok')
- ok
+ ... except pkg_resources.VersionConflict:
+ ... exc = sys.exc_info()[1]
+ ... print(str(exc))
+ ... else:
+ ... raise AssertionError("VersionConflict was not raised")
+ (Bar 0.9 (http://example.com/something), Requirement.parse('Bar==1.0'))
You can subscribe a callback function to receive notifications whenever a new
distribution is added to a working set. The callback is immediately invoked
@@ -222,14 +225,14 @@ again for new distributions added thereafter::
>>> def added(dist): print("Added %s" % dist)
>>> ws.subscribe(added)
Added Bar 0.9
- >>> foo12 = Distribution(project_name="Foo", version="1.2", location="f12")
+ >>> foo12 = Distribution(project_name="Foo", version="1.2", location="f12")
>>> ws.add(foo12)
Added Foo 1.2
Note, however, that only the first distribution added for a given project name
will trigger a callback, even during the initial ``subscribe()`` callback::
- >>> foo14 = Distribution(project_name="Foo", version="1.4", location="f14")
+ >>> foo14 = Distribution(project_name="Foo", version="1.4", location="f14")
>>> ws.add(foo14) # no callback, because Foo 1.2 is already active
>>> ws = WorkingSet([])
@@ -237,7 +240,7 @@ will trigger a callback, even during the initial ``subscribe()`` callback::
>>> ws.add(foo14)
>>> ws.subscribe(added)
Added Foo 1.2
-
+
And adding a callback more than once has no effect, either::
>>> ws.subscribe(added) # no callbacks
@@ -260,7 +263,7 @@ Finding Plugins
>>> plugins.add(foo12)
>>> plugins.add(foo14)
>>> plugins.add(just_a_test)
-
+
In the simplest case, we just get the newest version of each distribution in
the plugin environment::
@@ -318,7 +321,7 @@ number does not matter::
>>> cp("macosx-9.5-ppc", reqd)
False
-Backwards compatibility for packages made via earlier versions of
+Backwards compatibility for packages made via earlier versions of
setuptools is provided as well::
>>> cp("darwin-8.2.0-Power_Macintosh", reqd)
@@ -328,3 +331,94 @@ setuptools is provided as well::
>>> cp("darwin-8.2.0-Power_Macintosh", "macosx-10.3-ppc")
False
+
+Environment Markers
+-------------------
+
+ >>> from pkg_resources import invalid_marker as im, evaluate_marker as em
+ >>> import os
+
+ >>> print(im("sys_platform"))
+ Comparison or logical expression expected
+
+ >>> print(im("sys_platform==")) # doctest: +ELLIPSIS
+ unexpected EOF while parsing (...line 1)
+
+ >>> print(im("sys_platform=='win32'"))
+ False
+
+ >>> print(im("sys=='x'"))
+ Unknown name 'sys'
+
+ >>> print(im("(extra)"))
+ Comparison or logical expression expected
+
+ >>> print(im("(extra")) # doctest: +ELLIPSIS
+ unexpected EOF while parsing (...line 1)
+
+ >>> print(im("os.open('foo')=='y'"))
+ Language feature not supported in environment markers
+
+ >>> print(im("'x'=='y' and os.open('foo')=='y'")) # no short-circuit!
+ Language feature not supported in environment markers
+
+ >>> print(im("'x'=='x' or os.open('foo')=='y'")) # no short-circuit!
+ Language feature not supported in environment markers
+
+ >>> print(im("'x' < 'y'"))
+ '<' operator not allowed in environment markers
+
+ >>> print(im("'x' < 'y' < 'z'"))
+ Chained comparison not allowed in environment markers
+
+ >>> print(im("r'x'=='x'"))
+ Only plain strings allowed in environment markers
+
+ >>> print(im("'''x'''=='x'"))
+ Only plain strings allowed in environment markers
+
+ >>> print(im('"""x"""=="x"'))
+ Only plain strings allowed in environment markers
+
+ >>> print(im(r"'x\n'=='x'"))
+ Only plain strings allowed in environment markers
+
+ >>> print(im("os.open=='y'"))
+ Language feature not supported in environment markers
+
+ >>> em('"x"=="x"')
+ True
+
+ >>> em('"x"=="y"')
+ False
+
+ >>> em('"x"=="y" and "x"=="x"')
+ False
+
+ >>> em('"x"=="y" or "x"=="x"')
+ True
+
+ >>> em('"x"=="y" and "x"=="q" or "z"=="z"')
+ True
+
+ >>> em('"x"=="y" and ("x"=="q" or "z"=="z")')
+ False
+
+ >>> em('"x"=="y" and "z"=="z" or "x"=="q"')
+ False
+
+ >>> em('"x"=="x" and "z"=="z" or "x"=="q"')
+ True
+
+ >>> em("sys_platform=='win32'") == (sys.platform=='win32')
+ True
+
+ >>> em("'x' in 'yx'")
+ True
+
+ >>> em("'yx' in 'x'")
+ False
+
+
+
+
diff --git a/tests/install_test.py b/tests/install_test.py
deleted file mode 100644
index 97b79933..00000000
--- a/tests/install_test.py
+++ /dev/null
@@ -1,75 +0,0 @@
-import sys
-import os
-from setuptools.compat import urllib2
-
-if os.path.exists('distribute_setup.py'):
- print('distribute_setup.py exists in the current dir, aborting')
- sys.exit(2)
-
-print('**** Starting Test')
-print('\n\n')
-
-is_jython = sys.platform.startswith('java')
-if is_jython:
- import subprocess
-
-print('Downloading bootstrap')
-file = urllib2.urlopen('http://nightly.ziade.org/distribute_setup.py')
-f = open('distribute_setup.py', 'w')
-f.write(file.read())
-f.close()
-
-# running it
-args = [sys.executable] + ['distribute_setup.py']
-if is_jython:
- res = subprocess.call(args)
-else:
- res = os.spawnv(os.P_WAIT, sys.executable, args)
-
-if res != 0:
- print('**** Test failed, please send me the output at tarek@ziade.org')
- os.remove('distribute_setup.py')
- sys.exit(2)
-
-# now checking if Distribute is installed
-script = """\
-import sys
-try:
- import setuptools
-except ImportError:
- sys.exit(0)
-
-sys.exit(hasattr(setuptools, "_distribute"))
-"""
-
-root = 'script'
-seed = 0
-script_name = '%s%d.py' % (root, seed)
-
-while os.path.exists(script_name):
- seed += 1
- script_name = '%s%d.py' % (root, seed)
-
-f = open(script_name, 'w')
-try:
- f.write(script)
-finally:
- f.close()
-
-try:
- args = [sys.executable] + [script_name]
- if is_jython:
- res = subprocess.call(args)
- else:
- res = os.spawnv(os.P_WAIT, sys.executable, args)
-
- print('\n\n')
- if res:
- print('**** Test is OK')
- else:
- print('**** Test failed, please send me the output at tarek@ziade.org')
-finally:
- if os.path.exists(script_name):
- os.remove(script_name)
- os.remove('distribute_setup.py')
-
diff --git a/tests/manual_test.py b/tests/manual_test.py
index 223567f4..3eab99e1 100644
--- a/tests/manual_test.py
+++ b/tests/manual_test.py
@@ -51,9 +51,8 @@ eggs =
extensions
"""
-BOOTSTRAP = 'http://python-distribute.org/bootstrap.py'
+BOOTSTRAP = 'http://downloads.buildout.org/1/bootstrap.py'
PYVER = sys.version.split()[0][:3]
-DEV_URL = 'http://bitbucket.org/tarek/distribute/get/0.6-maintenance.zip#egg=distribute-dev'
_VARS = {'base': '.',
'py_version_short': PYVER}
@@ -66,26 +65,25 @@ else:
@tempdir
def test_virtualenv():
- """virtualenv with distribute"""
+ """virtualenv with setuptools"""
purelib = os.path.abspath(Template(PURELIB).substitute(**_VARS))
- _system_call('virtualenv', '--no-site-packages', '.', '--distribute')
- _system_call('bin/easy_install', 'distribute==dev')
+ _system_call('virtualenv', '--no-site-packages', '.')
+ _system_call('bin/easy_install', 'setuptools==dev')
# linux specific
site_pkg = os.listdir(purelib)
site_pkg.sort()
- assert 'distribute' in site_pkg[0]
+ assert 'setuptools' in site_pkg[0]
easy_install = os.path.join(purelib, 'easy-install.pth')
with open(easy_install) as f:
res = f.read()
- assert 'distribute' in res
- assert 'setuptools' not in res
+ assert 'setuptools' in res
@tempdir
def test_full():
"""virtualenv + pip + buildout"""
_system_call('virtualenv', '--no-site-packages', '.')
- _system_call('bin/easy_install', '-q', 'distribute==dev')
- _system_call('bin/easy_install', '-qU', 'distribute==dev')
+ _system_call('bin/easy_install', '-q', 'setuptools==dev')
+ _system_call('bin/easy_install', '-qU', 'setuptools==dev')
_system_call('bin/easy_install', '-q', 'pip')
_system_call('bin/pip', 'install', '-q', 'zc.buildout')
@@ -95,16 +93,16 @@ def test_full():
with open('bootstrap.py', 'w') as f:
f.write(urlopen(BOOTSTRAP).read())
- _system_call('bin/python', 'bootstrap.py', '--distribute')
+ _system_call('bin/python', 'bootstrap.py')
_system_call('bin/buildout', '-q')
eggs = os.listdir('eggs')
eggs.sort()
assert len(eggs) == 3
- assert eggs[0].startswith('distribute')
- assert eggs[1:] == ['extensions-0.3-py2.6.egg',
- 'zc.recipe.egg-1.2.2-py2.6.egg']
+ assert eggs[1].startswith('setuptools')
+ del eggs[1]
+ assert eggs == ['extensions-0.3-py2.6.egg',
+ 'zc.recipe.egg-1.2.2-py2.6.egg']
if __name__ == '__main__':
test_virtualenv()
test_full()
-
diff --git a/tests/test_distribute_setup.py b/tests/test_ez_setup.py
index 4f86c335..26881f52 100644
--- a/tests/test_distribute_setup.py
+++ b/tests/test_ez_setup.py
@@ -9,10 +9,9 @@ CURDIR = os.path.abspath(os.path.dirname(__file__))
TOPDIR = os.path.split(CURDIR)[0]
sys.path.insert(0, TOPDIR)
-from distribute_setup import (use_setuptools, _build_egg, _python_cmd,
- _do_download, _install, DEFAULT_URL,
- DEFAULT_VERSION)
-import distribute_setup
+from ez_setup import (use_setuptools, _build_egg, _python_cmd, _do_download,
+ _install, DEFAULT_URL, DEFAULT_VERSION)
+import ez_setup
class TestSetup(unittest.TestCase):
@@ -54,20 +53,11 @@ class TestSetup(unittest.TestCase):
def test_install(self):
def _faked(*args):
return True
- distribute_setup.python_cmd = _faked
+ ez_setup.python_cmd = _faked
_install(self.tarball)
def test_use_setuptools(self):
self.assertEqual(use_setuptools(), None)
- # make sure fake_setuptools is not called by default
- import pkg_resources
- del pkg_resources._distribute
- def fake_setuptools(*args):
- raise AssertionError
-
- pkg_resources._fake_setuptools = fake_setuptools
- use_setuptools()
-
if __name__ == '__main__':
unittest.main()
diff --git a/tests/test_pkg_resources.py b/tests/test_pkg_resources.py
index 7009b4ab..dfa27120 100644
--- a/tests/test_pkg_resources.py
+++ b/tests/test_pkg_resources.py
@@ -5,57 +5,70 @@ import zipfile
import pkg_resources
+try:
+ unicode
+except NameError:
+ unicode = str
+
class EggRemover(unicode):
- def __call__(self):
- if self in sys.path:
- sys.path.remove(self)
- if os.path.exists(self):
- os.remove(self)
+ def __call__(self):
+ if self in sys.path:
+ sys.path.remove(self)
+ if os.path.exists(self):
+ os.remove(self)
class TestZipProvider(object):
- finalizers = []
-
- @classmethod
- def setup_class(cls):
- "create a zip egg and add it to sys.path"
- egg = tempfile.NamedTemporaryFile(suffix='.egg', delete=False)
- zip_egg = zipfile.ZipFile(egg, 'w')
- zip_info = zipfile.ZipInfo()
- zip_info.filename = 'mod.py'
- zip_info.date_time = 2013, 5, 12, 13, 25, 0
- zip_egg.writestr(zip_info, 'x = 3\n')
- zip_info = zipfile.ZipInfo()
- zip_info.filename = 'data.dat'
- zip_info.date_time = 2013, 5, 12, 13, 25, 0
- zip_egg.writestr(zip_info, 'hello, world!')
- zip_egg.close()
- egg.close()
-
- sys.path.append(egg.name)
- cls.finalizers.append(EggRemover(egg.name))
-
- @classmethod
- def teardown_class(cls):
- for finalizer in cls.finalizers:
- finalizer()
-
- def test_resource_filename_rewrites_on_change(self):
- """
- If a previous call to get_resource_filename has saved the file, but
- the file has been subsequently mutated with different file of the
- same size and modification time, it should not be overwritten on a
- subsequent call to get_resource_filename.
- """
- import mod
- manager = pkg_resources.ResourceManager()
- zp = pkg_resources.ZipProvider(mod)
- filename = zp.get_resource_filename(manager, 'data.dat')
- assert os.stat(filename).st_mtime == 1368379500
- f = open(filename, 'wb')
- f.write('hello, world?')
- f.close()
- os.utime(filename, (1368379500, 1368379500))
- filename = zp.get_resource_filename(manager, 'data.dat')
- f = open(filename)
- assert f.read() == 'hello, world!'
- manager.cleanup_resources()
+ finalizers = []
+
+ @classmethod
+ def setup_class(cls):
+ "create a zip egg and add it to sys.path"
+ egg = tempfile.NamedTemporaryFile(suffix='.egg', delete=False)
+ zip_egg = zipfile.ZipFile(egg, 'w')
+ zip_info = zipfile.ZipInfo()
+ zip_info.filename = 'mod.py'
+ zip_info.date_time = 2013, 5, 12, 13, 25, 0
+ zip_egg.writestr(zip_info, 'x = 3\n')
+ zip_info = zipfile.ZipInfo()
+ zip_info.filename = 'data.dat'
+ zip_info.date_time = 2013, 5, 12, 13, 25, 0
+ zip_egg.writestr(zip_info, 'hello, world!')
+ zip_egg.close()
+ egg.close()
+
+ sys.path.append(egg.name)
+ cls.finalizers.append(EggRemover(egg.name))
+
+ @classmethod
+ def teardown_class(cls):
+ for finalizer in cls.finalizers:
+ finalizer()
+
+ def test_resource_filename_rewrites_on_change(self):
+ """
+ If a previous call to get_resource_filename has saved the file, but
+ the file has been subsequently mutated with different file of the
+ same size and modification time, it should not be overwritten on a
+ subsequent call to get_resource_filename.
+ """
+ import mod
+ manager = pkg_resources.ResourceManager()
+ zp = pkg_resources.ZipProvider(mod)
+ filename = zp.get_resource_filename(manager, 'data.dat')
+ assert os.stat(filename).st_mtime == 1368379500
+ f = open(filename, 'w')
+ f.write('hello, world?')
+ f.close()
+ os.utime(filename, (1368379500, 1368379500))
+ filename = zp.get_resource_filename(manager, 'data.dat')
+ f = open(filename)
+ assert f.read() == 'hello, world!'
+ manager.cleanup_resources()
+
+class TestResourceManager(object):
+ def test_get_cache_path(self):
+ mgr = pkg_resources.ResourceManager()
+ path = mgr.get_cache_path('foo')
+ type_ = str(type(path))
+ message = "Unexpected type from get_cache_path: " + type_
+ assert isinstance(path, (unicode, str)), message