Skip to content

Commit

Permalink
Add interactive property to session. (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
AGeekInside authored and theacodes committed May 29, 2019
1 parent b639b4e commit d49c148
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ matrix:
install:
- pip install --upgrade pip setuptools
- pip install .
script: nox --session "$NOXSESSION"
script: nox --non-interactive --session "$NOXSESSION"
deploy:
provider: pypi
user: theacodes
Expand Down
26 changes: 26 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,32 @@ Would run both ``install`` commands, but skip the ``run`` command:
nox > Session tests was successful.
Forcing non-interactive behavior
--------------------------------

:attr:`session.interactive <nox.sessions.Session.interactive>` can be used to tell if Nox is being run from an interactive terminal (such as an actual human running it on their computer) vs run in a non-interactive terminal (such as a continuous integration system).

.. code-block:: python
@nox.session
def docs(session):
...
if session.interactive:
nox.run("sphinx-autobuild", ...)
else:
nox.run("sphinx-build", ...)
Sometimes it's useful to force Nox to see the session as non-interactive. You can use the ``--non-interactive`` argument to do this:

.. code-block:: bash
nox --non-interactive
This will cause ``session.interactive`` to always return ``False``.


Controlling color output
------------------------

Expand Down
9 changes: 8 additions & 1 deletion nox/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _color_finalizer(value, args):
if args.nocolor is True:
return False

return sys.stderr.isatty()
return sys.stdin.isatty()


def _posargs_finalizer(value, unused_args):
Expand Down Expand Up @@ -216,6 +216,13 @@ def _posargs_finalizer(value, unused_args):
noxfile=True,
help="Output a report of all sessions to the given filename.",
),
_option_set.Option(
"non_interactive",
"--non-interactive",
group="secondary",
action="store_true",
help="Force session.interactive to always be False, even in interactive sessions.",
),
_option_set.Option(
"nocolor",
"--nocolor",
Expand Down
6 changes: 6 additions & 0 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import hashlib
import os
import re
import sys
import unicodedata

import py
Expand Down Expand Up @@ -115,6 +116,11 @@ def bin(self):
"""The bin directory for the virtualenv."""
return self._runner.venv.bin

@property
def interactive(self):
"""Returns True if Nox is being run in an interactive session or False otherwise."""
return not self._runner.global_config.non_interactive and sys.stdin.isatty()

def chdir(self, dir):
"""Change the current working directory."""
self.log("cd {}".format(dir))
Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def docs(session):
session.cd("docs")
sphinx_args = ["-b", "html", "-W", "-d", "_build/doctrees", ".", "_build/html"]

if "serve" not in session.posargs:
if not session.interactive:
sphinx_cmd = "sphinx-build"
else:
sphinx_cmd = "sphinx-autobuild"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def test_main_color_from_isatty(monkeypatch, isatty_value, expected):
monkeypatch.setattr(sys, "argv", [sys.executable])
with mock.patch("nox.workflow.execute") as execute:
execute.return_value = 0
with mock.patch("sys.stderr.isatty") as isatty:
with mock.patch("sys.stdin.isatty") as isatty:
isatty.return_value = isatty_value

# Call the main function.
Expand Down
21 changes: 21 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ def test_properties(self):
assert session.bin is runner.venv.bin
assert session.python is runner.func.python

def test_interactive(self):
session, runner = self.make_session_and_runner()

with mock.patch("nox.sessions.sys.stdin.isatty") as m_isatty:
m_isatty.return_value = True

assert session.interactive is True

m_isatty.return_value = False

assert session.interactive is False

def test_explicit_non_interactive(self):
session, runner = self.make_session_and_runner()

with mock.patch("nox.sessions.sys.stdin.isatty") as m_isatty:
m_isatty.return_value = True
runner.global_config.non_interactive = True

assert session.interactive is False

def test_chdir(self, tmpdir):
cdto = str(tmpdir.join("cdbby").ensure(dir=True))
current_cwd = os.getcwd()
Expand Down

0 comments on commit d49c148

Please sign in to comment.