Repository navigation
Expand file tree
/
Copy pathtest_bashlex.py
More file actions
60 lines (44 loc) · 1.58 KB
/
Copy pathtest_bashlex.py
File metadata and controls
60 lines (44 loc) · 1.58 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
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import os.path
from functools import partial
import attr
from packagedcode.bashlex import BashShellLexer
from packages_test_utils import build_tests
from packages_test_utils import PackageTester
from scancode_config import REGEN_TEST_FIXTURES
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
@attr.s
class PygmentsToken:
pos = attr.ib()
type = attr.ib()
value = attr.ib()
def to_dict(self):
return attr.asdict(self)
@classmethod
def lex(cls, lexer, text):
"""
Return a list of token mappings by running the ``lexer`` on the ``text``
string.
"""
return [cls(*ts).to_dict() for ts in lexer().get_tokens_unprocessed(text)]
class TestBashlexDatadriven(PackageTester):
test_data_dir = test_data_dir
def lexer_tester(location, lexer_class):
with open(location) as inp:
text = inp.read()
return PygmentsToken.lex(lexer_class, text)
build_tests(
test_dir=os.path.join(test_data_dir, 'bashlex'),
test_file_suffix=('.sh', 'APKBUILD', 'PKGBUILD',),
clazz=TestBashlexDatadriven,
tested_function=partial(lexer_tester, lexer_class=BashShellLexer),
test_method_prefix='test_collect_shell_variables_',
regen=REGEN_TEST_FIXTURES,
)