@@ -222,17 +222,42 @@ def test_get_client_environment_label_precedence(
222222 "colab-enterprise" ,
223223 )
224224
225+ @mock .patch ("IPython.get_ipython" , return_value = mock .MagicMock ())
226+ def test_is_interactive_ipython_true (self , mock_get_ipython ):
227+ self .assertTrue (environment .is_interactive ())
228+
229+ @mock .patch ("IPython.get_ipython" , return_value = None )
230+ @mock .patch ("google.cloud.dataproc_spark_connect.environment.sys" )
231+ def test_is_interactive_ipython_false (self , mock_sys , mock_get_ipython ):
232+ if hasattr (mock_sys , "ps1" ):
233+ del mock_sys .ps1
234+ mock_sys .flags .interactive = 0
235+ self .assertFalse (environment .is_interactive ())
236+
237+ @mock .patch ("IPython.get_ipython" , side_effect = ImportError )
225238 @mock .patch ("google.cloud.dataproc_spark_connect.environment.sys" )
226- def test_is_interactive_true (self , mock_sys ):
239+ def test_is_interactive_true_via_ps1 (self , mock_sys , mock_get_ipython ):
227240 # Simulate interactive environment by setting ps1
228241 mock_sys .ps1 = ">>>"
242+ mock_sys .flags .interactive = 0
229243 self .assertTrue (environment .is_interactive ())
230244
245+ @mock .patch ("IPython.get_ipython" , side_effect = ImportError )
231246 @mock .patch ("google.cloud.dataproc_spark_connect.environment.sys" )
232- def test_is_interactive_false (self , mock_sys ):
233- # Simulate non- interactive environment by removing ps1
247+ def test_is_interactive_true_via_flags (self , mock_sys , mock_get_ipython ):
248+ # Simulate interactive environment via sys.flags.interactive
234249 if hasattr (mock_sys , "ps1" ):
235250 del mock_sys .ps1
251+ mock_sys .flags .interactive = 1
252+ self .assertTrue (environment .is_interactive ())
253+
254+ @mock .patch ("IPython.get_ipython" , side_effect = ImportError )
255+ @mock .patch ("google.cloud.dataproc_spark_connect.environment.sys" )
256+ def test_is_interactive_false (self , mock_sys , mock_get_ipython ):
257+ # Simulate non-interactive environment
258+ if hasattr (mock_sys , "ps1" ):
259+ del mock_sys .ps1
260+ mock_sys .flags .interactive = 0
236261 self .assertFalse (environment .is_interactive ())
237262
238263 @mock .patch ("sys.stdin" )
@@ -254,9 +279,13 @@ def test_is_interactive_terminal_true(self, mock_sys, mock_stdin):
254279
255280 @mock .patch ("sys.stdin" )
256281 @mock .patch ("google.cloud.dataproc_spark_connect.environment.sys" )
257- def test_is_interactive_terminal_true (self , mock_sys , mock_stdin ):
282+ @mock .patch ("IPython.get_ipython" , side_effect = ImportError )
283+ def test_is_interactive_terminal_false (
284+ self , mock_get_ipython , mock_sys , mock_stdin
285+ ):
258286 if hasattr (mock_sys , "ps1" ):
259287 del mock_sys .ps1
288+ mock_sys .flags .interactive = 0
260289 mock_stdin .isatty .return_value = False
261290 self .assertFalse (environment .is_interactive_terminal ())
262291
0 commit comments