def test_constructor(self): o = modulegraph.ModuleGraph() self.assertTrue(o.path is sys.path) self.assertEqual(o.lazynodes, {}) self.assertEqual(o.replace_paths, ()) self.assertEqual(o.debug, 0) # Stricter tests would be nice, but that requires # better control over what's on sys.path self.assertIsInstance(o.nspackages, dict) g = Graph.Graph() o = modulegraph.ModuleGraph(['a', 'b', 'c'], ['modA'], [('fromA', 'toB'), ('fromC', 'toD')], { 'modA': ['modB', 'modC'], 'modC': ['modE', 'modF'], }, g, 1) self.assertEqual(o.path, ['a', 'b', 'c']) self.assertEqual(o.lazynodes, { 'modA': None, 'modC': ['modE', 'modF'], }) self.assertEqual(o.replace_paths, [('fromA', 'toB'), ('fromC', 'toD')]) self.assertEqual(o.nspackages, {}) self.assertTrue(o.graph is g) self.assertEqual(o.debug, 1)
def testPackagedImplies(self): root = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'testpkg-relimport') # First check that 'getopt' isn't accidently in the graph: mg = modulegraph.ModuleGraph(path=[root]+sys.path) mg.run_script(os.path.join(root, 'script.py')) node = mg.findNode('mod') self.assertIsInstance(node, modulegraph.SourceModule) node = mg.findNode('getopt') self.assertEqual(node, None) # Now check that adding an implied dependency actually adds # 'getopt' to the graph: mg = modulegraph.ModuleGraph(path=[root]+sys.path, implies={ 'pkg.relative': ['getopt']}) node = mg.findNode('getopt') self.assertEqual(node, None) mg.run_script(os.path.join(root, 'script.py')) node = mg.findNode('pkg.relative') self.assertIsInstance(node, modulegraph.SourceModule) node = mg.findNode('getopt') self.assertIsInstance(node, modulegraph.SourceModule) # Check that the edges are correct: self.assertIn(mg.findNode('pkg.relative'), mg.get_edges(node)[1]) self.assertIn(node, mg.get_edges(mg.findNode('pkg.relative'))[0])
def test_scan_code(self): mod = modulegraph.Node('root') graph = modulegraph.ModuleGraph() code = compile('', '<test>', 'exec', 0, False) graph.scan_code(code, mod) self.assertEqual(list(graph.nodes()), []) node_map = {} def _safe_import(name, mod, fromlist, level): if name in node_map: node = node_map[name] else: node = modulegraph.Node(name) node_map[name] = node return [node] graph = modulegraph.ModuleGraph() graph._safe_import_hook = _safe_import code = compile( textwrap.dedent('''\ import sys import os.path def testfunc(): import shutil '''), '<test>', 'exec', 0, False) graph.scan_code(code, mod) modules = [node.identifier for node in graph.nodes()] self.assertEqual(set(node_map), set(['sys', 'os.path', 'shutil'])) # from module import a, b, c # from module import * # both: # -> with/without globals # -> with/without modules in globals (e.g, # from os import * adds dependency to os.path) # from .module import a # from ..module import a # -> check levels # import name # import a.b # -> should add dependency to a # try to build case where commented out # code would behave different than current code # (Carbon.SomeMod contains 'import Sibling' seems # to cause difference in real code) self.fail("actual test needed")
def test_calc_setuptools_nspackages(self): stdlib = [ fn for fn in sys.path if fn.startswith(sys.prefix) and 'site-packages' not in fn ] for subdir in [nm for nm in os.listdir(TESTDATA) if nm != 'src']: graph = modulegraph.ModuleGraph(path=[ os.path.join(TESTDATA, subdir, "parent"), os.path.join(TESTDATA, subdir, "child"), ] + stdlib) pkgs = graph.nspackages self.assertTrue('namedpkg' in pkgs) self.assertEqual( set(pkgs['namedpkg']), set([ os.path.join(TESTDATA, subdir, "parent", "namedpkg"), os.path.join(TESTDATA, subdir, "child", "namedpkg"), ])) self.assertFalse( os.path.exists( os.path.join(TESTDATA, subdir, "parent", "namedpkg", "__init__.py"))) self.assertFalse( os.path.exists( os.path.join(TESTDATA, subdir, "child", "namedpkg", "__init__.py")))
def test_graphreport(self): def my_iter(flatpackages="packages"): yield "line1\n" yield str(flatpackages) + "\n" yield "line2\n" graph = modulegraph.ModuleGraph() graph.itergraphreport = my_iter fp = StringIO() graph.graphreport(fp) self.assertEqual(fp.getvalue(), "line1\n()\nline2\n") fp = StringIO() graph.graphreport(fp, "deps") self.assertEqual(fp.getvalue(), "line1\ndeps\nline2\n") saved_stdout = sys.stdout try: sys.stdout = fp = StringIO() graph.graphreport() self.assertEqual(fp.getvalue(), "line1\n()\nline2\n") finally: sys.stdout = saved_stdout
def testImpliedReference(self): graph = modulegraph.ModuleGraph() record = [] def import_hook(*args): record.append(('import_hook', ) + args) return [graph.createNode(modulegraph.Node, args[0])] def _safe_import_hook(*args): record.append(('_safe_import_hook', ) + args) return [graph.createNode(modulegraph.Node, args[0])] graph.import_hook = import_hook graph._safe_import_hook = _safe_import_hook n1 = graph.createNode(modulegraph.Node, 'n1') n2 = graph.createNode(modulegraph.Node, 'n2') graph.implyNodeReference(n1, n2) outs, ins = map(list, graph.get_edges(n1)) self.assertEqual(outs, [n2]) self.assertEqual(ins, []) self.assertEqual(record, []) graph.implyNodeReference(n2, "n3") n3 = graph.findNode('n3') outs, ins = map(list, graph.get_edges(n2)) self.assertEqual(outs, [n3]) self.assertEqual(ins, [n1]) self.assertEqual(record, [('_safe_import_hook', 'n3', n2, None)])
def test_swig_import_simple_BUGGY(tmpdir): libdir = tmpdir.join('lib') path = [str(libdir)] osgeo = libdir.join('pyi_test_osgeo') osgeo.join('__init__.py').ensure().write('#') osgeo.join('pyi_gdal.py').write('# automatically generated by SWIG\n' 'import _pyi_gdal') osgeo.join('_pyi_gdal.py').write('#') script = tmpdir.join('script.py') script.write('from pyi_test_osgeo import pyi_gdal') mg = modulegraph.ModuleGraph(path) mg.run_script(str(script)) assert isinstance(mg.findNode('pyi_test_osgeo'), modulegraph.Package) assert isinstance(mg.findNode('pyi_test_osgeo.pyi_gdal'), modulegraph.SourceModule) # The "C" module is frozen under its unqualified rather than qualified # name. See comment in modulegraph._safe_import_hook. # BUG: modulegraph contains a probable bug: Only the module's identifier # is changed, not the module's graphident. Thus the node is still found # under it's old name. The relevant code was brought from PyInstaller to # upstream, so this might be PyInstaller's fault. See # test_swig_import_simple for what it should be. # This is a separate test-case, not marked as xfail, so we can spot # whether the SWIG support works at all. assert isinstance(mg.findNode('pyi_test_osgeo._pyi_gdal'), modulegraph.SourceModule) # Due the the buggy implementation, the graphident is unchanged, but # at least the identifier should have changed. assert mg.findNode('pyi_test_osgeo._pyi_gdal').identifier \ == '_pyi_gdal' # Due the the buggy implementation, this node does not exist. assert mg.findNode('_pyi_gdal') is None return mg # for use in test_swig_import_simple_BUG
def test_determine_parent(self): graph = modulegraph.ModuleGraph() # FIXME PyInstaller: original _load_tail returned a MissingModule if # the module was not found. PyInstaller changed this in # cae47e4f5b51a94ac3ceb5d093283ba0cc895589 and raises an ImportError, # which makes these two calls fail. #graph.import_hook('os.path', None) #graph.import_hook('idlelib', None) graph.import_hook('xml.dom', None) for node in graph.nodes(): if isinstance(node, modulegraph.Package): break else: self.fail("No package located, should have at least 'os'") self.assertIsInstance(node, modulegraph.Package) parent = graph._determine_parent(node) self.assertEqual(parent.identifier, node.identifier) self.assertEqual(parent, graph.findNode(node.identifier)) self.assertTrue(isinstance(parent, modulegraph.Package)) # XXX: Might be a usecase for some odd code in determine_parent... #node = modulegraph.Package('encodings') #node.packagepath = parent.packagepath #m = graph._determine_parent(node) #self.assertTrue(m is parent) m = graph.findNode('xml') self.assertEqual(graph._determine_parent(m), m) m = graph.findNode('xml.dom') self.assertEqual(graph._determine_parent(m), graph.findNode('xml.dom'))
def test_swig_candidate_but_not_swig_missing2(tmpdir): """ Variation of test_swig_candidate_but_not_swig_missing using differnt import statements (like tifffile/tifffile.py does) """ libdir = tmpdir.join('lib') path = [str(libdir)] pkg = libdir.join('pkg') pkg.join('__init__.py').ensure().write('from . import mymod') pkg.join('mymod.py').write('from . import _mymod\n' 'import _mymod') # no module '_mymod.py' script = tmpdir.join('script.py') script.write('import pkg') mg = modulegraph.ModuleGraph(path) mg.run_script(str(script)) assert isinstance(mg.findNode('pkg'), modulegraph.Package) assert isinstance(mg.findNode('pkg.mymod'), modulegraph.SourceModule) assert isinstance(mg.findNode('pkg._mymod'), modulegraph.MissingModule) if is_py2: # In Python 2 both are relative imports, global module should not exist assert mg.findNode('_mymod') is None else: assert isinstance(mg.findNode('_mymod'), modulegraph.MissingModule)
def test_create_xref(self): # XXX: This test is far from optimal, it just ensures # that all code is exercised to catch small bugs and # py3k issues without verifying that the code actually # works.... graph = modulegraph.ModuleGraph() if __file__.endswith('.py'): graph.run_script(__file__) else: graph.run_script(__file__[:-1]) graph.import_hook('os') graph.import_hook('xml.etree') graph.import_hook('unittest') fp = StringIO() graph.create_xref(out=fp) data = fp.getvalue() # Don't tolerate any HTML `parsing errors <https://lxml.de/parsing.html#parser-options>`_. parser = etree.HTMLParser(recover=False) tree = etree.parse(StringIO(data), parser) assert tree is not None # Verify no `errors <https://lxml.de/parsing.html#error-log>`_ occurred. assert len(parser.error_log) == 0
def testCompiledModules(self): mf = modulegraph.ModuleGraph(path=[self.compiled_dir] + sys.path) #self.mf.debug = 999 mf.run_script(os.path.join(self.compiled_dir, 'script.py')) o = mf.findNode('mod1') self.assertIsInstance(o, modulegraph.CompiledModule) self.assertEqual(o._global_attr_names, {'mod2', 'mod3', 'foo'}) self.assertEqual(o._starimported_ignored_module_names, set()) o = mf.findNode('mod2') self.assertIsInstance(o, modulegraph.CompiledModule) self.assertEqual(o._global_attr_names, {'mod1', 'sys', 'testme', 'bar'}) self.assertEqual(o._starimported_ignored_module_names, set()) o = mf.findNode('mod3') self.assertIsInstance(o, modulegraph.CompiledModule) self.assertEqual(o._global_attr_names, {'os', 'path'}) self.assertEqual(o._starimported_ignored_module_names, set()) o = mf.findNode('mod4') other = mf.findNode('zipfile') self.assertIsInstance(o, modulegraph.CompiledModule) self.assertEqual(o._global_attr_names, other._global_attr_names) self.assertEqual(o._starimported_ignored_module_names, {'math'}) o = mf.findNode('mod5') self.assertIs(o, None)
def test_swig_import_from_top_level_missing(tmpdir): # Like test_swig_import_from_top_level, but the "C" module is missing and # should be reported as a MissingModule. libdir = tmpdir.join('lib') path = [str(libdir)] osgeo = libdir.join('pyi_test_osgeo') osgeo.join('__init__.py').ensure().write('import _pyi_gdal') osgeo.join('pyi_gdal.py').write('# automatically generated by SWIG\n' 'import _pyi_gdal') # no module '_pyi_gdal.py' script = tmpdir.join('script.py') script.write('from pyi_test_osgeo import pyi_gdal') mg = modulegraph.ModuleGraph(path) mg.run_script(str(script)) assert isinstance(mg.findNode('pyi_test_osgeo'), modulegraph.Package) assert isinstance(mg.findNode('pyi_test_osgeo.pyi_gdal'), modulegraph.SourceModule) # BUG: Again, this is unecpected behaviour in modulegraph: While # MissingModule('_pyi_gdal') is (arguable) removed when trying to import # the SWIG C module, there is no MissingModule('pyi_test_osgeo.pyi_gdal') # added, but again MissingModule('_pyi_gdal'). I still need to understand # why. assert mg.findNode('pyi_test_osgeo._pyi_gdal') is None assert isinstance(mg.findNode('_pyi_gdal'), modulegraph.MissingModule)
def testMissingPathEntry(self): root = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'nosuchdirectory') try: mf = modulegraph.ModuleGraph(path=[root] + sys.path) except os.error: self.fail('modulegraph initialiser raises os.error')
def setUp(self): root = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testpkg-relimport') self.mf = modulegraph.ModuleGraph(path=[root] + sys.path) #self.mf.debug = 999 self.script_name = os.path.join(root, 'script.py') self.mf.run_script(self.script_name)
def test_swig_import_from_top_level_but_nested(tmpdir): # Like test_swig_import_from_top_level, but both the wrapper and the "top # level" are nested. This is intented to test relative import of the "C" # module. libdir = tmpdir.join('lib') path = [str(libdir)] osgeo = libdir.join('pyi_test_osgeo') osgeo.join('__init__.py').ensure().write('#') osgeo.join('x', '__init__.py').ensure().write('#') osgeo.join('x', 'y', '__init__.py').ensure().write('import _pyi_gdal') osgeo.join('x', 'y', 'pyi_gdal.py').write('# automatically generated by SWIG\n' 'import _pyi_gdal') osgeo.join('x', 'y', '_pyi_gdal.py').write('#') script = tmpdir.join('script.py') script.write('from pyi_test_osgeo.x.y import pyi_gdal') mg = modulegraph.ModuleGraph(path) mg.run_script(str(script)) assert isinstance(mg.findNode('pyi_test_osgeo.x.y.pyi_gdal'), modulegraph.SourceModule) # The "C" module is frozen under its unqualified rather than qualified # name. See comment in modulegraph._safe_import_hook. # Due the the buggy implementation (see test_swig_import_simple): assert isinstance(mg.findNode('pyi_test_osgeo.x.y._pyi_gdal'), modulegraph.SourceModule) assert mg.findNode('_pyi_gdal') is None
def test_swig_candidate_but_not_swig(tmpdir): # From a package module import an absolute module which looks like a SWIG # candidate but is no SWIG module . See issue #2911 (tifffile). # The center of this test-case is that it doesn't raise a recursion too # deep error. libdir = tmpdir.join('lib') path = [str(libdir)] pkg = libdir.join('pkg') pkg.join('__init__.py').ensure().write('from . import mymod') pkg.join('mymod.py').write('import _mymod') pkg.join('_mymod.py').write('#') script = tmpdir.join('script.py') script.write('from pkg import XXX') mg = modulegraph.ModuleGraph(path) mg.run_script(str(script)) assert isinstance(mg.findNode('pkg'), modulegraph.Package) assert isinstance(mg.findNode('pkg.mymod'), modulegraph.SourceModule) if is_py2: # In Python 2 this is a relative import, global module should exist assert isinstance(mg.findNode('pkg._mymod'), modulegraph.SourceModule) assert mg.findNode('_mymod') is None else: assert mg.findNode('pkg._mymod') is None # This is not a SWIG module, thus the SWIG import mechanism should not # trigger. assert isinstance(mg.findNode('_mymod'), modulegraph.MissingModule)
def testNone(self): mg = modulegraph.ModuleGraph() # empty packagepath m = DummyModule(None) sub_ms = [] for sm in mg._find_all_submodules(m): sub_ms.append(sm) self.assertEqual(sub_ms, [])
def _import_and_get_node(tmpdir, module_name, path=None): script = tmpdir.join('script.py') script.write('import %s' % module_name) if path is None: path = [str(tmpdir)] mg = modulegraph.ModuleGraph(path) mg.run_script(str(script)) return mg.findNode(module_name)
def testSlashes(self): # a string does not break anything although it is split into its characters # BUG: "/xyz" will read "/" so this one already triggers missing itertools mg = modulegraph.ModuleGraph() m = DummyModule("/xyz") sub_ms = [] for sm in mg._find_all_submodules(m): sub_ms.append(sm) self.assertEqual(sub_ms, [])
def testSimple(self): mg = modulegraph.ModuleGraph() # a string does not break anything although it is split into its characters # BUG: "/hi/there" will read "/" m = DummyModule("xyz") sub_ms = [] for sm in mg._find_all_submodules(m): sub_ms.append(sm) self.assertEqual(sub_ms, [])
def test_find_module(self): record = [] class MockedModuleGraph(modulegraph.ModuleGraph): def _find_module(self, name, path, parent=None): if path == None: path = sys.path record.append((name, path)) return super(MockedModuleGraph, self)._find_module(name, path, parent) mockedgraph = MockedModuleGraph() try: graph = modulegraph.ModuleGraph() m = graph._find_module('sys', None) self.assertEqual(record, []) self.assertEqual(m, (None, None, ("", "", imp.C_BUILTIN))) xml = graph.import_hook("xml")[0] self.assertEqual(xml.identifier, 'xml') self.assertRaises(ImportError, graph._find_module, 'xml', None) self.assertEqual(record, []) m = mockedgraph._find_module('shutil', None) self.assertEqual(record, [ ('shutil', graph.path), ]) self.assertTrue(isinstance(m, tuple)) self.assertEqual(len(m), 3) self.assertTrue(hasattr(m[0], 'read')) self.assertIsInstance(m[0].read(), str) srcfn = shutil.__file__ if srcfn.endswith('.pyc'): srcfn = srcfn[:-1] self.assertEqual(os.path.realpath(m[1]), os.path.realpath(srcfn)) self.assertEqual(m[2], ('.py', READ_MODE, imp.PY_SOURCE)) m[0].close() m2 = graph._find_module('shutil', None) self.assertEqual(m[1:], m2[1:]) m2[0].close() record[:] = [] m = mockedgraph._find_module('sax', xml.packagepath, xml) # FIXME: PyInstaller appends `__init__.py` to the pkg-directory #self.assertEqual(m, # (None, os.path.join(os.path.dirname(xml.filename), 'sax'), # ('', '', imp.PKG_DIRECTORY))) self.assertEqual(record, [ ('sax', xml.packagepath), ]) if m[0] is not None: m[0].close() finally: pass
def setUp(self): test_dir = os.path.join(gRootDir, 'test.dir') if os.path.exists(test_dir): shutil.rmtree(test_dir) os.mkdir(test_dir) exit = install_testpkg(test_dir) self.assertEqual(exit, 0) self.mf = modulegraph.ModuleGraph(path=[test_dir] + sys.path)
def test_run_script(self): script = os.path.join(os.path.dirname(TESTDATA), 'script') graph = modulegraph.ModuleGraph() master = graph.createNode(modulegraph.Node, 'root') m = graph.run_script(script, master) self.assertEqual(list(graph.get_edges(master)[0])[0], m) self.assertEqual(set(graph.get_edges(m)[0]), set([ graph.findNode('sys'), graph.findNode('os'), ]))
def test_swig_top_level_but_no_swig_at_all_existing(tmpdir): # Like test_swig_top_level_but_no_swig_at_all, but the "C" module exists. # The test-case is here for symmetry. libdir = tmpdir.join('lib') path = [str(libdir)] libdir.join('pyi_dezimal.py').ensure().write('import _pyi_dezimal') libdir.join('_pyi_dezimal.py').ensure().write('#') script = tmpdir.join('script.py') script.write('import pyi_dezimal') mg = modulegraph.ModuleGraph(path) mg.run_script(str(script)) assert isinstance(mg.findNode('pyi_dezimal'), modulegraph.SourceModule) assert isinstance(mg.findNode('_pyi_dezimal'), modulegraph.SourceModule)
def __scan_code(code, use_ast, monkeypatch): mg = modulegraph.ModuleGraph() # _process_imports would set _deferred_imports to None monkeypatch.setattr(mg, '_process_imports', lambda m: None) module = mg.createNode(modulegraph.Script, 'dummy.py') code = textwrap.dedent(code) if use_ast: co_ast = compile(code, 'dummy', 'exec', ast.PyCF_ONLY_AST) co = compile(co_ast, 'dummy', 'exec') else: co_ast = None co = compile(code, 'dummy', 'exec') mg._scan_code(module, co) return module
def test_relative_import_too_far(self): # pkg.mod tries to import "..sys" (outside of the package...) root = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testpkg-regr7') mf = modulegraph.ModuleGraph(path=[root] + sys.path) mf.run_script(os.path.join(root, 'script.py')) m = mf.findNode('') self.assertIs(m, None) m = mf.findNode('pkg.mod') self.assertIsInstance(m, modulegraph.SourceModule) imported = list(mf.get_edges(m)[0]) self.assertEqual(len(imported), 5) im = imported[0] self.assertIsInstance(im, modulegraph.InvalidRelativeImport) self.assertEqual(im.relative_path, '..') self.assertEqual(im.from_name, 'sys') self.assertEqual(im.identifier, '..sys') im1 = imported[1] im2 = imported[2] if im1.identifier == '...xml': # Order of modules imported in a single 'from .. import a, b' list # is unspecified, ensure a fixed order for this test. im2, im1 = im1, im2 self.assertIsInstance(im1, modulegraph.InvalidRelativeImport) self.assertEqual(im1.relative_path, '...') self.assertEqual(im1.from_name, 'os') self.assertEqual(im1.identifier, '...os') im = imported[2] self.assertIsInstance(im2, modulegraph.InvalidRelativeImport) self.assertEqual(im2.relative_path, '...') self.assertEqual(im2.from_name, 'xml') self.assertEqual(im2.identifier, '...xml') im = imported[3] self.assertIsInstance(im, modulegraph.InvalidRelativeImport) self.assertEqual(im.relative_path, '..foo') self.assertEqual(im.from_name, 'bar') self.assertEqual(im.identifier, '..foo.bar') im = imported[4] self.assertIs(im, mf.findNode('pkg'))
def test_swig_top_level_but_no_swig_at_all(tmpdir): # From the script import an absolute module which looks like a SWIG # candidate but is no SWIG module. See issue #3040 ('_decimal') # The center of this test-case is that it doesn't raise a recursion too # deep error. libdir = tmpdir.join('lib') path = [str(libdir)] libdir.join('pyi_dezimal.py').ensure().write('import _pyi_dezimal') # no module '_pyi_dezimal.py' script = tmpdir.join('script.py') script.write('import pyi_dezimal') mg = modulegraph.ModuleGraph(path) mg.run_script(str(script)) assert isinstance(mg.findNode('pyi_dezimal'), modulegraph.SourceModule) assert isinstance(mg.findNode('_pyi_dezimal'), modulegraph.MissingModule)
def test_relative_import_missing(tmpdir): libdir = tmpdir.join('lib') path = [str(libdir)] pkg = libdir.join('pkg') pkg.join('__init__.py').ensure().write('#') pkg.join('x', '__init__.py').ensure().write('#') pkg.join('x', 'y', '__init__.py').ensure().write('#') pkg.join('x', 'y', 'z.py').ensure().write('from . import DoesNotExist') script = tmpdir.join('script.py') script.write('import pkg.x.y.z') mg = modulegraph.ModuleGraph(path) mg.run_script(str(script)) assert isinstance(mg.findNode('pkg.x.y.z'), modulegraph.SourceModule) assert isinstance(mg.findNode('pkg.x.y.DoesNotExist'), modulegraph.MissingModule)
def test_replace_paths_in_code(self): join = os.path.join # shortcut graph = modulegraph.ModuleGraph(replace_paths=[ ('path1', 'path2'), (join('path3', 'path5'), 'path4'), ]) co = compile( textwrap.dedent(""" [x for x in range(4)] """), join("path4", "index.py"), 'exec', 0, 1) co = graph._replace_paths_in_code(co) self.assertEqual(co.co_filename, join('path4', 'index.py')) co = compile( textwrap.dedent(""" [x for x in range(4)] (x for x in range(4)) """), join("path1", "index.py"), 'exec', 0, 1) self.assertEqual(co.co_filename, join('path1', 'index.py')) co = graph._replace_paths_in_code(co) self.assertEqual(co.co_filename, join('path2', 'index.py')) for c in co.co_consts: if isinstance(c, type(co)): self.assertEqual(c.co_filename, join('path2', 'index.py')) co = compile( textwrap.dedent(""" [x for x in range(4)] """), join("path3", "path4", "index.py"), 'exec', 0, 1) co = graph._replace_paths_in_code(co) self.assertEqual(co.co_filename, join('path3', 'path4', 'index.py')) co = compile( textwrap.dedent(""" [x for x in range(4)] """), join("path3", "path5.py"), 'exec', 0, 1) co = graph._replace_paths_in_code(co) self.assertEqual(co.co_filename, join('path3', 'path5.py')) co = compile( textwrap.dedent(""" [x for x in range(4)] """), join("path3", "path5", "index.py"), 'exec', 0, 1) co = graph._replace_paths_in_code(co) self.assertEqual(co.co_filename, join('path4', 'index.py'))
def test_report(self): graph = modulegraph.ModuleGraph() saved_stdout = sys.stdout try: fp = sys.stdout = StringIO() graph.report() lines = fp.getvalue().splitlines() fp.close() self.assertEqual(len(lines), 3) self.assertEqual(lines[0], '') self.assertEqual(lines[1], 'Class Name File') self.assertEqual(lines[2], '----- ---- ----') fp = sys.stdout = StringIO() graph._safe_import_hook('os', None, ()) graph._safe_import_hook('sys', None, ()) graph._safe_import_hook('nomod', None, ()) graph.report() lines = fp.getvalue().splitlines() fp.close() self.assertEqual(lines[0], '') self.assertEqual(lines[1], 'Class Name File') self.assertEqual(lines[2], '----- ---- ----') expected = [] for n in graph.flatten(): if n.filename: expected.append( [type(n).__name__, n.identifier, n.filename]) else: expected.append([type(n).__name__, n.identifier]) expected.sort() actual = [item.split() for item in lines[3:]] actual.sort() self.assertEqual(expected, actual) finally: sys.stdout = saved_stdout