Skip to content

Commit a98be39

Browse files
committed
Tests: Add test-cases for order of module import.
1 parent c91952d commit a98be39

File tree

1 file changed

+93
-1
lines changed

1 file changed

+93
-1
lines changed

tests/unit/test_modulegraph_more.py

+93-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import pytest
1717

1818
from PyInstaller.lib.modulegraph import modulegraph
19-
from PyInstaller.utils.tests import skipif, skipif_win, is_py2, is_py3
19+
from PyInstaller.utils.tests import xfail, skipif, skipif_win, is_py2, is_py3
2020

2121
def _import_and_get_node(tmpdir, module_name, path=None):
2222
script = tmpdir.join('script.py')
@@ -213,3 +213,95 @@ def test_symlinks(tmpdir):
213213

214214
node = _import_and_get_node(base_dir, 'p1.p2')
215215
assert isinstance(node, modulegraph.SourceModule)
216+
217+
218+
@xfail(reason="FIXME: modulegraph does not use correct order")
219+
def test_import_order_1(tmpdir):
220+
# Ensure modulegraph processes modules in the same order as Python does.
221+
222+
class MyModuleGraph(modulegraph.ModuleGraph):
223+
def _load_module(self, fqname, fp, pathname, info):
224+
if not record or record[-1] != fqname:
225+
record.append(fqname) # record non-consecutive entries
226+
return super(MyModuleGraph, self)._load_module(fqname, fp,
227+
pathname, info)
228+
229+
record = []
230+
231+
for filename, content in (
232+
('a/', ' from . import c, d'),
233+
('a/c', '#'),
234+
('a/d/', 'from . import f, g, h'),
235+
('a/d/f/', 'from . import j, k'),
236+
('a/d/f/j', '#'),
237+
('a/d/f/k', '#'),
238+
('a/d/g/', 'from . import l, m'),
239+
('a/d/g/l', '#'),
240+
('a/d/g/m', '#'),
241+
('a/d/h', '#'),
242+
('b/', 'from . import e'),
243+
('b/e/', 'from . import i'),
244+
('b/e/i', '#')):
245+
if filename.endswith('/'): filename += '__init__'
246+
tmpdir.join(*(filename+'.py').split('/')).ensure().write(content)
247+
248+
script = tmpdir.join('script.py')
249+
script.write('import a, b')
250+
mg = MyModuleGraph([str(tmpdir)])
251+
mg.run_script(str(script))
252+
253+
# This is the order Python imports these modules given that script.
254+
expected = ['a',
255+
'a.c', 'a.d', 'a.d.f', 'a.d.f.j', 'a.d.f.k',
256+
'a.d.g', 'a.d.g.l', 'a.d.g.m',
257+
'a.d.h',
258+
'b', 'b.e', 'b.e.i']
259+
assert record == expected
260+
261+
262+
def test_import_order_2(tmpdir):
263+
# Ensure modulegraph processes modules in the same order as Python does.
264+
265+
class MyModuleGraph(modulegraph.ModuleGraph):
266+
def _load_module(self, fqname, fp, pathname, info):
267+
if not record or record[-1] != fqname:
268+
record.append(fqname) # record non-consecutive entries
269+
return super(MyModuleGraph, self)._load_module(fqname, fp,
270+
pathname, info)
271+
272+
record = []
273+
274+
for filename, content in (
275+
('a/', '#'),
276+
('a/c/', '#'),
277+
('a/c/g', '#'),
278+
('a/c/h', 'from . import g'),
279+
('a/d/', '#'),
280+
('a/d/i', 'from ..c import h'),
281+
('a/d/j/', 'from .. import i'),
282+
('a/d/j/o', '#'),
283+
('b/', 'from .e import k'),
284+
('b/e/', 'import a.c.g'),
285+
('b/e/k', 'from .. import f'),
286+
('b/e/l', 'import a.d.j'),
287+
('b/f/', '#'),
288+
('b/f/m', '#'),
289+
('b/f/n/', '#'),
290+
('b/f/n/p', 'from ...e import l')):
291+
if filename.endswith('/'): filename += '__init__'
292+
tmpdir.join(*(filename+'.py').split('/')).ensure().write(content)
293+
294+
script = tmpdir.join('script.py')
295+
script.write('import b.f.n.p')
296+
mg = MyModuleGraph([str(tmpdir)])
297+
mg.run_script(str(script))
298+
299+
# This is the order Python imports these modules given that script.
300+
expected = ['b', 'b.e',
301+
'a', 'a.c', 'a.c.g',
302+
'b.e.k',
303+
'b.f', 'b.f.n', 'b.f.n.p',
304+
'b.e.l',
305+
'a.d', 'a.d.j', 'a.d.i', 'a.c.h']
306+
assert record == expected
307+
print(record)

0 commit comments

Comments
 (0)