Skip to content

interative shell dependent config #991

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

Merged
merged 1 commit into from
Sep 16, 2018
Merged
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
1 change: 1 addition & 0 deletions changelog/947.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allow injecting config value inside the ini file dependent of the fact that we're connected to an interactive shell or not - by :user:`gaborbernat`
11 changes: 11 additions & 0 deletions doc/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,17 @@ the above example is roughly equivalent to
.. _`command positional substitution`:
.. _`positional substitution`:

interactive shell substitution
++++++++++++++++++++++++++++++

It's possible to inject a config value only when tox is running in interactive shell (standard input):

{tty:ON_VALUE:OFF_VALUE}

The first value is the value to inject when the interactive terminal is available,
the second value is the value to use when it's not. The later on is optional. A good use case
for this is e.g. passing in the ``--pdb`` flag for pytest.

substitutions for positional arguments in commands
++++++++++++++++++++++++++++++++++++++++++++++++++

Expand Down
20 changes: 14 additions & 6 deletions src/tox/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1430,23 +1430,27 @@ def _replace_match(self, match):

if sub_type == "env":
return self._replace_env(match)
if sub_type == "tty":
if is_interactive():
return match.group("substitution_value")
return match.group("default_value")
if sub_type is not None:
raise tox.exception.ConfigError(
"No support for the {} substitution type".format(sub_type)
)
return self._replace_substitution(match)

def _replace_env(self, match):
envkey = match.group("substitution_value")
if not envkey:
key = match.group("substitution_value")
if not key:
raise tox.exception.ConfigError("env: requires an environment variable name")
default = match.group("default_value")
envvalue = self.reader.get_environ_value(envkey)
if envvalue is not None:
return envvalue
value = self.reader.get_environ_value(key)
if value is not None:
return value
if default is not None:
return default
raise tox.exception.MissingSubstitution(envkey)
raise tox.exception.MissingSubstitution(key)

def _substitute_from_other_section(self, key):
if key.startswith("[") and "]" in key:
Expand Down Expand Up @@ -1475,6 +1479,10 @@ def _replace_substitution(self, match):
return str(val)


def is_interactive():
return sys.stdin.isatty()


class _ArgvlistReader:
@classmethod
def getargvlist(cls, reader, value, replace=True):
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2724,3 +2724,29 @@ def test_config_bad_config_type_specified(monkeypatch, tmpdir, capsys):
msg = "\n".join(notes) + "\n"
assert err == msg
assert "ERROR:" not in out


def test_interactive_na(newconfig, monkeypatch):
monkeypatch.setattr(tox.config, "is_interactive", lambda: False)
config = newconfig(
"""
[testenv:py]
setenv = A = {tty:X:Y}
"""
)
assert config.envconfigs["py"].setenv["A"] == "Y"


def test_interactive_available(newconfig, monkeypatch):
monkeypatch.setattr(tox.config, "is_interactive", lambda: True)
config = newconfig(
"""
[testenv:py]
setenv = A = {tty:X:Y}
"""
)
assert config.envconfigs["py"].setenv["A"] == "X"


def test_interactive():
tox.config.is_interactive()