Skip to content

Commit

Permalink
Add substitution {/} to os.sep
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb committed Oct 20, 2020
1 parent e4d0d60 commit c5fb611
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,12 @@ Globally available substitutions
OS-specific path separator (``:`` os \*nix family, ``;`` on Windows). May be used in ``setenv``,
when target variable is path variable (e.g. PATH or PYTHONPATH).

``{/}``
OS-specific directory separator (``/`` os \*nix family, ``\\`` on Windows).
Useful for deriving filenames from preset paths, as arguments for commands
that requires ``\\`` on Windows. e.g. ``{distdir}{/}file.txt``.
It is not usually needed when using commands written in Python.

Substitutions for virtualenv-related sections
+++++++++++++++++++++++++++++++++++++++++++++

Expand Down
2 changes: 2 additions & 0 deletions src/tox/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1811,6 +1811,8 @@ def _replace_match(self, match):
"Malformed substitution; no substitution type provided",
)

if not sub_type and not g["default_value"] and sub_value == "/":
return os.sep
if sub_type == "env":
return self._replace_env(match)
if sub_type == "tty":
Expand Down
26 changes: 25 additions & 1 deletion tests/unit/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2792,7 +2792,8 @@ def test_multiple_homedir_relative_local_indexservers(self, newconfig):

class TestConfigConstSubstitutions:
@pytest.mark.parametrize("pathsep", [":", ";"])
def test_replace_pathsep_unix(self, monkeypatch, newconfig, pathsep):
def test_replace_pathsep(self, monkeypatch, newconfig, pathsep):
"""Replace {:} with OS path separator."""
monkeypatch.setattr("os.pathsep", pathsep)
config = newconfig(
"""
Expand All @@ -2813,6 +2814,29 @@ def test_pathsep_regex(self):
assert mdict["substitution_value"] == ""
assert mdict["default_value"] == ""

@pytest.mark.parametrize("dirsep", ["\\", "\\\\"])
def test_dirsep_replace(self, monkeypatch, newconfig, dirsep):
"""Replace {/} with OS directory separator."""
monkeypatch.setattr("os.sep", dirsep)
config = newconfig(
"""
[testenv]
setenv =
VAR = dira{/}subdirb{/}subdirc
""",
)
envconfig = config.envconfigs["python"]
assert envconfig.setenv["VAR"] == dirsep.join(["dira", "subdirb", "subdirc"])

def test_dirsep_regex(self):
"""Sanity check for regex behavior for directory separator."""
regex = tox.config.Replacer.RE_ITEM_REF
match = next(regex.finditer("{/}"))
mdict = match.groupdict()
assert mdict["sub_type"] is None
assert mdict["substitution_value"] == "/"
assert mdict["default_value"] is None


class TestParseEnv:
def test_parse_recreate(self, newconfig):
Expand Down

0 comments on commit c5fb611

Please sign in to comment.