Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""Tests for graph.py."""
import contextlib
import unittest
from importlab import environment
from importlab import fs
from importlab import graph
from importlab import parsepy
from importlab import resolve
from importlab import utils
class FakeImportGraph(graph.DependencyGraph):
"""An ImportGraph with file imports stubbed out.
Also adds ordered_foo() wrappers around output methods to help in testing.
"""
def __init__(self, deps, unreadable=None):
super(FakeImportGraph, self).__init__()
self.deps = deps
if unreadable:
self.unreadable = unreadable
else:
self.unreadable = set()
def get_source_file_provenance(self, filename):
return resolve.Direct(filename, "module.name")
def test_trim(self):
sources = [self.tempdir["x.py"]]
with self.patch_resolve_import():
# Untrimmed g1 contains foo.b, the dep of system module foo.a.
g1 = graph.ImportGraph.create(self.env, sources, trim=False)
self.assertEqual(
g1.sorted_source_files(),
[[self.tempdir[x]] for x in ["foo/b.py", "foo/a.py", "x.py"]])
# Trimmed g2 stops at foo.a.
g2 = graph.ImportGraph.create(self.env, sources, trim=True)
self.assertEqual(
g2.sorted_source_files(),
[[self.tempdir[x]] for x in ["foo/a.py", "x.py"]])
from importlab import resolve
from importlab import utils
class TestCycle(unittest.TestCase):
"""Tests for Cycle."""
def test_flatten(self):
a = graph.Cycle([[1, 2], [2, 3], [3, 1]])
b = graph.Cycle([[4, 5], [5, 4]])
c = graph.Cycle([[a, 6], [6, b], [b, 7], [7, a]])
nodes = c.flatten_nodes()
self.assertEqual(sorted(nodes), [1, 2, 3, 4, 5, 6, 7])
class FakeImportGraph(graph.DependencyGraph):
"""An ImportGraph with file imports stubbed out.
Also adds ordered_foo() wrappers around output methods to help in testing.
"""
def __init__(self, deps, unreadable=None):
super(FakeImportGraph, self).__init__()
self.deps = deps
if unreadable:
self.unreadable = unreadable
else:
self.unreadable = set()
def get_source_file_provenance(self, filename):
return resolve.Direct(filename, 'module.name')