summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/build/android/gyp/util/diff_utils.py
blob: 530a6881916b53fb5f9204acb8c26d146ea18ac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Copyright 2019 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.

import os
import sys

import difflib
from util import build_utils


def _SkipOmitted(line):
  """
  Skip lines that are to be intentionally omitted from the expectations file.

  This is required when the file to be compared against expectations contains
  a line that changes from build to build because - for instance - it contains
  version information.
  """
  if line.rstrip().endswith('# OMIT FROM EXPECTATIONS'):
    return '# THIS LINE WAS OMITTED\n'
  return line


def _GenerateDiffWithOnlyAdditons(expected_path, actual_data):
  """Generate a diff that only contains additions"""
  # Ignore blank lines when creating the diff to cut down on whitespace-only
  # lines in the diff. Also remove trailing whitespaces and add the new lines
  # manually (ndiff expects new lines but we don't care about trailing
  # whitespace).
  with open(expected_path) as expected:
    expected_lines = [l for l in expected.readlines() if l.strip()]
  actual_lines = [
      '{}\n'.format(l.rstrip()) for l in actual_data.splitlines() if l.strip()
  ]

  diff = difflib.ndiff(expected_lines, actual_lines)
  filtered_diff = (l for l in diff if l.startswith('+'))
  return ''.join(filtered_diff)


def _DiffFileContents(expected_path, actual_data):
  """Check file contents for equality and return the diff or None."""
  # Remove all trailing whitespace and add it explicitly in the end.
  with open(expected_path) as f_expected:
    expected_lines = [l.rstrip() for l in f_expected.readlines()]
  actual_lines = [
      _SkipOmitted(line).rstrip() for line in actual_data.splitlines()
  ]

  if expected_lines == actual_lines:
    return None

  expected_path = os.path.relpath(expected_path, build_utils.DIR_SOURCE_ROOT)

  diff = difflib.unified_diff(
      expected_lines,
      actual_lines,
      fromfile=os.path.join('before', expected_path),
      tofile=os.path.join('after', expected_path),
      n=0,
      lineterm='',
  )

  return '\n'.join(diff)


def AddCommandLineFlags(parser):
  group = parser.add_argument_group('Expectations')
  group.add_argument(
      '--expected-file',
      help='Expected contents for the check. If --expected-file-base  is set, '
      'this is a diff of --actual-file and --expected-file-base.')
  group.add_argument(
      '--expected-file-base',
      help='File to diff against before comparing to --expected-file.')
  group.add_argument('--actual-file',
                     help='Path to write actual file (for reference).')
  group.add_argument('--failure-file',
                     help='Write to this file if expectations fail.')
  group.add_argument('--fail-on-expectations',
                     action="store_true",
                     help='Fail on expectation mismatches.')
  group.add_argument('--only-verify-expectations',
                     action='store_true',
                     help='Verify the expectation and exit.')


def CheckExpectations(actual_data, options, custom_msg=''):
  if options.actual_file:
    with build_utils.AtomicOutput(options.actual_file) as f:
      f.write(actual_data.encode('utf8'))
  if options.expected_file_base:
    actual_data = _GenerateDiffWithOnlyAdditons(options.expected_file_base,
                                                actual_data)
  diff_text = _DiffFileContents(options.expected_file, actual_data)

  if not diff_text:
    fail_msg = ''
  else:
    fail_msg = """
Expectations need updating:
https://chromium.googlesource.com/chromium/src/+/HEAD/chrome/android/expectations/README.md

LogDog tip: Use "Raw log" or "Switch to lite mode" before copying:
https://bugs.chromium.org/p/chromium/issues/detail?id=984616

{}

To update expectations, run:
########### START ###########
 patch -p1 <<'END_DIFF'
{}
END_DIFF
############ END ############
""".format(custom_msg, diff_text)

    sys.stderr.write(fail_msg)

  if fail_msg and options.fail_on_expectations:
    # Don't write failure file when failing on expectations or else the target
    # will not be re-run on subsequent ninja invocations.
    sys.exit(1)

  if options.failure_file:
    with open(options.failure_file, 'w') as f:
      f.write(fail_msg)