funrollloops commented on code in PR #60362:
URL: https://github.com/apache/spark/pull/60362#discussion_r4570282467


##########
python/pyspark/sql/tests/df_golden/df_golden.py:
##########
@@ -228,8 +231,41 @@ def write_test_file(filepath, header, cases):
         case_lines.append(_CASE_END)
         blocks.append("\n".join(case_lines))
 
+    return "\n\n\n".join(blocks) + "\n"
+
+
+def write_test_file(filepath, header, cases):
+    """Write *header* and *cases* back into ``.test`` file format."""
     with open(filepath, "w") as f:
-        f.write("\n\n\n".join(blocks) + "\n")
+        f.write(serialize_test_file(header, cases))
+
+
+_GOLDEN_DIFF_BEGIN = "=== BEGIN GOLDEN DIFF ==="
+_GOLDEN_DIFF_END = "=== END GOLDEN DIFF ==="
+_GOLDEN_REL_DIR = "python/pyspark/sql/tests/df_golden"
+
+
+def _golden_diff(test_file, new_content):
+    """Return a repo-relative unified diff for use with ``git apply``."""
+    with open(test_file) as f:
+        old_content = f.read()
+    rel = "{}/{}".format(_GOLDEN_REL_DIR, os.path.basename(test_file))
+    lines = difflib.unified_diff(
+        old_content.splitlines(keepends=True),

Review Comment:
   Fixed in 91726d4aa77. The helper now splits only at `\n` using 
`io.StringIO(..., newline="\n").readlines()`. Added coverage for all eight 
separators on changed and context lines. I also checked that all 16 patches 
apply with `git apply` and reproduce the new contents byte-for-byte.
   



##########
python/pyspark/sql/tests/df_golden/df_golden.py:
##########
@@ -693,23 +729,28 @@ def run_golden_test(test_case, spark, test_file):
     regenerate = os.environ.get("SPARK_GENERATE_GOLDEN_FILES") is not None
     base_dir = os.path.dirname(test_file)
 
-    header, cases = parse_test_file(test_file, require_terminated=not 
regenerate)
-    _validate_test_file(test_file, header, cases, regenerate)
+    # Parse leniently to generate a diff for incomplete goldens; verify 
strictly below.
+    header, cases = parse_test_file(test_file)
+    _validate_test_file(test_file, header, cases, regenerate=True)
 
     # Golden files are generated with ANSI mode on, matching the SQL golden
     # tests.  The session is discarded after the file, so nothing to restore.
     spark.conf.set("spark.sql.ansi.enabled", "true")
 
-    regenerated_cases = []
-    for case in cases:
-        actual = compute_case_outputs(spark, case, base_dir)
-        if regenerate:
-            regenerated_cases.append(_regenerate_case(case, actual))
-        else:
-            _compare_case(test_case, case, actual)
+    # Capture every case once so a mismatch does not leave the diff incomplete.
+    actuals = [compute_case_outputs(spark, case, base_dir) for case in cases]
+    regenerated_cases = [_regenerate_case(case, actual) for case, actual in 
zip(cases, actuals)]
+    diff = _golden_diff(test_file, serialize_test_file(header, 
regenerated_cases))
+    if diff:
+        _print_golden_diff(diff)
 
     if regenerate:
         write_test_file(test_file, header, regenerated_cases)
+    else:
+        header, cases = parse_test_file(test_file, require_terminated=True)

Review Comment:
   Added in 91726d4aa77. The runner test covers both missing expected sections 
and a missing final terminator. Each case checks that the diff is printed 
before verification fails, the file remains byte-for-byte unchanged, and both 
scripts run exactly once.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to