Debian Codegraph @

tests/common.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 __metaclass__ = type
4 import os
5 import subprocess
6 from __main__ import py
7
8 recode_program = os.environ.get('RECODE')
9
10 import Recode
11 outer = Recode.Outer(iconv=False)
12 outer_iconv = Recode.Outer(iconv=True)
13
14 class Run(dict):
15
16 def __getattr__(self, attribute):
17 return self[attribute]
18
19 def __setattr__(self, attribute, value):
20 self[attribute] = value
21
22 run = Run()
23
24 def external(flag):
25 run.external = flag
26
27 def request(text, encoding='utf-8'):
28 run.request = bytes(text, encoding)
29
30 # Functions only meant to be imported into real testing modules, where
31 # pytest is meant to find and use them.
32
33 def setup_module(module):
34 for variable in ('LANG', 'LANGUAGE',
35 'LC_ALL', 'LC_MESSAGES', 'LC_COLLATE'):
36 if variable in os.environ:
37 del os.environ[variable]
38 os.environ['DEFAULT_CHARSET'] = 'ASCII'
39 run.external = outer is None
40 import tempfile
41 run.work = tempfile.mktemp()
42
43 def teardown_module(module):
44 if os.path.exists(run.work):
45 os.remove(run.work)
46
47 # Useful functions for various test modules.
48
49 def assert_or_diff(output, expected):
50 if output != expected:
51 import difflib
52 diff = difflib.ndiff(expected.splitlines(True),
53 output.splitlines(True))
54 import sys
55 sys.stdout.write(''.join(diff))
56 assert False, (len(output), len(expected))
57
58 def external_output(command):
59 if not recode_program:
60 py.test.skip()
61 command = command.replace('$R', recode_program)
62 # FIXME: Find a more portable solution than checking the OS
63 return subprocess.check_output(command, universal_newlines=True, shell=os.name != 'nt')
64
65 def recode_output(input, encoding='utf-8'):
66 if type(input) != bytes:
67 input = bytes(input, encoding)
68 if run.external:
69 open(run.work, 'wb').write(input)
70 return external_output('$R %s < %s' % (run.request, run.work))
71 if outer is None:
72 py.test.skip()
73 return outer.recode(run.request, input)
74
75 def recode_iconv_output(input, encoding='utf-8'):
76 if type(input) != bytes:
77 input = bytes(input, encoding)
78 if run.external or outer_iconv is None:
79 py.test.skip()
80 return outer_iconv.recode(run.request, input)
81
82 def recode_back_output(input, encoding='utf-8'):
83 if type(input) != bytes:
84 input = bytes(input, encoding)
85 before, after = run.request.split(b'..')
86 if run.external:
87 open(run.work, 'wb').write(input)
88 external_output(b'$R %s %s' % (run.request, run.work))
89 return external_output(b'$R %s..%s < %s' % (after, before, run.work))
90 if outer is None:
91 py.test.skip()
92 temp = outer.recode(run.request, input)
93 return outer.recode(b'%s..%s' % (after, before), temp)
94
95 def validate(input, expected, encoding='utf-8'):
96 output = recode_output(input)
97 if type(input) != bytes:
98 output = output.decode(encoding)
99 import sys
100 print(type(input), type(output), type(expected))
101 assert_or_diff(output, expected)
102
103 def validate_back(input, encoding='utf-8'):
104 output = recode_back_output(input)
105 if type(input) != bytes:
106 output = output.decode(encoding)
107 assert_or_diff(output, input)