Skip to content
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

Check for NOXSESSION environment variable #121

Merged
merged 7 commits into from
Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ By default nox will run all sessions defined in the noxfile. However, you can ch
nox -s lint tests
nox -e lint

You can also use the ``NOXSESSION`` environment variable::

NOXSESSION=lint nox
NOXSESSION=lint,tests nox

Nox will run these sessions in the same order they are specified.

You can also use `pytest-style keywords`_ to filter test sessions::
Expand Down
7 changes: 7 additions & 0 deletions nox/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def main():
"-e",
"--sessions",
nargs="*",
default=_get_default_sessions(),
help="Which sessions to run, by default, all sessions will run.",
)
parser.add_argument(
Expand Down Expand Up @@ -136,5 +137,11 @@ def main():
sys.exit(exit_code)


def _get_default_sessions():
nox_env = os.environ.get("NOXSESSION")
env_sessions = nox_env.split(",") if nox_env else None
return env_sessions


if __name__ == "__main__": # pragma: no cover
main()
25 changes: 25 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import contexter
import pkg_resources
import pytest

import nox
import nox.__main__
Expand Down Expand Up @@ -60,6 +61,8 @@ def test_global_config_constructor():


def test_main_no_args():
# Prevents any interference from outside
os.environ.pop("NOXSESSION", None)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if you are ok with this workaround

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind updating this to use monkeypatch.delenv? https://docs.pytest.org/en/latest/reference.html#_pytest.monkeypatch.MonkeyPatch.delenv and likewise with setenv for the test below?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that looks like what we need here

sys.argv = [sys.executable]
with mock.patch("nox.workflow.execute") as execute:
execute.return_value = 0
Expand Down Expand Up @@ -146,6 +149,28 @@ def test_main_explicit_sessions():
assert config.sessions == ["1", "2"]


@pytest.mark.parametrize(
"env,sessions", [("foo", ["foo"]), ("foo,bar", ["foo", "bar"])]
)
def test_main_session_from_nox_env_var(env, sessions):
os.environ["NOXSESSION"] = env
sys.argv = [sys.executable]
with mock.patch("nox.workflow.execute") as execute:
execute.return_value = 0

# Call the main function.
with mock.patch.object(sys, "exit") as exit:
nox.__main__.main()
exit.assert_called_once_with(0)
assert execute.called

# Verify that the sessions from the env var are listed in the config.
config = execute.call_args[1]["global_config"]
assert len(config.sessions) == len(sessions)
for session in sessions:
assert session in config.sessions


def test_main_positional_args():
sys.argv = [sys.executable, "1", "2", "3"]
with mock.patch("nox.workflow.execute") as execute:
Expand Down