Statistics
| Branch: | Revision:

root / env / lib / python2.7 / site-packages / distribute-0.6.19-py2.7.egg / setuptools / tests / __init__.py @ 1a305335

History | View | Annotate | Download (12 KB)

1
"""Tests for the 'setuptools' package"""
2
from unittest import TestSuite, TestCase, makeSuite, defaultTestLoader
3
import distutils.core, distutils.cmd
4
from distutils.errors import DistutilsOptionError, DistutilsPlatformError
5
from distutils.errors import DistutilsSetupError
6
import setuptools, setuptools.dist
7
from setuptools import Feature
8
from distutils.core import Extension
9
extract_constant, get_module_constant = None, None
10
from setuptools.depends import *
11
from distutils.version import StrictVersion, LooseVersion
12
from distutils.util import convert_path
13
import sys, os.path
14

    
15
def additional_tests():
16
    import doctest, unittest
17
    suite = unittest.TestSuite((
18
        doctest.DocFileSuite(
19
            os.path.join('tests', 'api_tests.txt'),
20
            optionflags=doctest.ELLIPSIS, package='pkg_resources',
21
            ),
22
        ))
23
    if sys.platform == 'win32':
24
        suite.addTest(doctest.DocFileSuite('win_script_wrapper.txt'))
25
    return suite
26

    
27
def makeSetup(**args):
28
    """Return distribution from 'setup(**args)', without executing commands"""
29

    
30
    distutils.core._setup_stop_after = "commandline"
31

    
32
    # Don't let system command line leak into tests!
33
    args.setdefault('script_args',['install'])
34

    
35
    try:
36
        return setuptools.setup(**args)
37
    finally:
38
        distutils.core_setup_stop_after = None
39

    
40

    
41

    
42

    
43
class DependsTests(TestCase):
44

    
45
    def testExtractConst(self):
46
        if not extract_constant: return  # skip on non-bytecode platforms
47

    
48
        def f1():
49
            global x,y,z
50
            x = "test"
51
            y = z
52

    
53
        # unrecognized name
54
        self.assertEqual(extract_constant(f1.func_code,'q', -1), None)
55

    
56
        # constant assigned
57
        self.assertEqual(extract_constant(f1.func_code,'x', -1), "test")
58

    
59
        # expression assigned
60
        self.assertEqual(extract_constant(f1.func_code,'y', -1), -1)
61

    
62
        # recognized name, not assigned
63
        self.assertEqual(extract_constant(f1.func_code,'z', -1), None)
64

    
65

    
66
    def testFindModule(self):
67
        self.assertRaises(ImportError, find_module, 'no-such.-thing')
68
        self.assertRaises(ImportError, find_module, 'setuptools.non-existent')
69
        f,p,i = find_module('setuptools.tests'); f.close()
70

    
71
    def testModuleExtract(self):
72
        if not get_module_constant: return  # skip on non-bytecode platforms
73
        from email import __version__
74
        self.assertEqual(
75
            get_module_constant('email','__version__'), __version__
76
        )
77
        self.assertEqual(
78
            get_module_constant('sys','version'), sys.version
79
        )
80
        self.assertEqual(
81
            get_module_constant('setuptools.tests','__doc__'),__doc__
82
        )
83

    
84
    def testRequire(self):
85
        if not extract_constant: return  # skip on non-bytecode platforms
86

    
87
        req = Require('Email','1.0.3','email')
88

    
89
        self.assertEqual(req.name, 'Email')
90
        self.assertEqual(req.module, 'email')
91
        self.assertEqual(req.requested_version, '1.0.3')
92
        self.assertEqual(req.attribute, '__version__')
93
        self.assertEqual(req.full_name(), 'Email-1.0.3')
94

    
95
        from email import __version__
96
        self.assertEqual(req.get_version(), __version__)
97
        self.assert_(req.version_ok('1.0.9'))
98
        self.assert_(not req.version_ok('0.9.1'))
99
        self.assert_(not req.version_ok('unknown'))
100

    
101
        self.assert_(req.is_present())
102
        self.assert_(req.is_current())
103

    
104
        req = Require('Email 3000','03000','email',format=LooseVersion)
105
        self.assert_(req.is_present())
106
        self.assert_(not req.is_current())
107
        self.assert_(not req.version_ok('unknown'))
108

    
109
        req = Require('Do-what-I-mean','1.0','d-w-i-m')
110
        self.assert_(not req.is_present())
111
        self.assert_(not req.is_current())
112

    
113
        req = Require('Tests', None, 'tests', homepage="http://example.com")
114
        self.assertEqual(req.format, None)
115
        self.assertEqual(req.attribute, None)
116
        self.assertEqual(req.requested_version, None)
117
        self.assertEqual(req.full_name(), 'Tests')
118
        self.assertEqual(req.homepage, 'http://example.com')
119

    
120
        paths = [os.path.dirname(p) for p in __path__]
121
        self.assert_(req.is_present(paths))
