From fde6e364a497cd44d049df17c93d1fd05ec09f90 Mon Sep 17 00:00:00 2001 From: Ron DuPlain Date: Sat, 7 Jan 2012 17:55:02 -0500 Subject: [PATCH] Update tests for new module helpers. --- flask/testsuite/config.py | 117 ++++++++++-------- .../lib/python2.5/site-packages/site_app.py | 3 + .../site-packages/site_package/__init__.py | 3 + flask/testsuite/test_apps/main_app.py | 4 + .../path/installed_package/__init__.py | 3 + 5 files changed, 78 insertions(+), 52 deletions(-) create mode 100644 flask/testsuite/test_apps/lib/python2.5/site-packages/site_app.py create mode 100644 flask/testsuite/test_apps/lib/python2.5/site-packages/site_package/__init__.py create mode 100644 flask/testsuite/test_apps/main_app.py create mode 100644 flask/testsuite/test_apps/path/installed_package/__init__.py diff --git a/flask/testsuite/config.py b/flask/testsuite/config.py index ad1721fd..e0f7005f 100644 --- a/flask/testsuite/config.py +++ b/flask/testsuite/config.py @@ -98,6 +98,14 @@ class InstanceTestCase(FlaskTestCase): app = flask.Flask(__name__, instance_path=here) self.assert_equal(app.instance_path, here) + def test_main_module_paths(self): + # Test an app with '__main__' as the import name, uses cwd. + from main_app import app + here = os.path.abspath(os.getcwd()) + self.assert_equal(app.instance_path, os.path.join(here, 'instance')) + if 'main_app' in sys.modules: + del sys.modules['main_app'] + def test_uninstalled_module_paths(self): from config_module_app import app here = os.path.abspath(os.path.dirname(__file__)) @@ -109,70 +117,75 @@ class InstanceTestCase(FlaskTestCase): self.assert_equal(app.instance_path, os.path.join(here, 'test_apps', 'instance')) def test_installed_module_paths(self): - import types - expected_prefix = os.path.abspath('foo') - mod = types.ModuleType('myapp') - mod.__file__ = os.path.join(expected_prefix, 'lib', 'python2.5', - 'site-packages', 'myapp.py') - sys.modules['myapp'] = mod + here = os.path.abspath(os.path.dirname(__file__)) + expected_prefix = os.path.join(here, 'test_apps') + real_prefix, sys.prefix = sys.prefix, expected_prefix + site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages') + sys.path.append(site_packages) try: - mod.app = flask.Flask(mod.__name__) - self.assert_equal(mod.app.instance_path, - os.path.join(expected_prefix, 'var', - 'myapp-instance')) + import site_app + self.assert_equal(site_app.app.instance_path, + os.path.join(expected_prefix, 'var', + 'site_app-instance')) finally: - sys.modules['myapp'] = None + sys.prefix = real_prefix + sys.path.remove(site_packages) + if 'site_app' in sys.modules: + del sys.modules['site_app'] def test_installed_package_paths(self): - import types - expected_prefix = os.path.abspath('foo') - package_path = os.path.join(expected_prefix, 'lib', 'python2.5', - 'site-packages', 'myapp') - mod = types.ModuleType('myapp') - mod.__path__ = [package_path] - mod.__file__ = os.path.join(package_path, '__init__.py') - sys.modules['myapp'] = mod + here = os.path.abspath(os.path.dirname(__file__)) + expected_prefix = os.path.join(here, 'test_apps') + real_prefix, sys.prefix = sys.prefix, expected_prefix + installed_path = os.path.join(expected_prefix, 'path') + sys.path.append(installed_path) try: - mod.app = flask.Flask(mod.__name__) - self.assert_equal(mod.app.instance_path, - os.path.join(expected_prefix, 'var', - 'myapp-instance')) + import installed_package + self.assert_equal(installed_package.app.instance_path, + os.path.join(expected_prefix, 'var', + 'installed_package-instance')) finally: - sys.modules['myapp'] = None - - def test_prefix_installed_paths(self): - import types - expected_prefix = os.path.abspath(sys.prefix) - package_path = os.path.join(expected_prefix, 'lib', 'python2.5', - 'site-packages', 'myapp') - mod = types.ModuleType('myapp') - mod.__path__ = [package_path] - mod.__file__ = os.path.join(package_path, '__init__.py') - sys.modules['myapp'] = mod + sys.prefix = real_prefix + sys.path.remove(installed_path) + if 'installed_package' in sys.modules: + del sys.modules['installed_package'] + + def test_prefix_package_paths(self): + here = os.path.abspath(os.path.dirname(__file__)) + expected_prefix = os.path.join(here, 'test_apps') + real_prefix, sys.prefix = sys.prefix, expected_prefix + site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages') + sys.path.append(site_packages) try: - mod.app = flask.Flask(mod.__name__) - self.assert_equal(mod.app.instance_path, - os.path.join(expected_prefix, 'var', - 'myapp-instance')) + import site_package + self.assert_equal(site_package.app.instance_path, + os.path.join(expected_prefix, 'var', + 'site_package-instance')) finally: - sys.modules['myapp'] = None + sys.prefix = real_prefix + sys.path.remove(site_packages) + if 'site_package' in sys.modules: + del sys.modules['site_package'] def test_egg_installed_paths(self): - import types - expected_prefix = os.path.abspath(sys.prefix) - package_path = os.path.join(expected_prefix, 'lib', 'python2.5', - 'site-packages', 'MyApp.egg', 'myapp') - mod = types.ModuleType('myapp') - mod.__path__ = [package_path] - mod.__file__ = os.path.join(package_path, '__init__.py') - sys.modules['myapp'] = mod + here = os.path.abspath(os.path.dirname(__file__)) + expected_prefix = os.path.join(here, 'test_apps') + real_prefix, sys.prefix = sys.prefix, expected_prefix + site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages') + egg_path = os.path.join(site_packages, 'SiteEgg.egg') + sys.path.append(site_packages) + sys.path.append(egg_path) try: - mod.app = flask.Flask(mod.__name__) - self.assert_equal(mod.app.instance_path, - os.path.join(expected_prefix, 'var', - 'myapp-instance')) + import site_egg # in SiteEgg.egg + self.assert_equal(site_egg.app.instance_path, + os.path.join(expected_prefix, 'var', + 'site_egg-instance')) finally: - sys.modules['myapp'] = None + sys.prefix = real_prefix + sys.path.remove(site_packages) + sys.path.remove(egg_path) + if 'site_egg' in sys.modules: + del sys.modules['site_egg'] def suite(): diff --git a/flask/testsuite/test_apps/lib/python2.5/site-packages/site_app.py b/flask/testsuite/test_apps/lib/python2.5/site-packages/site_app.py new file mode 100644 index 00000000..06271108 --- /dev/null +++ b/flask/testsuite/test_apps/lib/python2.5/site-packages/site_app.py @@ -0,0 +1,3 @@ +import flask + +app = flask.Flask(__name__) diff --git a/flask/testsuite/test_apps/lib/python2.5/site-packages/site_package/__init__.py b/flask/testsuite/test_apps/lib/python2.5/site-packages/site_package/__init__.py new file mode 100644 index 00000000..06271108 --- /dev/null +++ b/flask/testsuite/test_apps/lib/python2.5/site-packages/site_package/__init__.py @@ -0,0 +1,3 @@ +import flask + +app = flask.Flask(__name__) diff --git a/flask/testsuite/test_apps/main_app.py b/flask/testsuite/test_apps/main_app.py new file mode 100644 index 00000000..a5f372c8 --- /dev/null +++ b/flask/testsuite/test_apps/main_app.py @@ -0,0 +1,4 @@ +import flask + +# Test Flask initialization with main module. +app = flask.Flask('__main__') diff --git a/flask/testsuite/test_apps/path/installed_package/__init__.py b/flask/testsuite/test_apps/path/installed_package/__init__.py new file mode 100644 index 00000000..06271108 --- /dev/null +++ b/flask/testsuite/test_apps/path/installed_package/__init__.py @@ -0,0 +1,3 @@ +import flask + +app = flask.Flask(__name__)