/usr/share/pyshared/blockdiag/tests/utils.py is in python-blockdiag 1.3.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- coding: utf-8 -*-
from __future__ import print_function
import sys
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
import os
import re
import functools
from shutil import rmtree
from tempfile import mkdtemp, mkstemp
from blockdiag.builder import ScreenNodeBuilder
from blockdiag.parser import parse_file
try:
from io import StringIO
except ImportError:
from cStringIO import StringIO
def supported_pil():
try:
from PIL import _imagingft
_imagingft
return True
except:
return False
def with_pil(fn):
if not supported_pil():
fn.__test__ = False
return fn
def supported_pdf():
try:
import reportlab
reportlab
return True
except:
return False
def with_pdf(fn):
if not supported_pdf():
fn.__test__ = False
return fn
def capture_stderr(func):
def wrap(*args, **kwargs):
try:
stderr = sys.stderr
sys.stderr = StringIO()
func(*args, **kwargs)
if re.search('ERROR', sys.stderr.getvalue()):
raise AssertionError('Caught error')
finally:
if sys.stderr.getvalue():
print("---[ stderr ] ---")
print(sys.stderr.getvalue())
sys.stderr = stderr
return functools.wraps(func)(wrap)
stderr_wrapper = capture_stderr # FIXME: deprecated
class TemporaryDirectory(object):
def __init__(self, suffix='', prefix='tmp', dir=None):
self.name = mkdtemp(suffix, prefix, dir)
def __del__(self):
self.clean()
def clean(self):
if os.path.exists(self.name):
rmtree(self.name)
def mkstemp(self, suffix='', prefix='tmp', text=False):
return mkstemp(suffix, prefix, self.name, text)
class BuilderTestCase(unittest.TestCase):
def build(self, filename):
basedir = os.path.dirname(__file__)
pathname = os.path.join(basedir, 'diagrams', filename)
return self._build(parse_file(pathname))
def _build(self, tree):
return ScreenNodeBuilder.build(tree)
def __getattr__(self, name):
if name.startswith('assertNode'):
def asserter(diagram, attributes):
attr_name = name.replace('assertNode', '').lower()
print("[node.%s]" % attr_name)
for node in (n for n in diagram.nodes if n.drawable):
print(node)
excepted = attributes[node.id]
self.assertEqual(excepted, getattr(node, attr_name))
return asserter
elif name.startswith('assertEdge'):
def asserter(diagram, attributes):
attr_name = name.replace('assertEdge', '').lower()
print("[edge.%s]" % attr_name)
for edge in diagram.edges:
print(edge)
expected = attributes[(edge.node1.id, edge.node2.id)]
self.assertEqual(expected, getattr(edge, attr_name))
return asserter
else:
return getattr(super(BuilderTestCase, self), name)
|