Skip to content

Commit

Permalink
fix bugs related to type of resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Nfsaavedra committed May 30, 2024
1 parent 42da0d4 commit acacfa2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
36 changes: 29 additions & 7 deletions puppetparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,18 +684,25 @@ def p_resource_body(p: YaccProduction):

def p_virtual_resource(p: YaccProduction):
r"resource : AT ID LBRACKET resource_list RBRACKET"
id = Id(
p.lineno(2),
find_column(script, p.lexpos(2)),
p.lineno(2),
find_column(script, p.lexpos(2)) + len(p[2]),
"@" + p[2],
)
if len(p[4]) == 1:
p[0] = Resource(
p.lineno(2),
find_column(script, p.lexpos(2)),
p.lineno(5),
find_column(script, p.lexpos(5)) + 1,
"@" + p[2],
id,
p[4][0][0],
p[4][0][1],
)
else:
resources = map(lambda r: Resource(r[2], r[3], r[4], r[5], "@" + p[2], r[0], r[1]), p[4]) # type: ignore
resources = map(lambda r: Resource(r[2], r[3], r[4], r[5], id, r[0], r[1]), p[4]) # type: ignore
default = None
for r in resources:
if isinstance(r.title, Value) and r.title.value == "default":
Expand All @@ -719,18 +726,26 @@ def p_virtual_resource(p: YaccProduction):

def p_exported_resource(p: YaccProduction):
r"resource : AT AT ID LBRACKET resource_list RBRACKET"
id = Id(
p.lineno(3),
find_column(script, p.lexpos(3)),
p.lineno(3),
find_column(script, p.lexpos(3)) + len(p[3]),
"@@" + p[3],
)

if len(p[4]) == 1:
p[0] = Resource(
p.lineno(3),
find_column(script, p.lexpos(3)),
p.lineno(6),
find_column(script, p.lexpos(6)) + 1,
"@@" + p[3],
id,
p[5][0][0],
p[5][0][1],
)
else:
resources = map(lambda r: Resource(r[2], r[3], r[4], r[5], "@@" + p[3], r[0], r[1]), p[5]) # type: ignore
resources = map(lambda r: Resource(r[2], r[3], r[4], r[5], id, r[0], r[1]), p[5]) # type: ignore
default = None
for r in resources:
if isinstance(r.title, Value) and r.title.value == "default":
Expand Down Expand Up @@ -792,12 +807,19 @@ def p_change_resource_collector(p: YaccProduction):

def p_resource_default(p: YaccProduction):
r"resource : ID_TYPE LBRACKET attributes RBRACKET"
id = Id(
p.lineno(1),
find_column(script, p.lexpos(1)),
p.lineno(1),
find_column(script, p.lexpos(1)) + len(p[1]),
p[1],
)
p[0] = Resource(
p.lineno(1),
find_column(script, p.lexpos(1)),
p.lineno(4),
find_column(script, p.lexpos(4)) + 1,
p[1],
id,
None,
p[3],
)
Expand Down Expand Up @@ -832,7 +854,7 @@ def p_resource_collector(p: YaccProduction):
p.lineno(1),
find_column(script, p.lexpos(1)),
p.lineno(4),
find_column(script, p.lexpos(4)) + 1,
find_column(script, p.lexpos(4)) + 2,
p[1],
p[3],
)
Expand All @@ -843,7 +865,7 @@ def p_resource_collector_empty(p: YaccProduction):
p.lineno(1),
find_column(script, p.lexpos(1)),
p.lineno(3),
find_column(script, p.lexpos(3)) + 1,
find_column(script, p.lexpos(3)) + 2,
p[1],
None,
)
Expand Down
5 changes: 3 additions & 2 deletions tests/test_exported_resource.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

from puppetparser.parser import parse
from puppetparser.model import Resource
from puppetparser.model import Resource, Id


class TestClass(unittest.TestCase):
Expand All @@ -19,4 +19,5 @@ def test_exported_resource(self):

res = parse(code)[0]
self.assertIsInstance(res[0], Resource)
self.assertEqual(res[0].type, "@@nagios_service")
self.assertIsInstance(res[0].type, Id)
self.assertEqual(res[0].type.value, "@@nagios_service")
4 changes: 3 additions & 1 deletion tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ResourceCollector,
ResourceDeclaration,
ResourceExpression,
Id,
)
from tests.utility import assertArray, assertHash

Expand Down Expand Up @@ -219,7 +220,8 @@ def test_resource_default(self):

res = parse(code)[0]
self.assertIsInstance(res[0], Resource)
self.assertEqual(res[0].type, "Exec")
self.assertIsInstance(res[0].type, Id)
self.assertEqual(res[0].type.value, "Exec")

def test_resource_expression(self):
code = """
Expand Down
2 changes: 2 additions & 0 deletions tests/test_resource_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ def test_resource_collector(self):
self.assertIsInstance(res[0], ResourceCollector)
self.assertIsInstance(res[0].search, Operation)
self.assertEqual(res[0].search.operator, "==")
self.assertEqual(res[0].col, 13)
self.assertEqual(res[0].end_col, 39)
5 changes: 3 additions & 2 deletions tests/test_virtual_resource.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

from puppetparser.parser import parse
from puppetparser.model import Realize, Resource
from puppetparser.model import Realize, Resource, Id


class TestClass(unittest.TestCase):
Expand All @@ -18,5 +18,6 @@ def test_virtual_resource(self):

res = parse(code)[0]
self.assertIsInstance(res[0], Resource)
self.assertEqual(res[0].type, "@user")
self.assertIsInstance(res[0].type, Id)
self.assertEqual(res[0].type.value, "@user")
self.assertIsInstance(res[1], Realize)

0 comments on commit acacfa2

Please sign in to comment.