@@ -351,7 +351,7 @@ def __init__(self):
351
351
assert f"Fail to load '{ agent_name } ' module." in str (exc_info .value )
352
352
assert "No module named 'non_existent_module'" in str (exc_info .value )
353
353
354
- def test_agent_internal_import_error (self ):
354
+ def test_agent_internal_syntax_error (self ):
355
355
"""Test other import errors within an agent's code (e.g., SyntaxError)."""
356
356
with tempfile .TemporaryDirectory () as temp_dir :
357
357
temp_path = Path (temp_dir )
@@ -380,10 +380,47 @@ def __init__(self):
380
380
) as exc_info : # Or potentially ImportError depending on Python version specifics with importlib
381
381
loader .load_agent (agent_name )
382
382
383
- assert f"Fail to load '{ agent_name } ' module." in str (exc_info .value )
383
+ assert str (exc_info .value ).startswith (
384
+ f"Fail to load '{ agent_name } ' module."
385
+ )
384
386
# Check for part of the original SyntaxError message
385
387
assert "invalid syntax" in str (exc_info .value ).lower ()
386
388
389
+ def test_agent_internal_name_error (self ):
390
+ """Test other import errors within an agent's code (e.g., SyntaxError)."""
391
+ with tempfile .TemporaryDirectory () as temp_dir :
392
+ temp_path = Path (temp_dir )
393
+ agent_name = "name_error_agent"
394
+
395
+ # Create agent with a syntax error (which leads to ImportError)
396
+ agent_file = temp_path / f"{ agent_name } .py"
397
+ agent_file .write_text (dedent (f"""
398
+ from google.adk.agents.base_agent import BaseAgent
399
+
400
+ # name is not defined
401
+ print(non_existing_name)
402
+
403
+ class { agent_name .title ()} Agent(BaseAgent):
404
+ def __init__(self):
405
+ super().__init__(name="{ agent_name } ")
406
+
407
+ root_agent = { agent_name .title ()} Agent()
408
+ """ ))
409
+
410
+ loader = AgentLoader (str (temp_path ))
411
+ # SyntaxError is a subclass of Exception, and importlib might wrap it
412
+ # The loader is expected to prepend its message and re-raise.
413
+ with pytest .raises (
414
+ NameError
415
+ ) as exc_info : # Or potentially ImportError depending on Python version specifics with importlib
416
+ loader .load_agent (agent_name )
417
+
418
+ assert str (exc_info .value ).startswith (
419
+ f"Fail to load '{ agent_name } ' module."
420
+ )
421
+ # Check for part of the original SyntaxError message
422
+ assert "is not defined" in str (exc_info .value ).lower ()
423
+
387
424
def test_sys_path_modification (self ):
388
425
3F4C
"""Test that agents_dir is added to sys.path correctly."""
389
426
with tempfile .TemporaryDirectory () as temp_dir :
0 commit comments