aboutsummaryrefslogtreecommitdiff
path: root/catapult/telemetry/third_party/modulegraph/modulegraph_tests/test_relimport2.py
blob: 0a465a28075c4e0f504cd1449b5fa5023f1be759 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Test for import machinery
"""
import unittest
import sys
import textwrap
import subprocess
import os
from modulegraph import modulegraph

class TestModuleGraphImport (unittest.TestCase):
    if not hasattr(unittest.TestCase, 'assertIsInstance'):
        def assertIsInstance(self, value, types):
            if not isinstance(value, types):
                self.fail("%r is not an instance of %r"%(value, types))

    def setUp(self):
        self.root = os.path.join(
                os.path.dirname(os.path.abspath(__file__)),
                'testpkg-relimport2')
        self.mf = modulegraph.ModuleGraph(path=[ self.root ] + sys.path)


    def test_init_as_script(self):
        self.mf.run_script(os.path.join(self.root, 'pkg/__init__.py'))
        n = self.mf.findNode('mod1')
        self.assertIs(n, None)

        n = self.mf.findNode('mod2')
        self.assertIsInstance(n, modulegraph.MissingModule)

    def test_subpkg_bad_import(self):
        self.mf.import_hook('pkg.sub')

        n = self.mf.findNode('toplevel')
        self.assertIs(n, None)

        n = self.mf.findNode('pkg.mod1')
        self.assertIsInstance(n, modulegraph.SourceModule)

        n = self.mf.findNode('pkg.mod3')
        self.assertIsInstance(n, modulegraph.SourceModule)

if __name__ == "__main__":
    unittest.main()