Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/allpy/file/tip/test/test_silence.py
Дата изменения: Unknown
Дата индексирования: Tue Apr 12 11:24:01 2016
Кодировка:
allpy: b556c96c6719 test/test_silence.py

allpy

view test/test_silence.py @ 1168:b556c96c6719

blocks3d/www Makefile: never check certificates of github, they are too confusing for wget
author Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru>
date Mon, 26 May 2014 17:20:29 +0400
parents
children
line source
1 """Test for allpy.util.Silence
3 >>> print check_output(["python", __file__], stderr=STDOUT)
4 Example1
5 Stdout must be seen (only stderr silenced)
6 Example2
7 Example3
8 Stdout must be seen (error raised)
9 Example4
10 Stdout must be seen (only stderr silenced)
11 Example5
12 Example6
13 Stdout must be seen (error raised)
14 Stderr must be seen too
15 ...
16 done
17 <BLANKLINE>
19 """
20 import sys
21 from subprocess import call, check_call, Popen, PIPE, STDOUT
22 from allpy.util import Silence
24 def check_output(*args, **kws):
25 """`check_output` is only introduced in 2.7, we need it NOW"""
26 p = Popen(stdout=PIPE, *args, **kws)
27 out, _ = p.communicate()
28 assert p.poll() == 0
29 return out
31 class Unbuffered(object):
32 """Fix python stdIO buffering by wrapping this around stdout/stderr.
34 Python uses different buffering strategies in interactive and
35 non-interactive invocations making this test output look ugly and
36 difficult to understand. This class is a crutch for the problem.
37 """
38 def __init__(self, stream):
39 self.stream = stream
40 def write(self, data):
41 self.stream.write(data)
42 self.stream.flush()
43 def __getattr__(self, attr):
44 return getattr(self.stream, attr)
46 if __name__ == "__main__":
48 # Only make uniform buffering when running as __main__ program.
49 # Otherwise, importing this module would cripple interactive interpreter.
50 sys.stdout = Unbuffered(sys.stdout)
51 sys.stderr = Unbuffered(sys.stderr)
53 print "Example1"
55 with Silence():
56 print "Stdout must be seen (only stderr silenced)"
57 sys.stderr.write("Stderr must not be seen")
59 print "Example2"
61 with Silence(hide="both"):
62 print "Stdout must not be seen (both silenced)"
64 print "Example3"
66 try:
67 with Silence(hide="both"):
68 print "Stdout must be seen (error raised)"
69 raise Exception()
70 except Exception:
71 pass
73 call(["echo", "Example4"])
75 with Silence(dup="stderr"):
76 check_call(["echo", "Stdout must be seen (only stderr silenced)"])
77 check_call(["echo", "Stderr must not be seen"], stdout=sys.stderr)
79 call(["echo", "Example5"])
81 with Silence(dup="both"):
82 check_call(["echo", "Stdout must not be seen (both silenced)"])
84 call(["echo", "Example6"])
86 try:
87 with Silence(dup="both"):
88 check_call(["echo", "Stdout must be seen (error raised)"])
89 check_call(["echo", "Stderr must be seen too"], stdout=sys.stderr)
90 check_call(["false"])
91 except Exception:
92 pass
94 print "..."
95 call(["echo", "done"])