File: test_reporter.py

package info (click to toggle)
doit 0.31.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,504 kB
  • sloc: python: 10,835; makefile: 168; ansic: 14; sh: 4
file content (331 lines) | stat: -rw-r--r-- 11,337 bytes parent folder | download | duplicates (2)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import sys
import json
from io import StringIO

from doit import reporter
from doit.task import Stream, Task
from doit.exceptions import CatchedException


class TestConsoleReporter(object):

    def test_initialize(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        rep.initialize([Task("t_name", None)], ["t_name"])
        # no output on initialize
        assert "" in rep.outstream.getvalue()

    def test_startTask(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        rep.get_status(Task("t_name", None))
        # no output on start task
        assert "" in rep.outstream.getvalue()

    def test_executeTask(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        def do_nothing():pass
        t1 = Task("with_action",[(do_nothing,)])
        rep.execute_task(t1)
        assert ".  with_action\n" == rep.outstream.getvalue()

    def test_executeTask_unicode(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        def do_nothing():pass
        task_name = "中文 with_action"
        t1 = Task(task_name, [(do_nothing,)])
        rep.execute_task(t1)
        assert ".  中文 with_action\n" == rep.outstream.getvalue()


    def test_executeHidden(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        def do_nothing():pass
        t1 = Task("_hidden",[(do_nothing,)])
        rep.execute_task(t1)
        assert "" == rep.outstream.getvalue()

    def test_executeGroupTask(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        rep.execute_task(Task("t_name", None))
        assert "" == rep.outstream.getvalue()

    def test_skipUptodate(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        rep.skip_uptodate(Task("t_name", None))
        assert "-- " in rep.outstream.getvalue()
        assert "t_name" in rep.outstream.getvalue()

    def test_skipUptodate_hidden(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        rep.skip_uptodate(Task("_name", None))
        assert "" == rep.outstream.getvalue()

    def test_skipIgnore(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        rep.skip_ignore(Task("t_name", None))
        assert "!! " in rep.outstream.getvalue()
        assert "t_name" in rep.outstream.getvalue()


    def test_cleanupError(self, capsys):
        rep = reporter.ConsoleReporter(StringIO(), {})
        exception = CatchedException("I got you")
        rep.cleanup_error(exception)
        err = capsys.readouterr()[1]
        assert "I got you" in err

    def test_teardownTask(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        rep.teardown_task(Task("t_name", None))
        # no output on teardown task
        assert "" in rep.outstream.getvalue()

    def test_addSuccess(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        rep.add_success(Task("t_name", None))
        # no output on success task
        assert "" in rep.outstream.getvalue()

    def test_addFailure(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        try:
            raise Exception("original 中文 exception message here")
        except Exception as e:
            catched = CatchedException("catched exception there", e)
        rep.add_failure(Task("t_name", None, verbosity=1), catched)
        rep.complete_run()
        got = rep.outstream.getvalue()
        # description
        assert "Exception: original 中文 exception message here" in got, got
        # traceback
        assert """raise Exception("original 中文 exception message here")""" in got
        # catched message
        assert "catched exception there" in got

    def test_runtime_error(self):
        msg = "runtime error"
        rep = reporter.ConsoleReporter(StringIO(), {})
        assert [] == rep.runtime_errors
        # no imediate output
        rep.runtime_error(msg)
        assert 1 == len(rep.runtime_errors)
        assert msg == rep.runtime_errors[0]
        assert "" in rep.outstream.getvalue()
        # runtime errors abort execution
        rep.complete_run()
        got = rep.outstream.getvalue()
        assert msg in got
        assert "Execution aborted" in got


    def test_complete_run_verbosity0(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        catched = CatchedException("catched exception there",
                                   Exception("foo"))

        task = Task("t_name", None, verbosity=0)
        task.executed = True
        rep.add_failure(task, catched)

        # assign new StringIO so output is only from complete_run()
        rep.outstream = StringIO()
        rep.complete_run()
        got = rep.outstream.getvalue()
        assert "<stdout>" in got
        assert "<stderr>" in got

    def test_complete_run_verbosity0_not_executed(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        catched = CatchedException("catched exception there",
                                   Exception("foo"))

        task = Task("t_name", None, verbosity=0)
        task.executed = False
        rep.add_failure(task, catched)

        # assign new StringIO so output is only from complete_run()
        rep.outstream = StringIO()
        rep.complete_run()
        got = rep.outstream.getvalue()
        assert "<stdout>" not in got
        assert "<stderr>" not in got

    def test_complete_run_verbosity1(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        catched = CatchedException("catched exception there",
                                   Exception("foo"))

        task = Task("t_name", None, verbosity=1)
        task.executed = True
        rep.add_failure(task, catched)

        # assign new StringIO so output is only from complete_run()
        rep.outstream = StringIO()
        rep.complete_run()
        got = rep.outstream.getvalue()
        assert "<stdout>" in got
        assert "<stderr>" not in got

    def test_complete_run_verbosity2(self):
        rep = reporter.ConsoleReporter(StringIO(), {})
        catched = CatchedException("catched exception there",
                                   Exception("foo"))

        rep.add_failure(Task("t_name", None, verbosity=2), catched)

        # assign new StringIO so output is only from complete_run()
        rep.outstream = StringIO()
        rep.complete_run()
        got = rep.outstream.getvalue()
        assert "<stdout>" not in got
        assert "<stderr>" not in got


    def test_complete_run_verbosity2_redisplay(self):
        rep = reporter.ConsoleReporter(StringIO(), {'failure_verbosity': 2})
        catched = CatchedException("catched exception there",
                                   Exception("foo"))

        task = Task("t_name", None, verbosity=2)
        task.executed = True
        rep.add_failure(task, catched)

        # assign new StringIO so output is only from complete_run()
        rep.outstream = StringIO()
        rep.complete_run()
        got = rep.outstream.getvalue()
        assert "<stdout>" in got
        assert "<stderr>" in got



class TestExecutedOnlyReporter(object):
    def test_skipUptodate(self):
        rep = reporter.ExecutedOnlyReporter(StringIO(), {})
        rep.skip_uptodate(Task("t_name", None))
        assert "" == rep.outstream.getvalue()

    def test_skipIgnore(self):
        rep = reporter.ExecutedOnlyReporter(StringIO(), {})
        rep.skip_ignore(Task("t_name", None))
        assert "" == rep.outstream.getvalue()


class TestZeroReporter(object):
    def test_executeTask(self):
        rep = reporter.ZeroReporter(StringIO(), {})
        def do_nothing():pass
        t1 = Task("with_action",[(do_nothing,)])
        rep.execute_task(t1)
        assert "" == rep.outstream.getvalue()

    def test_runtime_error(self, capsys):
        msg = "zero runtime error"
        rep = reporter.ZeroReporter(StringIO(), {})
        # imediate output
        rep.runtime_error(msg)
        assert msg in capsys.readouterr()[1]


class TestTaskResult(object):
    def test(self):
        def sample():
            print("this is printed")
        t1 = Task("t1", [(sample,)])
        result = reporter.TaskResult(t1)
        result.start()
        t1.execute(Stream(0))
        result.set_result('success')
        got = result.to_dict()
        assert t1.name == got['name'], got
        assert 'success' == got['result'], got
        assert "this is printed\n" == got['out'], got
        assert "" == got['err'], got
        assert got['started']
        assert 'elapsed' in got


class TestJsonReporter(object):

    def test_normal(self):
        output = StringIO()
        rep = reporter.JsonReporter(output)
        t1 = Task("t1", None)
        t2 = Task("t2", None)
        t3 = Task("t3", None)
        t4 = Task("t4", None)
        expected = {'t1':'fail', 't2':'up-to-date',
                    't3':'success', 't4':'ignore'}
        # t1 fail
        rep.get_status(t1)
        rep.execute_task(t1)
        rep.add_failure(t1, CatchedException('t1 failed!'))
        # t2 skipped
        rep.get_status(t2)
        rep.skip_uptodate(t2)
        # t3 success
        rep.get_status(t3)
        rep.execute_task(t3)
        rep.add_success(t3)
        # t4 ignore
        rep.get_status(t4)
        rep.skip_ignore(t4)

        rep.teardown_task(t4)

        rep.complete_run()
        got = json.loads(output.getvalue())
        for task_result in got['tasks']:
            assert expected[task_result['name']] == task_result['result'], got
            if task_result['name'] == 't1':
                assert 't1 failed!' in task_result['error']

    def test_cleanup_error(self, capsys):
        output = StringIO()
        rep = reporter.JsonReporter(output)
        t1 = Task("t1", None)
        msg = "cleanup error"
        exception = CatchedException(msg)
        assert [] == rep.errors
        rep.get_status(t1)
        rep.execute_task(t1)
        rep.add_success(t1)
        rep.cleanup_error(exception)
        assert [msg+'\n'] == rep.errors
        assert "" in rep.outstream.getvalue()
        rep.complete_run()
        got = json.loads(output.getvalue())
        assert msg in got['err']

    def test_runtime_error(self):
        output = StringIO()
        rep = reporter.JsonReporter(output)
        t1 = Task("t1", None)
        msg = "runtime error"
        assert [] == rep.errors
        rep.get_status(t1)
        rep.execute_task(t1)
        rep.add_success(t1)
        rep.runtime_error(msg)
        assert [msg] == rep.errors
        assert "" in rep.outstream.getvalue()
        # runtime errors abort execution
        rep.complete_run()
        got = json.loads(output.getvalue())
        assert msg in got['err']

    def test_ignore_stdout(self):
        output = StringIO()
        rep = reporter.JsonReporter(output)
        sys.stdout.write("info that doesnt belong to any task...")
        sys.stderr.write('something on err')
        t1 = Task("t1", None)
        expected = {'t1':'success'}
        rep.get_status(t1)
        rep.execute_task(t1)
        rep.add_success(t1)
        rep.complete_run()
        got = json.loads(output.getvalue())
        assert expected[got['tasks'][0]['name']] == got['tasks'][0]['result']
        assert "info that doesnt belong to any task..." == got['out']
        assert "something on err" == got['err']