Skip to content

Commit 75d7009

Browse files
authored
Merge pull request #6 from chaen/sanity
Add sanity check to match opening and closing brackets
2 parents c54b03b + cdeb2f5 commit 75d7009

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

src/diraccfg/cfg.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,8 @@ def loadFromBuffer(self, data):
922922
:type data: string
923923
:param data: Contents of the CFG
924924
:return: This CFG
925+
926+
:raise SyntaxError: in case the number of opening and closing brackets do not match
925927
"""
926928
commentRE = re.compile(r"^\s*#")
927929
self.reset()
@@ -945,7 +947,10 @@ def loadFromBuffer(self, data):
945947
currentlyParsedString = ""
946948
currentComment = ""
947949
elif line[index] == "}":
948-
currentLevel = levelList.pop()
950+
try:
951+
currentLevel = levelList.pop()
952+
except IndexError:
953+
raise ValueError("The cfg file seems to close more sections than it opens (i.e. to many '}' vs '{'")
949954
elif line[index] == "=":
950955
lFields = line.split("=")
951956
currentLevel.setOption(lFields[0].strip(), "=".join(lFields[1:]).strip(), currentComment)
@@ -960,6 +965,9 @@ def loadFromBuffer(self, data):
960965
break
961966
else:
962967
currentlyParsedString += line[index]
968+
# At this point, the levelList should be empty
969+
if levelList:
970+
raise ValueError("The cfg file seems to open more sections than it closes (i.e. to many '{' vs '}'")
963971
return self
964972

965973
@gCFGSynchro

tests/broken_close.cfg

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Broken because more opening brackets than closing
2+
DefaultModules = DIRAC
3+
Sources
4+
{
5+
DIRAC = git://github.com/DIRACGrid/DIRAC.git
6+
}
7+
Releases
8+
{
9+
integration
10+
{
11+
Modules = DIRAC, WebAppDIRAC, VMDIRAC
12+
Externals = v6r6p8
13+
DIRACOS = master
14+
# Here is a missing }
15+
v7r0-pre19
16+
{
17+
Modules = DIRAC, VMDIRAC:v2r4-pre2, RESTDIRAC:v0r5, COMDIRAC:v0r17, WebAppDIRAC:v4r0p7, OAuthDIRAC:v0r1-pre1
18+
DIRACOS = master
19+
}
20+
}

tests/broken_open.cfg

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# That is a broken file, with more closing bracket than opening
2+
#
3+
DefaultModules = DIRAC
4+
Sources
5+
{
6+
DIRAC = git://github.com/DIRACGrid/DIRAC.git
7+
}
8+
Releases
9+
{
10+
integration
11+
# here is a missing {
12+
Modules = DIRAC, WebAppDIRAC, VMDIRAC
13+
Externals = v6r6p8
14+
DIRACOS = master
15+
}
16+
v7r0-pre19
17+
{
18+
Modules = DIRAC, VMDIRAC:v2r4-pre2, RESTDIRAC:v0r5, COMDIRAC:v0r17, WebAppDIRAC:v4r0p7, OAuthDIRAC:v0r1-pre1
19+
DIRACOS = master
20+
}
21+
}

tests/test_cfg.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
2-
2+
import pytest
33
from diraccfg.cfg import CFG
44

55
EXAMPLE_CFG_FILE = os.path.join(os.path.dirname(__file__), 'releases.cfg')
6+
BROKEN_OPEN_CFG_FILE = os.path.join(os.path.dirname(__file__), 'broken_open.cfg')
7+
BROKEN_CLOSE_CFG_FILE = os.path.join(os.path.dirname(__file__), 'broken_close.cfg')
68

79

810
def test_load():
@@ -13,3 +15,13 @@ def test_load():
1315
def test_comment():
1416
c = CFG().loadFromFile(EXAMPLE_CFG_FILE)
1517
c.getComment('Releases').strip() == 'Here is where the releases go:'
18+
19+
20+
def test_sanity():
21+
with pytest.raises(ValueError) as excinfo:
22+
rels = CFG().loadFromFile(BROKEN_OPEN_CFG_FILE)
23+
assert 'close more section' in str(excinfo)
24+
25+
with pytest.raises(ValueError) as excinfo:
26+
rels = CFG().loadFromFile(BROKEN_CLOSE_CFG_FILE)
27+
assert 'open more section' in str(excinfo)

0 commit comments

Comments
 (0)