@@ -132,19 +132,57 @@ def mock_process():
132132 assert session_id == 1234
133133
134134
135- def test_get_context_session_id_no_grandparent_process (monkeypatch ):
136- parent_process = MockProcess (pid = 1234 , parent = None )
137- current_process = MockProcess (pid = 9999 , parent = parent_process )
135+ def test_get_context_session_id_in_venv (monkeypatch ):
136+ great_grandparent_process = MockProcess (pid = 1111 , parent = None )
137+ grandparent_process = MockProcess (pid = 2222 , parent = great_grandparent_process )
138+ parent_process = MockProcess (pid = 3333 , parent = grandparent_process )
139+ current_process = MockProcess (pid = 4444 , parent = parent_process )
138140
139141 def mock_process ():
140142 return current_process
141143
142144 monkeypatch .setattr ("fabric_cli.core.fab_context.psutil.Process" , mock_process )
145+ context = Context ()
146+ monkeypatch .setattr (context , "_is_in_venv" , lambda : True )
147+ session_id = context ._get_context_session_id ()
148+
149+ assert session_id == 1111
150+
151+
152+ def test_get_context_session_id_in_venv_no_great_grandparent (monkeypatch ):
153+ grandparent_process = MockProcess (pid = 2222 , parent = None )
154+ parent_process = MockProcess (pid = 3333 , parent = grandparent_process )
155+ current_process = MockProcess (pid = 4444 , parent = parent_process )
143156
157+ def mock_process ():
158+ return current_process
159+
160+ monkeypatch .setattr ("fabric_cli.core.fab_context.psutil.Process" , mock_process )
144161 context = Context ()
162+ monkeypatch .setattr (context , "_is_in_venv" , lambda : True )
145163 session_id = context ._get_context_session_id ()
146164
147- assert session_id == 1234
165+ # Falls back to grandparent PID
166+ assert session_id == 2222
167+
168+
169+ def test_get_context_session_id_not_in_venv (monkeypatch ):
170+ great_grandparent_process = MockProcess (pid = 1111 , parent = None )
171+ grandparent_process = MockProcess (pid = 2222 , parent = great_grandparent_process )
172+ parent_process = MockProcess (pid = 3333 , parent = grandparent_process )
173+ current_process = MockProcess (pid = 4444 , parent = parent_process )
174+
175+ def mock_process ():
176+ return current_process
177+
178+ monkeypatch .setattr ("fabric_cli.core.fab_context.psutil.Process" , mock_process )
179+
180+ context = Context ()
181+ monkeypatch .setattr (context , "_is_in_venv" , lambda : False )
182+ session_id = context ._get_context_session_id ()
183+
184+ # Should return grandparent PID, not great-grandparent
185+ assert session_id == 2222
148186
149187
150188def test_get_context_session_id_no_parent_process (monkeypatch ):
@@ -172,9 +210,9 @@ def mock_log_debug(message):
172210
173211 assert session_id == 9999
174212 assert len (log_calls ) == 1
175- assert "No parent process was found" in log_calls [0 ]
176213 assert (
177- "Falling back to the current process for session ID resolution" in log_calls [0 ]
214+ "No parent process found. Falling back to current process for session ID"
215+ in log_calls [0 ]
178216 )
179217
180218
@@ -227,11 +265,11 @@ def mock_log_debug(message):
227265
228266 assert session_id == 5678 # Falls back to parent process PID
229267 assert len (log_calls ) == 1
230- assert "Failed to get grandparent process:" in log_calls [0 ]
268+ assert "Failed to get session process:" in log_calls [0 ]
231269 assert "Falling back to parent process for session ID resolution" in log_calls [0 ]
232270
233271
234- def test_get_context_session_id_grandparent_process_none (monkeypatch ):
272+ def test_get_context_session_id_no_grandparent_process (monkeypatch ):
235273 parent_process = MockProcess (pid = 5678 , parent = None )
236274 current_process = MockProcess (pid = 9999 , parent = parent_process )
237275
@@ -253,8 +291,10 @@ def mock_log_debug(message):
253291
254292 assert session_id == 5678 # Falls back to parent process PID
255293 assert len (log_calls ) == 1
256- assert "No grandparent process was found" in log_calls [0 ]
257- assert "Falling back to parent process for session ID resolution" in log_calls [0 ]
294+ assert (
295+ "No grandparent process was found. Falling back to parent process for session ID resolution"
296+ in log_calls [0 ]
297+ )
258298
259299
260300class MockProcess :
0 commit comments