122
        self.assert_(req.is_current(paths))
123

    
124

    
125
class DistroTests(TestCase):
126

    
127
    def setUp(self):
128
        self.e1 = Extension('bar.ext',['bar.c'])
129
        self.e2 = Extension('c.y', ['y.c'])
130

    
131
        self.dist = makeSetup(
132
            packages=['a', 'a.b', 'a.b.c', 'b', 'c'],
133
            py_modules=['b.d','x'],
134
            ext_modules = (self.e1, self.e2),
135
            package_dir = {},
136
        )
137

    
138

    
139
    def testDistroType(self):
140
        self.assert_(isinstance(self.dist,setuptools.dist.Distribution))
141

    
142

    
143
    def testExcludePackage(self):
144
        self.dist.exclude_package('a')
145
        self.assertEqual(self.dist.packages, ['b','c'])
146

    
147
        self.dist.exclude_package('b')
148
        self.assertEqual(self.dist.packages, ['c'])
149
        self.assertEqual(self.dist.py_modules, ['x'])
150
        self.assertEqual(self.dist.ext_modules, [self.e1, self.e2])
151

    
152
        self.dist.exclude_package('c')
153
        self.assertEqual(self.dist.packages, [])
154
        self.assertEqual(self.dist.py_modules, ['x'])
155
        self.assertEqual(self.dist.ext_modules, [self.e1])
156

    
157
        # test removals from unspecified options
158
        makeSetup().exclude_package('x')
159

    
160

    
161

    
162

    
163

    
164

    
165

    
166
    def testIncludeExclude(self):
167
        # remove an extension
168
        self.dist.exclude(ext_modules=[self.e1])
169
        self.assertEqual(self.dist.ext_modules, [self.e2])
170

    
171
        # add it back in
172
        self.dist.include(ext_modules=[self.e1])
173
        self.assertEqual(self.dist.ext_modules, [self.e2, self.e1])
174

    
175
        # should not add duplicate
176
        self.dist.include(ext_modules=[self.e1])
177
        self.assertEqual(self.dist.ext_modules, [self.e2, self.e1])
178

    
179
    def testExcludePackages(self):
180
        self.dist.exclude(packages=['c','b','a'])
181
        self.assertEqual(self.dist.packages, [])
182
        self.assertEqual(self.dist.py_modules, ['x'])
183
        self.assertEqual(self.dist.ext_modules, [self.e1])
184

    
185
    def testEmpty(self):
186
        dist = makeSetup()
187
        dist.include(packages=['a'], py_modules=['b'], ext_modules=[self.e2])
188
        dist = makeSetup()
189
        dist.exclude(packages=['a'], py_modules=['b'], ext_modules=[self.e2])
190

    
191
    def testContents(self):
192
        self.assert_(self.dist.has_contents_for('a'))
193
        self.dist.exclude_package('a')
194
        self.assert_(not self.dist.has_contents_for('a'))
195

    
196
        self.assert_(self.dist.has_contents_for('b'))
197
        self.dist.exclude_package('b')
198
        self.assert_(not self.dist.has_contents_for('b'))
199

    
200
        self.assert_(self.dist.has_contents_for('c'))
201
        self.dist.exclude_package('c')
202
        self.assert_(not self.dist.has_contents_for('c'))
203

    
204

    
205

    
206

    
207
    def testInvalidIncludeExclude(self):
208
        self.assertRaises(DistutilsSetupError,
209
            self.dist.include, nonexistent_option='x'
210
        )
211
        self.assertRaises(DistutilsSetupError,
212
            self.dist.exclude, nonexistent_option='x'
213
        )
214
        self.assertRaises(DistutilsSetupError,
215
            self.dist.include, packages={'x':'y'}
216
        )
217
        self.assertRaises(DistutilsSetupError,
218
            self.dist.exclude, packages={'x':'y'}
219
        )
220
        self.assertRaises(DistutilsSetupError,
221
            self.dist.include, ext_modules={'x':'y'}
222
        )
223
        self.assertRaises(DistutilsSetupError,
224
            self.dist.exclude, ext_modules={'x':'y'}
225
        )
226

    
227
        self.assertRaises(DistutilsSetupError,
228
            self.dist.include, package_dir=['q']
229
        )
230
        self.assertRaises(DistutilsSetupError,
231
            self.dist.exclude, package_dir=['q']
232
        )
233

    
234

    
235

    
236

    
237

    
238

    
239

    
240

    
241

    
242

    
243

    
244

    
245

    
246

    
247

    
248
class FeatureTests(TestCase):
249

    
250
    def setUp(self):
251
        self.req = Require('Distutils','1.0.3','distutils')
