Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add comment stripping from config file (issue #332) #529

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,76 @@
from tox.config import (
SectionReader, is_section_substitution, CommandParser,
parseconfig, DepOption, get_homedir, getcontextname,
strip_comments,
)
from tox.venv import VirtualEnv


class TestVenvConfig:
@pytest.mark.parametrize("config_string", [
("""
[tox] # comment
# comment
toxworkdir = {0}
indexserver =
xyz = xyz_repo
[testenv:py1]
deps=hello
[testenv:py2]
deps=
world1 # comment
:xyz:http://hello/world#page
"""),
("""
[tox] ; comment
; comment
toxworkdir = {0}
indexserver =
xyz = xyz_repo
[testenv:py1]
deps=hello
[testenv:py2]
deps=
world1 ; comment
:xyz:http://hello/world#page
""")
])
def test_with_comments(self, tmpdir, newconfig, config_string):
config = newconfig([], config_string.format(tmpdir))
assert config.toxworkdir == tmpdir
assert len(config.envconfigs) == 2
assert config.envconfigs['py1'].envdir == tmpdir.join("py1")
dep = config.envconfigs['py1'].deps[0]
assert dep.name == "hello"
assert dep.indexserver is None
assert config.envconfigs['py2'].envdir == tmpdir.join("py2")
dep1, dep2 = config.envconfigs['py2'].deps
assert dep1.name == "world1"
assert dep2.name == "http://hello/world#page"
assert dep2.indexserver.name == "xyz"
assert dep2.indexserver.url == "xyz_repo"

def test_stripping_comments(self):
config = """
#comment
[block] # comment
# comment
; comment
#comment
#comment#with#hashtags
url = address#importantpart "something # something ; importantthing"
name = value #comment
name = value # comment etc
name = value ;comment
name = value ; comment etc
quotes after ";this"
"""
config = strip_comments(config)
assert ('comment' in config) is False
assert ('importantpart' in config) is True
assert ('importantthing' in config) is True
assert ('this' in config) is True

def test_config_parsing_minimal(self, tmpdir, newconfig):
config = newconfig([], """
[testenv:py1]
Expand Down
28 changes: 27 additions & 1 deletion tox/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,12 +679,38 @@ def make_hashseed():
return str(random.randint(1, max_seed))


def strip_comments(config):
if re.search('((\s#(.*))|(\n#(.*))|(^#.*)|( ;(.*))|(\n;(.*))|(^;.*))', config):
i = 0
strings = []
quotes = re.search('([\'"].*[\'"])', config)
while quotes:
strings.append(quotes.group(1))
config = re.sub('([\'"].*[\'"])', '{' + str(i) + '}', config, 1)
i += 1
quotes = re.search('([\'"].*[\'"])', config)

config = re.sub('((\s#(.*))|(\n#(.*))|(^#.*))', '', config)
config = re.sub('((\s;(.*))|(\n;(.*))|(^;.*))', '', config)

if strings:
config = config.format(*strings)

return config


class parseini:
def __init__(self, config, inipath):
config.toxinipath = inipath
config.toxinidir = config.toxinipath.dirpath()

self._cfg = py.iniconfig.IniConfig(config.toxinipath)
ini_data = []
with open(config.toxinipath.strpath) as f:
ini_data = f.read()

ini_data = strip_comments(ini_data)

self._cfg = py.iniconfig.IniConfig(path=config.toxinipath, data=ini_data)
config._cfg = self._cfg
self.config = config

Expand Down