Skip to content

Commit fea384a

Browse files
committed
Set env on the Hap instantiation
1 parent 82f118c commit fea384a

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

hapless/hap.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def set_return_code(self, rc: int):
6868
with open(self._rc_file, "w") as f:
6969
f.write(f"{rc}")
7070

71-
def _set_raw_name(self, raw_name: Optional[str]):
71+
def _set_raw_name(self, raw_name: Optional[str]) -> None:
7272
"""
7373
Set name for the first time on hap creation.
7474
"""
@@ -110,7 +110,7 @@ def _set_pid(self, pid: int):
110110
if not psutil.pid_exists(pid):
111111
raise RuntimeError(f"Process with pid {pid} is gone")
112112

113-
def _set_logfiles(self, redirect_stderr: bool):
113+
def _set_logfiles(self, redirect_stderr: bool) -> None:
114114
if redirect_stderr:
115115
logger.debug("Process stderr will be redirected to stdout file")
116116
if not self._stdout_path.exists() and not redirect_stderr:
@@ -127,12 +127,16 @@ def _get_proc_env(self) -> Dict[str, str]:
127127
logger.error(f"Cannot get environment: {e}")
128128
return environ
129129

130-
def _set_env(self, env: Optional[Dict[str, str]] = None):
131-
if env is None:
132-
env = dict(os.environ)
130+
def _set_env(self, env: Optional[Dict[str, str]] = None) -> None:
131+
"""
132+
Set environment variables for the first time on hap creation.
133+
"""
134+
if self.env is None:
135+
if env is None:
136+
env = dict(os.environ)
133137

134-
with open(self._env_file, "w") as env_file:
135-
env_file.write(json.dumps(env))
138+
with open(self._env_file, "w") as env_file:
139+
env_file.write(json.dumps(env))
136140

137141
def bind(self, pid: int):
138142
"""
@@ -247,10 +251,7 @@ def pid(self) -> Optional[int]:
247251
@property
248252
@allow_missing
249253
def env(self) -> Optional[Dict[str, str]]:
250-
proc = self.proc
251-
environ = {}
252-
if proc is not None:
253-
environ = proc.environ()
254+
environ = self._get_proc_env()
254255

255256
if environ:
256257
return environ

tests/test_hap_env.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ def test_empty_env_provided_on_creation(tmp_path):
2929
assert hap.env == {}
3030

3131

32+
def test_correct_env_is_picked_up_on_instance_creation(tmp_path):
33+
with patch.dict(os.environ, {"TESTING": "true"}, clear=True):
34+
hap = Hap(tmp_path, cmd="true")
35+
assert hap.pid is None
36+
assert hap.proc is None
37+
assert hap.rc is None
38+
assert not hap.active
39+
assert hap.env == {"TESTING": "true"}
40+
41+
# Check reread picks up previously saved state
42+
with patch.dict(os.environ, {"TESTING": "false"}, clear=True):
43+
hap = Hap(tmp_path)
44+
assert hap.pid is None
45+
assert hap.proc is None
46+
assert hap.rc is None
47+
assert not hap.active
48+
assert hap.env == {"TESTING": "true"}
49+
50+
3251
@patch.dict(os.environ, {"ENV_KEY": "TEST_VALUE"}, clear=True)
3352
def test_env_defaults_to_current_env_if_none(tmp_path):
3453
hap = Hap(tmp_path, cmd="false")
@@ -66,20 +85,6 @@ def test_proc_env_is_used_as_primaty_source(hap: Hap, write_env_factory):
6685
assert hap.env == {"ENV_KEY": "ENV_VALUE_FROM_PROC"}
6786

6887

69-
# def test_correct_env_is_picked_up():
70-
# hapless = Hapless()
71-
# hap = hapless.create_hap(
72-
# cmd="python -c 'import os; print(os.getenv(\"HAPLESS_TEST_ENV_VAR\"))'",
73-
# name="hap-env-test",
74-
# env={"HAPLESS_TEST_ENV_VAR": "hapless_env_value"},
75-
# )
76-
77-
# hapless.run_hap(hap, blocking=True)
78-
# assert hap.rc == 0
79-
# assert hap.stdout_path.exists()
80-
# assert hap.stdout_path.read_text().strip() == "hapless_env_value"
81-
82-
8388
def test_env_preserved_on_restart(hapless: Hapless):
8489
env = {"TESTING": "true"}
8590
# NOTE: providing absolute path as environment got erased for this test

0 commit comments

Comments
 (0)