-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgnlint_unittest.py
executable file
·395 lines (358 loc) · 12.5 KB
/
gnlint_unittest.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Unittests for gnlint."""
from __future__ import print_function
import os
import sys
import unittest
# Find chromite!
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
'..', '..', '..'))
# pylint: disable=wrong-import-position
from chromite.lib import commandline
from chromite.lib import cros_logging as logging
from chromite.lib import cros_test_lib
from chromite.lib import osutils
import gnlint
# pylint: enable=wrong-import-position
class LintTestCase(cros_test_lib.TestCase):
"""Helper for running linters."""
def _CheckLinter(self, functor, inputs, is_bad_input=True):
"""Make sure |functor| rejects or accepts every input in |inputs|."""
# First run a sanity check.
ret = functor(self.STUB_DATA)
self.assertEqual(ret, [])
# Then run through all the bad inputs.
for x in inputs:
ret = functor(x)
if is_bad_input:
self.assertNotEqual(ret, [])
else:
self.assertEqual(ret, [])
class UtilityTests(cros_test_lib.MockTestCase):
"""Tests for utility funcs."""
def testFilterFiles(self):
"""Check filtering of files based on extension works."""
exp = [
'cow.gn',
'cow.gni',
]
files = [
'.gitignore',
'.gitignore.gn',
'cow.gn',
'cow.gn.orig',
'cow.gni',
'gn',
'README.md',
]
extensions = set(('gn', 'gni'))
result = sorted(gnlint.FilterFiles(files, extensions))
self.assertEqual(result, exp)
def testGetParser(self):
"""Make sure it doesn't crash."""
parser = gnlint.GetParser()
self.assertTrue(isinstance(parser, commandline.ArgumentParser))
def testMain(self):
"""Make sure it doesn't crash."""
gnlint.main(['foo'])
def testMainErrors(self):
"""Make sure outputting results doesn't crash."""
self.PatchObject(gnlint, 'CheckGnFile', return_value=[
gnlint.LintResult('LintFunc', 'foo.gn', 'msg!', logging.ERROR),
])
gnlint.main(['foo.gn'])
class FilesystemUtilityTests(cros_test_lib.MockTempDirTestCase):
"""Tests for utility funcs that access the filesystem."""
@unittest.skipIf(not os.path.exists(gnlint.GetGnPath()),
'Skipping since gn is not available: crbug.com/1078990.')
def testCheckGnFile(self):
"""Check CheckGnFile tails down correctly."""
content = '# gn file\n'
gnfile = os.path.join(self.tempdir, 'asdf')
osutils.WriteFile(gnfile, content)
self.assertExists(gnlint.GetGnPath())
ret = gnlint.CheckGnFile(gnfile)
self.assertEqual(ret, [])
@unittest.skipIf(not os.path.exists(gnlint.GetGnPath()),
'Skipping since gn is not available: crbug.com/1078990.')
def testGnFileOption(self):
"""Check CheckGnFile processes file options correctly."""
static_library_with_visibility_flag = (
'static_library("a") {\n'
' cflags = [ "-fvisibility=default" ]\n'
'}\n')
gn_options = '#gnlint: disable=GnLintVisibilityFlags\n'
gnfile = os.path.join(self.tempdir, 'asdf')
osutils.WriteFile(gnfile, static_library_with_visibility_flag)
self.assertExists(gnlint.GetGnPath())
ret = gnlint.CheckGnFile(gnfile)
self.assertEqual(len(ret), 1)
osutils.WriteFile(gnfile, gn_options+static_library_with_visibility_flag)
ret = gnlint.CheckGnFile(gnfile)
self.assertEqual(ret, [])
@unittest.skipIf(not os.path.exists(gnlint.GetGnPath()),
'Skipping since gn is not available: crbug.com/1078990.')
def testCheckFormatDetectError(self):
"""Check CheckGnFile detects non-standard format."""
content = 'executable("foo"){\n}\n' # no space after ')'
gnfile = os.path.join(self.tempdir, 'asdf')
osutils.WriteFile(gnfile, content)
self.assertExists(gnlint.GetGnPath())
ret = gnlint.CheckGnFile(gnfile)
self.assertEqual(len(ret), 1)
def testFilterPaths(self):
"""Check filtering of files in subdirs."""
subfile = os.path.join(self.tempdir, 'a/b/c.gn')
osutils.Touch(subfile, makedirs=True)
subdir = os.path.join(self.tempdir, 'src')
for f in ('blah.gni', 'Makefile', 'source.cc'):
osutils.Touch(os.path.join(subdir, f), makedirs=True)
exp = sorted([
os.path.join(subdir, 'blah.gni'),
subfile,
])
paths = [
subdir,
subfile,
]
extensions = set(('gn', 'gni'))
result = sorted(gnlint.FilterPaths(paths, extensions))
self.assertEqual(result, exp)
def CreateTestData(flag_name, operator, value):
"""Creates a dictionary for testing simple assignment in a static_library."""
# static_library("my_static_library") {
# <flag_name> <operator> [ <value> ]
# }
#
# for example, when flag_name='cflags', operator='+=', value='"-lfoo"',
# the result stands for a gn file like this:
# static_library("my_static_library") {
# cflags += [ "-lfoo" ]
# }
if not isinstance(value, list):
value = [value]
value_list = []
for item in value:
value_list.append({'type': 'LITERAL', 'value': item})
return {
'child': [{
'child': [{
'child': [{
'type': 'LITERAL',
'value': '\"my_static_library\"'
}],
'type': 'LIST'
}, {
'child': [{
'child': [{
'type': 'IDENTIFIER',
'value': flag_name
}, {
'child': value_list,
'type': 'LIST'
}],
'type': 'BINARY',
'value': operator
}],
'type': 'BLOCK'
}],
'type': 'FUNCTION',
'value': 'static_library'
}],
'type': 'BLOCK'
}
class GnLintTests(LintTestCase):
"""Tests of various gn linters."""
STUB_DATA = {'type': 'BLOCK'}
def testGnLintLibFlags(self):
"""Verify GnLintLibFlags catches bad inputs."""
self._CheckLinter(gnlint.GnLintLibFlags, [
CreateTestData('ldflags', '=', '-lfoo'),
CreateTestData('ldflags', '+=', '-lfoo'),
CreateTestData('ldflags', '-=', '-lfoo'),
])
def testGnLintVisibilityFlags(self):
"""Verify GnLintVisibilityFlags catches bad inputs."""
self._CheckLinter(gnlint.GnLintVisibilityFlags, [
CreateTestData('cflags', '=', '"-fvisibility"'),
CreateTestData('cflags', '+=', '"-fvisibility"'),
CreateTestData('cflags', '-=', '"-fvisibility=default"'),
CreateTestData('cflags_c', '-=', '"-fvisibility=hidden"'),
CreateTestData('cflags_cc', '-=', '"-fvisibility=internal"'),
])
def testGnLintDefineFlags(self):
"""Verify GnLintDefineFlags catches bad inputs."""
self._CheckLinter(gnlint.GnLintDefineFlags, [
CreateTestData('cflags', '=', '"-D_FLAG"'),
CreateTestData('cflags', '+=', '"-D_FLAG"'),
CreateTestData('cflags', '-=', '"-D_FLAG=1"'),
CreateTestData('cflags_c', '=', '"-D_FLAG=0"'),
CreateTestData('cflags_cc', '=', '"-D_FLAG=\"something\""'),
])
def testGnLintCommonTesting(self):
"""Verify GnLintCommonTesting catches bad inputs."""
self._CheckLinter(gnlint.GnLintCommonTesting, [
CreateTestData('libs', '=', '"gmock"'),
CreateTestData('libs', '=', '"gtest"'),
CreateTestData('libs', '=', ['"gmock"', '"gtest"'])
])
def testGnLintStaticSharedLibMixing(self):
"""Verify GnLintStaticSharedLibMixing catches bad inputs."""
# static_library("static_pie") {
# configs += [ "//common-mk:pie" ]
# }
# shared_library("shared") {
# deps = [ ":static_pie" ]
# }
self._CheckLinter(gnlint.GnLintStaticSharedLibMixing, [{
'child': [{
'child': [{
'child': [{
'type': 'LITERAL',
'value': '\"static_pie\"'
}],
'type': 'LIST'
}, {
'child': [{
'child': [{
'type': 'IDENTIFIER',
'value': 'configs'
}, {
'child': [{
'type': 'LITERAL',
'value': '\"//common-mk:pie\"'
}],
'type': 'LIST'
}],
'type': 'BINARY',
'value': '+='
}],
'type': 'BLOCK'
}],
'type': 'FUNCTION',
'value': 'static_library'
}, {
'child': [{
'child': [{
'type': 'LITERAL',
'value': '\"shared\"'
}],
'type': 'LIST'
}, {
'child': [{
'child': [{
'type': 'IDENTIFIER',
'value': 'deps'
}, {
'child': [{
'type': 'LITERAL',
'value': '\":static_pie\"'
}],
'type': 'LIST'
}],
'type': 'BINARY',
'value': '='
}],
'type': 'BLOCK'
}],
'type': 'FUNCTION',
'value': 'shared_library'
}],
'type': 'BLOCK'
}])
# Negative test case which makes linked library PIC. Should be accepted.
# static_library("static_pic") {
# configs += [ "//common-mk:pic" ]
# configs -= [ "//common-mk:pie" ]
# }
# shared_library("shared") {
# deps = [ ":static_pic" ]
# }
self._CheckLinter(gnlint.GnLintStaticSharedLibMixing, [{
'child': [{
'child': [{
'child': [{
'type': 'LITERAL',
'value': '\"static_pic\"'
}],
'type': 'LIST'
}, {
'child': [{
'child': [{
'type': 'IDENTIFIER',
'value': 'configs'
}, {
'child': [{
'type': 'LITERAL',
'value': '\"//common-mk:pic\"'
}],
'type': 'LIST'
}],
'type': 'BINARY',
'value': '+='
}, {
'child': [{
'type': 'IDENTIFIER',
'value': 'configs'
}, {
'child': [{
'type': 'LITERAL',
'value': '\"//common-mk:pie\"'
}],
'type': 'LIST'
}],
'type': 'BINARY',
'value': '-='
}],
'type': 'BLOCK'
}],
'type': 'FUNCTION',
'value': 'static_library'
}, {
'child': [{
'child': [{
'type': 'LITERAL',
'value': '\"shared\"'
}],
'type': 'LIST'
}, {
'child': [{
'child': [{
'type': 'IDENTIFIER',
'value': 'deps'
}, {
'child': [{
'type': 'LITERAL',
'value': '\":static_pic\"'
}],
'type': 'LIST'
}],
'type': 'BINARY',
'value': '='
}],
'type': 'BLOCK'
}],
'type': 'FUNCTION',
'value': 'shared_library'
}],
'type': 'BLOCK'
}], is_bad_input=False)
def testGnLintSourceFileNames(self):
"""Verify GnLintSourceFileNames catches bad inputs."""
self._CheckLinter(gnlint.GnLintSourceFileNames, [
CreateTestData('sources', '=', 'foo_unittest.c'),
CreateTestData('sources', '=', 'foo_unittest.cc'),
CreateTestData('sources', '=', 'foo_unittest.h'),
])
def testGnLintPkgConfigs(self):
"""Verify GnLintPkgConfigs catches bad inputs."""
self._CheckLinter(gnlint.GnLintPkgConfigs, [
CreateTestData('libs', '=', 'z'),
CreateTestData('libs', '=', 'ssl'),
])
if __name__ == '__main__':
cros_test_lib.main(module=__name__)