|
9 | 9 | from flake8_builtins import BuiltinsChecker
|
10 | 10 |
|
11 | 11 |
|
| 12 | +class FakeOptions: |
| 13 | + builtins_ignorelist = [] |
| 14 | + |
| 15 | + def __init__(self, ignore_list=''): |
| 16 | + if ignore_list: |
| 17 | + self.builtins_ignorelist = ignore_list |
| 18 | + |
| 19 | + |
12 | 20 | class TestBuiltins(unittest.TestCase):
|
13 |
| - def check_code(self, source, expected_codes=None): |
| 21 | + def check_code(self, source, expected_codes=None, ignore_list=None): |
14 | 22 | """Check if the given source code generates the given flake8 errors
|
15 | 23 |
|
16 | 24 | If `expected_codes` is a string is converted to a list,
|
17 | 25 | if it is not given, then it is expected to **not** generate any error.
|
| 26 | +
|
| 27 | + If `ignore_list` is provided, it should be a list of names |
| 28 | + that will be ignored if found, as if they were a builtin. |
18 | 29 | """
|
19 | 30 | if isinstance(expected_codes, str):
|
20 | 31 | expected_codes = [expected_codes]
|
21 | 32 | elif expected_codes is None:
|
22 | 33 | expected_codes = []
|
| 34 | + if ignore_list is None: |
| 35 | + ignore_list = [] |
23 | 36 | tree = ast.parse(textwrap.dedent(source))
|
24 | 37 | checker = BuiltinsChecker(tree, '/home/script.py')
|
| 38 | + checker.parse_options(FakeOptions(ignore_list=ignore_list), None) |
25 | 39 | return_statements = list(checker.run())
|
26 | 40 |
|
27 | 41 | self.assertEqual(
|
@@ -169,13 +183,17 @@ def bla():
|
169 | 183 | """
|
170 | 184 | self.check_code(source, ['A001', 'A001'])
|
171 | 185 |
|
172 |
| - def test_ignore_whitelisted_names(self): |
| 186 | + def test_default_ignored_names(self): |
173 | 187 | source = """
|
174 | 188 | class MyClass(object):
|
175 | 189 | __name__ = 4
|
176 | 190 | """
|
177 | 191 | self.check_code(source)
|
178 | 192 |
|
| 193 | + def test_custom_ignored_names(self): |
| 194 | + source = 'copyright = 4' |
| 195 | + self.check_code(source, ignore_list=('copyright',)) |
| 196 | + |
179 | 197 | def test_for_loop_variable(self):
|
180 | 198 | source = """
|
181 | 199 | for format in (1, 2, 3):
|
|
0 commit comments