-
-
Notifications
You must be signed in to change notification settings - Fork 327
Expand file tree
/
Copy pathtest_interface.py
More file actions
248 lines (190 loc) · 8.53 KB
/
test_interface.py
File metadata and controls
248 lines (190 loc) · 8.53 KB
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
import json
import locale
import logging
import os
import shutil
import sys
import pytest
sys.path.append(os.getcwd().split('/tests')[0])
from code2flow.engine import code2flow, main, _generate_graphviz, SubsetParams
from code2flow import model
IMG_PATH = '/tmp/code2flow/output.png'
GRAPHVIZ_PATH = '/tmp/code2flow/out.gv'
if os.path.exists("/tmp/code2flow"):
try:
shutil.rmtree('/tmp/code2flow')
except FileNotFoundError:
os.remove('/tmp/code2flow')
os.mkdir('/tmp/code2flow')
os.chdir(os.path.dirname(os.path.abspath(__file__)))
def test_generate_image():
if os.path.exists(IMG_PATH):
os.remove(IMG_PATH)
code2flow(os.path.abspath(__file__),
output_file=IMG_PATH,
hide_legend=True)
assert os.path.exists(IMG_PATH)
os.remove(IMG_PATH)
code2flow(os.path.abspath(__file__),
output_file=IMG_PATH,
hide_legend=False)
assert os.path.exists(IMG_PATH)
def test_not_installed():
if os.path.exists(IMG_PATH):
os.remove(IMG_PATH)
tmp_path = os.environ['PATH']
os.environ['PATH'] = ''
with pytest.raises(AssertionError):
code2flow(os.path.abspath(__file__),
output_file=IMG_PATH)
os.environ['PATH'] = tmp_path
def test_invalid_extension():
with pytest.raises(AssertionError):
code2flow(os.path.abspath(__file__),
output_file='out.pdf')
def test_no_files():
with pytest.raises(AssertionError):
code2flow(os.path.abspath(__file__) + "fakefile",
output_file=IMG_PATH)
def test_graphviz_error(caplog):
caplog.set_level(logging.DEBUG)
_generate_graphviz("/tmp/code2flow/nothing", "/tmp/code2flow/nothing",
"/tmp/code2flow/nothing")
assert "non-zero exit" in caplog.text
def test_no_files_2():
if not os.path.exists('/tmp/code2flow/no_source_dir'):
os.mkdir('/tmp/code2flow/no_source_dir')
if not os.path.exists('/tmp/code2flow/no_source_dir/fakefile'):
with open('/tmp/code2flow/no_source_dir/fakefile', 'w') as f:
f.write("hello world")
with pytest.raises(AssertionError):
code2flow('/tmp/code2flow/no_source_dir',
output_file=IMG_PATH)
with pytest.raises(AssertionError):
code2flow('/tmp/code2flow/no_source_dir',
language='py',
output_file=IMG_PATH)
def test_json():
code2flow('test_code/py/simple_b',
output_file='/tmp/code2flow/out.json',
hide_legend=False)
with open('/tmp/code2flow/out.json') as f:
jobj = json.loads(f.read())
assert set(jobj.keys()) == {'graph'}
assert set(jobj['graph'].keys()) == {'nodes', 'edges', 'directed'}
assert jobj['graph']['directed'] is True
assert isinstance(jobj['graph']['nodes'], dict)
assert len(jobj['graph']['nodes']) == 4
assert set(n['name'] for n in jobj['graph']['nodes'].values()) == {'simple_b::a', 'simple_b::(global)',
'simple_b::c.d', 'simple_b::b'}
assert isinstance(jobj['graph']['edges'], list)
assert len(jobj['graph']['edges']) == 4
assert len(set(n['source'] for n in jobj['graph']['edges'])) == 4
assert len(set(n['target'] for n in jobj['graph']['edges'])) == 3
def test_weird_encoding():
"""
To address https://github.com/scottrogowski/code2flow/issues/28
The windows user had an error b/c their default encoding was cp1252
and they were trying to read a unicode file with emojis
I don't have that installed but was able to reproduce by changing to
US-ASCII which I assume is a little more universal anyway.
"""
locale.setlocale(locale.LC_ALL, 'en_US.US-ASCII')
code2flow('test_code/py/weird_encoding',
output_file='/tmp/code2flow/out.json',
hide_legend=False)
with open('/tmp/code2flow/out.json') as f:
jobj = json.loads(f.read())
assert set(jobj.keys()) == {'graph'}
def test_repr():
module = model.Group('my_file', model.GROUP_TYPE.FILE, [], 0)
group = model.Group('Obj', model.GROUP_TYPE.CLASS, [], 0)
call = model.Call('tostring', 'obj', 42)
variable = model.Variable('the_string', call, 42)
node_a = model.Node('tostring', [], [], [], 13, group)
node_b = model.Node('main', [call], [], [], 59, module)
edge = model.Edge(node_b, node_a)
print(module)
print(group)
print(call)
print(variable)
print(node_a)
print(node_b)
print(edge)
def test_bad_acorn(mocker, caplog):
caplog.set_level(logging.DEBUG)
mocker.patch('code2flow.javascript.get_acorn_version', return_value='7.6.9')
code2flow("test_code/js/simple_a_js", "/tmp/code2flow/out.json")
assert "Acorn" in caplog.text and "8.*" in caplog.text
def test_bad_ruby_parse(mocker):
mocker.patch('subprocess.check_output', return_value=b'blah blah')
with pytest.raises(AssertionError) as ex:
code2flow("test_code/rb/simple_b", "/tmp/code2flow/out.json")
assert "ruby-parse" in ex and "syntax" in ex
def test_bad_php_parse_a():
with pytest.raises(AssertionError) as ex:
code2flow("test_code/php/bad_php/bad_php_a.php", "/tmp/code2flow/out.json")
assert "parse" in ex and "syntax" in ex
def test_bad_php_parse_b():
with pytest.raises(AssertionError) as ex:
code2flow("test_code/php/bad_php/bad_php_b.php", "/tmp/code2flow/out.json")
assert "parse" in ex and "php" in ex.lower()
def test_no_source_type():
with pytest.raises(AssertionError):
code2flow('test_code/js/exclude_modules_es6',
output_file='/tmp/code2flow/out.json',
hide_legend=False)
def test_cli_no_args(capsys):
with pytest.raises(SystemExit):
main([])
assert 'the following arguments are required' in capsys.readouterr().err
def test_cli_verbose_quiet(capsys):
with pytest.raises(AssertionError):
main(['test_code/py/simple_a', '--verbose', '--quiet'])
def test_cli_log_default(mocker):
logging.basicConfig = mocker.MagicMock()
main(['test_code/py/simple_a'])
logging.basicConfig.assert_called_once_with(format="Code2Flow: %(message)s",
level=logging.INFO)
def test_cli_log_verbose(mocker):
logging.basicConfig = mocker.MagicMock()
main(['test_code/py/simple_a', '--verbose'])
logging.basicConfig.assert_called_once_with(format="Code2Flow: %(message)s",
level=logging.DEBUG)
def test_cli_log_quiet(mocker):
logging.basicConfig = mocker.MagicMock()
main(['test_code/py/simple_a', '--quiet'])
logging.basicConfig.assert_called_once_with(format="Code2Flow: %(message)s",
level=logging.WARNING)
def test_subset_cli(mocker):
with pytest.raises(AssertionError):
SubsetParams.generate(target_function='', upstream_depth=1, downstream_depth=0)
with pytest.raises(AssertionError):
SubsetParams.generate(target_function='', upstream_depth=0, downstream_depth=1)
with pytest.raises(AssertionError):
SubsetParams.generate(target_function='test', upstream_depth=0, downstream_depth=0)
with pytest.raises(AssertionError):
SubsetParams.generate(target_function='test', upstream_depth=-1, downstream_depth=0)
with pytest.raises(AssertionError):
SubsetParams.generate(target_function='test', upstream_depth=0, downstream_depth=-1)
with pytest.raises(AssertionError):
main(['test_code/py/subset_find_exception/zero.py', '--target-function', 'func', '--upstream-depth', '1'])
with pytest.raises(AssertionError):
main(['test_code/py/subset_find_exception/two.py', '--target-function', 'func', '--upstream-depth', '1'])
def test_color_scheme_invalid_input(capsys):
with pytest.raises(SystemExit):
main(['test_code/py/simple_a', '--color-scheme', 'invalid'])
assert 'error: argument --color-scheme: invalid choice:' in capsys.readouterr().err
def test_color_scheme_none_given(capsys):
code2flow(['test_code/py/simple_a'], output_file=IMG_PATH)
assert os.path.exists(IMG_PATH)
def test_color_scheme_test_not_default(capsys):
code2flow(['test_code/py/two_file_simple', '--color-scheme', 'blue'], output_file=GRAPHVIZ_PATH, color_scheme='blue')
assert os.path.exists(GRAPHVIZ_PATH)
contents = ''
with open(GRAPHVIZ_PATH, 'r') as file:
contents = "".join(file.readlines())
blue = ('#138bfa', '#989a97', '#044a9a')
assert blue[0] in contents
assert blue[1] in contents
assert blue[2] in contents