Imported Upstream version 12.1.0
[contrib/python-twisted.git] / twisted / trial / test / test_output.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Tests for the output generated by trial.
6 """
7
8 import os, StringIO
9
10 from twisted.scripts import trial
11 from twisted.trial import runner
12 from twisted.trial.test import packages
13
14
15 def runTrial(*args):
16     from twisted.trial import reporter
17     config = trial.Options()
18     config.parseOptions(args)
19     output = StringIO.StringIO()
20     myRunner = runner.TrialRunner(
21         reporter.VerboseTextReporter,
22         stream=output,
23         workingDirectory=config['temp-directory'])
24     suite = trial._getSuite(config)
25     result = myRunner.run(suite)
26     return output.getvalue()
27
28
29 class TestImportErrors(packages.SysPathManglingTest):
30     """Actually run trial as if on the command line and check that the output
31     is what we expect.
32     """
33
34     debug = False
35     parent = "_testImportErrors"
36     def runTrial(self, *args):
37         return runTrial('--temp-directory', self.mktemp(), *args)
38
39     def _print(self, stuff):
40         print stuff
41         return stuff
42
43     def failUnlessIn(self, container, containee, *args, **kwargs):
44         # redefined to be useful in callbacks
45         super(TestImportErrors, self).failUnlessIn(
46             containee, container, *args, **kwargs)
47         return container
48
49     def failIfIn(self, container, containee, *args, **kwargs):
50         # redefined to be useful in callbacks
51         super(TestImportErrors, self).failIfIn(
52             containee, container, *args, **kwargs)
53         return container
54
55     def test_trialRun(self):
56         self.runTrial()
57
58     def test_nonexistentModule(self):
59         d = self.runTrial('twisted.doesntexist')
60         self.failUnlessIn(d, '[ERROR]')
61         self.failUnlessIn(d, 'twisted.doesntexist')
62         return d
63
64     def test_nonexistentPackage(self):
65         d = self.runTrial('doesntexist')
66         self.failUnlessIn(d, 'doesntexist')
67         self.failUnlessIn(d, 'ModuleNotFound')
68         self.failUnlessIn(d, '[ERROR]')
69         return d
70
71     def test_nonexistentPackageWithModule(self):
72         d = self.runTrial('doesntexist.barney')
73         self.failUnlessIn(d, 'doesntexist.barney')
74         self.failUnlessIn(d, 'ObjectNotFound')
75         self.failUnlessIn(d, '[ERROR]')
76         return d
77
78     def test_badpackage(self):
79         d = self.runTrial('badpackage')
80         self.failUnlessIn(d, '[ERROR]')
81         self.failUnlessIn(d, 'badpackage')
82         self.failIfIn(d, 'IOError')
83         return d
84
85     def test_moduleInBadpackage(self):
86         d = self.runTrial('badpackage.test_module')
87         self.failUnlessIn(d, "[ERROR]")
88         self.failUnlessIn(d, "badpackage.test_module")
89         self.failIfIn(d, 'IOError')
90         return d
91
92     def test_badmodule(self):
93         d = self.runTrial('package.test_bad_module')
94         self.failUnlessIn(d, '[ERROR]')
95         self.failUnlessIn(d, 'package.test_bad_module')
96         self.failIfIn(d, 'IOError')
97         self.failIfIn(d, '<module ')
98         return d
99
100     def test_badimport(self):
101         d = self.runTrial('package.test_import_module')
102         self.failUnlessIn(d, '[ERROR]')
103         self.failUnlessIn(d, 'package.test_import_module')
104         self.failIfIn(d, 'IOError')
105         self.failIfIn(d, '<module ')
106         return d
107
108     def test_recurseImport(self):
109         d = self.runTrial('package')
110         self.failUnlessIn(d, '[ERROR]')
111         self.failUnlessIn(d, 'test_bad_module')
112         self.failUnlessIn(d, 'test_import_module')
113         self.failIfIn(d, '<module ')
114         self.failIfIn(d, 'IOError')
115         return d
116
117     def test_recurseImportErrors(self):
118         d = self.runTrial('package2')
119         self.failUnlessIn(d, '[ERROR]')
120         self.failUnlessIn(d, 'package2')
121         self.failUnlessIn(d, 'test_module')
122         self.failUnlessIn(d, "No module named frotz")
123         self.failIfIn(d, '<module ')
124         self.failIfIn(d, 'IOError')
125         return d
126
127     def test_nonRecurseImportErrors(self):
128         d = self.runTrial('-N', 'package2')
129         self.failUnlessIn(d, '[ERROR]')
130         self.failUnlessIn(d, "No module named frotz")
131         self.failIfIn(d, '<module ')
132         return d
133
134     def test_regularRun(self):
135         d = self.runTrial('package.test_module')
136         self.failIfIn(d, '[ERROR]')
137         self.failIfIn(d, 'IOError')
138         self.failUnlessIn(d, 'OK')
139         self.failUnlessIn(d, 'PASSED (successes=1)')
140         return d
141
142     def test_filename(self):
143         self.mangleSysPath(self.oldPath)
144         d = self.runTrial(
145             os.path.join(self.parent, 'package', 'test_module.py'))
146         self.failIfIn(d, '[ERROR]')
147         self.failIfIn(d, 'IOError')
148         self.failUnlessIn(d, 'OK')
149         self.failUnlessIn(d, 'PASSED (successes=1)')
150         return d
151
152     def test_dosFile(self):
153         ## XXX -- not really an output test, more of a script test
154         self.mangleSysPath(self.oldPath)
155         d = self.runTrial(
156             os.path.join(self.parent,
157                          'package', 'test_dos_module.py'))
158         self.failIfIn(d, '[ERROR]')
159         self.failIfIn(d, 'IOError')
160         self.failUnlessIn(d, 'OK')
161         self.failUnlessIn(d, 'PASSED (successes=1)')
162         return d