Skip to content

Commit

Permalink
config: Fix parsing posargs default with colon
Browse files Browse the repository at this point in the history
e4d0d60 introduced a regression when posargs default
contained a colon, as the common substitution arg
regex splits on the colon, and uses different replace
types depending on the number of colons found.

Fixes #1785
  • Loading branch information
jayvdb committed Jan 11, 2021
1 parent ad6ca10 commit 7f13cf6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/changelog/1785.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix regression parsing posargs default containing colon. - by :user:`jayvdb`
7 changes: 7 additions & 0 deletions src/tox/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,13 @@ def _replace_match(self, match):
return self.reader.getposargs(default_value)

sub_type = g["sub_type"]
if sub_type == "posargs":
if default_value:
value = "{}:{}".format(sub_value, default_value)
else:
value = sub_value
return self.reader.getposargs(value)

if not sub_type and not sub_value:
raise tox.exception.ConfigError(
"Malformed substitution; no substitution type provided. "
Expand Down
49 changes: 46 additions & 3 deletions tests/unit/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ def test_command_substitution_from_other_section_posargs(self, newconfig):
""",
)
reader = SectionReader("testenv", config._cfg)
reader.addsubstitutions([r"argpos"])
reader.addsubstitutions(["argpos"])
x = reader.getargvlist("commands")
assert x == [["thing", "argpos", "arg2"]]

Expand All @@ -701,10 +701,23 @@ def test_command_section_and_posargs_substitution(self, newconfig):
""",
)
reader = SectionReader("testenv", config._cfg)
reader.addsubstitutions([r"argpos"])
reader.addsubstitutions(["argpos"])
x = reader.getargvlist("commands")
assert x == [["thing", "arg1", "argpos", "endarg"]]

def test_command_posargs_with_colon(self, newconfig):
"""Ensure posargs with default containing : succeeds"""
config = newconfig(
r"""
[testenv]
commands =
pytest {posargs:default with : colon after}
""",
)
reader = SectionReader("testenv", config._cfg)
x = reader.getargvlist("commands")
assert x[0] == ["pytest", "default", "with", ":", "colon", "after"]

def test_command_missing_substitution(self, newconfig):
config = newconfig(
"""
Expand Down Expand Up @@ -747,8 +760,38 @@ def test_command_env_substitution_posargs(self, newconfig):
""",
)
envconfig = config.envconfigs["py27"]
assert envconfig.commands == [["ls", "default"]]
assert envconfig.setenv["TEST"] == "default"
assert envconfig.commands == [["ls", "default"]]

def test_command_env_substitution_posargs_with_colon(self, newconfig):
"""Ensure {posargs} values are substituted correctly."""
config = newconfig(
"""
[testenv:py27]
setenv =
TEST=pytest {posargs:default with:colon after}
commands =
ls {env:TEST}
""",
)
envconfig = config.envconfigs["py27"]
assert envconfig.setenv["TEST"] == "pytest default with:colon after"
assert envconfig.commands == [["ls", "pytest", "default", "with:colon", "after"]]

def test_command_env_substitution_posargs_with_spaced_colon(self, newconfig):
"""Ensure {posargs} values are substituted correctly."""
config = newconfig(
"""
[testenv:py27]
setenv =
TEST=pytest {posargs:default with : colon after}
commands =
ls {env:TEST}
""",
)
envconfig = config.envconfigs["py27"]
assert envconfig.setenv["TEST"] == "pytest default with : colon after"
assert envconfig.commands == [["ls", "pytest", "default", "with", ":", "colon", "after"]]

def test_command_env_substitution_global(self, newconfig):
"""Ensure referenced {env:key:default} values are substituted correctly."""
Expand Down

0 comments on commit 7f13cf6

Please sign in to comment.