252
        self.dist = makeSetup(
253
            features={
254
                'foo': Feature("foo",standard=True,require_features=['baz',self.req]),
255
                'bar': Feature("bar",  standard=True, packages=['pkg.bar'],
256
                               py_modules=['bar_et'], remove=['bar.ext'],
257
                       ),
258
                'baz': Feature(
259
                        "baz", optional=False, packages=['pkg.baz'],
260
                        scripts = ['scripts/baz_it'],
261
                        libraries=[('libfoo','foo/foofoo.c')]
262
                       ),
263
                'dwim': Feature("DWIM", available=False, remove='bazish'),
264
            },
265
            script_args=['--without-bar', 'install'],
266
            packages = ['pkg.bar', 'pkg.foo'],
267
            py_modules = ['bar_et', 'bazish'],
268
            ext_modules = [Extension('bar.ext',['bar.c'])]
269
        )
270

    
271
    def testDefaults(self):
272
        self.assert_(not
273
            Feature(
274
                "test",standard=True,remove='x',available=False
275
            ).include_by_default()
276
        )
277
        self.assert_(
278
            Feature("test",standard=True,remove='x').include_by_default()
279
        )
280
        # Feature must have either kwargs, removes, or require_features
281
        self.assertRaises(DistutilsSetupError, Feature, "test")
282

    
283
    def testAvailability(self):
284
        self.assertRaises(
285
            DistutilsPlatformError,
286
            self.dist.features['dwim'].include_in, self.dist
287
        )
288

    
289
    def testFeatureOptions(self):
290
        dist = self.dist
291
        self.assert_(
292
            ('with-dwim',None,'include DWIM') in dist.feature_options
293
        )
294
        self.assert_(
295
            ('without-dwim',None,'exclude DWIM (default)') in dist.feature_options
296
        )
297
        self.assert_(
298
            ('with-bar',None,'include bar (default)') in dist.feature_options
299
        )
300
        self.assert_(
301
            ('without-bar',None,'exclude bar') in dist.feature_options
302
        )
303
        self.assertEqual(dist.feature_negopt['without-foo'],'with-foo')
304
        self.assertEqual(dist.feature_negopt['without-bar'],'with-bar')
305
        self.assertEqual(dist.feature_negopt['without-dwim'],'with-dwim')
306
        self.assert_(not 'without-baz' in dist.feature_negopt)
307

    
308
    def testUseFeatures(self):
309
        dist = self.dist
310
        self.assertEqual(dist.with_foo,1)
311
        self.assertEqual(dist.with_bar,0)
312
        self.assertEqual(dist.with_baz,1)
313
        self.assert_(not 'bar_et' in dist.py_modules)
314
        self.assert_(not 'pkg.bar' in dist.packages)
315
        self.assert_('pkg.baz' in dist.packages)
316
        self.assert_('scripts/baz_it' in dist.scripts)
317
        self.assert_(('libfoo','foo/foofoo.c') in dist.libraries)
318
        self.assertEqual(dist.ext_modules,[])
319
        self.assertEqual(dist.require_features, [self.req])
320

    
321
        # If we ask for bar, it should fail because we explicitly disabled
322
        # it on the command line
323
        self.assertRaises(DistutilsOptionError, dist.include_feature, 'bar')
324

    
325
    def testFeatureWithInvalidRemove(self):
326
        self.assertRaises(
327
            SystemExit, makeSetup, features = {'x':Feature('x', remove='y')}
328
        )
329

    
330
class TestCommandTests(TestCase):
331

    
332
    def testTestIsCommand(self):
333
        test_cmd = makeSetup().get_command_obj('test')
334
        self.assert_(isinstance(test_cmd, distutils.cmd.Command))
335

    
336
    def testLongOptSuiteWNoDefault(self):
337
        ts1 = makeSetup(script_args=['test','--test-suite=foo.tests.suite'])
338
        ts1 = ts1.get_command_obj('test')
339
        ts1.ensure_finalized()
340
        self.assertEqual(ts1.test_suite, 'foo.tests.suite')
341

    
342
    def testDefaultSuite(self):
343
        ts2 = makeSetup(test_suite='bar.tests.suite').get_command_obj('test')
344
        ts2.ensure_finalized()
345
        self.assertEqual(ts2.test_suite, 'bar.tests.suite')
346

    
347
    def testDefaultWModuleOnCmdLine(self):
348
        ts3 = makeSetup(
349
            test_suite='bar.tests',
350
            script_args=['test','-m','foo.tests']
351
        ).get_command_obj('test')
352
        ts3.ensure_finalized()
353
        self.assertEqual(ts3.test_module, 'foo.tests')
354
        self.assertEqual(ts3.test_suite,  'foo.tests.test_suite')
355

    
356
    def testConflictingOptions(self):
357
        ts4 = makeSetup(
358
            script_args=['test','-m','bar.tests', '-s','foo.tests.suite']
359
        ).get_command_obj('test')
360
        self.assertRaises(DistutilsOptionError, ts4.ensure_finalized)
361

    
362
    def testNoSuite(self):
363
        ts5 = makeSetup().get_command_obj('test')
364
        ts5.ensure_finalized()
365
        self.assertEqual(ts5.test_suite, None)
366

    
367

    
368

    
369

    
370