22
33import os
44import sys
5+ from pathlib import Path
56
67from basic_memory import utils
78
@@ -11,6 +12,7 @@ def test_setup_logging_uses_shared_log_file_off_windows(monkeypatch, tmp_path) -
1112 added_sinks : list [str ] = []
1213
1314 monkeypatch .setenv ("BASIC_MEMORY_ENV" , "dev" )
15+ monkeypatch .delenv ("BASIC_MEMORY_CONFIG_DIR" , raising = False )
1416 monkeypatch .setattr (utils .os , "name" , "posix" )
1517 monkeypatch .setattr (utils .Path , "home" , lambda : tmp_path )
1618 monkeypatch .setattr (utils .logger , "remove" , lambda * args , ** kwargs : None )
@@ -32,6 +34,7 @@ def test_setup_logging_uses_per_process_log_file_on_windows(monkeypatch, tmp_pat
3234 added_sinks : list [str ] = []
3335
3436 monkeypatch .setenv ("BASIC_MEMORY_ENV" , "dev" )
37+ monkeypatch .delenv ("BASIC_MEMORY_CONFIG_DIR" , raising = False )
3538 monkeypatch .setattr (utils .os , "name" , "nt" )
3639 monkeypatch .setattr (utils .os , "getpid" , lambda : 4242 )
3740 monkeypatch .setattr (utils .Path , "home" , lambda : tmp_path )
@@ -63,6 +66,7 @@ def test_setup_logging_trims_stale_windows_pid_logs(monkeypatch, tmp_path) -> No
6366 stale_logs .append (log_path )
6467
6568 monkeypatch .setenv ("BASIC_MEMORY_ENV" , "dev" )
69+ monkeypatch .delenv ("BASIC_MEMORY_CONFIG_DIR" , raising = False )
6670 monkeypatch .setattr (utils .os , "name" , "nt" )
6771 monkeypatch .setattr (utils .os , "getpid" , lambda : 4242 )
6872 monkeypatch .setattr (utils .Path , "home" , lambda : tmp_path )
@@ -82,6 +86,44 @@ def test_setup_logging_trims_stale_windows_pid_logs(monkeypatch, tmp_path) -> No
8286 ]
8387
8488
89+ def test_setup_logging_honors_basic_memory_config_dir (monkeypatch , tmp_path ) -> None :
90+ """Regression guard for #742: log path must follow BASIC_MEMORY_CONFIG_DIR.
91+
92+ Prior to #742 the log path was hardcoded to ``~/.basic-memory/``, which
93+ split state across instances when users set BASIC_MEMORY_CONFIG_DIR to
94+ isolate config and the database elsewhere.
95+
96+ Asserts on the log *directory* rather than the exact filename because
97+ Windows uses a per-process ``basic-memory-<pid>.log`` while POSIX
98+ shares a single ``basic-memory.log``. The thing this regression guard
99+ cares about is that the log lives under the redirected config dir,
100+ not at ``Path.home() / ".basic-memory"``. Patching ``utils.os.name``
101+ to force one branch would break ``Path(str)`` dispatch on the other
102+ platform, so we stay platform-agnostic.
103+ """
104+ added_sinks : list [str ] = []
105+
106+ custom_dir = tmp_path / "instance-x" / "state"
107+ monkeypatch .setenv ("BASIC_MEMORY_ENV" , "dev" )
108+ monkeypatch .setenv ("BASIC_MEMORY_CONFIG_DIR" , str (custom_dir ))
109+ monkeypatch .setattr (utils .logger , "remove" , lambda * args , ** kwargs : None )
110+ monkeypatch .setattr (
111+ utils .logger ,
112+ "add" ,
113+ lambda sink , ** kwargs : added_sinks .append (str (sink )),
114+ )
115+ monkeypatch .setattr (utils .telemetry , "get_logfire_handler" , lambda : None )
116+ monkeypatch .setattr (utils .telemetry , "pop_telemetry_warnings" , lambda : [])
117+
118+ utils .setup_logging (log_to_file = True )
119+
120+ assert len (added_sinks ) == 1
121+ log_path = Path (added_sinks [0 ])
122+ assert log_path .parent == custom_dir
123+ assert log_path .name .startswith ("basic-memory" )
124+ assert log_path .suffix == ".log"
125+
126+
85127def test_setup_logging_test_env_uses_stderr_only (monkeypatch ) -> None :
86128 """Test mode should add one stderr sink and return before other branches run."""
87129 added_sinks : list [object ] = []
0 commit comments