diff --git a/tests/test_config.py b/tests/test_config.py index d03a5d2949..634c35616d 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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] diff --git a/tox/config.py b/tox/config.py index 406cc5dead..9bcc8b7cd2 100755 --- a/tox/config.py +++ b/tox/config.py @@ -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