@@ -232,13 +232,11 @@ def instrument_for_benchmarking(
232232
233233
234234def instrument_existing_test (
235- test_path : Path ,
236- call_positions : Sequence ,
235+ test_string : str ,
237236 function_to_optimize : Any , # FunctionToOptimize or FunctionToOptimize
238- tests_project_root : Path ,
239237 mode : str , # "behavior" or "performance"
240- analyzer : JavaAnalyzer | None = None ,
241- output_class_suffix : str | None = None , # Suffix for renamed class
238+ test_path : Path | None = None ,
239+ test_class_name : str | None = None ,
242240) -> tuple [bool , str | None ]:
243241 """Inject profiling code into an existing test file.
244242
@@ -248,7 +246,7 @@ def instrument_existing_test(
248246 3. For performance mode: adds timing instrumentation with stdout markers
249247
250248 Args:
251- test_path: Path to the test file.
249+ test_string: String to the test file.
252250 call_positions: List of code positions where the function is called.
253251 function_to_optimize: The function being optimized.
254252 tests_project_root: Root directory of tests.
@@ -260,16 +258,16 @@ def instrument_existing_test(
260258 Tuple of (success, modified_source).
261259
262260 """
263- try :
264- source = test_path .read_text (encoding = "utf-8" )
265- except Exception as e :
266- logger .exception ("Failed to read test file %s: %s" , test_path , e )
267- return False , f"Failed to read test file: { e } "
268-
261+ source = test_string
269262 func_name = _get_function_name (function_to_optimize )
270263
271264 # Get the original class name from the file name
272- original_class_name = test_path .stem # e.g., "AlgorithmsTest"
265+ if test_path :
266+ original_class_name = test_path .stem # e.g., "AlgorithmsTest"
267+ elif test_class_name is not None :
268+ original_class_name = test_class_name
269+ else :
270+ raise ValueError ("test_path or test_class_name must be provided" )
273271
274272 # Determine the new class name based on mode
275273 if mode == "behavior" :
@@ -298,7 +296,7 @@ def instrument_existing_test(
298296 modified_source = _add_behavior_instrumentation (modified_source , original_class_name , func_name )
299297
300298 logger .debug ("Java %s testing for %s: renamed class %s -> %s" , mode , func_name , original_class_name , new_class_name )
301-
299+ # Why return True here?
302300 return True , modified_source
303301
304302
@@ -800,6 +798,7 @@ def instrument_generated_java_test(
800798 function_name : str ,
801799 qualified_name : str ,
802800 mode : str , # "behavior" or "performance"
801+ function_to_optimize : FunctionToOptimize ,
803802) -> str :
804803 """Instrument a generated Java test for behavior or performance testing.
805804
@@ -834,26 +833,31 @@ def instrument_generated_java_test(
834833
835834 original_class_name = class_match .group (1 )
836835
837- # Rename class based on mode
838- if mode == "behavior" :
839- new_class_name = f"{ original_class_name } __perfinstrumented"
840- else :
841- new_class_name = f"{ original_class_name } __perfonlyinstrumented"
842-
843- # Rename all references to the original class name in the source.
844- # This includes the class declaration, return types, constructor calls, etc.
845- modified_code = re .sub (
846- rf"\b{ re .escape (original_class_name )} \b" , new_class_name , test_code
847- )
848836
849837 # For performance mode, add timing instrumentation
850838 # Use original class name (without suffix) in timing markers for consistency with Python
851839 if mode == "performance" :
840+
841+
842+ # Rename class based on mode
843+ if mode == "behavior" :
844+ new_class_name = f"{ original_class_name } __perfinstrumented"
845+ else :
846+ new_class_name = f"{ original_class_name } __perfonlyinstrumented"
847+
848+ # Rename all references to the original class name in the source.
849+ # This includes the class declaration, return types, constructor calls, etc.
850+ modified_code = re .sub (
851+ rf"\b{ re .escape (original_class_name )} \b" , new_class_name , test_code
852+ )
853+
852854 modified_code = _add_timing_instrumentation (
853855 modified_code ,
854856 original_class_name , # Use original name in markers, not the renamed class
855857 function_name ,
856858 )
859+ elif mode == "behavior" :
860+ _ , modified_code = instrument_existing_test (test_string = test_code , mode = mode , function_to_optimize = function_to_optimize , test_class_name = original_class_name )
857861
858862 logger .debug ("Instrumented generated Java test for %s (mode=%s)" , function_name , mode )
859863 return modified_code
0 commit comments