33import runpy
44import tempfile
55import unittest
6+ from importlib import import_module
67from io import StringIO
78from unittest .mock import MagicMock , patch
89
910from iop .cli .main import _format_test_response , main
1011
12+ cli_main_module = import_module ("iop.cli.main" )
13+
1114
1215class TestIOPCli (unittest .TestCase ):
1316 """Test cases for IOP CLI functionality."""
1417
1518 def setUp (self ):
1619 # Force local mode regardless of any IOP_URL / IOP_SETTINGS env vars
1720 # that may be set by a parallel e2e test session.
18- self ._remote_patcher = patch (
19- "iop.cli.main. get_remote_settings" , return_value = None
21+ self ._remote_patcher = patch . object (
22+ cli_main_module , " get_remote_settings" , return_value = None
2023 )
2124 self ._remote_patcher .start ()
2225
2326 def tearDown (self ):
2427 self ._remote_patcher .stop ()
2528
2629 def test_module_entrypoint_calls_cli_main (self ):
27- with patch ( "iop.cli.main. main" ) as mock_main :
30+ with patch . object ( cli_main_module , " main" ) as mock_main :
2831 runpy .run_module ("iop" , run_name = "__main__" )
2932
3033 mock_main .assert_called_once_with ()
@@ -275,8 +278,9 @@ def test_export_defaults_to_json(self):
275278 def test_export_python_format_uses_production_reconstruction (self ):
276279 production = MagicMock ()
277280 production .to_python .return_value = "from iop import Production\n "
278- with patch (
279- "iop.cli.main.Production.from_iris" ,
281+ with patch .object (
282+ cli_main_module .Production ,
283+ "from_iris" ,
280284 return_value = production ,
281285 ) as mock_from_iris :
282286 with patch ("sys.stdout" , new = StringIO ()) as fake_out :
@@ -294,8 +298,9 @@ def test_export_python_format_uses_production_reconstruction(self):
294298 def test_export_class_format_uses_production_reconstruction (self ):
295299 production = MagicMock ()
296300 production .to_class .return_value = "from iop import Production, ServiceItem\n "
297- with patch (
298- "iop.cli.main.Production.from_iris" ,
301+ with patch .object (
302+ cli_main_module .Production ,
303+ "from_iris" ,
299304 return_value = production ,
300305 ) as mock_from_iris :
301306 with patch ("sys.stdout" , new = StringIO ()) as fake_out :
@@ -316,8 +321,9 @@ def test_export_class_format_uses_production_reconstruction(self):
316321 def test_export_graph_format_prints_reconstructed_graph (self ):
317322 production = MagicMock ()
318323 production .graph .return_value = "Demo.Production\n FileInput"
319- with patch (
320- "iop.cli.main.Production.from_iris" ,
324+ with patch .object (
325+ cli_main_module .Production ,
326+ "from_iris" ,
321327 return_value = production ,
322328 ):
323329 with patch ("sys.stdout" , new = StringIO ()) as fake_out :
@@ -331,8 +337,9 @@ def test_export_graph_format_prints_reconstructed_graph(self):
331337 def test_export_mermaid_format_prints_reconstructed_graph (self ):
332338 production = MagicMock ()
333339 production .to_mermaid .return_value = "flowchart LR\n FileInput --> Order\n "
334- with patch (
335- "iop.cli.main.Production.from_iris" ,
340+ with patch .object (
341+ cli_main_module .Production ,
342+ "from_iris" ,
336343 return_value = production ,
337344 ) as mock_from_iris :
338345 with patch ("sys.stdout" , new = StringIO ()) as fake_out :
0 commit comments