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

feat: rename run_always to run_install #770

Merged
merged 2 commits into from
Feb 20, 2024
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
9 changes: 6 additions & 3 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,20 @@ If your project is a Python package and you want to install it:

In some cases such as Python binary extensions, your package may depend on
code compiled outside of the Python ecosystem. To make sure a low-level
dependency (e.g. ``libfoo``) is available during installation
dependency (e.g. ``libfoo``) is available during installation:

.. code-block:: python

@nox.session
def tests(session):
...
session.run_always(
session.run_install(
"cmake", "-DCMAKE_BUILD_TYPE=Debug",
"-S", libfoo_src_dir,
"-B", build_dir,
external=True,
)
session.run_always(
session.run_install(
"cmake",
"--build", build_dir,
"--config", "Debug",
Expand All @@ -166,6 +166,9 @@ dependency (e.g. ``libfoo``) is available during installation
session.install(".")
...

These commands will run even if you are only installing, and will not run if
the environment is being reused without reinstallation.

Running commands
----------------

Expand Down
2 changes: 1 addition & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ The ``--no-install`` option causes the following session methods to return early

- :func:`session.install <nox.sessions.Session.install>`
- :func:`session.conda_install <nox.sessions.Session.conda_install>`
- :func:`session.run_always <nox.sessions.Session.run_always>`
- :func:`session.run_install <nox.sessions.Session.run_install>`

This option has no effect if the virtualenv is not being reused.

Expand Down
2 changes: 1 addition & 1 deletion nox/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ def _session_completer(
action="store_true",
help=(
"Skip invocations of session methods for installing packages"
" (session.install, session.conda_install, session.run_always)"
" (session.install, session.conda_install, session.run_install)"
" when a virtualenv is being reused."
),
),
Expand Down
26 changes: 22 additions & 4 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,18 +390,20 @@ def run(
**kwargs,
)

def run_always(
def run_install(
self,
*args: str | os.PathLike[str],
env: Mapping[str, str] | None = None,
include_outer_env: bool = True,
**kwargs: Any,
) -> Any | None:
"""Run a command **always**.
"""Run a command in the install step.

This is a variant of :meth:`run` that runs even in the presence of
``--install-only``. This method returns early if ``--no-install`` is
specified and the virtualenv is being reused.
specified and the virtualenv is being reused. (In nox 2023.04.22 and
earlier, this was called ``run_always``, and that continues to be
available as an alias.)

Here are some cases where this method is useful:

Expand Down Expand Up @@ -447,7 +449,7 @@ def run_always(
return None

if not args:
raise ValueError("At least one argument required to run_always().")
raise ValueError("At least one argument required to run_install().")

return self._run(
*args,
Expand All @@ -456,6 +458,22 @@ def run_always(
**kwargs,
)

def run_always(
self,
*args: str | os.PathLike[str],
env: Mapping[str, str] | None = None,
include_outer_env: bool = True,
**kwargs: Any,
) -> Any | None:
"""This is an alias to ``run_install``, which better describes the use case.

:meta private:
"""

return self.run_install(
*args, env=env, include_outer_env=include_outer_env, **kwargs
)

def _run(
self,
*args: str | os.PathLike[str],
Expand Down
22 changes: 13 additions & 9 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,25 +358,25 @@ def test_run_external_with_error_on_external_run_condaenv(self):
with pytest.raises(nox.command.CommandFailed, match="External"):
session.run(sys.executable, "--version")

def test_run_always_bad_args(self):
def test_run_install_bad_args(self):
session, _ = self.make_session_and_runner()

with pytest.raises(ValueError) as exc_info:
session.run_always()
session.run_install()

exc_args = exc_info.value.args
assert exc_args == ("At least one argument required to run_always().",)
assert exc_args == ("At least one argument required to run_install().",)

def test_run_always_success(self):
def test_run_install_success(self):
session, _ = self.make_session_and_runner()

assert session.run_always(operator.add, 1300, 37) == 1337
assert session.run_install(operator.add, 1300, 37) == 1337

def test_run_always_install_only(self, caplog):
def test_run_install_install_only(self, caplog):
session, runner = self.make_session_and_runner()
runner.global_config.install_only = True

assert session.run_always(operator.add, 23, 19) == 42
assert session.run_install(operator.add, 23, 19) == 42

@pytest.mark.parametrize(
(
Expand Down Expand Up @@ -437,13 +437,17 @@ def test_run_shutdown_process_timeouts(
(False, False, True),
],
)
def test_run_always_no_install(self, no_install, reused, run_called):
@pytest.mark.parametrize("run_install_func", ["run_always", "run_install"])
def test_run_install_no_install(
self, no_install, reused, run_called, run_install_func
):
session, runner = self.make_session_and_runner()
runner.global_config.no_install = no_install
runner.venv._reused = reused

with mock.patch.object(nox.command, "run") as run:
session.run_always("python", "-m", "pip", "install", "requests")
run_install = getattr(session, run_install_func)
run_install("python", "-m", "pip", "install", "requests")

assert run.called is run_called

Expand Down
Loading