Skip to content

Commit ec33d18

Browse files
committed
fix(config): handle empty MEMPALACE_HOOKS_AUTO_MINE env var
1 parent 4967008 commit ec33d18

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

mempalace/config.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,18 @@ def hook_auto_mine(self):
305305
306306
Env var ``MEMPALACE_HOOKS_AUTO_MINE`` (or legacy ``MEMPAL_HOOKS_AUTO_MINE``)
307307
overrides the config file. Accepts ``0``/``false``/``no``/``off`` as
308-
falsey; anything else is truthy. Default: ``True`` (preserves existing
308+
falsey; anything else (including ``true``/``1``/``yes``/``on``) is
309+
truthy. Empty / whitespace-only values are treated as unset and fall
310+
back to the config file. Default: ``True`` (preserves existing
309311
behavior for users who don't opt out).
310312
"""
311-
env_val = os.environ.get("MEMPALACE_HOOKS_AUTO_MINE") or os.environ.get(
312-
"MEMPAL_HOOKS_AUTO_MINE"
313-
)
314-
if env_val:
315-
return env_val.strip().lower() not in ("0", "false", "no", "off")
313+
env_val = os.environ.get("MEMPALACE_HOOKS_AUTO_MINE")
314+
if env_val is None:
315+
env_val = os.environ.get("MEMPAL_HOOKS_AUTO_MINE")
316+
if env_val is not None:
317+
stripped = env_val.strip().lower()
318+
if stripped:
319+
return stripped not in ("0", "false", "no", "off")
316320
return self._file_config.get("hooks", {}).get("auto_mine", True)
317321

318322
@property

tests/test_config.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,36 @@ def test_hook_auto_mine_legacy_env_prefix():
176176
del os.environ["MEMPAL_HOOKS_AUTO_MINE"]
177177

178178

179+
@pytest.mark.parametrize("empty", ["", " ", "\t", "\n"])
180+
def test_hook_auto_mine_empty_env_falls_back_to_config(empty):
181+
"""Empty / whitespace-only env values must fall back to config, not flip to True."""
182+
tmpdir = tempfile.mkdtemp()
183+
with open(os.path.join(tmpdir, "config.json"), "w") as f:
184+
json.dump({"hooks": {"auto_mine": False}}, f)
185+
os.environ["MEMPALACE_HOOKS_AUTO_MINE"] = empty
186+
try:
187+
cfg = MempalaceConfig(config_dir=tmpdir)
188+
assert cfg.hook_auto_mine is False
189+
finally:
190+
del os.environ["MEMPALACE_HOOKS_AUTO_MINE"]
191+
192+
193+
def test_hook_auto_mine_empty_primary_does_not_leak_to_legacy():
194+
"""An empty primary env var must not silently fall through to the legacy var."""
195+
tmpdir = tempfile.mkdtemp()
196+
with open(os.path.join(tmpdir, "config.json"), "w") as f:
197+
json.dump({"hooks": {"auto_mine": False}}, f)
198+
os.environ["MEMPALACE_HOOKS_AUTO_MINE"] = ""
199+
os.environ["MEMPAL_HOOKS_AUTO_MINE"] = "true"
200+
try:
201+
cfg = MempalaceConfig(config_dir=tmpdir)
202+
# Primary is set (to ""), so legacy must be ignored; empty -> fall back to config.
203+
assert cfg.hook_auto_mine is False
204+
finally:
205+
del os.environ["MEMPALACE_HOOKS_AUTO_MINE"]
206+
del os.environ["MEMPAL_HOOKS_AUTO_MINE"]
207+
208+
179209
# --- sanitize_name ---
180210

181211

0 commit comments

Comments
 (0)