Skip to content

Commit 9e28bc5

Browse files
authored
feat: Add OPENJD_SESSION_WORKING_DIR environment variable (#309)
Expose the session working directory as an environment variable so that nested subprocesses can access it. Template variables like {{Session.WorkingDirectory}} are only available at template evaluation time, but this env var propagates to all child processes. - Add OPENJD_SESSION_WORKING_DIR to _process_env in Session.__init__ - Update 5 existing tests to use superset checks for env var assertions - Add new test to verify the env var is set correctly Signed-off-by: Sean Tang <171081544+seant-aws@users.noreply.github.com>
1 parent 86ec2b5 commit 9e28bc5

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

‎src/openjd/sessions/_session.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,10 @@ def __init__(
452452
extra=LogExtraInfo(openjd_log_content=LogContent.FILE_PATH),
453453
)
454454

455+
# Expose working directory as env var for nested subprocesses that can't access template variables.
456+
# This env var is part of the public API — removing or renaming it is a breaking change.
457+
self._process_env["OPENJD_SESSION_WORKING_DIR"] = str(self.working_directory)
458+
455459
self._state = SessionState.READY
456460

457461
def cleanup(self) -> None:

‎test/openjd/sessions/test_session.py‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

933956
class 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

14771506
class 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

Comments
 (0)