Home | History | Annotate | Download | only in modulegraph_tests
      1 import unittest
      2 
      3 import os, shutil, sys
      4 
      5 from modulegraph import modulegraph
      6 
      7 class ImpliesTestCase(unittest.TestCase):
      8     if not hasattr(unittest.TestCase, 'assertIsInstance'):
      9         def assertIsInstance(self, object, types, message=None):
     10             self.assertTrue(isinstance(object, types),
     11                     message or '%r is not an instance of %r'%(object, types))
     12 
     13     def testBasicImplies(self):
     14         root = os.path.join(
     15                 os.path.dirname(os.path.abspath(__file__)),
     16                 'testpkg-relimport')
     17 
     18         # First check that 'syslog' isn't accidently in the graph:
     19         mg = modulegraph.ModuleGraph(path=[root]+sys.path)
     20         mg.run_script(os.path.join(root, 'script.py'))
     21         node = mg.findNode('mod')
     22         self.assertIsInstance(node, modulegraph.SourceModule)
     23 
     24         node = mg.findNode('syslog')
     25         self.assertEqual(node, None)
     26 
     27         # Now check that adding an implied dependency actually adds
     28         # 'syslog' to the graph:
     29         mg = modulegraph.ModuleGraph(path=[root]+sys.path, implies={
     30             'mod': ['syslog']})
     31         self.assertEqual(node, None)
     32         mg.run_script(os.path.join(root, 'script.py'))
     33         node = mg.findNode('mod')
     34         self.assertIsInstance(node, modulegraph.SourceModule)
     35 
     36         node = mg.findNode('syslog')
     37         self.assertIsInstance(node, modulegraph.Extension)
     38 
     39         # Check that the edges are correct:
     40         self.assertTrue(mg.findNode('mod') in mg.get_edges(node)[1])
     41         self.assertTrue(node in mg.get_edges(mg.findNode('mod'))[0])
     42 
     43     def testPackagedImplies(self):
     44         root = os.path.join(
     45                 os.path.dirname(os.path.abspath(__file__)),
     46                 'testpkg-relimport')
     47 
     48         # First check that 'syslog' isn't accidently in the graph:
     49         mg = modulegraph.ModuleGraph(path=[root]+sys.path)
     50         mg.run_script(os.path.join(root, 'script.py'))
     51         node = mg.findNode('mod')
     52         self.assertIsInstance(node, modulegraph.SourceModule)
     53 
     54         node = mg.findNode('syslog')
     55         self.assertEqual(node, None)
     56 
     57 
     58         # Now check that adding an implied dependency actually adds
     59         # 'syslog' to the graph:
     60         mg = modulegraph.ModuleGraph(path=[root]+sys.path, implies={
     61             'pkg.relative': ['syslog']})
     62         node = mg.findNode('syslog')
     63         self.assertEqual(node, None)
     64 
     65         mg.run_script(os.path.join(root, 'script.py'))
     66         node = mg.findNode('pkg.relative')
     67         self.assertIsInstance(node, modulegraph.SourceModule)
     68 
     69         node = mg.findNode('syslog')
     70         self.assertIsInstance(node, modulegraph.Extension)
     71 
     72         # Check that the edges are correct:
     73         self.assertTrue(mg.findNode('pkg.relative') in mg.get_edges(node)[1])
     74         self.assertTrue(node in mg.get_edges(mg.findNode('pkg.relative'))[0])
     75 
     76 
     77 if __name__ == '__main__':
     78     unittest.main()
     79