File: aquery_differ_test.py

package info (click to toggle)
bazel-bootstrap 4.2.3%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 85,476 kB
  • sloc: java: 721,710; sh: 55,859; cpp: 35,359; python: 12,139; xml: 295; objc: 269; makefile: 113; ansic: 106; ruby: 3
file content (262 lines) | stat: -rwxr-xr-x 8,715 bytes parent folder | download | duplicates (4)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# Lint as: python2, python3
# pylint: disable=g-direct-third-party-import
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

# Do not edit this line. Copybara replaces it with PY2 migration helper.
from third_party.py import mock
import six
if six.PY2:
  from cStringIO import StringIO
else:
  from io import StringIO

from src.main.protobuf import analysis_pb2
from tools.aquery_differ import aquery_differ


def make_aquery_output(actions, artifact_paths):
  action_graph = analysis_pb2.ActionGraphContainer()

  for artifact_path in artifact_paths:
    next_id = len(action_graph.artifacts)
    artifact = action_graph.artifacts.add()
    artifact.id = str(next_id)
    artifact.exec_path = artifact_path

  for next_action in actions:
    action = action_graph.actions.add()
    action.output_ids.extend(next_action["output_ids"])
    action.arguments.extend(next_action["arguments"])

    if "input_dep_set_ids" in next_action:
      action.input_dep_set_ids.extend(next_action["input_dep_set_ids"])

  return action_graph


def make_aquery_output_with_dep_set(actions, artifact_paths, dep_sets):
  action_graph = make_aquery_output(actions, artifact_paths)

  for ds in dep_sets:
    next_id = len(action_graph.dep_set_of_files)
    dep_set = action_graph.dep_set_of_files.add()
    dep_set.id = str(next_id)
    dep_set.direct_artifact_ids.extend(ds["direct_artifact_ids"])
    dep_set.transitive_dep_set_ids.extend(ds["transitive_dep_set_ids"])

  return action_graph


