|
b45288f…
|
lmata
|
1 |
"""Tests for navegador.ingestion.puppet — PuppetParser internal methods.""" |
|
b45288f…
|
lmata
|
2 |
|
|
b45288f…
|
lmata
|
3 |
from unittest.mock import MagicMock, patch |
|
b45288f…
|
lmata
|
4 |
|
|
b45288f…
|
lmata
|
5 |
import pytest |
|
b45288f…
|
lmata
|
6 |
|
|
b45288f…
|
lmata
|
7 |
from navegador.graph.schema import NodeLabel |
|
b45288f…
|
lmata
|
8 |
|
|
b45288f…
|
lmata
|
9 |
|
|
b45288f…
|
lmata
|
10 |
class MockNode: |
|
b45288f…
|
lmata
|
11 |
_id_counter = 0 |
|
b45288f…
|
lmata
|
12 |
|
|
b45288f…
|
lmata
|
13 |
def __init__( |
|
b45288f…
|
lmata
|
14 |
self, |
|
b45288f…
|
lmata
|
15 |
type_: str, |
|
b45288f…
|
lmata
|
16 |
text: bytes = b"", |
|
b45288f…
|
lmata
|
17 |
children: list = None, |
|
b45288f…
|
lmata
|
18 |
start_byte: int = 0, |
|
b45288f…
|
lmata
|
19 |
end_byte: int = 0, |
|
b45288f…
|
lmata
|
20 |
start_point: tuple = (0, 0), |
|
b45288f…
|
lmata
|
21 |
end_point: tuple = (0, 0), |
|
b45288f…
|
lmata
|
22 |
parent=None, |
|
b45288f…
|
lmata
|
23 |
): |
|
b45288f…
|
lmata
|
24 |
MockNode._id_counter += 1 |
|
b45288f…
|
lmata
|
25 |
self.id = MockNode._id_counter |
|
b45288f…
|
lmata
|
26 |
self.type = type_ |
|
b45288f…
|
lmata
|
27 |
self._text = text |
|
b45288f…
|
lmata
|
28 |
self.children = children or [] |
|
b45288f…
|
lmata
|
29 |
self.start_byte = start_byte |
|
b45288f…
|
lmata
|
30 |
self.end_byte = end_byte |
|
b45288f…
|
lmata
|
31 |
self.start_point = start_point |
|
b45288f…
|
lmata
|
32 |
self.end_point = end_point |
|
b45288f…
|
lmata
|
33 |
self.parent = parent |
|
b45288f…
|
lmata
|
34 |
self._fields: dict = {} |
|
b45288f…
|
lmata
|
35 |
for child in self.children: |
|
b45288f…
|
lmata
|
36 |
child.parent = self |
|
b45288f…
|
lmata
|
37 |
|
|
b45288f…
|
lmata
|
38 |
def child_by_field_name(self, name: str): |
|
b45288f…
|
lmata
|
39 |
return self._fields.get(name) |
|
b45288f…
|
lmata
|
40 |
|
|
b45288f…
|
lmata
|
41 |
def set_field(self, name: str, node): |
|
b45288f…
|
lmata
|
42 |
self._fields[name] = node |
|
b45288f…
|
lmata
|
43 |
node.parent = self |
|
b45288f…
|
lmata
|
44 |
return self |
|
b45288f…
|
lmata
|
45 |
|
|
b45288f…
|
lmata
|
46 |
|
|
b45288f…
|
lmata
|
47 |
def _text_node(text: bytes, type_: str = "identifier") -> MockNode: |
|
b45288f…
|
lmata
|
48 |
return MockNode(type_, text, start_byte=0, end_byte=len(text)) |
|
b45288f…
|
lmata
|
49 |
|
|
b45288f…
|
lmata
|
50 |
|
|
b45288f…
|
lmata
|
51 |
def _make_store(): |
|
b45288f…
|
lmata
|
52 |
store = MagicMock() |
|
b45288f…
|
lmata
|
53 |
store.query.return_value = MagicMock(result_set=[]) |
|
b45288f…
|
lmata
|
54 |
return store |
|
b45288f…
|
lmata
|
55 |
|
|
b45288f…
|
lmata
|
56 |
|
|
b45288f…
|
lmata
|
57 |
def _make_parser(): |
|
b45288f…
|
lmata
|
58 |
from navegador.ingestion.puppet import PuppetParser |
|
b45288f…
|
lmata
|
59 |
|
|
b45288f…
|
lmata
|
60 |
parser = PuppetParser.__new__(PuppetParser) |
|
b45288f…
|
lmata
|
61 |
parser._parser = MagicMock() |
|
b45288f…
|
lmata
|
62 |
return parser |
|
b45288f…
|
lmata
|
63 |
|
|
b45288f…
|
lmata
|
64 |
|
|
b45288f…
|
lmata
|
65 |
class TestPuppetGetLanguage: |
|
b45288f…
|
lmata
|
66 |
def test_raises_when_not_installed(self): |
|
b45288f…
|
lmata
|
67 |
from navegador.ingestion.puppet import _get_puppet_language |
|
b45288f…
|
lmata
|
68 |
|
|
b45288f…
|
lmata
|
69 |
with patch.dict( |
|
b45288f…
|
lmata
|
70 |
"sys.modules", |
|
b45288f…
|
lmata
|
71 |
{ |
|
b45288f…
|
lmata
|
72 |
"tree_sitter_puppet": None, |
|
b45288f…
|
lmata
|
73 |
"tree_sitter": None, |
|
b45288f…
|
lmata
|
74 |
}, |
|
b45288f…
|
lmata
|
75 |
): |
|
b45288f…
|
lmata
|
76 |
with pytest.raises(ImportError, match="tree-sitter-puppet"): |
|
b45288f…
|
lmata
|
77 |
_get_puppet_language() |
|
b45288f…
|
lmata
|
78 |
|
|
b45288f…
|
lmata
|
79 |
def test_returns_language_object(self): |
|
b45288f…
|
lmata
|
80 |
from navegador.ingestion.puppet import _get_puppet_language |
|
b45288f…
|
lmata
|
81 |
|
|
b45288f…
|
lmata
|
82 |
mock_tspuppet = MagicMock() |
|
b45288f…
|
lmata
|
83 |
mock_ts = MagicMock() |
|
b45288f…
|
lmata
|
84 |
with patch.dict( |
|
b45288f…
|
lmata
|
85 |
"sys.modules", |
|
b45288f…
|
lmata
|
86 |
{ |
|
b45288f…
|
lmata
|
87 |
"tree_sitter_puppet": mock_tspuppet, |
|
b45288f…
|
lmata
|
88 |
"tree_sitter": mock_ts, |
|
b45288f…
|
lmata
|
89 |
}, |
|
b45288f…
|
lmata
|
90 |
): |
|
b45288f…
|
lmata
|
91 |
result = _get_puppet_language() |
|
b45288f…
|
lmata
|
92 |
assert result is mock_ts.Language.return_value |
|
b45288f…
|
lmata
|
93 |
|
|
b45288f…
|
lmata
|
94 |
|
|
b45288f…
|
lmata
|
95 |
class TestPuppetHandleClass: |
|
b45288f…
|
lmata
|
96 |
def test_creates_class_with_puppet_class_semantic_type(self): |
|
b45288f…
|
lmata
|
97 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
98 |
store = _make_store() |
|
b45288f…
|
lmata
|
99 |
source = b"nginx" |
|
b45288f…
|
lmata
|
100 |
class_ident = MockNode( |
|
b45288f…
|
lmata
|
101 |
"class_identifier", |
|
b45288f…
|
lmata
|
102 |
children=[ |
|
b45288f…
|
lmata
|
103 |
MockNode( |
|
b45288f…
|
lmata
|
104 |
"identifier", |
|
b45288f…
|
lmata
|
105 |
start_byte=0, |
|
b45288f…
|
lmata
|
106 |
end_byte=5, |
|
b45288f…
|
lmata
|
107 |
), |
|
b45288f…
|
lmata
|
108 |
], |
|
b45288f…
|
lmata
|
109 |
start_byte=0, |
|
b45288f…
|
lmata
|
110 |
end_byte=5, |
|
b45288f…
|
lmata
|
111 |
) |
|
b45288f…
|
lmata
|
112 |
node = MockNode( |
|
b45288f…
|
lmata
|
113 |
"class_definition", |
|
b45288f…
|
lmata
|
114 |
children=[class_ident], |
|
b45288f…
|
lmata
|
115 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
116 |
end_point=(5, 1), |
|
b45288f…
|
lmata
|
117 |
) |
|
b45288f…
|
lmata
|
118 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
119 |
parser._handle_class(node, source, "nginx.pp", store, stats) |
|
b45288f…
|
lmata
|
120 |
assert stats["classes"] == 1 |
|
b45288f…
|
lmata
|
121 |
assert stats["edges"] == 1 |
|
b45288f…
|
lmata
|
122 |
label = store.create_node.call_args[0][0] |
|
b45288f…
|
lmata
|
123 |
props = store.create_node.call_args[0][1] |
|
b45288f…
|
lmata
|
124 |
assert label == NodeLabel.Class |
|
b45288f…
|
lmata
|
125 |
assert props["name"] == "nginx" |
|
b45288f…
|
lmata
|
126 |
assert props["semantic_type"] == "puppet_class" |
|
b45288f…
|
lmata
|
127 |
|
|
b45288f…
|
lmata
|
128 |
def test_skips_when_no_class_identifier(self): |
|
b45288f…
|
lmata
|
129 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
130 |
store = _make_store() |
|
b45288f…
|
lmata
|
131 |
node = MockNode( |
|
b45288f…
|
lmata
|
132 |
"class_definition", |
|
b45288f…
|
lmata
|
133 |
children=[], |
|
b45288f…
|
lmata
|
134 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
135 |
end_point=(0, 5), |
|
b45288f…
|
lmata
|
136 |
) |
|
b45288f…
|
lmata
|
137 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
138 |
parser._handle_class(node, b"", "test.pp", store, stats) |
|
b45288f…
|
lmata
|
139 |
assert stats["classes"] == 0 |
|
b45288f…
|
lmata
|
140 |
store.create_node.assert_not_called() |
|
b45288f…
|
lmata
|
141 |
|
|
b45288f…
|
lmata
|
142 |
|
|
b45288f…
|
lmata
|
143 |
class TestPuppetHandleDefinedType: |
|
b45288f…
|
lmata
|
144 |
def test_creates_class_with_puppet_defined_type(self): |
|
b45288f…
|
lmata
|
145 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
146 |
store = _make_store() |
|
b45288f…
|
lmata
|
147 |
source = b"nginx::vhost" |
|
b45288f…
|
lmata
|
148 |
class_ident = MockNode( |
|
b45288f…
|
lmata
|
149 |
"class_identifier", |
|
b45288f…
|
lmata
|
150 |
children=[ |
|
b45288f…
|
lmata
|
151 |
MockNode( |
|
b45288f…
|
lmata
|
152 |
"identifier", |
|
b45288f…
|
lmata
|
153 |
start_byte=0, |
|
b45288f…
|
lmata
|
154 |
end_byte=5, |
|
b45288f…
|
lmata
|
155 |
), |
|
b45288f…
|
lmata
|
156 |
MockNode( |
|
b45288f…
|
lmata
|
157 |
"identifier", |
|
b45288f…
|
lmata
|
158 |
start_byte=7, |
|
b45288f…
|
lmata
|
159 |
end_byte=12, |
|
b45288f…
|
lmata
|
160 |
), |
|
b45288f…
|
lmata
|
161 |
], |
|
b45288f…
|
lmata
|
162 |
start_byte=0, |
|
b45288f…
|
lmata
|
163 |
end_byte=12, |
|
b45288f…
|
lmata
|
164 |
) |
|
b45288f…
|
lmata
|
165 |
node = MockNode( |
|
b45288f…
|
lmata
|
166 |
"defined_resource_type", |
|
b45288f…
|
lmata
|
167 |
children=[class_ident], |
|
b45288f…
|
lmata
|
168 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
169 |
end_point=(3, 1), |
|
b45288f…
|
lmata
|
170 |
) |
|
b45288f…
|
lmata
|
171 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
172 |
parser._handle_defined_type(node, source, "vhost.pp", store, stats) |
|
b45288f…
|
lmata
|
173 |
assert stats["classes"] == 1 |
|
b45288f…
|
lmata
|
174 |
label = store.create_node.call_args[0][0] |
|
b45288f…
|
lmata
|
175 |
props = store.create_node.call_args[0][1] |
|
b45288f…
|
lmata
|
176 |
assert label == NodeLabel.Class |
|
b45288f…
|
lmata
|
177 |
assert props["name"] == "nginx::vhost" |
|
b45288f…
|
lmata
|
178 |
assert props["semantic_type"] == "puppet_defined_type" |
|
b45288f…
|
lmata
|
179 |
|
|
b45288f…
|
lmata
|
180 |
|
|
b45288f…
|
lmata
|
181 |
class TestPuppetHandleNode: |
|
b45288f…
|
lmata
|
182 |
def test_creates_class_with_puppet_node(self): |
|
b45288f…
|
lmata
|
183 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
184 |
store = _make_store() |
|
b45288f…
|
lmata
|
185 |
source = b"'webserver'" |
|
b45288f…
|
lmata
|
186 |
string_node = MockNode( |
|
b45288f…
|
lmata
|
187 |
"string", |
|
b45288f…
|
lmata
|
188 |
start_byte=0, |
|
b45288f…
|
lmata
|
189 |
end_byte=11, |
|
b45288f…
|
lmata
|
190 |
) |
|
b45288f…
|
lmata
|
191 |
node_name = MockNode( |
|
b45288f…
|
lmata
|
192 |
"node_name", |
|
b45288f…
|
lmata
|
193 |
children=[string_node], |
|
b45288f…
|
lmata
|
194 |
) |
|
b45288f…
|
lmata
|
195 |
node = MockNode( |
|
b45288f…
|
lmata
|
196 |
"node_definition", |
|
b45288f…
|
lmata
|
197 |
children=[node_name], |
|
b45288f…
|
lmata
|
198 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
199 |
end_point=(3, 1), |
|
b45288f…
|
lmata
|
200 |
) |
|
b45288f…
|
lmata
|
201 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
202 |
parser._handle_node(node, source, "nodes.pp", store, stats) |
|
b45288f…
|
lmata
|
203 |
assert stats["classes"] == 1 |
|
b45288f…
|
lmata
|
204 |
label = store.create_node.call_args[0][0] |
|
b45288f…
|
lmata
|
205 |
props = store.create_node.call_args[0][1] |
|
b45288f…
|
lmata
|
206 |
assert label == NodeLabel.Class |
|
b45288f…
|
lmata
|
207 |
assert props["name"] == "webserver" |
|
b45288f…
|
lmata
|
208 |
assert props["semantic_type"] == "puppet_node" |
|
b45288f…
|
lmata
|
209 |
|
|
b45288f…
|
lmata
|
210 |
def test_skips_when_no_node_name(self): |
|
b45288f…
|
lmata
|
211 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
212 |
store = _make_store() |
|
b45288f…
|
lmata
|
213 |
node = MockNode( |
|
b45288f…
|
lmata
|
214 |
"node_definition", |
|
b45288f…
|
lmata
|
215 |
children=[], |
|
b45288f…
|
lmata
|
216 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
217 |
end_point=(0, 5), |
|
b45288f…
|
lmata
|
218 |
) |
|
b45288f…
|
lmata
|
219 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
220 |
parser._handle_node(node, b"", "nodes.pp", store, stats) |
|
b45288f…
|
lmata
|
221 |
assert stats["classes"] == 0 |
|
b45288f…
|
lmata
|
222 |
|
|
b45288f…
|
lmata
|
223 |
|
|
b45288f…
|
lmata
|
224 |
class TestPuppetHandleResource: |
|
b45288f…
|
lmata
|
225 |
def test_creates_function_with_puppet_resource(self): |
|
b45288f…
|
lmata
|
226 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
227 |
store = _make_store() |
|
b45288f…
|
lmata
|
228 |
source = b"package 'nginx'" |
|
b45288f…
|
lmata
|
229 |
ident = MockNode( |
|
b45288f…
|
lmata
|
230 |
"identifier", |
|
b45288f…
|
lmata
|
231 |
start_byte=0, |
|
b45288f…
|
lmata
|
232 |
end_byte=7, |
|
b45288f…
|
lmata
|
233 |
) |
|
b45288f…
|
lmata
|
234 |
title = MockNode( |
|
b45288f…
|
lmata
|
235 |
"string", |
|
b45288f…
|
lmata
|
236 |
start_byte=8, |
|
b45288f…
|
lmata
|
237 |
end_byte=15, |
|
b45288f…
|
lmata
|
238 |
) |
|
b45288f…
|
lmata
|
239 |
node = MockNode( |
|
b45288f…
|
lmata
|
240 |
"resource_declaration", |
|
b45288f…
|
lmata
|
241 |
children=[ident, title], |
|
b45288f…
|
lmata
|
242 |
start_point=(1, 0), |
|
b45288f…
|
lmata
|
243 |
end_point=(3, 1), |
|
b45288f…
|
lmata
|
244 |
) |
|
b45288f…
|
lmata
|
245 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
246 |
parser._handle_resource(node, source, "nginx.pp", "nginx", store, stats) |
|
b45288f…
|
lmata
|
247 |
assert stats["functions"] == 1 |
|
b45288f…
|
lmata
|
248 |
assert stats["edges"] == 1 |
|
b45288f…
|
lmata
|
249 |
label = store.create_node.call_args[0][0] |
|
b45288f…
|
lmata
|
250 |
props = store.create_node.call_args[0][1] |
|
b45288f…
|
lmata
|
251 |
assert label == NodeLabel.Function |
|
b45288f…
|
lmata
|
252 |
assert props["name"] == "package[nginx]" |
|
b45288f…
|
lmata
|
253 |
assert props["semantic_type"] == "puppet_resource" |
|
b45288f…
|
lmata
|
254 |
|
|
b45288f…
|
lmata
|
255 |
def test_skips_when_no_type_identifier(self): |
|
b45288f…
|
lmata
|
256 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
257 |
store = _make_store() |
|
b45288f…
|
lmata
|
258 |
node = MockNode( |
|
b45288f…
|
lmata
|
259 |
"resource_declaration", |
|
b45288f…
|
lmata
|
260 |
children=[], |
|
b45288f…
|
lmata
|
261 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
262 |
end_point=(0, 5), |
|
b45288f…
|
lmata
|
263 |
) |
|
b45288f…
|
lmata
|
264 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
265 |
parser._handle_resource(node, b"", "test.pp", "myclass", store, stats) |
|
b45288f…
|
lmata
|
266 |
assert stats["functions"] == 0 |
|
b45288f…
|
lmata
|
267 |
|
|
b45288f…
|
lmata
|
268 |
|
|
b45288f…
|
lmata
|
269 |
class TestPuppetHandleInclude: |
|
b45288f…
|
lmata
|
270 |
def test_creates_import_node(self): |
|
b45288f…
|
lmata
|
271 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
272 |
store = _make_store() |
|
b45288f…
|
lmata
|
273 |
source = b"stdlib" |
|
b45288f…
|
lmata
|
274 |
class_ident = MockNode( |
|
b45288f…
|
lmata
|
275 |
"class_identifier", |
|
b45288f…
|
lmata
|
276 |
children=[ |
|
b45288f…
|
lmata
|
277 |
MockNode( |
|
b45288f…
|
lmata
|
278 |
"identifier", |
|
b45288f…
|
lmata
|
279 |
start_byte=0, |
|
b45288f…
|
lmata
|
280 |
end_byte=6, |
|
b45288f…
|
lmata
|
281 |
), |
|
b45288f…
|
lmata
|
282 |
], |
|
b45288f…
|
lmata
|
283 |
) |
|
b45288f…
|
lmata
|
284 |
node = MockNode( |
|
b45288f…
|
lmata
|
285 |
"include_statement", |
|
b45288f…
|
lmata
|
286 |
children=[class_ident], |
|
b45288f…
|
lmata
|
287 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
288 |
end_point=(0, 14), |
|
b45288f…
|
lmata
|
289 |
) |
|
b45288f…
|
lmata
|
290 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
291 |
parser._handle_include(node, source, "init.pp", store, stats) |
|
b45288f…
|
lmata
|
292 |
assert stats["edges"] == 1 |
|
b45288f…
|
lmata
|
293 |
label = store.create_node.call_args[0][0] |
|
b45288f…
|
lmata
|
294 |
props = store.create_node.call_args[0][1] |
|
b45288f…
|
lmata
|
295 |
assert label == NodeLabel.Import |
|
b45288f…
|
lmata
|
296 |
assert props["name"] == "stdlib" |
|
b45288f…
|
lmata
|
297 |
assert props["semantic_type"] == "puppet_include" |
|
b45288f…
|
lmata
|
298 |
|
|
b45288f…
|
lmata
|
299 |
def test_skips_when_no_class_identifier(self): |
|
b45288f…
|
lmata
|
300 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
301 |
store = _make_store() |
|
b45288f…
|
lmata
|
302 |
node = MockNode( |
|
b45288f…
|
lmata
|
303 |
"include_statement", |
|
b45288f…
|
lmata
|
304 |
children=[], |
|
b45288f…
|
lmata
|
305 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
306 |
end_point=(0, 7), |
|
b45288f…
|
lmata
|
307 |
) |
|
b45288f…
|
lmata
|
308 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
309 |
parser._handle_include(node, b"", "init.pp", store, stats) |
|
b45288f…
|
lmata
|
310 |
assert stats["edges"] == 0 |
|
b45288f…
|
lmata
|
311 |
store.create_node.assert_not_called() |
|
b45288f…
|
lmata
|
312 |
|
|
b45288f…
|
lmata
|
313 |
|
|
b45288f…
|
lmata
|
314 |
class TestPuppetHandleParameters: |
|
b45288f…
|
lmata
|
315 |
def test_creates_variable_nodes(self): |
|
b45288f…
|
lmata
|
316 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
317 |
store = _make_store() |
|
b45288f…
|
lmata
|
318 |
source = b"$port" |
|
b45288f…
|
lmata
|
319 |
var_node = MockNode( |
|
b45288f…
|
lmata
|
320 |
"variable", |
|
b45288f…
|
lmata
|
321 |
start_byte=0, |
|
b45288f…
|
lmata
|
322 |
end_byte=5, |
|
b45288f…
|
lmata
|
323 |
) |
|
b45288f…
|
lmata
|
324 |
param = MockNode( |
|
b45288f…
|
lmata
|
325 |
"parameter", |
|
b45288f…
|
lmata
|
326 |
children=[var_node], |
|
b45288f…
|
lmata
|
327 |
start_point=(1, 2), |
|
b45288f…
|
lmata
|
328 |
end_point=(1, 7), |
|
b45288f…
|
lmata
|
329 |
) |
|
b45288f…
|
lmata
|
330 |
param_list = MockNode( |
|
b45288f…
|
lmata
|
331 |
"parameter_list", |
|
b45288f…
|
lmata
|
332 |
children=[param], |
|
b45288f…
|
lmata
|
333 |
) |
|
b45288f…
|
lmata
|
334 |
class_ident = MockNode( |
|
b45288f…
|
lmata
|
335 |
"class_identifier", |
|
b45288f…
|
lmata
|
336 |
children=[ |
|
b45288f…
|
lmata
|
337 |
MockNode( |
|
b45288f…
|
lmata
|
338 |
"identifier", |
|
b45288f…
|
lmata
|
339 |
start_byte=0, |
|
b45288f…
|
lmata
|
340 |
end_byte=5, |
|
b45288f…
|
lmata
|
341 |
), |
|
b45288f…
|
lmata
|
342 |
], |
|
b45288f…
|
lmata
|
343 |
start_byte=0, |
|
b45288f…
|
lmata
|
344 |
end_byte=5, |
|
b45288f…
|
lmata
|
345 |
) |
|
b45288f…
|
lmata
|
346 |
node = MockNode( |
|
b45288f…
|
lmata
|
347 |
"class_definition", |
|
b45288f…
|
lmata
|
348 |
children=[class_ident, param_list], |
|
b45288f…
|
lmata
|
349 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
350 |
end_point=(5, 1), |
|
b45288f…
|
lmata
|
351 |
) |
|
b45288f…
|
lmata
|
352 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
353 |
parser._extract_parameters(node, source, "nginx.pp", "nginx", store, stats) |
|
b45288f…
|
lmata
|
354 |
store.create_node.assert_called_once() |
|
b45288f…
|
lmata
|
355 |
label = store.create_node.call_args[0][0] |
|
b45288f…
|
lmata
|
356 |
props = store.create_node.call_args[0][1] |
|
b45288f…
|
lmata
|
357 |
assert label == NodeLabel.Variable |
|
b45288f…
|
lmata
|
358 |
assert props["name"] == "port" |
|
b45288f…
|
lmata
|
359 |
assert props["semantic_type"] == "puppet_parameter" |
|
b45288f…
|
lmata
|
360 |
assert stats["edges"] == 1 |
|
b45288f…
|
lmata
|
361 |
|
|
b45288f…
|
lmata
|
362 |
def test_skips_param_without_variable(self): |
|
b45288f…
|
lmata
|
363 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
364 |
store = _make_store() |
|
b45288f…
|
lmata
|
365 |
param = MockNode( |
|
b45288f…
|
lmata
|
366 |
"parameter", |
|
b45288f…
|
lmata
|
367 |
children=[MockNode("type")], |
|
b45288f…
|
lmata
|
368 |
start_point=(1, 2), |
|
b45288f…
|
lmata
|
369 |
end_point=(1, 7), |
|
b45288f…
|
lmata
|
370 |
) |
|
b45288f…
|
lmata
|
371 |
param_list = MockNode( |
|
b45288f…
|
lmata
|
372 |
"parameter_list", |
|
b45288f…
|
lmata
|
373 |
children=[param], |
|
b45288f…
|
lmata
|
374 |
) |
|
b45288f…
|
lmata
|
375 |
node = MockNode( |
|
b45288f…
|
lmata
|
376 |
"class_definition", |
|
b45288f…
|
lmata
|
377 |
children=[param_list], |
|
b45288f…
|
lmata
|
378 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
379 |
end_point=(5, 1), |
|
b45288f…
|
lmata
|
380 |
) |
|
b45288f…
|
lmata
|
381 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
382 |
parser._extract_parameters(node, b"", "test.pp", "myclass", store, stats) |
|
b45288f…
|
lmata
|
383 |
store.create_node.assert_not_called() |
|
b45288f…
|
lmata
|
384 |
|
|
b45288f…
|
lmata
|
385 |
|
|
b45288f…
|
lmata
|
386 |
class TestPuppetWalkDispatch: |
|
b45288f…
|
lmata
|
387 |
def test_walk_dispatches_class_definition(self): |
|
b45288f…
|
lmata
|
388 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
389 |
store = _make_store() |
|
b45288f…
|
lmata
|
390 |
source = b"nginx" |
|
b45288f…
|
lmata
|
391 |
class_ident = MockNode( |
|
b45288f…
|
lmata
|
392 |
"class_identifier", |
|
b45288f…
|
lmata
|
393 |
children=[ |
|
b45288f…
|
lmata
|
394 |
MockNode( |
|
b45288f…
|
lmata
|
395 |
"identifier", |
|
b45288f…
|
lmata
|
396 |
start_byte=0, |
|
b45288f…
|
lmata
|
397 |
end_byte=5, |
|
b45288f…
|
lmata
|
398 |
), |
|
b45288f…
|
lmata
|
399 |
], |
|
b45288f…
|
lmata
|
400 |
) |
|
b45288f…
|
lmata
|
401 |
class_def = MockNode( |
|
b45288f…
|
lmata
|
402 |
"class_definition", |
|
b45288f…
|
lmata
|
403 |
children=[class_ident], |
|
b45288f…
|
lmata
|
404 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
405 |
end_point=(5, 1), |
|
b45288f…
|
lmata
|
406 |
) |
|
b45288f…
|
lmata
|
407 |
root = MockNode("program", children=[class_def]) |
|
b45288f…
|
lmata
|
408 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
409 |
parser._walk(root, source, "nginx.pp", store, stats) |
|
b45288f…
|
lmata
|
410 |
assert stats["classes"] == 1 |
|
b45288f…
|
lmata
|
411 |
|
|
b45288f…
|
lmata
|
412 |
def test_walk_dispatches_defined_resource_type(self): |
|
b45288f…
|
lmata
|
413 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
414 |
store = _make_store() |
|
b45288f…
|
lmata
|
415 |
source = b"vhost" |
|
b45288f…
|
lmata
|
416 |
class_ident = MockNode( |
|
b45288f…
|
lmata
|
417 |
"class_identifier", |
|
b45288f…
|
lmata
|
418 |
children=[ |
|
b45288f…
|
lmata
|
419 |
MockNode( |
|
b45288f…
|
lmata
|
420 |
"identifier", |
|
b45288f…
|
lmata
|
421 |
start_byte=0, |
|
b45288f…
|
lmata
|
422 |
end_byte=5, |
|
b45288f…
|
lmata
|
423 |
), |
|
b45288f…
|
lmata
|
424 |
], |
|
b45288f…
|
lmata
|
425 |
) |
|
b45288f…
|
lmata
|
426 |
define_node = MockNode( |
|
b45288f…
|
lmata
|
427 |
"defined_resource_type", |
|
b45288f…
|
lmata
|
428 |
children=[class_ident], |
|
b45288f…
|
lmata
|
429 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
430 |
end_point=(3, 1), |
|
b45288f…
|
lmata
|
431 |
) |
|
b45288f…
|
lmata
|
432 |
root = MockNode("program", children=[define_node]) |
|
b45288f…
|
lmata
|
433 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
434 |
parser._walk(root, source, "vhost.pp", store, stats) |
|
b45288f…
|
lmata
|
435 |
assert stats["classes"] == 1 |
|
b45288f…
|
lmata
|
436 |
|
|
b45288f…
|
lmata
|
437 |
def test_walk_dispatches_node_definition(self): |
|
b45288f…
|
lmata
|
438 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
439 |
store = _make_store() |
|
b45288f…
|
lmata
|
440 |
source = b"'webserver'" |
|
b45288f…
|
lmata
|
441 |
string_node = MockNode( |
|
b45288f…
|
lmata
|
442 |
"string", |
|
b45288f…
|
lmata
|
443 |
start_byte=0, |
|
b45288f…
|
lmata
|
444 |
end_byte=11, |
|
b45288f…
|
lmata
|
445 |
) |
|
b45288f…
|
lmata
|
446 |
node_name = MockNode( |
|
b45288f…
|
lmata
|
447 |
"node_name", |
|
b45288f…
|
lmata
|
448 |
children=[string_node], |
|
b45288f…
|
lmata
|
449 |
) |
|
b45288f…
|
lmata
|
450 |
node_def = MockNode( |
|
b45288f…
|
lmata
|
451 |
"node_definition", |
|
b45288f…
|
lmata
|
452 |
children=[node_name], |
|
b45288f…
|
lmata
|
453 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
454 |
end_point=(3, 1), |
|
b45288f…
|
lmata
|
455 |
) |
|
b45288f…
|
lmata
|
456 |
root = MockNode("program", children=[node_def]) |
|
b45288f…
|
lmata
|
457 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
458 |
parser._walk(root, source, "nodes.pp", store, stats) |
|
b45288f…
|
lmata
|
459 |
assert stats["classes"] == 1 |
|
b45288f…
|
lmata
|
460 |
|
|
b45288f…
|
lmata
|
461 |
def test_walk_dispatches_include_statement(self): |
|
b45288f…
|
lmata
|
462 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
463 |
store = _make_store() |
|
b45288f…
|
lmata
|
464 |
source = b"stdlib" |
|
b45288f…
|
lmata
|
465 |
class_ident = MockNode( |
|
b45288f…
|
lmata
|
466 |
"class_identifier", |
|
b45288f…
|
lmata
|
467 |
children=[ |
|
b45288f…
|
lmata
|
468 |
MockNode( |
|
b45288f…
|
lmata
|
469 |
"identifier", |
|
b45288f…
|
lmata
|
470 |
start_byte=0, |
|
b45288f…
|
lmata
|
471 |
end_byte=6, |
|
b45288f…
|
lmata
|
472 |
), |
|
b45288f…
|
lmata
|
473 |
], |
|
b45288f…
|
lmata
|
474 |
) |
|
b45288f…
|
lmata
|
475 |
include = MockNode( |
|
b45288f…
|
lmata
|
476 |
"include_statement", |
|
b45288f…
|
lmata
|
477 |
children=[class_ident], |
|
b45288f…
|
lmata
|
478 |
start_point=(0, 0), |
|
b45288f…
|
lmata
|
479 |
end_point=(0, 14), |
|
b45288f…
|
lmata
|
480 |
) |
|
b45288f…
|
lmata
|
481 |
root = MockNode("program", children=[include]) |
|
b45288f…
|
lmata
|
482 |
stats = {"functions": 0, "classes": 0, "edges": 0} |
|
b45288f…
|
lmata
|
483 |
parser._walk(root, source, "init.pp", store, stats) |
|
b45288f…
|
lmata
|
484 |
assert stats["edges"] == 1 |
|
b45288f…
|
lmata
|
485 |
|
|
b45288f…
|
lmata
|
486 |
|
|
b45288f…
|
lmata
|
487 |
class TestPuppetParseFile: |
|
b45288f…
|
lmata
|
488 |
def test_creates_file_node(self): |
|
b45288f…
|
lmata
|
489 |
import tempfile |
|
b45288f…
|
lmata
|
490 |
from pathlib import Path |
|
b45288f…
|
lmata
|
491 |
|
|
b45288f…
|
lmata
|
492 |
parser = _make_parser() |
|
b45288f…
|
lmata
|
493 |
store = _make_store() |
|
b45288f…
|
lmata
|
494 |
mock_tree = MagicMock() |
|
b45288f…
|
lmata
|
495 |
mock_tree.root_node.type = "program" |
|
b45288f…
|
lmata
|
496 |
mock_tree.root_node.children = [] |
|
b45288f…
|
lmata
|
497 |
parser._parser.parse.return_value = mock_tree |
|
b45288f…
|
lmata
|
498 |
with tempfile.NamedTemporaryFile(suffix=".pp", delete=False) as f: |
|
b45288f…
|
lmata
|
499 |
f.write(b"class nginx {}\n") |
|
b45288f…
|
lmata
|
500 |
fpath = Path(f.name) |
|
b45288f…
|
lmata
|
501 |
try: |
|
b45288f…
|
lmata
|
502 |
parser.parse_file(fpath, fpath.parent, store) |
|
b45288f…
|
lmata
|
503 |
store.create_node.assert_called_once() |
|
b45288f…
|
lmata
|
504 |
label = store.create_node.call_args[0][0] |
|
b45288f…
|
lmata
|
505 |
props = store.create_node.call_args[0][1] |
|
b45288f…
|
lmata
|
506 |
assert label == NodeLabel.File |
|
b45288f…
|
lmata
|
507 |
assert props["language"] == "puppet" |
|
b45288f…
|
lmata
|
508 |
finally: |
|
b45288f…
|
lmata
|
509 |
fpath.unlink() |