Repository navigation
Expand file tree
/
Copy pathtest_compiler.py
More file actions
271 lines (222 loc) · 9.58 KB
/
Copy pathtest_compiler.py
File metadata and controls
271 lines (222 loc) · 9.58 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from hackedit.app import settings
from hackedit.api import plugins
import time
import sys
import os
import re
import pytest
import pytest_hackedit
from hackedit.api import compiler
def create_test_config():
cfg = compiler.CompilerConfig()
cfg.name = 'Test'
cfg.compiler = __file__
cfg.flags = ['-debug']
cfg.include_paths = ['/usr/share/include']
cfg.library_paths = ['/usr/lib']
cfg.libraries = ['QtCore']
cfg.environment_variables = {'COB_CONFIG_DIR': '/usr/share/gnu-cobol/config/'}
cfg.vcvarsall = ''
cfg.vcvarsall_arch = 'x86'
cfg.type_name = 'GnuCOBOL'
return cfg
class TestCompilerConfig:
def test_default(self):
cfg = compiler.CompilerConfig()
assert cfg.name == ''
assert cfg.compiler == ''
assert cfg.flags == []
assert cfg.include_paths == []
assert cfg.library_paths == []
assert cfg.libraries == []
assert cfg.environment_variables == {}
assert cfg.vcvarsall == ''
assert cfg.vcvarsall_arch == 'x86'
assert cfg.type_name == ''
def test_custom_config(self):
cfg = create_test_config()
assert cfg.name == 'Test'
assert cfg.compiler == __file__
assert cfg.flags == ['-debug']
assert cfg.include_paths == ['/usr/share/include']
assert cfg.library_paths == ['/usr/lib']
assert cfg.libraries == ['QtCore']
assert cfg.environment_variables == {'COB_CONFIG_DIR': '/usr/share/gnu-cobol/config/'}
assert cfg.vcvarsall == ''
assert cfg.vcvarsall_arch == 'x86'
assert cfg.type_name == 'GnuCOBOL'
def test_serialization(self):
cfg = create_test_config()
assert cfg.to_json() == compiler.CompilerConfig().from_json(cfg.to_json()).to_json()
def test_copy(self):
cfg = create_test_config()
cpy = cfg.copy()
assert cfg != cpy
assert cfg.to_json() == cpy.to_json()
def test_compiler_config_widget(qtbot):
widget = compiler.CompilerConfigWidget()
qtbot.addWidget(widget)
cfg = create_test_config()
widget.set_config(cfg)
assert widget.is_dirty() is False
assert widget.get_config().to_json() == cfg.to_json()
def test_generic_compiler_config_widget(qtbot):
"""
:type qtbot: pytestqt.qtbot.QtBot
"""
widget = compiler.GenericCompilerCongigWidget()
widget.show()
qtbot.addWidget(widget)
cfg = create_test_config()
widget.set_config(cfg)
assert widget.is_dirty() is False
assert widget.get_config().to_json() == cfg.to_json()
assert widget.ui.list_include_paths.count() == 1
widget.ui.list_include_paths.addItem('../include')
assert widget.ui.list_include_paths.count() == 2
widget.ui.list_lib_paths.addItem('../libs')
widget.ui.edit_flags.setText(widget.ui.edit_flags.text() + ' -Wall --verbose ')
widget.ui.edit_libs.setText(widget.ui.edit_libs.text() + ' QtWidgets QtGui ')
assert widget.is_dirty() is True
assert widget.get_config().to_json() != cfg.to_json()
updated_cfg = widget.get_config()
assert updated_cfg.include_paths == ['/usr/share/include', '../include']
assert updated_cfg.library_paths == ['/usr/lib', '../libs']
assert updated_cfg.flags == ['-debug', '-Wall', '--verbose']
assert updated_cfg.libraries == ['QtCore', 'QtWidgets', 'QtGui']
def test_parser_gcc():
gcc_sample_output = '''test.cbl: 1: Error: PROGRAM-ID header missing
test.cbl: 1: Error: PROCEDURE DIVISION header missing
test.cbl: 1: Error: syntax error, unexpected Literal
test.cbl: 2: Error: syntax error, unexpected Identifier
In file included from /usr/include/stdio.h:27:0,
from /tmp/cob17702_0.c:8:
/usr/include/features.h:331:4: attention : #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
# warning _FORTIFY_SOURCE requires compiling with optimization (-O)
^
'''
from pyqode.core.modes import CheckerMessage, CheckerMessages
parser = compiler.CompilerOutputParser()
parser.patterns.insert(0, re.compile('.*')) # invalid regex because it miss the msg capture group
messages = parser.parse(gcc_sample_output, '~/Documents', use_tuples=True)
assert len(messages) == 5
assert isinstance(messages[0], tuple)
messages = parser.parse(gcc_sample_output, '~/Documents', use_tuples=False)
assert len(messages) == 5
assert isinstance(messages[0], CheckerMessage)
assert messages[0].line == 0
assert messages[3].line == 1
assert messages[0].path == os.path.normpath(os.path.expanduser('~/Documents/test.cbl'))
assert messages[0].status == CheckerMessages.ERROR
assert messages[-1].status == CheckerMessages.WARNING
def test_compiler_check_failed_error():
err = compiler.CompilerCheckFailedError('blabla', -1)
assert err.message == 'blabla'
assert err.return_code == -1
wd = os.path.dirname(__file__)
cfg = create_test_config()
my_compiler = compiler.Compiler(cfg, working_dir=wd)
class TestCompilerBaseClass:
def test_compile(self):
with pytest.raises(NotImplementedError):
my_compiler.compile_files(['test.cbl'], 'bin', 'test', target_type=compiler.TargetType.SHARED_LIBRARY)
def test_constructor(self):
c = compiler.Compiler(compiler.CompilerConfig())
assert c.working_dir == os.path.expanduser('~')
assert c.print_output is True
c = compiler.Compiler(compiler.CompilerConfig(), working_dir=wd)
assert c.working_dir == wd
assert c.print_output is True
c = compiler.Compiler(compiler.CompilerConfig(), working_dir=wd, print_output=False)
assert c.working_dir == wd
assert c.print_output is False
def test_get_full_compiler_path(self):
# using an absolute path is the easiest case
my_compiler.get_full_compiler_path() == __file__
# resolve full name using PATH
cfg.compiler = 'python'
assert os.path.exists(my_compiler.get_full_compiler_path())
# should return an empty string if program path cannot be resolved using PATH
cfg.compiler = 'foo'
assert my_compiler.get_full_compiler_path() == ''
cfg.compiler == __file__
def test_make_destination_folder(self):
# absolute dest folder
path = os.path.join(wd, 'bin1')
assert os.path.exists(path) is False
my_compiler._make_destination_folder(path)
assert os.path.exists(path) is True
os.rmdir(path)
# special case where user must be expanded
path = '~/bin1'
expanded_path = os.path.expanduser(path)
assert os.path.exists(path) is False
my_compiler._make_destination_folder(path)
assert os.path.exists(expanded_path) is True
os.rmdir(expanded_path)
# relative dest folder
name = 'bin1'
path = os.path.join(wd, name)
assert os.path.exists(path) is False
my_compiler._make_destination_folder(name)
assert os.path.exists(path) is True
os.rmdir(path)
def test_is_outdated(self):
source = 'test.cbl'
dest = 'test.exe'
source_path = os.path.join(wd, source)
destination_path = os.path.join(wd, dest)
with open(source_path, 'w'):
pass
time.sleep(0.1)
with open(destination_path, 'w'):
pass
outdated = my_compiler._is_outdated(source_path, destination_path)
assert outdated is False
outdated = my_compiler._is_outdated(destination_path, source_path)
assert outdated is True
# this should work with relative paths as well
outdated = my_compiler._is_outdated(source, dest)
assert outdated is False
outdated = my_compiler._is_outdated(dest, source)
assert outdated is True
# check corner cases (things that do not exist or strange args)
outdated = my_compiler._is_outdated('/foo/bar', '')
assert outdated is True
outdated = my_compiler._is_outdated('', '')
assert outdated is False
with pytest.raises(ValueError):
outdated = my_compiler._is_outdated(None, None)
with pytest.raises(ValueError):
outdated = my_compiler._is_outdated(4, 5.2)
os.remove(source_path)
os.remove(destination_path)
def test_setup_environment(self):
cfg.compiler = __file__
cfg.environment_variables['PATH'] = wd
cfg.environment_variables['EMPTY'] = ''
env = my_compiler.get_process_environment()
assert env.contains('PATH')
assert env.value('PATH').startswith(wd)
def test_run_compiler_command(self):
my_compiler.config.compiler = 'python'
ret, output = my_compiler._run_compiler_command(['--version'])
assert ret == 0
assert output != ''
with pytest.raises(ValueError):
my_compiler.config.compiler = ''
my_compiler._run_compiler_command(['-c', '"import sys; print(sys.executable)"'])
with pytest.raises(ValueError):
my_compiler.config.compiler = 'python'
my_compiler._run_compiler_command([])
# cover the case where "" must be appended to the program name but hard to test
my_compiler.config.compiler = 'py thon'
ret, output = my_compiler._run_compiler_command(['--version'])
def test_get_configurations_by_mtype():
pytest_hackedit.app()
plugin = plugins.get_compiler_plugin_by_typename('GnuCOBOL')
config = plugin.create_new_configuration('name', 'cobc')
settings.save_compiler_configurations({'test': config})
mtype_cfg = compiler.get_configs_for_mimetype(config.mimetypes[0])[0]
mtype_cfg.name = config.name
assert mtype_cfg.to_json() == config.to_json()