Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / trial / test / test_script.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 import gc
5 import StringIO, sys, types
6
7 from twisted.trial import unittest, runner
8 from twisted.scripts import trial
9 from twisted.python import util, deprecate, versions
10 from twisted.python.compat import set
11 from twisted.python.filepath import FilePath
12
13 from twisted.trial.test.test_loader import testNames
14
15 pyunit = __import__('unittest')
16
17
18 def sibpath(filename):
19     """For finding files in twisted/trial/test"""
20     return util.sibpath(__file__, filename)
21
22
23
24 class ForceGarbageCollection(unittest.TestCase):
25     """
26     Tests for the --force-gc option.
27     """
28
29     def setUp(self):
30         self.config = trial.Options()
31         self.log = []
32         self.patch(gc, 'collect', self.collect)
33         test = pyunit.FunctionTestCase(self.simpleTest)
34         self.test = runner.TestSuite([test, test])
35
36
37     def simpleTest(self):
38         """
39         A simple test method that records that it was run.
40         """
41         self.log.append('test')
42
43
44     def collect(self):
45         """
46         A replacement for gc.collect that logs calls to itself.
47         """
48         self.log.append('collect')
49
50
51     def makeRunner(self):
52         """
53         Return a L{runner.TrialRunner} object that is safe to use in tests.
54         """
55         runner = trial._makeRunner(self.config)
56         runner.stream = StringIO.StringIO()
57         return runner
58
59
60     def test_forceGc(self):
61         """
62         Passing the --force-gc option to the trial script forces the garbage
63         collector to run before and after each test.
64         """
65         self.config['force-gc'] = True
66         self.config.postOptions()
67         runner = self.makeRunner()
68         runner.run(self.test)
69         self.assertEqual(self.log, ['collect', 'test', 'collect',
70                                     'collect', 'test', 'collect'])
71
72
73     def test_unforceGc(self):
74         """
75         By default, no garbage collection is forced.
76         """
77         self.config.postOptions()
78         runner = self.makeRunner()
79         runner.run(self.test)
80         self.assertEqual(self.log, ['test', 'test'])
81
82
83
84 class TestSuiteUsed(unittest.TestCase):
85     """
86     Check the category of tests suite used by the loader.
87     """
88
89     def setUp(self):
90         """
91         Create a trial configuration object.
92         """
93         self.config = trial.Options()
94
95
96     def test_defaultSuite(self):
97         """
98         By default, the loader should use L{runner.DestructiveTestSuite}
99         """
100         loader = trial._getLoader(self.config)
101         self.assertEqual(loader.suiteFactory, runner.DestructiveTestSuite)
102
103
104     def test_untilFailureSuite(self):
105         """
106         The C{until-failure} configuration uses the L{runner.TestSuite} to keep
107         instances alive across runs.
108         """
109         self.config['until-failure'] = True
110         loader = trial._getLoader(self.config)
111         self.assertEqual(loader.suiteFactory, runner.TestSuite)
112
113
114
115 class TestModuleTest(unittest.TestCase):
116     def setUp(self):
117         self.config = trial.Options()
118
119     def tearDown(self):
120         self.config = None
121
122     def test_testNames(self):
123         """
124         Check that the testNames helper method accurately collects the
125         names of tests in suite.
126         """
127         self.assertEqual(testNames(self), [self.id()])
128
129     def assertSuitesEqual(self, test1, names):
130         loader = runner.TestLoader()
131         names1 = testNames(test1)
132         names2 = testNames(runner.TestSuite(map(loader.loadByName, names)))
133         names1.sort()
134         names2.sort()
135         self.assertEqual(names1, names2)
136
137     def test_baseState(self):
138         self.assertEqual(0, len(self.config['tests']))
139
140     def test_testmoduleOnModule(self):
141         """
142         Check that --testmodule loads a suite which contains the tests
143         referred to in test-case-name inside its parameter.
144         """
145         self.config.opt_testmodule(sibpath('moduletest.py'))
146         self.assertSuitesEqual(trial._getSuite(self.config),
147                                ['twisted.trial.test.test_test_visitor'])
148
149     def test_testmoduleTwice(self):
150         """
151         When the same module is specified with two --testmodule flags, it
152         should only appear once in the suite.
153         """
154         self.config.opt_testmodule(sibpath('moduletest.py'))
155         self.config.opt_testmodule(sibpath('moduletest.py'))
156         self.assertSuitesEqual(trial._getSuite(self.config),
157                                ['twisted.trial.test.test_test_visitor'])
158
159     def test_testmoduleOnSourceAndTarget(self):
160         """
161         If --testmodule is specified twice, once for module A and once for
162         a module which refers to module A, then make sure module A is only
163         added once.
164         """
165         self.config.opt_testmodule(sibpath('moduletest.py'))
166         self.config.opt_testmodule(sibpath('test_test_visitor.py'))
167         self.assertSuitesEqual(trial._getSuite(self.config),
168                                ['twisted.trial.test.test_test_visitor'])
169
170     def test_testmoduleOnSelfModule(self):
171         """
172         When given a module that refers to *itself* in the test-case-name
173         variable, check that --testmodule only adds the tests once.
174         """
175         self.config.opt_testmodule(sibpath('moduleself.py'))
176         self.assertSuitesEqual(trial._getSuite(self.config),
177                                ['twisted.trial.test.moduleself'])
178
179     def test_testmoduleOnScript(self):
180         """
181         Check that --testmodule loads tests referred to in test-case-name
182         buffer variables.
183         """
184         self.config.opt_testmodule(sibpath('scripttest.py'))
185         self.assertSuitesEqual(trial._getSuite(self.config),
186                                ['twisted.trial.test.test_test_visitor',
187                                 'twisted.trial.test.test_class'])
188
189     def test_testmoduleOnNonexistentFile(self):
190         """
191         Check that --testmodule displays a meaningful error message when
192         passed a non-existent filename.
193         """
194         buffy = StringIO.StringIO()
195         stderr, sys.stderr = sys.stderr, buffy
196         filename = 'test_thisbetternoteverexist.py'
197         try:
198             self.config.opt_testmodule(filename)
199             self.assertEqual(0, len(self.config['tests']))
200             self.assertEqual("File %r doesn't exist\n" % (filename,),
201                                  buffy.getvalue())
202         finally:
203             sys.stderr = stderr
204
205     def test_testmoduleOnEmptyVars(self):
206         """
207         Check that --testmodule adds no tests to the suite for modules
208         which lack test-case-name buffer variables.
209         """
210         self.config.opt_testmodule(sibpath('novars.py'))
211         self.assertEqual(0, len(self.config['tests']))
212
213     def test_testmoduleOnModuleName(self):
214         """
215         Check that --testmodule does *not* support module names as arguments
216         and that it displays a meaningful error message.
217         """
218         buffy = StringIO.StringIO()
219         stderr, sys.stderr = sys.stderr, buffy
220         moduleName = 'twisted.trial.test.test_script'
221         try:
222             self.config.opt_testmodule(moduleName)
223             self.assertEqual(0, len(self.config['tests']))
224             self.assertEqual("File %r doesn't exist\n" % (moduleName,),
225                                  buffy.getvalue())
226         finally:
227             sys.stderr = stderr
228
229     def test_parseLocalVariable(self):
230         declaration = '-*- test-case-name: twisted.trial.test.test_tests -*-'
231         localVars = trial._parseLocalVariables(declaration)
232         self.assertEqual({'test-case-name':
233                               'twisted.trial.test.test_tests'},
234                              localVars)
235
236     def test_trailingSemicolon(self):
237         declaration = '-*- test-case-name: twisted.trial.test.test_tests; -*-'
238         localVars = trial._parseLocalVariables(declaration)
239         self.assertEqual({'test-case-name':
240                               'twisted.trial.test.test_tests'},
241                              localVars)
242
243     def test_parseLocalVariables(self):
244         declaration = ('-*- test-case-name: twisted.trial.test.test_tests; '
245                        'foo: bar -*-')
246         localVars = trial._parseLocalVariables(declaration)
247         self.assertEqual({'test-case-name':
248                               'twisted.trial.test.test_tests',
249                               'foo': 'bar'},
250                              localVars)
251
252     def test_surroundingGuff(self):
253         declaration = ('## -*- test-case-name: '
254                        'twisted.trial.test.test_tests -*- #')
255         localVars = trial._parseLocalVariables(declaration)
256         self.assertEqual({'test-case-name':
257                               'twisted.trial.test.test_tests'},
258                              localVars)
259
260     def test_invalidLine(self):
261         self.failUnlessRaises(ValueError, trial._parseLocalVariables,
262                               'foo')
263
264     def test_invalidDeclaration(self):
265         self.failUnlessRaises(ValueError, trial._parseLocalVariables,
266                               '-*- foo -*-')
267         self.failUnlessRaises(ValueError, trial._parseLocalVariables,
268                               '-*- foo: bar; qux -*-')
269         self.failUnlessRaises(ValueError, trial._parseLocalVariables,
270                               '-*- foo: bar: baz; qux: qax -*-')
271
272     def test_variablesFromFile(self):
273         localVars = trial.loadLocalVariables(sibpath('moduletest.py'))
274         self.assertEqual({'test-case-name':
275                               'twisted.trial.test.test_test_visitor'},
276                              localVars)
277
278     def test_noVariablesInFile(self):
279         localVars = trial.loadLocalVariables(sibpath('novars.py'))
280         self.assertEqual({}, localVars)
281
282     def test_variablesFromScript(self):
283         localVars = trial.loadLocalVariables(sibpath('scripttest.py'))
284         self.assertEqual(
285             {'test-case-name': ('twisted.trial.test.test_test_visitor,'
286                                 'twisted.trial.test.test_class')},
287             localVars)
288
289     def test_getTestModules(self):
290         modules = trial.getTestModules(sibpath('moduletest.py'))
291         self.assertEqual(modules, ['twisted.trial.test.test_test_visitor'])
292
293     def test_getTestModules_noVars(self):
294         modules = trial.getTestModules(sibpath('novars.py'))
295         self.assertEqual(len(modules), 0)
296
297     def test_getTestModules_multiple(self):
298         modules = trial.getTestModules(sibpath('scripttest.py'))
299         self.assertEqual(set(modules),
300                              set(['twisted.trial.test.test_test_visitor',
301                                   'twisted.trial.test.test_class']))
302
303     def test_looksLikeTestModule(self):
304         for filename in ['test_script.py', 'twisted/trial/test/test_script.py']:
305             self.failUnless(trial.isTestFile(filename),
306                             "%r should be a test file" % (filename,))
307         for filename in ['twisted/trial/test/moduletest.py',
308                          sibpath('scripttest.py'), sibpath('test_foo.bat')]:
309             self.failIf(trial.isTestFile(filename),
310                         "%r should *not* be a test file" % (filename,))
311
312
313 class WithoutModuleTests(unittest.TestCase):
314     """
315     Test the C{without-module} flag.
316     """
317
318     def setUp(self):
319         """
320         Create a L{trial.Options} object to be used in the tests, and save
321         C{sys.modules}.
322         """
323         self.config = trial.Options()
324         self.savedModules = dict(sys.modules)
325
326
327     def tearDown(self):
328         """
329         Restore C{sys.modules}.
330         """
331         for module in ('imaplib', 'smtplib'):
332             if module in self.savedModules:
333                 sys.modules[module] = self.savedModules[module]
334             else:
335                 sys.modules.pop(module, None)
336
337
338     def _checkSMTP(self):
339         """
340         Try to import the C{smtplib} module, and return it.
341         """
342         import smtplib
343         return smtplib
344
345
346     def _checkIMAP(self):
347         """
348         Try to import the C{imaplib} module, and return it.
349         """
350         import imaplib
351         return imaplib
352
353
354     def test_disableOneModule(self):
355         """
356         Check that after disabling a module, it can't be imported anymore.
357         """
358         self.config.parseOptions(["--without-module", "smtplib"])
359         self.assertRaises(ImportError, self._checkSMTP)
360         # Restore sys.modules
361         del sys.modules["smtplib"]
362         # Then the function should succeed
363         self.assertIsInstance(self._checkSMTP(), types.ModuleType)
364
365
366     def test_disableMultipleModules(self):
367         """
368         Check that several modules can be disabled at once.
369         """
370         self.config.parseOptions(["--without-module", "smtplib,imaplib"])
371         self.assertRaises(ImportError, self._checkSMTP)
372         self.assertRaises(ImportError, self._checkIMAP)
373         # Restore sys.modules
374         del sys.modules["smtplib"]
375         del sys.modules["imaplib"]
376         # Then the functions should succeed
377         self.assertIsInstance(self._checkSMTP(), types.ModuleType)
378         self.assertIsInstance(self._checkIMAP(), types.ModuleType)
379
380
381     def test_disableAlreadyImportedModule(self):
382         """
383         Disabling an already imported module should produce a warning.
384         """
385         self.assertIsInstance(self._checkSMTP(), types.ModuleType)
386         self.assertWarns(RuntimeWarning,
387                 "Module 'smtplib' already imported, disabling anyway.",
388                 trial.__file__,
389                 self.config.parseOptions, ["--without-module", "smtplib"])
390         self.assertRaises(ImportError, self._checkSMTP)
391
392
393
394 class CoverageTests(unittest.TestCase):
395     """
396     Tests for the I{coverage} option.
397     """
398     if getattr(sys, 'gettrace', None) is None:
399         skip = (
400             "Cannot test trace hook installation without inspection API.")
401
402     def setUp(self):
403         """
404         Arrange for the current trace hook to be restored when the
405         test is complete.
406         """
407         self.addCleanup(sys.settrace, sys.gettrace())
408
409
410     def test_tracerInstalled(self):
411         """
412         L{trial.Options} handles C{"--coverage"} by installing a trace
413         hook to record coverage information.
414         """
415         options = trial.Options()
416         options.parseOptions(["--coverage"])
417         self.assertEqual(sys.gettrace(), options.tracer.globaltrace)
418
419
420     def test_coverdirDefault(self):
421         """
422         L{trial.Options.coverdir} returns a L{FilePath} based on the default
423         for the I{temp-directory} option if that option is not specified.
424         """
425         options = trial.Options()
426         self.assertEqual(
427             options.coverdir(),
428             FilePath(".").descendant([options["temp-directory"], "coverage"]))
429
430
431     def test_coverdirOverridden(self):
432         """
433         If a value is specified for the I{temp-directory} option,
434         L{trial.Options.coverdir} returns a child of that path.
435         """
436         path = self.mktemp()
437         options = trial.Options()
438         options.parseOptions(["--temp-directory", path])
439         self.assertEqual(
440             options.coverdir(), FilePath(path).child("coverage"))
441
442
443 class ExtraTests(unittest.TestCase):
444     """
445     Tests for the I{extra} option.
446     """
447
448     def setUp(self):
449         self.config = trial.Options()
450
451
452     def tearDown(self):
453         self.config = None
454
455
456     def assertDeprecationWarning(self, deprecatedCallable, warnings):
457         """
458         Check for a deprecation warning
459         """
460         self.assertEqual(len(warnings), 1)
461         self.assertEqual(warnings[0]['category'], DeprecationWarning)
462         self.assertEqual(warnings[0]['message'], 
463                           deprecate.getDeprecationWarningString(
464                               deprecatedCallable, versions.Version('Twisted', 11, 0, 0)))
465
466
467     def test_extraDeprecation(self):
468         """
469         Check that --extra  will emit a deprecation warning
470         """
471         self.config.opt_extra('some.sample.test')
472         self.assertDeprecationWarning(self.config.opt_extra,
473                                       self.flushWarnings([self.test_extraDeprecation]))
474
475     def test_xDeprecation(self):
476         """
477         Check that -x will emit a deprecation warning
478         """
479         self.config.opt_x('some.sample.text')
480         self.assertDeprecationWarning(self.config.opt_extra,
481                                       self.flushWarnings([self.test_xDeprecation]))
482