Skip to content

Commit d7f95a5

Browse files
authored
Merge pull request #2150 from codeflash-ai/fix/pytohn-stdout-comparator
[FIX] Python stdout comparator when the stdout is empty
2 parents eb3fcb6 + 807669e commit d7f95a5

2 files changed

Lines changed: 58 additions & 8 deletions

File tree

codeflash/verification/equivalence.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,12 @@ def compare_test_results(
129129
)
130130
except Exception as e:
131131
logger.error(e)
132-
elif (
133-
not pass_fail_only
134-
and (original_test_result.stdout and cdd_test_result.stdout)
135-
and not comparator(original_test_result.stdout, cdd_test_result.stdout)
136-
):
132+
elif not pass_fail_only and not comparator(original_test_result.stdout or "", cdd_test_result.stdout or ""):
137133
test_diffs.append(
138134
TestDiff(
139135
scope=TestDiffScope.STDOUT,
140-
original_value=str(original_test_result.stdout),
141-
candidate_value=str(cdd_test_result.stdout),
136+
original_value=original_test_result.stdout or "",
137+
candidate_value=cdd_test_result.stdout or "",
142138
test_src_code=original_test_result.id.get_src_code(original_test_result.file_name),
143139
candidate_pytest_error=cdd_pytest_error,
144140
original_pass=original_test_result.did_pass,

tests/test_comparator.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import pytest
1717

1818
from codeflash.either import Failure, Success
19-
from codeflash.models.models import FunctionTestInvocation, InvocationId, TestResults, TestType
19+
from codeflash.models.models import FunctionTestInvocation, InvocationId, TestDiffScope, TestResults, TestType
2020
from codeflash.verification.comparator import (
2121
PYTEST_TEMP_PATH_PATTERN,
2222
PYTHON_TEMPFILE_PATTERN,
@@ -2999,6 +2999,60 @@ def test_compare_results_fn():
29992999
assert not match
30003000

30013001

3002+
def test_compare_results_detects_stdout_mismatch_when_candidate_stdout_is_empty() -> None:
3003+
original_results = TestResults()
3004+
original_results.add(
3005+
FunctionTestInvocation(
3006+
id=InvocationId(
3007+
test_module_path="test_module_path",
3008+
test_class_name="test_class_name",
3009+
test_function_name="test_function_name",
3010+
function_getting_tested="function_getting_tested",
3011+
iteration_id="0",
3012+
),
3013+
file_name=Path("file_name"),
3014+
did_pass=True,
3015+
runtime=5,
3016+
test_framework="pytest",
3017+
test_type=TestType.EXISTING_UNIT_TEST,
3018+
return_value=[0, 1, 2],
3019+
timed_out=False,
3020+
loop_index=1,
3021+
stdout="codeflash stdout: Sorting list\nresult: [0, 1, 2]\n",
3022+
)
3023+
)
3024+
3025+
candidate_results = TestResults()
3026+
candidate_results.add(
3027+
FunctionTestInvocation(
3028+
id=InvocationId(
3029+
test_module_path="test_module_path",
3030+
test_class_name="test_class_name",
3031+
test_function_name="test_function_name",
3032+
function_getting_tested="function_getting_tested",
3033+
iteration_id="0",
3034+
),
3035+
file_name=Path("file_name"),
3036+
did_pass=True,
3037+
runtime=5,
3038+
test_framework="pytest",
3039+
test_type=TestType.EXISTING_UNIT_TEST,
3040+
return_value=[0, 1, 2],
3041+
timed_out=False,
3042+
loop_index=1,
3043+
stdout="",
3044+
)
3045+
)
3046+
3047+
match, diffs = compare_test_results(original_results, candidate_results)
3048+
3049+
assert not match
3050+
assert len(diffs) == 1
3051+
assert diffs[0].scope == TestDiffScope.STDOUT
3052+
assert diffs[0].original_value == "codeflash stdout: Sorting list\nresult: [0, 1, 2]\n"
3053+
assert diffs[0].candidate_value == ""
3054+
3055+
30023056
def test_exceptions():
30033057
type_error = TypeError("This is a type error")
30043058

0 commit comments

Comments
 (0)