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

Add dry run option, test, and documentation #823

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 16 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,22 @@ Would run both ``install`` commands, but skip the ``run`` command:
nox > Skipping pytest run, as --install-only is set.
nox > Session tests was successful.

Skipping every command and only outputting the commands that would be run
-------------------------------------------------------------------------

If you want to see what commands would be run without actually running them, you can use ``--dry-run``:

.. code-block:: console

nox --dry-run
nox > Would run session tests but --dry-run is set.
nox > Running session tests
nox > Creating virtualenv using python3.7 in ./.nox/tests
nox > python -m pip install pytest
nox > python -m pip install .
nox > python -m pytest
nox > Session tests was successful.


Forcing non-interactive behavior
--------------------------------
Expand Down
7 changes: 7 additions & 0 deletions nox/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,13 @@ def _tag_completer(
hidden=True,
default=os.getcwd,
),
_option_set.Option(
"dry_run",
"--dry-run",
group=options.groups["execution"],
action="store_true",
help="Print the commands that would run without running them.",
),
)


Expand Down
20 changes: 20 additions & 0 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ def run(
msg = "First argument to `session.run` is a list. Did you mean to use `session.run(*args)`?"
raise ValueError(msg)

if self._runner.global_config.dry_run:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't these be moved down? It would not run {args} even if --dry-run was not set, because --install-only was set too.

logger.info(f"Would run: {args}, but --dry-run is set.")
return None

if self._runner.global_config.install_only:
logger.info(f"Skipping {args[0]} run, as --install-only is set.")
return None
Expand Down Expand Up @@ -481,6 +485,11 @@ def run_install(
Default: ``0.2``
:type terminate_timeout: float or None
"""

if self._runner.global_config.dry_run:
logger.info(f"Would run: {args}, but --dry-run is set.")
return None

if (
self._runner.global_config.no_install
and self._runner.venv is not None
Expand Down Expand Up @@ -664,6 +673,10 @@ def conda_install(
if not args:
raise ValueError("At least one argument required to install().")

if self._runner.global_config.dry_run:
logger.info(f"Would run: {args}, but --dry-run is set.")
return None

if self._runner.global_config.no_install and (
isinstance(venv, PassthroughEnv) or venv._reused
):
Expand Down Expand Up @@ -761,6 +774,9 @@ def install(
"A session without a virtualenv can not install dependencies."
)
if isinstance(venv, PassthroughEnv):
if self._runner.global_config.dry_run:
logger.info(f"Would run: {args}, but --dry-run is set.")
return None
if self._runner.global_config.no_install:
return
raise ValueError(
Expand All @@ -772,6 +788,10 @@ def install(
if not args:
raise ValueError("At least one argument required to install().")

if self._runner.global_config.dry_run:
logger.info(f"Would run: {args}, but --dry-run is set.")
return

if self._runner.global_config.no_install and venv._reused:
return

Expand Down
8 changes: 8 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ def test_run_install_only_should_install(self):
**run_with_defaults(paths=mock.ANY, silent=True, env={}, external="error"),
)

def test_run_dry_run(self):
session, _ = self.make_session_and_runner()

with mock.patch("nox.command.run", autospec=True) as run:
session.run(sys.executable, "--version", dry_run=True)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This wasn't added. And I don't think it shovel be, though, I think is should be a command line flag only. Probably monkeypatch the value here instead.


run.assert_not_called()

def test_run_success(self):
session, _ = self.make_session_and_runner()
result = session.run(sys.executable, "-c", "print(123)")
Expand Down
Loading