-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest_cppdep.py
More file actions
325 lines (282 loc) · 13.7 KB
/
Copy pathtest_cppdep.py
File metadata and controls
325 lines (282 loc) · 13.7 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# Copyright (C) 2017 Olzhas Rakhimov
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Tests for the analysis facilities."""
from __future__ import absolute_import
import os
import platform
import re
from unittest import mock
import pytest
from cppdep import cppdep
from cppdep.cppdep import Include
def path_relpath_posix(path, root):
"""Returns relative path with posix separators."""
return cppdep.path_to_posix_sep(os.path.relpath(path, root))
@pytest.mark.parametrize('filename,expected',
[('path.cc', 'path'), ('path.', 'path'),
('.path', '.path'), ('path', 'path'),
('very/long/path.h', 'very/long/path'),
('./.././path.cc', './.././path')])
def test_strip_ext(filename, expected):
"""Test extraction of file name."""
assert cppdep.strip_ext(filename) == expected
@pytest.mark.skipif(platform.system() == 'Windows', reason='non-POSIX')
@pytest.mark.parametrize('path,paths,expected',
[('root', ('../file',), 'file'),
('root', ('file',), 'root/file'),
('.', ('./file',), 'file')])
def test_path_normjoin_posix(path, paths, expected):
"""Test the normalized join of paths on POSIX systems."""
assert cppdep.path_normjoin(path, *paths) == expected
@pytest.mark.skipif(platform.system() != 'Windows', reason='non-DOS')
@pytest.mark.parametrize('path,paths,expected',
[(r'C:\root', (r'..\file',), r'C:\file'),
('root', ('file',), r'root\file'),
('.', (r'.\file',), 'file'),
('root\\', ('dir/file',), r'root\dir\file')])
def test_path_normjoin_dos(path, paths, expected):
"""Test the normalized join of paths on DOS systems."""
assert cppdep.path_normjoin(path, *paths) == expected
@pytest.mark.skipif(platform.system() == 'Windows',
reason='The same logic with different path separators.')
@pytest.mark.parametrize('paths,expected',
[(['/path', '/path/file', '/path/file2'], '/path'),
(['/path', '/dir'], '/'),
(['/path/file', '/pa'], '/'),
(['/path/dir/file', '/path/dir1/file'], '/path'),
(['/path/dir/', '/path/dir/file'], '/path/dir'),
([], ''), (['/dir'], '/dir'),
pytest.mark.xfail((['/dir/*'], '/dir'))])
def test_path_common_posix(paths, expected):
"""Test common directory for paths."""
assert cppdep.path_common(paths) == expected
@pytest.mark.skipif(platform.system() == 'Windows',
reason='The same logic with different path separators.')
@pytest.mark.parametrize('parent,child,expected',
[('/dir', '/dir/file', True),
('/dir/file', '/dir', False),
('/dir/', '/dir/file', True),
('/di', '/dir/file', False),
('/', '/dir/file', True),
('/dir', '/dir/dir2/file', True),
('/tar', '/dir/file', False),
('/dir', '/dir', True)])
def test_path_isancestor(parent, child, expected):
"""Test proper ancestor directory check for paths."""
assert cppdep.path_isancestor(parent, child) == expected
@pytest.mark.skipif(platform.system() != 'Windows', reason='POSIX is noop.')
@pytest.mark.parametrize('path,expected',
[('file', 'file'), ('/dir/file', '/dir/file'),
(r'\dir\file', '/dir/file'),
(r'/dir\file', '/dir/file')])
def test_path_to_posix_sep(path, expected):
"""Test POSIX separator normalization for DOS paths."""
assert cppdep.path_to_posix_sep(path) == expected
@pytest.mark.parametrize('dictionary,element,default_value,expected',
[({'tag': 'value'}, 'tag', 'default', 'value'),
({}, 'tag', 'default', 'default'),
({'label': 'value'}, 'tag', 'default', 'default')])
def test_yaml_optional(dictionary, element, default_value, expected):
"""Test retrieval of an optional value from yaml configuration."""
assert cppdep.yaml_optional(dictionary, element, default_value) == expected
@pytest.mark.parametrize(
'dictionary,element,expected',
[pytest.mark.xfail(({'tag': 'value'}, 'tag', ['value'])),
({'tag': ['value']}, 'tag', ['value']),
({}, 'tag', []),
({'label': 'value'}, 'tag', [])])
def test_yaml_optional_list(dictionary, element, expected):
"""Test special handling of optional lists in yaml configurations."""
assert (cppdep.yaml_optional_list(dictionary, element) == expected)
@pytest.mark.parametrize(
'include,expected',
[(Include('vector', with_quotes=True), '"vector"'),
(Include('vector', with_quotes=False), '<vector>'),
(Include('dir/vector.h', with_quotes=False), '<dir/vector.h>'),
(Include(r'dir\vector.h', with_quotes=False), r'<dir\vector.h>')])
def test_include_str(include, expected):
"""Tests proper string representation of include upon string conversion."""
assert str(include) == expected
@pytest.mark.parametrize(
'include_one,include_two',
[(Include('vector', True), Include('vector', True)),
(Include('vector', True), Include('vector', False)),
(Include('./vector', True), Include('vector', True)),
(Include('include/./vector', True), Include('include/vector', True))])
def test_include_eq(include_one, include_two):
"""Include equality and hash tests for storage in containers."""
assert include_one == include_two
assert hash(include_one) == hash(include_two)
def test_include_ne_impl():
"""Makes sure that __ne__ is implemented."""
with mock.patch('cppdep.cppdep.Include.__eq__') as mock_eq:
include_one = Include('vector', True)
check = include_one != include_one
assert mock_eq.called
assert not check
@pytest.mark.parametrize(
'include_one,include_two',
[(Include('vector.hpp', True), Include('vector', True)),
(Include('dir/vector', True), Include('include/vector', True))])
def test_include_neq(include_one, include_two):
"""__ne__ doesn't imply (not __eq__) in Python."""
assert include_one != include_two
@pytest.mark.parametrize(
'text,expected',
[('#include <vector>', ['<vector>']),
('#include "vector"', ['"vector"']),
('# include <vector>', ['<vector>']),
('#\tinclude <vector>', ['<vector>']),
('#include "vector.h"', ['"vector.h"']),
('#include "vector.h++"', ['"vector.h++"']),
('#include "vector.any"', ['"vector.any"']),
('#include "vector.hpp"', ['"vector.hpp"']),
('#include "vector.cpp"', ['"vector.cpp"']),
('#include "dir/vector.hpp"', ['"dir/vector.hpp"']),
(r'#include "dir\vector.hpp"', [r'"dir\vector.hpp"']),
('#include "./vector"', ['"./vector"']),
('#include <./vector>', ['<./vector>']),
('#include <a>\n#include <b>', ['<a>', '<b>']),
('#include <b>\n#include <a>', ['<b>', '<a>']),
('#include <b> // a>', ['<b>']),
('#include "b" // a"', ['"b"']),
('#include <b> /* a> */', ['<b>']),
('#include "b" /* a" */', ['"b"']),
('#include ""', []),
('#include <>', []),
('//#include <vector>', []),
('/*#include <vector>*/', []),
('#import <vector>', []),
('include <vector>', []),
('#nclude <vector>', []),
('<vector>', []),
('"vector"', []),
('#<vector>', []),
('#include < vector>', []),
('#include <vector >', []),
('#include <vector nonconventional>', []),
('#include " vector"', []),
('#include "vector "', []),
(' #include <vector>', ['<vector>']),
('#include <vector> ', ['<vector>']),
('some_code #include <vector>', []),
('#include <vector> some_code', ['<vector>']),
pytest.mark.xfail(('#if 0\n#include <vector>\n#endif', [])),
pytest.mark.xfail(('/*\n#include <vector>\n*/', [])),
pytest.mark.xfail(('#define V <vector>\n#include V\n', ['<vector>']))])
def test_include_grep(text, expected, tmpdir):
"""Tests the include directive search from a text."""
src = tmpdir.join('include_grep')
src.write(text)
assert [str(x) for x in Include.grep(str(src))] == expected
@pytest.fixture()
def include_setup(tmpdir):
"""Sets up the system for include header search."""
dirs = ['project1', 'external1', 'external2']
files = [tmpdir.mkdir(x).join('header').write('') for x in dirs]
return tmpdir, [os.path.join(str(tmpdir), x) for x in dirs]
#pylint: disable=redefined-outer-name
@pytest.mark.parametrize(
'include,cwd,include_dirs,expected',
[(Include('header', True), '.', [], None),
(Include('header', True), 'project1', [], 'project1/header'),
(Include('header', False), 'project1', [], None),
(Include('header', True), 'project1', ['external2', 'external1'],
'project1/header'),
(Include('header', False), 'project1', ['external2', 'external1'],
'external1/header'),
(Include('header', False), 'project1', ['external1', 'external2'],
'external2/header')])
def test_include_locate(include, cwd, include_dirs, expected, include_setup):
"""The search for header locations from include paths."""
tmpdir, _ = include_setup
abs_cwd = cppdep.path_normjoin(str(tmpdir), cwd)
include_dirs = [cppdep.path_normjoin(str(tmpdir), x) for x in include_dirs]
hpath, package = include.locate(abs_cwd, include_dirs, [])
assert package is None
assert include.hpath == hpath
if expected is None:
assert include.hpath is None
else:
assert include.hpath is not None
assert path_relpath_posix(include.hpath, str(tmpdir)) == expected
@pytest.mark.parametrize(
'include,cwd,include_patterns,expected',
[(Include('header_foo', True), '.', [], (None, None)),
(Include('header', True), '.', [('foo', 'header')], ('header', 'foo')),
(Include('header', False), '.', [('foo', 'header')], ('header', 'foo')),
(Include('header', False), 'project1', [('foo', 'header')],
('header', 'foo')),
(Include('header', False), '.', [('foo', 'header'), ('bar', 'header')],
('header', 'foo')),
(Include('header', False), '.', [('bar', 'header'), ('foo', 'header')],
('header', 'bar')),
(Include('header_foo', False), '.', [('foo', 'header$')], (None, None)),
(Include('header_foo', False), '.', [('foo', 'header')],
('header_foo', 'foo')),
(Include('header_foo', False), '.', [('foo', 'header_foo')],
('header_foo', 'foo'))])
def test_include_locate_pattern(include, cwd, include_patterns, expected,
include_setup):
"""Pattern based include header location."""
tmpdir, include_dirs = include_setup
abs_cwd = cppdep.path_normjoin(str(tmpdir), cwd)
include_patterns = [(x, [re.compile(y)]) for x, y in include_patterns]
assert include.locate(abs_cwd, include_dirs, include_patterns) == expected
@pytest.mark.parametrize('hpath,cpath',
[('header', None), (None, 'source'),
('header', 'source')])
def test_component_init(hpath, cpath, tmpdir, monkeypatch):
"""Component construction from header and implementation files."""
mock_warn = mock.MagicMock(spec=cppdep.warn)
monkeypatch.setattr(cppdep, 'warn', mock_warn)
package = mock.MagicMock(spec=cppdep.Package)
package.name = 'mock_package'
package.group = mock.MagicMock(spec=cppdep.PackageGroup)
package.group.name = 'mock_group'
package.root = str(tmpdir)
if hpath:
tmpdir.join(hpath).write('')
hpath = cppdep.path_normjoin(str(tmpdir), hpath)
if cpath:
tmpdir.join(cpath).write('')
cpath = cppdep.path_normjoin(str(tmpdir), cpath)
component = cppdep.Component(hpath, cpath, package)
assert (component.name ==
path_relpath_posix(cppdep.strip_ext(cpath or hpath), str(tmpdir)))
assert str(component) == component.name
assert component.package == package
assert component.hpath == hpath
assert component.cpath == cpath
assert hpath or mock_warn.called
@pytest.mark.parametrize('filename,is_header',
[('', None), ('.file', None), ('header', True),
('head.er', None), ('header.h', True),
('head.er.h', None), ('dir/header.h', None),
('header.hpp', True), ('header.h++', True),
('header.hh', True), ('header.hxx', True),
('src.c', False), ('src.cc', False),
('src.c++', False), ('src.cxx', False),
('src.cpp', False), ('src.java', None),
('unconvetional header.hpp', None)])
def test_package_src_regex(filename, is_header):
"""Test the regex for matching and gathering C/C++ header/source files."""
src_match = cppdep.Package._RE_SRC.match(filename)
if is_header is None:
assert not src_match
elif is_header:
assert src_match.group('h') is not None
else:
assert src_match.group('c') is not None