Repository: wntrblm/nox (python)

Issue:
Set environment variable when running commands
### How would this feature be useful?

Setting an environment variable in the environment of `session.run`'d commands would allow the child process to know it is being executed by nox and perhaps even what session of nox is being. Personally, I would find this useful when `session.run`ing `bash` or `code` (using nox as a development environment manager) so that I can adjust my PS1 with the name of the session I'm in.

### Describe the solution you'd like

When running any command with `session.run`, the command environment is adjusted to set the `NOX_CURRENT_SESSION` variable with the name of the current session.

### Describe alternatives you've considered

It's entirely possible to do this myself using `env` argument to `session.run`. However it is something innocuous that takes the burden off the user without considerable maintenance effort.

### Anything else?

_No response_

Pull request:
Export session name in envvar NOX_CURRENT_SESSION
Closes #636. The environment variable `NOX_CURRENT_SESSION` is populated with the name of the current session in all subcommands of that session. 

There were a couple places to place this small bit of code. Each location caused some problems with the existing tests. The current place was chosen because it intuitively makes sense to take code that the user would write at the top of their session functions and move it up into the thing that calls the session function. Let me know if you think it belongs elsewhere.

Patch:
diff --git a/nox/sessions.py b/nox/sessions.py
index d017dc8..4a66229 100644
--- a/nox/sessions.py
+++ b/nox/sessions.py
@@ -724,6 +724,7 @@ class SessionRunner:
             with cwd:
                 self._create_venv()
                 session = Session(self)
+                session.env["NOX_CURRENT_SESSION"] = session.name
                 self.func(session)
 
             # Nothing went wrong; return a success.


Test patch:
diff --git a/tests/test_sessions.py b/tests/test_sessions.py
index f753595..5f1422f 100644
--- a/tests/test_sessions.py
+++ b/tests/test_sessions.py
@@ -900,6 +900,8 @@ class TestSessionRunner:
     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 @@ class TestSessionRunner:
 
         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):