forked from danmar/simplecpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.py
More file actions
300 lines (249 loc) · 11.4 KB
/
integration_test.py
File metadata and controls
300 lines (249 loc) · 11.4 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
## test with python -m pytest integration_test.py
import os
import pathlib
import platform
import pytest
from testutils import simplecpp, format_include_path_arg, format_include
def __test_relative_header_create_header(dir, with_pragma_once=True):
header_file = os.path.join(dir, 'test.h')
with open(header_file, 'wt') as f:
f.write(f"""
{"#pragma once" if with_pragma_once else ""}
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
#else
#error header_was_already_included
#endif
const int dummy = 1;
""")
return header_file, "error: #error header_was_already_included"
def __test_relative_header_create_source(dir, include1, include2, is_include1_sys=False, is_include2_sys=False, inv=False):
if inv:
return __test_relative_header_create_source(dir, include1=include2, include2=include1, is_include1_sys=is_include2_sys, is_include2_sys=is_include1_sys)
## otherwise
src_file = os.path.join(dir, 'test.c')
with open(src_file, 'wt') as f:
f.write(f"""
#undef TEST_H_INCLUDED
#include {format_include(include1, is_include1_sys)}
#include {format_include(include2, is_include2_sys)}
""")
return src_file
@pytest.mark.parametrize("with_pragma_once", (False, True))
@pytest.mark.parametrize("is_sys", (False, True))
def test_relative_header_1(record_property, tmpdir, with_pragma_once, is_sys):
_, double_include_error = __test_relative_header_create_header(tmpdir, with_pragma_once=with_pragma_once)
test_file = __test_relative_header_create_source(tmpdir, "test.h", "test.h", is_include1_sys=is_sys, is_include2_sys=is_sys)
args = ([format_include_path_arg(tmpdir)] if is_sys else []) + [test_file]
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
record_property("stdout", stdout)
record_property("stderr", stderr)
if with_pragma_once:
assert stderr == ''
else:
assert double_include_error in stderr
@pytest.mark.parametrize("with_pragma_once", (False, True))
@pytest.mark.parametrize("inv", (False, True))
@pytest.mark.parametrize("source_relative", (False, True))
def test_relative_header_2(record_property, tmpdir, with_pragma_once, inv, source_relative):
header_file, double_include_error = __test_relative_header_create_header(tmpdir, with_pragma_once=with_pragma_once)
test_file = __test_relative_header_create_source(tmpdir, "test.h", header_file, inv=inv)
args = ["test.c" if source_relative else test_file]
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
record_property("stdout", stdout)
record_property("stderr", stderr)
if with_pragma_once:
assert stderr == ''
if inv or not source_relative:
assert f'#line 8 "{pathlib.PurePath(tmpdir).as_posix()}/test.h"' in stdout
else:
assert '#line 8 "test.h"' in stdout
else:
assert double_include_error in stderr
@pytest.mark.parametrize("is_sys", (False, True))
@pytest.mark.parametrize("inv", (False, True))
@pytest.mark.parametrize("source_relative", (False, True))
def test_relative_header_3(record_property, tmpdir, is_sys, inv, source_relative):
test_subdir = os.path.join(tmpdir, "test_subdir")
os.mkdir(test_subdir)
header_file, _ = __test_relative_header_create_header(test_subdir)
test_file = __test_relative_header_create_source(tmpdir, "test_subdir/test.h", header_file, is_include1_sys=is_sys, inv=inv)
args = ["test.c" if source_relative else test_file]
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
record_property("stdout", stdout)
record_property("stderr", stderr)
if is_sys:
assert "missing header: Header not found" in stderr
else:
assert stderr == ''
if source_relative and not inv:
assert '#line 8 "test_subdir/test.h"' in stdout
else:
assert f'#line 8 "{pathlib.PurePath(test_subdir).as_posix()}/test.h"' in stdout
@pytest.mark.parametrize("use_short_path", (False, True))
@pytest.mark.parametrize("relative_include_dir", (False, True))
@pytest.mark.parametrize("is_sys", (False, True))
@pytest.mark.parametrize("inv", (False, True))
@pytest.mark.parametrize("source_relative", (False, True))
def test_relative_header_4(record_property, tmpdir, use_short_path, relative_include_dir, is_sys, inv, source_relative):
test_subdir = os.path.join(tmpdir, "test_subdir")
os.mkdir(test_subdir)
header_file, _ = __test_relative_header_create_header(test_subdir)
if use_short_path:
header_file = "test_subdir/test.h"
test_file = __test_relative_header_create_source(tmpdir, header_file, "test.h", is_include2_sys=is_sys, inv=inv)
args = [format_include_path_arg("test_subdir" if relative_include_dir else test_subdir), "test.c" if source_relative else test_file]
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
record_property("stdout", stdout)
record_property("stderr", stderr)
assert stderr == ''
if (source_relative and use_short_path and not inv) or (relative_include_dir and inv):
assert '#line 8 "test_subdir/test.h"' in stdout
else:
assert f'#line 8 "{pathlib.PurePath(test_subdir).as_posix()}/test.h"' in stdout
@pytest.mark.parametrize("with_pragma_once", (False, True))
@pytest.mark.parametrize("relative_include_dir", (False, True))
@pytest.mark.parametrize("is_sys", (False, True))
@pytest.mark.parametrize("inv", (False, True))
@pytest.mark.parametrize("source_relative", (False, True))
def test_relative_header_5(record_property, tmpdir, with_pragma_once, relative_include_dir, is_sys, inv, source_relative): # test relative paths with ..
## in this test, the subdir role is the opposite then the previous - it contains the test.c file, while the parent tmpdir contains the header file
header_file, double_include_error = __test_relative_header_create_header(tmpdir, with_pragma_once=with_pragma_once)
if is_sys:
header_file_second_path = "test.h"
else:
header_file_second_path = "../test.h"
test_subdir = os.path.join(tmpdir, "test_subdir")
os.mkdir(test_subdir)
test_file = __test_relative_header_create_source(test_subdir, header_file, header_file_second_path, is_include2_sys=is_sys, inv=inv)
args = ([format_include_path_arg(".." if relative_include_dir else tmpdir)] if is_sys else []) + ["test.c" if source_relative else test_file]
_, stdout, stderr = simplecpp(args, cwd=test_subdir)
record_property("stdout", stdout)
record_property("stderr", stderr)
if with_pragma_once:
assert stderr == ''
if (relative_include_dir if is_sys else source_relative) and inv:
assert '#line 8 "../test.h"' in stdout
else:
assert f'#line 8 "{pathlib.PurePath(tmpdir).as_posix()}/test.h"' in stdout
else:
assert double_include_error in stderr
@pytest.mark.parametrize("with_pragma_once", (False, True))
@pytest.mark.parametrize("relative_include_dir", (False, True))
@pytest.mark.parametrize("is_sys", (False, True))
@pytest.mark.parametrize("inv", (False, True))
@pytest.mark.parametrize("source_relative", (False, True))
def test_relative_header_6(record_property, tmpdir, with_pragma_once, relative_include_dir, is_sys, inv, source_relative): # test relative paths with .. that is resolved only by an include dir
## in this test, both the header and the source file are at the same dir, but there is a dummy inclusion dir as a subdir
header_file, double_include_error = __test_relative_header_create_header(tmpdir, with_pragma_once=with_pragma_once)
test_subdir = os.path.join(tmpdir, "test_subdir")
os.mkdir(test_subdir)
test_file = __test_relative_header_create_source(tmpdir, header_file, "../test.h", is_include2_sys=is_sys, inv=inv)
args = [format_include_path_arg("test_subdir" if relative_include_dir else test_subdir), "test.c" if source_relative else test_file]
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
record_property("stdout", stdout)
record_property("stderr", stderr)
if with_pragma_once:
assert stderr == ''
if relative_include_dir and inv:
assert '#line 8 "test.h"' in stdout
else:
assert f'#line 8 "{pathlib.PurePath(tmpdir).as_posix()}/test.h"' in stdout
else:
assert double_include_error in stderr
def test_same_name_header(record_property, tmpdir):
include_a = os.path.join(tmpdir, "include_a")
include_b = os.path.join(tmpdir, "include_b")
test_file = os.path.join(tmpdir, "test.c")
header_a = os.path.join(include_a, "header_a.h")
header_b = os.path.join(include_b, "header_b.h")
same_name_a = os.path.join(include_a, "same_name.h")
same_name_b = os.path.join(include_b, "same_name.h")
os.mkdir(include_a)
os.mkdir(include_b)
with open(test_file, "wt") as f:
f.write("""
#include <header_a.h>
#include <header_b.h>
TEST
""")
with open(header_a, "wt") as f:
f.write("""
#include "same_name.h"
""")
with open(header_b, "wt") as f:
f.write("""
#include "same_name.h"
""")
with open(same_name_a, "wt") as f:
f.write("""
#define TEST E
""")
with open(same_name_b, "wt") as f:
f.write("""
#define TEST OK
""")
args = [
format_include_path_arg(include_a),
format_include_path_arg(include_b),
test_file
]
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
record_property("stdout", stdout)
record_property("stderr", stderr)
assert "OK" in stdout
assert stderr == ""
def test_pragma_once_matching(record_property, tmpdir):
test_dir = os.path.join(tmpdir, "test_dir")
test_subdir = os.path.join(test_dir, "test_subdir")
test_file = os.path.join(test_dir, "test.c")
once_header = os.path.join(test_dir, "once.h")
if platform.system() == "Windows":
names_to_test = [
'"once.h"',
'"Once.h"',
'<once.h>',
'<Once.h>',
'"../test_dir/once.h"',
'"../test_dir/Once.h"',
'"../Test_Dir/once.h"',
'"../Test_Dir/Once.h"',
'"test_subdir/../once.h"',
'"test_subdir/../Once.h"',
'"Test_Subdir/../once.h"',
'"Test_Subdir/../Once.h"',
f"\"{test_dir}/once.h\"",
f"\"{test_dir}/Once.h\"",
f"<{test_dir}/once.h>",
f"<{test_dir}/Once.h>",
]
else:
names_to_test = [
'"once.h"',
'<once.h>',
'"../test_dir/once.h"',
'"test_subdir/../once.h"',
f"\"{test_dir}/once.h\"",
f"<{test_dir}/once.h>",
]
os.mkdir(test_dir)
os.mkdir(test_subdir)
with open(test_file, "wt") as f:
for n in names_to_test:
f.write(f"""
#include {n}
""");
with open(once_header, "wt") as f:
f.write(f"""
#pragma once
ONCE
""");
args = [
format_include_path_arg(test_dir),
test_file
]
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
record_property("stdout", stdout)
record_property("stderr", stderr)
assert stdout.count("ONCE") == 1
assert stderr == ""