-
Notifications
You must be signed in to change notification settings - Fork 3
/
tests.py
185 lines (151 loc) · 4.55 KB
/
tests.py
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
import subprocess
import json
import os
import pytest
TEST_DIR = os.path.abspath(os.path.dirname(__file__))
@pytest.fixture
def change_and_clean_dir():
cmd = ';'.join(('cd {}'.format(TEST_DIR),
'rm -f *.json .*.json *.o *.os hello* build*/*'))
subprocess.call(cmd, shell=True)
pytestmark = pytest.mark.usefixtures("change_and_clean_dir")
def run_scons(cmd):
cmd = "cd {}; {}".format(TEST_DIR, cmd)
subprocess.check_output(cmd, shell=True)
def read_compile_db(db_name='compile_commands.json'):
db_path = '{}/{}'.format(TEST_DIR, db_name)
if not os.path.exists(db_path):
return None
with open(db_path) as f:
return json.load(f)
def test_basic():
run_scons('scons -f sconstruct_basic')
db = read_compile_db()
assert db == [
{
'directory': TEST_DIR,
'command': "gcc -o a.o -c -DD1 -II1 a.c",
'file': 'a.c'
},
{
'directory': TEST_DIR,
'command': "g++ -o b.o -c -DD1 -II1 b.cpp",
'file': 'b.cpp'
},
]
run_scons('scons -f sconstruct_basic -c')
assert not read_compile_db()
def test_enable_with_cmdline():
run_scons('scons -f sconstruct_cmdline')
assert read_compile_db() is None
run_scons('scons -f sconstruct_cmdline --compiledb=')
assert read_compile_db() is not None
def test_config_db():
run_scons('scons -f sconstruct_config_db')
assert read_compile_db('compile_commands.json') is None
assert read_compile_db('foo.json') is not None
def test_config_entry_func_simple():
run_scons('scons -f sconstruct_config_entry_func_simple')
db = read_compile_db()
assert db == [
{
'directory': TEST_DIR,
'command': 'clang -DD1 -DD2 -II1 -II2 -c a.c',
'file': 'a.c'
},
{
'directory': TEST_DIR,
'command': 'clang++ -DD1 -DD2 -II1 -II2 -c b.cpp',
'file': 'b.cpp'
},
]
def test_config_entry_func_simple_with_variantdir():
run_scons('scons -f sconstruct_variant')
db = read_compile_db()
assert db == [
{
'directory': TEST_DIR,
'command': ("clang -DD1 -Ibuild_variant/I1 -Isrc_variant/I1 "
"-c src_variant/a.c"),
'file': 'src_variant/a.c'
},
]
def test_config_custom_entry_func():
run_scons('scons -f sconstruct_config_custom_entry_func')
db = read_compile_db()
assert db == [
{
'directory': "c:", # Changed
'command': 'clang -DD1 -DD2 -II1 -II2 -c a.c',
'file': 'a.c'
}
]
def test_merge():
run_scons('scons -f sconstruct_merge')
run_scons('scons -f sconstruct_merge2')
db = read_compile_db()
assert db == [
{
'directory': TEST_DIR,
'command': "gcc -o a.o -c -DD1 -II1 a.c",
'file': 'a.c'
},
{
'directory': TEST_DIR,
'command': "gcc -o b.o -c -DD2 -II2 b.c",
'file': 'b.c'
},
]
def test_config_reset():
run_scons('scons -f sconstruct_merge')
run_scons('scons -f sconstruct_config_reset')
db = read_compile_db()
assert db == [
{
'directory': TEST_DIR,
'command': "gcc -o b.o -c -DD2 -II2 b.c",
'file': 'b.c'
},
]
def test_same_source_compiled_multiple_times():
run_scons('scons -f sconstruct_same_source')
db = read_compile_db()
assert db == [
{
'directory': TEST_DIR,
'command': "gcc -o build2/a.o -c -DD2 a.c",
'file': 'a.c'
},
]
def test_config_multi():
run_scons('scons -f sconstruct_config_multi')
db = read_compile_db()
assert db == [
{
'directory': TEST_DIR,
'command': "gcc -o build/a.o -c -DD1 a.c",
'file': 'a.c'
},
{
'directory': TEST_DIR,
'command': "gcc -o build2/a.o -c -DD2 a.c",
'file': 'a.c'
},
]
def test_enable_with_cmdline_with_config():
run_scons('scons -f sconstruct_cmdline --compiledb=')
assert read_compile_db() is not None
run_scons('scons -f sconstruct_cmdline_config --compiledb=reset,multi')
db = read_compile_db()
assert db == [
{
'directory': TEST_DIR,
'command': "gcc -o build/b.o -c -DD1 b.c",
'file': 'b.c'
},
{
'directory': TEST_DIR,
'command': "gcc -o build2/b.o -c -DD2 b.c",
'file': 'b.c'
},
]