@@ -44,36 +44,21 @@ def mock_agent_card_builder():
4444
4545
4646def test_generate_agent_card_missing_a2a (runner ):
47- with patch .dict (
48- "sys.modules" , {"google.adk.a2a.utils.agent_card_builder" : None }
49- ):
50- # Simulate ImportError by ensuring the module cannot be imported
51- with patch (
52- "builtins.__import__" ,
53- side_effect = ImportError ("No module named 'google.adk.a2a'" ),
54- ):
55- # We need to target the specific import in the function
56- # Since it's a local import inside the function, we can mock sys.modules or use side_effect on import
57- # However, patching builtins.__import__ is risky and affects everything.
58- # A better way is to mock the module in sys.modules to raise ImportError on access or just rely on the fact that if it's not there it fails.
59- # But here we want to force failure even if it is installed.
60-
61- # Let's try to patch the specific module import path in the function if possible,
62- # but since it is inside the function, we can use patch.dict on sys.modules with a mock that raises ImportError when accessed?
63- # No, that's for import time.
64-
65- # Actually, the easiest way to test the ImportError branch is to mock the import itself.
66- # But `from ..a2a.utils.agent_card_builder import AgentCardBuilder` is hard to mock if it exists.
67- pass
68-
69- # Alternative: Mock the function `_generate_agent_card_async` to raise ImportError?
70- # No, the import is INSIDE `_generate_agent_card_async`.
71-
72- # Let's use a patch on the module where `_generate_agent_card_async` is defined,
73- # but we can't easily patch the import statement itself.
74- # We can use `patch.dict(sys.modules, {'google.adk.a2a.utils.agent_card_builder': None})`
75- # and ensure the previous import is cleared?
76- pass
47+ # Simulate the module being missing from the environment
48+ with patch .dict ("sys.modules" , {"google.adk.a2a.utils.agent_card_builder" : None }):
49+ result = runner .invoke (generate_agent_card )
50+
51+ assert result .exit_code != 0
52+ assert "Error: 'a2a' package is required for this command." in result .stderr
53+
54+
55+ def test_generate_agent_card_import_error (runner ):
56+ # Simulate a generic ImportError during import
57+ with patch .dict ("sys.modules" , {"google.adk.a2a.utils.agent_card_builder" : None }):
58+ result = runner .invoke (generate_agent_card )
59+
60+ assert result .exit_code != 0
61+ assert isinstance (result .exception , SystemExit )
7762
7863
7964@patch ("google.adk.cli.cli_generate_agent_card.AgentLoader" )
@@ -184,34 +169,3 @@ def side_effect(name):
184169 output = json .loads (result .stdout )
185170 assert len (output ) == 1
186171 assert output [0 ]["name" ] == "agent2"
187-
188-
189- def test_generate_agent_card_import_error (runner ):
190- # We need to mock the import failure.
191- # Since the import is inside the function, we can patch `google.adk.cli.cli_generate_agent_card.AgentCardBuilder`
192- # but that's not imported at top level.
193- # We can try to patch `sys.modules` to hide `google.adk.a2a`.
194-
195- with patch .dict (
196- "sys.modules" , {"google.adk.a2a.utils.agent_card_builder" : None }
197- ):
198- # We also need to ensure it tries to import it.
199- # The code does `from ..a2a.utils.agent_card_builder import AgentCardBuilder`
200- # This is a relative import.
201-
202- # A reliable way to test ImportError inside a function is to mock the module that contains the function
203- # and replace the class/function being imported with something that raises ImportError? No.
204-
205- # Let's just use `patch` on the target module path if we can resolve it.
206- # But it's a local import.
207-
208- # Let's try to use `patch.dict` on `sys.modules` and remove the module if it exists.
209- # And we need to make sure `google.adk.cli.cli_generate_agent_card` is re-imported or we are running the function fresh?
210- # The function `_generate_agent_card_async` imports it every time.
211-
212- # If we set `sys.modules['google.adk.a2a.utils.agent_card_builder'] = None`, the import might fail or return None.
213- # If it returns None, `from ... import ...` will fail with ImportError or AttributeError.
214- pass
215-
216- # Actually, let's skip the ImportError test for now as it's tricky with local imports and existing environment.
217- # The other tests cover the main logic.
0 commit comments