From be6bda7f64dcbdce0568c85bedb6a68fc6490706 Mon Sep 17 00:00:00 2001 From: Gavin Mak Date: Fri, 26 Jul 2024 19:50:13 +0000 Subject: [PATCH] Flush NewContents cache in CheckPatchFormatted Prior to https://crrev.com/c/5740609, NewContents never flushed cache so the second NewContents() would always produce the same contents post-yapf as as pre-yapf. Flush cache on second NewContents() call to get updated file contents. Also fix the formatting a bit. Bug: b/333744051 Change-Id: Ic627dd72675d7d3694b1978635ae047b38f06596 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/357960 Auto-Submit: Gavin Mak Commit-Queue: Jeremy Leconte Reviewed-by: Jeremy Leconte Cr-Commit-Position: refs/heads/main@{#42677} --- infra/specs/PRESUBMIT.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/infra/specs/PRESUBMIT.py b/infra/specs/PRESUBMIT.py index 0886ee6682..a38bc8d566 100644 --- a/infra/specs/PRESUBMIT.py +++ b/infra/specs/PRESUBMIT.py @@ -34,17 +34,18 @@ def CheckPatchFormatted(input_api, output_api): output_api.PresubmitError('Error calling "' + shlex.join(cmd) + '"') ) - new_content = f.NewContents() + # Make sure NewContents reads the updated files from disk and not cache. + new_content = f.NewContents(flush_cache=True) if new_content != prev_content: path = f.LocalPath() - diff = difflib.unified_diff(prev_content, new_content, path, path) - diffs.append(''.join(diff)) + diff = difflib.unified_diff(prev_content, new_content, path, path, lineterm='') + diffs.append('\n'.join(diff)) if diffs: combined_diffs = '\n'.join(diffs) msg = ( - 'Diff found after running "yapf -i" on modified .pyl files:\n' - f'{combined_diffs}\n' + 'Diff found after running "yapf -i" on modified .pyl files:\n\n' + f'{combined_diffs}\n\n' 'Please commit or discard the new changes.' ) results.append(output_api.PresubmitError(msg))