Skip to content

Commit

Permalink
Merge pull request #6 from chaen/sanity
Browse files Browse the repository at this point in the history
Add sanity check to match opening and closing brackets
  • Loading branch information
chrisburr authored Jul 2, 2021
2 parents c54b03b + cdeb2f5 commit 75d7009
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/diraccfg/cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,8 @@ def loadFromBuffer(self, data):
:type data: string
:param data: Contents of the CFG
:return: This CFG
:raise SyntaxError: in case the number of opening and closing brackets do not match
"""
commentRE = re.compile(r"^\s*#")
self.reset()
Expand All @@ -945,7 +947,10 @@ def loadFromBuffer(self, data):
currentlyParsedString = ""
currentComment = ""
elif line[index] == "}":
currentLevel = levelList.pop()
try:
currentLevel = levelList.pop()
except IndexError:
raise ValueError("The cfg file seems to close more sections than it opens (i.e. to many '}' vs '{'")
elif line[index] == "=":
lFields = line.split("=")
currentLevel.setOption(lFields[0].strip(), "=".join(lFields[1:]).strip(), currentComment)
Expand All @@ -960,6 +965,9 @@ def loadFromBuffer(self, data):
break
else:
currentlyParsedString += line[index]
# At this point, the levelList should be empty
if levelList:
raise ValueError("The cfg file seems to open more sections than it closes (i.e. to many '{' vs '}'")
return self

@gCFGSynchro
Expand Down
20 changes: 20 additions & 0 deletions tests/broken_close.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Broken because more opening brackets than closing
DefaultModules = DIRAC
Sources
{
DIRAC = git://github.com/DIRACGrid/DIRAC.git
}
Releases
{
integration
{
Modules = DIRAC, WebAppDIRAC, VMDIRAC
Externals = v6r6p8
DIRACOS = master
# Here is a missing }
v7r0-pre19
{
Modules = DIRAC, VMDIRAC:v2r4-pre2, RESTDIRAC:v0r5, COMDIRAC:v0r17, WebAppDIRAC:v4r0p7, OAuthDIRAC:v0r1-pre1
DIRACOS = master
}
}
21 changes: 21 additions & 0 deletions tests/broken_open.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# That is a broken file, with more closing bracket than opening
#
DefaultModules = DIRAC
Sources
{
DIRAC = git://github.com/DIRACGrid/DIRAC.git
}
Releases
{
integration
# here is a missing {
Modules = DIRAC, WebAppDIRAC, VMDIRAC
Externals = v6r6p8
DIRACOS = master
}
v7r0-pre19
{
Modules = DIRAC, VMDIRAC:v2r4-pre2, RESTDIRAC:v0r5, COMDIRAC:v0r17, WebAppDIRAC:v4r0p7, OAuthDIRAC:v0r1-pre1
DIRACOS = master
}
}
14 changes: 13 additions & 1 deletion tests/test_cfg.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os

import pytest
from diraccfg.cfg import CFG

EXAMPLE_CFG_FILE = os.path.join(os.path.dirname(__file__), 'releases.cfg')
BROKEN_OPEN_CFG_FILE = os.path.join(os.path.dirname(__file__), 'broken_open.cfg')
BROKEN_CLOSE_CFG_FILE = os.path.join(os.path.dirname(__file__), 'broken_close.cfg')


def test_load():
Expand All @@ -13,3 +15,13 @@ def test_load():
def test_comment():
c = CFG().loadFromFile(EXAMPLE_CFG_FILE)
c.getComment('Releases').strip() == 'Here is where the releases go:'


def test_sanity():
with pytest.raises(ValueError) as excinfo:
rels = CFG().loadFromFile(BROKEN_OPEN_CFG_FILE)
assert 'close more section' in str(excinfo)

with pytest.raises(ValueError) as excinfo:
rels = CFG().loadFromFile(BROKEN_CLOSE_CFG_FILE)
assert 'open more section' in str(excinfo)

0 comments on commit 75d7009

Please sign in to comment.