sources for test_apigen_functional.py [rev. unknown]
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
""" functional test for apigen.py
    script to build api + source docs from py.test
"""
import py
from py.__.apigen import apigen
def setup_module(mod):
    if py.std.sys.platform == "win32":
        py.test.skip("not supported with win32 yet")
def setup_fs_project(name):
    temp = py.test.ensuretemp(name)
    assert temp.listdir() == []
    temp.ensure("pak/func.py").write(py.code.Source("""\
        def func(arg1):
            "docstring"
        def func_2(arg1, arg2):
            return arg1(arg2)
    """))
    temp.ensure('pak/sometestclass.py').write(py.code.Source("""\
        class SomeTestClass(object):
            " docstring sometestclass "
            someattr = 'somevalue'
            def __init__(self, somevar):
                self.somevar = somevar
                
            def get_somevar(self):
                " get_somevar docstring "
                return self.somevar
            
            def get_some_source(self):
                ret = py.code.Source('''\\
                    def foo():
                      return 'bar'
                ''')
                return ret
    """))
    temp.ensure('pak/sometestsubclass.py').write(py.code.Source("""\
        from sometestclass import SomeTestClass
        class SomeTestSubClass(SomeTestClass):
            " docstring sometestsubclass "
            def get_somevar(self):
                return self.somevar + 1
    """))
    temp.ensure('pak/somenamespace.py').write(py.code.Source("""\
        def foo():
            return 'bar'
        def baz(qux):
            return qux
        def _hidden():
            return 'quux'
    """))
    temp.ensure("pak/__init__.py").write(py.code.Source("""\
        '''pkg docstring'''
        from py.initpkg import initpkg
        initpkg(__name__,
                long_description=globals()['__doc__'],
                exportdefs={'main.sub.func': ("./func.py", "func"),
                            'main.func': ("./func.py", "func_2"),
                            'main.SomeTestClass': ('./sometestclass.py',
                                                   'SomeTestClass'),
                            'main.SomeTestSubClass': ('./sometestsubclass.py',
                                                      'SomeTestSubClass'),
                            'somenamespace': ('./somenamespace.py', '*')})
    """))
    temp.ensure('apigen.py').write(py.code.Source("""\
        import py
        py.std.sys.path.insert(0,
            py.magic.autopath().dirpath().dirpath().dirpath().strpath)
        from py.__.apigen.apigen import build, \
            get_documentable_items_pkgdir as get_documentable_items
    """))
    temp.ensure('pak/test/test_pak.py').write(py.code.Source("""\
        import py
        py.std.sys.path.insert(0,
            py.magic.autopath().dirpath().dirpath().dirpath().strpath)
        import pak
        # this mainly exists to provide some data to the tracer
        def test_pak():
            s = pak.main.SomeTestClass(10)
            assert s.get_somevar() == 10
            s = pak.main.SomeTestClass('10')
            assert s.get_somevar() == '10'
            s = pak.main.SomeTestSubClass(10)
            assert s.get_somevar() == 11
            s = pak.main.SomeTestSubClass('10')
            py.test.raises(TypeError, 's.get_somevar()')
            assert pak.main.sub.func(10) is None
            assert pak.main.sub.func(20) is None
            s = pak.main.func(pak.main.SomeTestClass, 10)
            assert isinstance(s, pak.main.SomeTestClass)
            # some nice things to confuse the tracer/storage
            source = py.code.Source('''\
                pak.main.sub.func(10)
            ''')
            c = compile(str(source), '<test>', 'exec')
            exec c in globals()
            assert pak.somenamespace._hidden() == 'quux'
            # this just to see a multi-level stack in the docs
            def foo():
                return pak.main.sub.func(10)
            assert foo() is None
    """))
    return temp, 'pak'
def test_get_documentable_items():
    fs_root, package_name = setup_fs_project('test_get_documentable_items')
    pkgname, documentable = apigen.get_documentable_items_pkgdir(
                                               fs_root.join(package_name))
    assert pkgname == 'pak'
    keys = documentable.keys()
    keys.sort()
    assert keys ==  [
        'main.SomeTestClass', 'main.SomeTestSubClass', 'main.func',
        'main.sub.func', 'somenamespace.baz', 'somenamespace.foo']
def test_apigen_functional():
    #if py.std.sys.platform == "win32":
    #    py.test.skip("XXX test fails on windows")
    fs_root, package_name = setup_fs_project('test_apigen_functional')
    tempdir = py.test.ensuretemp('test_apigen_functional_results')
    pydir = py.magic.autopath().dirpath().dirpath().dirpath()
    pakdir = fs_root.join('pak')
    if py.std.sys.platform == 'win32':
        cmd = ('set APIGENPATH=%s && set PYTHONPATH=%s && '
               'python "%s/bin/py.test"') % (tempdir, fs_root, pydir)
    else:
        cmd = ('APIGENPATH="%s" PYTHONPATH="%s" '
               'python "%s/bin/py.test"') % (tempdir, fs_root, pydir)
    try:
        output = py.process.cmdexec('%s --apigen="%s/apigen.py" "%s"' % (
                                        cmd, fs_root, pakdir))
    except py.error.Error, e:
        print e.out
        raise
    assert output.lower().find('traceback') == -1
    # just some quick content checks
    apidir = tempdir.join('api')
    assert apidir.check(dir=True)
    sometestclass_api = apidir.join('main.SomeTestClass.html')
    assert sometestclass_api.check(file=True)
    html = sometestclass_api.read()
    print html
    assert '<a href="main.SomeTestClass.html">SomeTestClass</a>' in html
    assert 'someattr: <em>somevalue</em>' in html
    
    namespace_api = apidir.join('main.html')
    assert namespace_api.check(file=True)
    html = namespace_api.read()
    assert '<a href="main.SomeTestClass.html">SomeTestClass</a>' in html
    index = apidir.join('index.html')
    assert index.check(file=True)
    html = index.read()
    assert 'pkg docstring' in html
    sourcedir = tempdir.join('source')
    assert sourcedir.check(dir=True)
    sometestclass_source = sourcedir.join('sometestclass.py.html')
    assert sometestclass_source.check(file=True)
    html = sometestclass_source.read()
    assert '<div class="project_title">sources for sometestclass.py</div>' in html
    index = sourcedir.join('index.html')
    assert index.check(file=True)
    html = index.read()
    print html
    assert '<a href="test/index.html">test</a>' in html
    assert 'href="../../py/doc/home.html"'