class CmdLineDifferTest(unittest.TestCase):

  def test_no_difference(self):
    action_graph = make_aquery_output(
        actions=[{
            "arguments": ["-a", "-b"],
            "output_ids": ["0", "1"]
        }, {
            "arguments": ["-c"],
            "output_ids": ["2"]
        }],
        artifact_paths=["exec/path/zero", "exec/path/one", "exec/path/two"])
    mock_stdout = StringIO()
    attrs = ["cmdline"]
    with mock.patch("sys.stdout", mock_stdout):
      aquery_differ._aquery_diff(action_graph, action_graph, attrs, "before",
                                 "after")
      self.assertEqual(mock_stdout.getvalue(), "No difference\n")

  def test_no_difference_different_output_files_order(self):
    first = make_aquery_output(
        actions=[
            {
                "arguments": ["-a", "-b"],
                "output_ids": ["0", "1"]
            },
        ],
        artifact_paths=["exec/path/zero", "exec/path/one"])
    second = make_aquery_output(
        actions=[
            {
                "arguments": ["-a", "-b"],
                "output_ids": ["1", "0"]
            },
        ],
        artifact_paths=["exec/path/zero", "exec/path/one"])

    mock_stdout = StringIO()
    attrs = ["cmdline"]
    with mock.patch("sys.stdout", mock_stdout):
      aquery_differ._aquery_diff(first, second, attrs, "before", "after")
      self.assertEqual(mock_stdout.getvalue(), "No difference\n")

  def test_first_has_extra_output_files(self):
    first = make_aquery_output(
        actions=[
            {
                "arguments": ["-a", "-b"],
                "output_ids": ["0", "1"]
            },
            {
                "arguments": ["-c"],
                "output_ids": ["2"]
            },
        ],
        artifact_paths=["exec/path/zero", "exec/path/one", "exec/path/two"],
    )
    second = make_aquery_output(
        actions=[
            {
                "arguments": ["-a", "-b"],
                "output_ids": ["1", "0"]
            },
        ],
        artifact_paths=["exec/path/zero", "exec/path/one", "exec/path/two"],
    )

    expected_error = ("Aquery output 'before' change contains an action "
                      "that generates the following outputs that aquery "
                      "output 'after' change doesn't:\nexec/path/two\n\n")
    mock_stdout = StringIO()
    attrs = ["cmdline"]
    with mock.patch("sys.stdout", mock_stdout):
      aquery_differ._aquery_diff(first, second, attrs, "before", "after")
      self.assertEqual(mock_stdout.getvalue(), expected_error)

  def test_second_has_extra_output_files(self):
    first = make_aquery_output(
        actions=[
            {
                "arguments": ["-a", "-b"],
                "output_ids": ["0", "1"]
            },
        ],
        artifact_paths=["exec/path/zero", "exec/path/one", "exec/path/two"],
    )
    second = make_aquery_output(
        actions=[
            {
                "arguments": ["-a", "-b"],
                "output_ids": ["0", "1"]
            },
            {
                "arguments": ["-c"],
                "output_ids": ["2"]
            },
        ],
        artifact_paths=["exec/path/zero", "exec/path/one", "exec/path/two"],
    )

    expected_error = ("Aquery output 'after' change contains an action that"
                      " generates the following outputs that aquery"
                      " output 'before' change doesn't:\nexec/path/two\n\n")
    mock_stdout = StringIO()
    attrs = ["cmdline"]
    with mock.patch("sys.stdout", mock_stdout):
      aquery_differ._aquery_diff(first, second, attrs, "before", "after")
      self.assertEqual(mock_stdout.getvalue(), expected_error)

  def test_different_command_lines(self):
    first = make_aquery_output(
        actions=[
            {
                "arguments": ["-a", "-d"],
                "output_ids": ["0", "1"]
            },
            {
                "arguments": ["-c"],
                "output_ids": ["2"]
            },
        ],
        artifact_paths=["exec/path/zero", "exec/path/one", "exec/path/two"],
    )
    second = make_aquery_output(
        actions=[
            {
                "arguments": ["-a", "-b"],
                "output_ids": ["0", "1"]
            },
            {
                "arguments": ["-c", "-d"],
                "output_ids": ["2"]
            },
        ],
        artifact_paths=["exec/path/zero", "exec/path/one", "exec/path/two"],
    )

    expected_error_one = "\n".join([
        "Difference in the action that generates the following output(s):",
        "\texec/path/two", "--- before", "+++ after", "@@ -1 +1,2 @@", " -c",
        "+-d", "\n"
    ])
    expected_error_two = "\n".join([
        "Difference in the action that generates the following output(s):",
        "\texec/path/one", "\texec/path/zero", "--- before", "+++ after",
        "@@ -1,2 +1,2 @@", " -a", "--d", "+-b", "\n"
    ])
    attrs = ["cmdline"]

    mock_stdout = StringIO()
    with mock.patch("sys.stdout", mock_stdout):
      aquery_differ._aquery_diff(first, second, attrs, "before", "after")
      self.assertIn(expected_error_one, mock_stdout.getvalue())
      self.assertIn(expected_error_two, mock_stdout.getvalue())

  def test_different_inputs(self):
    first = make_aquery_output_with_dep_set(
        actions=[{
            "arguments": [],
            "output_ids": ["0", "1"],
            "input_dep_set_ids": ["1"]
        }],
        artifact_paths=["exec/path/zero", "exec/path/one"],
        dep_sets=[{
            "transitive_dep_set_ids": [],
            "direct_artifact_ids": ["0"]
        }, {
            "transitive_dep_set_ids": ["0"],
            "direct_artifact_ids": ["1"]
        }])
    second = make_aquery_output_with_dep_set(
        actions=[
            {
                "arguments": [],
                "output_ids": ["0", "1"],
                "input_dep_set_ids": ["0"]
            },
        ],
        artifact_paths=["exec/path/zero", "exec/path/one"],
        dep_sets=[{
            "transitive_dep_set_ids": [],
            "direct_artifact_ids": ["0"]
        }])

    expected_error_one = "\n".join([
        "Difference in the action that generates the following output(s):",
        "\texec/path/one", "\texec/path/zero", "--- before", "+++ after",
        "@@ -1,2 +1 @@", "-exec/path/one", " exec/path/zero", "\n"
    ])
    attrs = ["inputs"]

    mock_stdout = StringIO()
    with mock.patch("sys.stdout", mock_stdout):
      aquery_differ._aquery_diff(first, second, attrs, "before", "after")
      self.assertIn(expected_error_one, mock_stdout.getvalue())


if __name__ == "__main__":
  unittest.main()