Skip to content
Myr

test_ext.py

This file

Path

tests/test_ext.py

187 lines · 5.7 KiB · Python

Last changed by

Rename fixture apps_tmpdirMarkus Unterwaditzerbefore adoption77d8875

History of this file

Take it

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 1–12why, one step back
1# -*- coding: utf-8 -*-
2"""3 tests.ext4 ~~~~~~~~~~~~~~~~~~~56 Tests the extension import thing.78 :copyright: (c) 2014 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112import sys

Rewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00

lines 13–14why, one step back
13import pytest
14

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 15–18why, one step back
15try:
16 from imp import reload as reload_module17except ImportError:18 reload_module = reload

Remove flask superclassMarkus Unterwaditzerbefore adoptionaf4cb0f

line 19why, one step back
19

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 20–22why, one step back
20from flask._compat import PY2
2122

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 23–29why, one step back
23@pytest.fixture(autouse=True)
24def importhook_setup(monkeypatch, request):25 # we clear this out for various reasons. The most important one is26 # that a real flaskext could be in there which would disable our27 # fake package. Secondly we want to make sure that the flaskext28 # import hook does not break on reloading.29 for entry, value in list(sys.modules.items()):

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 30–31why, one step back
30 if (
31 entry.startswith('flask.ext.') or

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 32–33why, one step back
32 entry.startswith('flask_') or
33 entry.startswith('flaskext.') or

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 34–35why, one step back
34 entry == 'flaskext'
35 ) and value is not None:

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 36–38why, one step back
36 monkeypatch.delitem(sys.modules, entry)
37 from flask import ext38 reload_module(ext)

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 39why, one step back
39

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 40–47why, one step back
40 # reloading must not add more hooks
41 import_hooks = 042 for item in sys.meta_path:43 cls = type(item)44 if cls.__module__ == 'flask.exthook' and \45 cls.__name__ == 'ExtensionImporter':46 import_hooks += 147 assert import_hooks == 1

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 48why, one step back
48

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

line 49why, one step back
49 def teardown():

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 50–51why, one step back
50 from flask import ext
51 for key in ext.__dict__:

Rewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00

line 52why, one step back
52 assert '.' not in key

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 53why, one step back
53

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

line 54why, one step back
54 request.addfinalizer(teardown)

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 55why, one step back
55

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 56–57why, one step back
56
57@pytest.fixture

Rename fixture apps_tmpdirMarkus Unterwaditzerbefore adoption77d8875

lines 58–59why, one step back
58def newext_simple(modules_tmpdir):
59 x = modules_tmpdir.join('flask_newext_simple.py')

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 60–63why, one step back
60 x.write('ext_id = "newext_simple"')
616263@pytest.fixture

Rename fixture apps_tmpdirMarkus Unterwaditzerbefore adoption77d8875

lines 64–65why, one step back
64def oldext_simple(modules_tmpdir):
65 flaskext = modules_tmpdir.mkdir('flaskext')

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 66–70why, one step back
66 flaskext.join('__init__.py').write('\n')
67 flaskext.join('oldext_simple.py').write('ext_id = "oldext_simple"')686970@pytest.fixture

Rename fixture apps_tmpdirMarkus Unterwaditzerbefore adoption77d8875

lines 71–72why, one step back
71def newext_package(modules_tmpdir):
72 pkg = modules_tmpdir.mkdir('flask_newext_package')

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 73–77why, one step back
73 pkg.join('__init__.py').write('ext_id = "newext_package"')
74 pkg.join('submodule.py').write('def test_function():\n return 42\n')757677@pytest.fixture

Rename fixture apps_tmpdirMarkus Unterwaditzerbefore adoption77d8875

lines 78–79why, one step back
78def oldext_package(modules_tmpdir):
79 flaskext = modules_tmpdir.mkdir('flaskext')

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 80–87why, one step back
80 flaskext.join('__init__.py').write('\n')
81 oldext = flaskext.mkdir('oldext_package')82 oldext.join('__init__.py').write('ext_id = "oldext_package"')83 oldext.join('submodule.py').write('def test_function():\n'84 ' return 42')858687@pytest.fixture

Rename fixture apps_tmpdirMarkus Unterwaditzerbefore adoption77d8875

lines 88–89why, one step back
88def flaskext_broken(modules_tmpdir):
89 ext = modules_tmpdir.mkdir('flask_broken')

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 90–95why, one step back
90 ext.join('b.py').write('\n')
91 ext.join('__init__.py').write('import flask.ext.broken.b\n'92 'import missing_module')939495def test_flaskext_new_simple_import_normal(newext_simple):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 96–97why, one step back
96 from flask.ext.newext_simple import ext_id
97 assert ext_id == 'newext_simple'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 98why, one step back
98

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 99–100why, one step back
99
100def test_flaskext_new_simple_import_module(newext_simple):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 101–103why, one step back
101 from flask.ext import newext_simple
102 assert newext_simple.ext_id == 'newext_simple'103 assert newext_simple.__name__ == 'flask_newext_simple'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 104why, one step back
104

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 105–106why, one step back
105
106def test_flaskext_new_package_import_normal(newext_package):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 107–108why, one step back
107 from flask.ext.newext_package import ext_id
108 assert ext_id == 'newext_package'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 109why, one step back
109

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 110–111why, one step back
110
111def test_flaskext_new_package_import_module(newext_package):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 112–114why, one step back
112 from flask.ext import newext_package
113 assert newext_package.ext_id == 'newext_package'114 assert newext_package.__name__ == 'flask_newext_package'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 115why, one step back
115

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 116–117why, one step back
116
117def test_flaskext_new_package_import_submodule_function(newext_package):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 118–119why, one step back
118 from flask.ext.newext_package.submodule import test_function
119 assert test_function() == 42

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 120why, one step back
120

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 121–122why, one step back
121
122def test_flaskext_new_package_import_submodule(newext_package):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 123–125why, one step back
123 from flask.ext.newext_package import submodule
124 assert submodule.__name__ == 'flask_newext_package.submodule'125 assert submodule.test_function() == 42

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 126why, one step back
126

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 127–128why, one step back
127
128def test_flaskext_old_simple_import_normal(oldext_simple):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 129–130why, one step back
129 from flask.ext.oldext_simple import ext_id
130 assert ext_id == 'oldext_simple'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 131why, one step back
131

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 132–133why, one step back
132
133def test_flaskext_old_simple_import_module(oldext_simple):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 134–136why, one step back
134 from flask.ext import oldext_simple
135 assert oldext_simple.ext_id == 'oldext_simple'136 assert oldext_simple.__name__ == 'flaskext.oldext_simple'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 137why, one step back
137

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 138–139why, one step back
138
139def test_flaskext_old_package_import_normal(oldext_package):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 140–141why, one step back
140 from flask.ext.oldext_package import ext_id
141 assert ext_id == 'oldext_package'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 142why, one step back
142

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 143–144why, one step back
143
144def test_flaskext_old_package_import_module(oldext_package):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 145–147why, one step back
145 from flask.ext import oldext_package
146 assert oldext_package.ext_id == 'oldext_package'147 assert oldext_package.__name__ == 'flaskext.oldext_package'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 148why, one step back
148

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 149–150why, one step back
149
150def test_flaskext_old_package_import_submodule(oldext_package):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 151–153why, one step back
151 from flask.ext.oldext_package import submodule
152 assert submodule.__name__ == 'flaskext.oldext_package.submodule'153 assert submodule.test_function() == 42

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 154why, one step back
154

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 155–156why, one step back
155
156def test_flaskext_old_package_import_submodule_function(oldext_package):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 157–158why, one step back
157 from flask.ext.oldext_package.submodule import test_function
158 assert test_function() == 42

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 159why, one step back
159

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 160–161why, one step back
160
161def test_flaskext_broken_package_no_module_caching(flaskext_broken):

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

lines 162–163why, one step back
162 for x in range(2):
163 with pytest.raises(ImportError):

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 164–165why, one step back
164 import flask.ext.broken
165

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 166–168why, one step back
166
167def test_no_error_swallowing(flaskext_broken):168 with pytest.raises(ImportError) as excinfo:

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

line 169why, one step back
169 import flask.ext.broken

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 170why, one step back
170

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 171–173why, one step back
171 assert excinfo.type is ImportError
172 if PY2:173 message = 'No module named missing_module'

WIPMarkus Unterwaditzerbefore adoptiond0cf5ef

line 174why, one step back
174 else:

Remove fake extensions from test_appsMarkus Unterwaditzerbefore adoptionb88a837

lines 175–187why, one step back
175 message = 'No module named \'missing_module\''
176 assert str(excinfo.value) == message177 assert excinfo.tb.tb_frame.f_globals is globals()178179 # reraise() adds a second frame so we need to skip that one too.180 # On PY3 we even have another one :(181 next = excinfo.tb.tb_next.tb_next182 if not PY2:183 next = next.tb_next184185 import os.path186 assert os.path.join('flask_broken', '__init__.py') in \187 next.tb_frame.f_code.co_filename