From 871dc1d7f9fedd463efdddd2fa2f57a06aa5fadc Mon Sep 17 00:00:00 2001 From: Kaleb Barrett Date: Mon, 1 Aug 2022 22:20:13 -0500 Subject: [PATCH] Export session name in envvar NOX_CURRENT_SESSION --- nox/sessions.py | 1 + tests/test_sessions.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/nox/sessions.py b/nox/sessions.py index d017dc83..4a66229e 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -724,6 +724,7 @@ def execute(self) -> Result: with cwd: self._create_venv() session = Session(self) + session.env["NOX_CURRENT_SESSION"] = session.name self.func(session) # Nothing went wrong; return a success. diff --git a/tests/test_sessions.py b/tests/test_sessions.py index f7535958..5f1422f1 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -900,6 +900,8 @@ def test__create_venv_unexpected_venv_backend(self): def make_runner_with_mock_venv(self): runner = self.make_runner() runner._create_venv = mock.Mock() + runner.venv = mock.create_autospec(nox.virtualenv.VirtualEnv) + runner.venv.env = {} return runner def test_execute_noop_success(self, caplog): @@ -1017,6 +1019,23 @@ def func(session): assert result.status == nox.sessions.Status.FAILED + def test_execute_check_env(self): + runner = self.make_runner_with_mock_venv() + + def func(session): + session.run( + sys.executable, + "-c", + 'import os; raise SystemExit(0 if os.environ["NOX_CURRENT_SESSION"] ==' + f" {session.name!r} else 0)", + ) + + runner.func = func + + result = runner.execute() + + assert result + class TestResult: def test_init(self):