summaryrefslogtreecommitdiff
path: root/git/test/test_import.py
blob: 606d4b03e8c53b09e274d6a3a147f82bc2cf8777 (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
# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors
#
# This module is part of GitDB and is released under
# the New BSD License: http://www.opensource.org/licenses/bsd-license.php
"""This module's whole purpose is to verify the __all__ descriptions in the respective
module, by importing using from x import *"""

# perform the actual imports
import os

from git import *

def import_all(topdir, topmodule='git', skip = "test"):
    base = os.path.basename
    join = os.path.join
    init_script = '__init__.py'
    prev_cwd = os.getcwd()
    try:
        os.chdir(os.path.dirname(topdir))
        for root, dirs, files in os.walk(base(topdir)):
            if init_script not in files:
                del(dirs[:])
                continue
            #END ignore non-packages
            
            if skip in root:
                continue
            #END handle ignores
            
            for relafile in files:
                if not relafile.endswith('.py'):
                    continue
                if relafile == init_script:
                    continue
                module_path = join(root, os.path.splitext(relafile)[0]).replace("/", ".").replace("\\", ".")
                
                m = __import__(module_path, globals(), locals(), [""])
                try:
                    attrlist = m.__all__
                    for attr in attrlist:
                        assert hasattr(m, attr), "Invalid item in %s.__all__: %s" % (module_path, attr) 
                    #END veriy
                except AttributeError:
                    pass
                # END try each listed attribute
            #END for each file in dir
        #END for each item
    finally:
        os.chdir(prev_cwd)
    #END handle previous currentdir
    
    

class TestDummy(object):
    def test_base(self):
        dn = os.path.dirname
        # NOTE: i don't think this is working, as the __all__ variable is not used in this case
        import_all(dn(dn(__file__)))