summaryrefslogtreecommitdiff
path: root/unit_tests/test_importer.py
blob: b8fd596a3e4e5e973142937ee311ea2f68d05ae6 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import sys
import unittest
import nose.config
import nose.importer

class TestImporter(unittest.TestCase):

    def setUp(self):
        self.p = sys.path[:]

    def tearDown(self):
        sys.path = self.p[:]
    
    def test_add_paths(self):
        where = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                             'support'))
        foo = os.path.join(where, 'foo')
        foobar = os.path.join(foo, 'bar')
        nose.importer.add_path(foobar)
        
        assert not foobar in sys.path
        assert not foo in sys.path
        assert where in sys.path
        assert sys.path[0] == where, "%s first should be %s" % (sys.path, where)

    def test_import(self):
        where = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                             'support'))
        foo = os.path.join(where, 'foo')
        foobar = os.path.join(foo, 'bar')

        imp = nose.importer.Importer()
        mod = imp.importFromDir(foobar, 'buz')
        assert where in sys.path
        # buz has an intra-package import that sets boodle
        assert mod.boodle

    def test_module_no_file(self):
        where = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                             'support'))
        foo = os.path.join(where, 'foo')
        foobar = os.path.join(foo, 'bar')

        # something that's not a real module and has no __file__
        sys.modules['buz'] = 'Whatever'

        imp = nose.importer.Importer()
        mod = imp.importFromDir(foobar, 'buz')
        assert where in sys.path
        # buz has an intra-package import that sets boodle
        assert mod.boodle

    def test_module_init_prefix(self):
        where = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                             'support', 'init_prefix_bug'))
        nose.importer.add_path(where)
        mod = os.path.join(where, '__init__not.py')
        fqname = 'init_prefix_bug.__init__not'

        imp = nose.importer.Importer()
        mod = imp.importFromPath(mod, fqname)
        
if __name__ == '__main__':
    unittest.main()