@@ -136,6 +136,23 @@ def test_initiaize_basic(self) -> None:
136136
137137 session .cleanup ()
138138
139+ def test_initialize_sets_working_dir_env_var (self ) -> None :
140+ # OPENJD_SESSION_WORKING_DIR is part of the public API — removing or renaming it is a breaking change.
141+ # This test ensures the env var contract is maintained.
142+
143+ # GIVEN
144+ session_id = uuid .uuid4 ().hex
145+ job_params = dict [str , ParameterValue ]()
146+
147+ # WHEN
148+ session = Session (session_id = session_id , job_parameter_values = job_params )
149+
150+ # THEN
151+ assert "OPENJD_SESSION_WORKING_DIR" in session ._process_env
152+ assert session ._process_env ["OPENJD_SESSION_WORKING_DIR" ] == str (session .working_directory )
153+
154+ session .cleanup ()
155+
139156 @pytest .mark .usefixtures ("tmp_path" ) # built-in fixture
140157 def test_initialize_with_root_dir (self , tmp_path : Path ) -> None :
141158 # Test that we create the Session Working Directory in the given directory, if there is one.
@@ -927,7 +944,13 @@ def test_run_task_with_variables(
927944 # THEN
928945 assert session ._runner is not None
929946 assert fix_foo_baz_environment .variables is not None
930- assert session ._runner ._os_env_vars == dict (fix_foo_baz_environment .variables )
947+ # Check that environment variables are present (superset check to allow for
948+ # session-injected vars like OPENJD_SESSION_WORKING_DIR)
949+ assert session ._runner ._os_env_vars is not None
950+ assert (
951+ dict (fix_foo_baz_environment .variables ).items ()
952+ <= session ._runner ._os_env_vars .items ()
953+ )
931954
932955
933956class TestSessionCancel :
@@ -1401,7 +1424,10 @@ def test_enter_environment_with_variables(self, python_exe: str) -> None:
14011424 time .sleep (0.1 )
14021425 assert session .state == SessionState .READY
14031426 assert session ._runner is not None
1404- assert session ._runner ._os_env_vars == dict (variables )
1427+ # Check that environment variables are present (superset check to allow for
1428+ # session-injected vars like OPENJD_SESSION_WORKING_DIR)
1429+ assert session ._runner ._os_env_vars is not None
1430+ assert dict (variables ).items () <= session ._runner ._os_env_vars .items ()
14051431
14061432 @pytest .mark .usefixtures ("caplog" ) # builtin fixture
14071433 def test_enter_environment_with_resolved_variables (
@@ -1471,7 +1497,10 @@ def test_enter_two_environments_with_variables(self, python_exe: str) -> None:
14711497 time .sleep (0.1 )
14721498
14731499 assert session ._runner is not None
1474- assert session ._runner ._os_env_vars == dict (variables2 )
1500+ # Check that environment variables are present (superset check to allow for
1501+ # session-injected vars like OPENJD_SESSION_WORKING_DIR)
1502+ assert session ._runner ._os_env_vars is not None
1503+ assert dict (variables2 ).items () <= session ._runner ._os_env_vars .items ()
14751504
14761505
14771506class TestSessionExitEnvironment_2023_09 : # noqa: N801
@@ -1763,7 +1792,9 @@ def test_exit_environment_with_variables(self, python_exe: str) -> None:
17631792 # THEN
17641793 assert session .state == SessionState .READY_ENDING
17651794 assert session ._runner is not None
1766- assert session ._runner ._os_env_vars == dict (variables )
1795+ # Check that environment variables are present (superset check to allow for
1796+ # session-injected vars like OPENJD_SESSION_WORKING_DIR)
1797+ assert dict (variables ).items () <= session ._runner ._os_env_vars .items ()
17671798
17681799 def test_exit_two_environments_with_variables (self , python_exe : str ) -> None :
17691800 # GIVEN
@@ -1798,7 +1829,9 @@ def test_exit_two_environments_with_variables(self, python_exe: str) -> None:
17981829 # THEN
17991830 assert session .state == SessionState .READY_ENDING
18001831 assert session ._runner is not None
1801- assert session ._runner ._os_env_vars == dict (variables2 )
1832+ # Check that environment variables are present (superset check to allow for
1833+ # session-injected vars like OPENJD_SESSION_WORKING_DIR)
1834+ assert dict (variables2 ).items () <= session ._runner ._os_env_vars .items ()
18021835
18031836 session .exit_environment (identifier = identifier1 )
18041837 # Wait for the process to exit
@@ -1808,7 +1841,9 @@ def test_exit_two_environments_with_variables(self, python_exe: str) -> None:
18081841 # THEN
18091842 assert session .state == SessionState .READY_ENDING
18101843 assert session ._runner is not None
1811- assert session ._runner ._os_env_vars == dict (variables1 )
1844+ # Check that environment variables are present (superset check to allow for
1845+ # session-injected vars like OPENJD_SESSION_WORKING_DIR)
1846+ assert dict (variables1 ).items () <= session ._runner ._os_env_vars .items ()
18121847
18131848 def test_run_task_after_env_exit (self , python_exe : str ) -> None :
18141849 # By default, tasks can't run after an environment exit. The exit_environment call
0 commit comments