Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Unified Diff: third_party/WebKit/Tools/Scripts/webkitpy/common/unified_diff.py

Issue 2595983002: Extract diff_text function out of the Port class. (Closed)
Patch Set: Change indentation Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Tools/Scripts/webkitpy/common/unified_diff_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Tools/Scripts/webkitpy/common/unified_diff.py
diff --git a/third_party/WebKit/Tools/Scripts/webkitpy/common/unified_diff.py b/third_party/WebKit/Tools/Scripts/webkitpy/common/unified_diff.py
new file mode 100644
index 0000000000000000000000000000000000000000..1b5d37e22b58825f4c214ed65973fff300b3cfe6
--- /dev/null
+++ b/third_party/WebKit/Tools/Scripts/webkitpy/common/unified_diff.py
@@ -0,0 +1,42 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""A utility function to do text diffs of expected and actual layout test results."""
+
+import difflib
+
+
+def unified_diff(expected_text, actual_text, expected_filename, actual_filename):
+ """Returns a string containing the diff of the two text strings
+ in 'unified diff' format.
+ """
+ # The filenames show up in the diff output, make sure they're
+ # raw bytes and not unicode, so that they don't trigger join()
+ # trying to decode the input.
+ expected_filename = _to_raw_bytes(expected_filename)
+ actual_filename = _to_raw_bytes(actual_filename)
+
+ diff = difflib.unified_diff(
+ expected_text.splitlines(True),
+ actual_text.splitlines(True),
+ expected_filename,
+ actual_filename)
+
+ return ''.join(_diff_fixup(diff))
+
+
+def _to_raw_bytes(string_value):
+ if isinstance(string_value, unicode):
+ return string_value.encode('utf-8')
+ return string_value
+
+
+def _diff_fixup(diff):
+ # The diff generated by the difflib is incorrect if one of the files
+ # does not have a newline at the end of the file and it is present in
+ # the diff. Relevant Python issue: http://bugs.python.org/issue2142
+ for line in diff:
+ yield line
+ if not line.endswith('\n'):
+ yield '\n\\ No newline at end of file\n'
« no previous file with comments | « no previous file | third_party/WebKit/Tools/Scripts/webkitpy/common/unified_diff_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698