aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/patch_manager_unittest.py
blob: 444156a551e5a8d578ce1be28cd0361ff844de40 (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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python3
# Copyright 2019 The ChromiumOS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Unit tests when handling patches."""

import json
from pathlib import Path
import tempfile
from typing import Callable
import unittest
from unittest import mock

import patch_manager
import patch_utils


class PatchManagerTest(unittest.TestCase):
    """Test class when handling patches of packages."""

    # Simulate behavior of 'os.path.isdir()' when the path is not a directory.
    @mock.patch.object(Path, "is_dir", return_value=False)
    def testInvalidDirectoryPassedAsCommandLineArgument(self, mock_isdir):
        src_dir = "/some/path/that/is/not/a/directory"
        patch_metadata_file = "/some/path/that/is/not/a/file"

        # Verify the exception is raised when the command line argument for
        # '--filesdir_path' or '--src_path' is not a directory.
        with self.assertRaises(ValueError):
            patch_manager.main(
                [
                    "--src_path",
                    src_dir,
                    "--patch_metadata_file",
                    patch_metadata_file,
                ]
            )
        mock_isdir.assert_called_once()

    # Simulate behavior of 'os.path.isfile()' when the patch metadata file is does
    # not exist.
    @mock.patch.object(Path, "is_file", return_value=False)
    def testInvalidPathToPatchMetadataFilePassedAsCommandLineArgument(
        self, mock_isfile
    ):
        src_dir = "/some/path/that/is/not/a/directory"
        patch_metadata_file = "/some/path/that/is/not/a/file"

        # Verify the exception is raised when the command line argument for
        # '--filesdir_path' or '--src_path' is not a directory.
        with mock.patch.object(Path, "is_dir", return_value=True):
            with self.assertRaises(ValueError):
                patch_manager.main(
                    [
                        "--src_path",
                        src_dir,
                        "--patch_metadata_file",
                        patch_metadata_file,
                    ]
                )
        mock_isfile.assert_called_once()

    @mock.patch("builtins.print")
    def testRemoveOldPatches(self, _):
        """Can remove old patches from PATCHES.json."""
        one_patch_dict = {
            "metadata": {
                "title": "[some label] hello world",
            },
            "platforms": [
                "chromiumos",
            ],
            "rel_patch_path": "x/y/z",
            "version_range": {
                "from": 4,
                "until": 5,
            },
        }
        patches = [
            one_patch_dict,
            {**one_patch_dict, "version_range": {"until": None}},
            {**one_patch_dict, "version_range": {"from": 100}},
            {**one_patch_dict, "version_range": {"until": 8}},
        ]
        cases = [
            (0, lambda x: self.assertEqual(len(x), 4)),
            (6, lambda x: self.assertEqual(len(x), 3)),
            (8, lambda x: self.assertEqual(len(x), 2)),
            (1000, lambda x: self.assertEqual(len(x), 2)),
        ]

        def _t(dirname: str, svn_version: int, assertion_f: Callable):
            json_filepath = Path(dirname) / "PATCHES.json"
            with json_filepath.open("w", encoding="utf-8") as f:
                json.dump(patches, f)
            patch_manager.RemoveOldPatches(svn_version, Path(), json_filepath)
            with json_filepath.open("r", encoding="utf-8") as f:
                result = json.load(f)
            assertion_f(result)

        with tempfile.TemporaryDirectory(
            prefix="patch_manager_unittest"
        ) as dirname:
            for r, a in cases:
                _t(dirname, r, a)

    @mock.patch("builtins.print")
    @mock.patch.object(patch_utils, "git_clean_context")
    def testCheckPatchApplies(self, _, mock_git_clean_context):
        """Tests whether we can apply a single patch for a given svn_version."""
        mock_git_clean_context.return_value = mock.MagicMock()
        with tempfile.TemporaryDirectory(
            prefix="patch_manager_unittest"
        ) as dirname:
            dirpath = Path(dirname)
            patch_entries = [
                patch_utils.PatchEntry(
                    dirpath,
                    metadata=None,
                    platforms=[],
                    rel_patch_path="another.patch",
                    version_range={
                        "from": 9,
                        "until": 20,
                    },
                ),
                patch_utils.PatchEntry(
                    dirpath,
                    metadata=None,
                    platforms=["chromiumos"],
                    rel_patch_path="example.patch",
                    version_range={
                        "from": 1,
                        "until": 10,
                    },
                ),
                patch_utils.PatchEntry(
                    dirpath,
                    metadata=None,
                    platforms=["chromiumos"],
                    rel_patch_path="patch_after.patch",
                    version_range={
                        "from": 1,
                        "until": 5,
                    },
                ),
            ]
            patches_path = dirpath / "PATCHES.json"
            with patch_utils.atomic_write(patches_path, encoding="utf-8") as f:
                json.dump([pe.to_dict() for pe in patch_entries], f)

            def _harness1(
                version: int,
                return_value: patch_utils.PatchResult,
                expected: patch_manager.GitBisectionCode,
            ):
                with mock.patch.object(
                    patch_utils.PatchEntry,
                    "apply",
                    return_value=return_value,
                ) as m:
                    result = patch_manager.CheckPatchApplies(
                        version,
                        dirpath,
                        patches_path,
                        "example.patch",
                    )
                    self.assertEqual(result, expected)
                    m.assert_called()

            _harness1(
                1,
                patch_utils.PatchResult(True, {}),
                patch_manager.GitBisectionCode.GOOD,
            )
            _harness1(
                2,
                patch_utils.PatchResult(True, {}),
                patch_manager.GitBisectionCode.GOOD,
            )
            _harness1(
                2,
                patch_utils.PatchResult(False, {}),
                patch_manager.GitBisectionCode.BAD,
            )
            _harness1(
                11,
                patch_utils.PatchResult(False, {}),
                patch_manager.GitBisectionCode.BAD,
            )

            def _harness2(
                version: int,
                application_func: Callable,
                expected: patch_manager.GitBisectionCode,
            ):
                with mock.patch.object(
                    patch_utils,
                    "apply_single_patch_entry",
                    application_func,
                ):
                    result = patch_manager.CheckPatchApplies(
                        version,
                        dirpath,
                        patches_path,
                        "example.patch",
                    )
                    self.assertEqual(result, expected)

            # Check patch can apply and fail with good return codes.
            def _apply_patch_entry_mock1(v, _, patch_entry, **__):
                return patch_entry.can_patch_version(v), None

            _harness2(
                1,
                _apply_patch_entry_mock1,
                patch_manager.GitBisectionCode.GOOD,
            )
            _harness2(
                11,
                _apply_patch_entry_mock1,
                patch_manager.GitBisectionCode.BAD,
            )

            # Early exit check, shouldn't apply later failing patch.
            def _apply_patch_entry_mock2(v, _, patch_entry, **__):
                if (
                    patch_entry.can_patch_version(v)
                    and patch_entry.rel_patch_path == "patch_after.patch"
                ):
                    return False, {"filename": mock.Mock()}
                return True, None

            _harness2(
                1,
                _apply_patch_entry_mock2,
                patch_manager.GitBisectionCode.GOOD,
            )

            # Skip check, should exit early on the first patch.
            def _apply_patch_entry_mock3(v, _, patch_entry, **__):
                if (
                    patch_entry.can_patch_version(v)
                    and patch_entry.rel_patch_path == "another.patch"
                ):
                    return False, {"filename": mock.Mock()}
                return True, None

            _harness2(
                9,
                _apply_patch_entry_mock3,
                patch_manager.GitBisectionCode.SKIP,
            )

    @mock.patch("patch_utils.git_clean_context", mock.MagicMock)
    def testUpdateVersionRanges(self):
        """Test the UpdateVersionRanges function."""
        with tempfile.TemporaryDirectory(
            prefix="patch_manager_unittest"
        ) as dirname:
            dirpath = Path(dirname)
            patches = [
                patch_utils.PatchEntry(
                    workdir=dirpath,
                    rel_patch_path="x.patch",
                    metadata=None,
                    platforms=None,
                    version_range={
                        "from": 0,
                        "until": 2,
                    },
                ),
                patch_utils.PatchEntry(
                    workdir=dirpath,
                    rel_patch_path="y.patch",
                    metadata=None,
                    platforms=None,
                    version_range={
                        "from": 0,
                        "until": 2,
                    },
                ),
            ]
            patches[0].apply = mock.MagicMock(
                return_value=patch_utils.PatchResult(
                    succeeded=False, failed_hunks={"a/b/c": []}
                )
            )
            patches[1].apply = mock.MagicMock(
                return_value=patch_utils.PatchResult(succeeded=True)
            )
            results = patch_manager.UpdateVersionRangesWithEntries(
                1, dirpath, patches
            )
            # We should only have updated the version_range of the first patch,
            # as that one failed to apply.
            self.assertEqual(len(results), 1)
            self.assertEqual(results[0].version_range, {"from": 0, "until": 1})
            self.assertEqual(patches[0].version_range, {"from": 0, "until": 1})
            self.assertEqual(patches[1].version_range, {"from": 0, "until": 2})


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