aboutsummaryrefslogtreecommitdiff
path: root/rust_tools/auto_update_rust_bootstrap_test.py
blob: 0578539d5d56e67771b0ffaf160bb59ae0416fab (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#!/usr/bin/env python3
# Copyright 2023 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Tests for auto_update_rust_bootstrap."""

import os
from pathlib import Path
import shutil
import tempfile
import textwrap
import unittest
from unittest import mock

import auto_update_rust_bootstrap


_GIT_PUSH_OUTPUT = r"""
remote: Waiting for private key checker: 2/2 objects left
remote:
remote: Processing changes: new: 1 (\)
remote: Processing changes: new: 1 (|)
remote: Processing changes: new: 1 (/)
remote: Processing changes: refs: 1, new: 1 (/)
remote: Processing changes: refs: 1, new: 1 (/)
remote: Processing changes: refs: 1, new: 1 (/)
remote: Processing changes: refs: 1, new: 1, done
remote:
remote: SUCCESS
remote:
remote:   https://chromium-review.googlesource.com/c/chromiumos/overlays/chromiumos-overlay/+/5018826 rust-bootstrap: use prebuilts [WIP] [NEW]
remote:
To https://chromium.googlesource.com/chromiumos/overlays/chromiumos-overlay
 * [new reference]             HEAD -> refs/for/main
"""

_GIT_PUSH_MULTI_CL_OUTPUT = r"""
remote: Waiting for private key checker: 2/2 objects left
remote:
remote: Processing changes: new: 1 (\)
remote: Processing changes: new: 1 (|)
remote: Processing changes: new: 1 (/)
remote: Processing changes: refs: 1, new: 1 (/)
remote: Processing changes: refs: 1, new: 1 (/)
remote: Processing changes: refs: 1, new: 1 (/)
remote: Processing changes: refs: 1, new: 1, done
remote:
remote: SUCCESS
remote:
remote:   https://chromium-review.googlesource.com/c/chromiumos/overlays/chromiumos-overlay/+/5339923 rust-bootstrap: add version 1.75.0 [NEW]
remote:   https://chromium-review.googlesource.com/c/chromiumos/overlays/chromiumos-overlay/+/5339924 rust-bootstrap: remove unused ebuilds [NEW]
remote:
To https://chromium.googlesource.com/chromiumos/overlays/chromiumos-overlay
 * [new reference]             HEAD -> refs/for/main
"""


class Test(unittest.TestCase):
    """Tests for auto_update_rust_bootstrap."""

    def make_tempdir(self) -> Path:
        tempdir = Path(
            tempfile.mkdtemp(prefix="auto_update_rust_bootstrap_test_")
        )
        self.addCleanup(shutil.rmtree, tempdir)
        return tempdir

    def test_git_cl_id_scraping(self):
        self.assertEqual(
            auto_update_rust_bootstrap.scrape_git_push_cl_id_strs(
                _GIT_PUSH_OUTPUT
            ),
            ["5018826"],
        )

        self.assertEqual(
            auto_update_rust_bootstrap.scrape_git_push_cl_id_strs(
                _GIT_PUSH_MULTI_CL_OUTPUT
            ),
            ["5339923", "5339924"],
        )

    def test_ebuild_linking_logic_handles_direct_relative_symlinks(self):
        tempdir = self.make_tempdir()
        target = tempdir / "target.ebuild"
        target.touch()
        (tempdir / "symlink.ebuild").symlink_to(target.name)
        self.assertTrue(
            auto_update_rust_bootstrap.is_ebuild_linked_to_in_dir(target)
        )

    def test_ebuild_linking_logic_handles_direct_absolute_symlinks(self):
        tempdir = self.make_tempdir()
        target = tempdir / "target.ebuild"
        target.touch()
        (tempdir / "symlink.ebuild").symlink_to(target)
        self.assertTrue(
            auto_update_rust_bootstrap.is_ebuild_linked_to_in_dir(target)
        )

    def test_ebuild_linking_logic_handles_indirect_relative_symlinks(self):
        tempdir = self.make_tempdir()
        target = tempdir / "target.ebuild"
        target.touch()
        (tempdir / "symlink.ebuild").symlink_to(
            Path("..") / tempdir.name / target.name
        )
        self.assertTrue(
            auto_update_rust_bootstrap.is_ebuild_linked_to_in_dir(target)
        )

    def test_ebuild_linking_logic_handles_broken_symlinks(self):
        tempdir = self.make_tempdir()
        target = tempdir / "target.ebuild"
        target.touch()
        (tempdir / "symlink.ebuild").symlink_to("doesnt_exist.ebuild")
        self.assertFalse(
            auto_update_rust_bootstrap.is_ebuild_linked_to_in_dir(target)
        )

    def test_ebuild_linking_logic_only_steps_through_one_symlink(self):
        tempdir = self.make_tempdir()
        target = tempdir / "target.ebuild"
        target.symlink_to("doesnt_exist.ebuild")
        (tempdir / "symlink.ebuild").symlink_to(target.name)
        self.assertTrue(
            auto_update_rust_bootstrap.is_ebuild_linked_to_in_dir(target)
        )

    def test_raw_bootstrap_seq_finding_functions(self):
        ebuild_contents = textwrap.dedent(
            """\
            # Some copyright
            FOO=bar
            # Comment about RUSTC_RAW_FULL_BOOTSTRAP_SEQUENCE=(
            RUSTC_RAW_FULL_BOOTSTRAP_SEQUENCE=( # another comment
                1.2.3 # (with a comment with parens)
                4.5.6
            )
            """
        )

        ebuild_lines = ebuild_contents.splitlines()
        (
            start,
            end,
        ) = auto_update_rust_bootstrap.find_raw_bootstrap_sequence_lines(
            ebuild_lines
        )
        self.assertEqual(start, len(ebuild_lines) - 4)
        self.assertEqual(end, len(ebuild_lines) - 1)

    def test_collect_ebuilds_by_version_ignores_older_versions(self):
        tempdir = self.make_tempdir()
        ebuild_170 = tempdir / "rust-bootstrap-1.70.0.ebuild"
        ebuild_170.touch()
        ebuild_170_r1 = tempdir / "rust-bootstrap-1.70.0-r1.ebuild"
        ebuild_170_r1.touch()
        ebuild_171_r2 = tempdir / "rust-bootstrap-1.71.1-r2.ebuild"
        ebuild_171_r2.touch()

        self.assertEqual(
            auto_update_rust_bootstrap.collect_ebuilds_by_version(tempdir),
            [
                (
                    auto_update_rust_bootstrap.EbuildVersion(
                        major=1, minor=70, patch=0, rev=1
                    ),
                    ebuild_170_r1,
                ),
                (
                    auto_update_rust_bootstrap.EbuildVersion(
                        major=1, minor=71, patch=1, rev=2
                    ),
                    ebuild_171_r2,
                ),
            ],
        )

    def test_has_prebuilt_works(self):
        tempdir = self.make_tempdir()
        ebuild = tempdir / "rust-bootstrap-1.70.0.ebuild"
        ebuild.write_text(
            textwrap.dedent(
                """\
                # Some copyright
                FOO=bar
                # Comment about RUSTC_RAW_FULL_BOOTSTRAP_SEQUENCE=(
                RUSTC_RAW_FULL_BOOTSTRAP_SEQUENCE=( # another comment
                    1.67.0
                    1.68.1
                    1.69.0
                )
                """
            ),
            encoding="utf-8",
        )

        self.assertTrue(
            auto_update_rust_bootstrap.version_listed_in_bootstrap_sequence(
                ebuild,
                auto_update_rust_bootstrap.EbuildVersion(
                    major=1,
                    minor=69,
                    patch=0,
                    rev=0,
                ),
            )
        )

        self.assertFalse(
            auto_update_rust_bootstrap.version_listed_in_bootstrap_sequence(
                ebuild,
                auto_update_rust_bootstrap.EbuildVersion(
                    major=1,
                    minor=70,
                    patch=0,
                    rev=0,
                ),
            )
        )

    def test_ebuild_updating_works(self):
        tempdir = self.make_tempdir()
        ebuild = tempdir / "rust-bootstrap-1.70.0.ebuild"
        ebuild.write_text(
            textwrap.dedent(
                """\
                # Some copyright
                FOO=bar
                RUSTC_RAW_FULL_BOOTSTRAP_SEQUENCE=(
                \t1.67.0
                \t1.68.1
                \t1.69.0
                )
                """
            ),
            encoding="utf-8",
        )

        auto_update_rust_bootstrap.add_version_to_bootstrap_sequence(
            ebuild,
            auto_update_rust_bootstrap.EbuildVersion(
                major=1,
                minor=70,
                patch=1,
                rev=2,
            ),
            dry_run=False,
        )

        self.assertEqual(
            ebuild.read_text(encoding="utf-8"),
            textwrap.dedent(
                """\
                # Some copyright
                FOO=bar
                RUSTC_RAW_FULL_BOOTSTRAP_SEQUENCE=(
                \t1.67.0
                \t1.68.1
                \t1.69.0
                \t1.70.1-r2
                )
                """
            ),
        )

    def test_ebuild_version_parsing_works(self):
        self.assertEqual(
            auto_update_rust_bootstrap.parse_ebuild_version(
                "rust-bootstrap-1.70.0-r2.ebuild"
            ),
            auto_update_rust_bootstrap.EbuildVersion(
                major=1, minor=70, patch=0, rev=2
            ),
        )

        self.assertEqual(
            auto_update_rust_bootstrap.parse_ebuild_version(
                "rust-bootstrap-2.80.3.ebuild"
            ),
            auto_update_rust_bootstrap.EbuildVersion(
                major=2, minor=80, patch=3, rev=0
            ),
        )

        with self.assertRaises(ValueError):
            auto_update_rust_bootstrap.parse_ebuild_version(
                "rust-bootstrap-2.80.3_pre1234.ebuild"
            )

    def test_raw_ebuild_version_parsing_works(self):
        self.assertEqual(
            auto_update_rust_bootstrap.parse_raw_ebuild_version("1.70.0-r2"),
            auto_update_rust_bootstrap.EbuildVersion(
                major=1, minor=70, patch=0, rev=2
            ),
        )

        with self.assertRaises(ValueError):
            auto_update_rust_bootstrap.parse_ebuild_version("2.80.3_pre1234")

    def test_ensure_newest_version_does_nothing_if_no_new_rust_version(self):
        tempdir = self.make_tempdir()
        rust = tempdir / "rust"
        rust.mkdir()
        (rust / "rust-1.70.0-r1.ebuild").touch()
        rust_bootstrap = tempdir / "rust-bootstrap"
        rust_bootstrap.mkdir()
        (rust_bootstrap / "rust-bootstrap-1.70.0.ebuild").touch()

        self.assertFalse(
            auto_update_rust_bootstrap.maybe_add_new_rust_bootstrap_version(
                tempdir, rust_bootstrap, dry_run=True
            )
        )

    @mock.patch.object(auto_update_rust_bootstrap, "update_ebuild_manifest")
    def test_ensure_newest_version_upgrades_rust_bootstrap_properly(
        self, update_ebuild_manifest
    ):
        tempdir = self.make_tempdir()
        rust = tempdir / "rust"
        rust.mkdir()
        (rust / "rust-1.71.0-r1.ebuild").touch()
        rust_bootstrap = tempdir / "rust-bootstrap"
        rust_bootstrap.mkdir()
        rust_bootstrap_1_70 = rust_bootstrap / "rust-bootstrap-1.70.0-r2.ebuild"

        rust_bootstrap_contents = textwrap.dedent(
            """\
            # Some copyright
            FOO=bar
            RUSTC_RAW_FULL_BOOTSTRAP_SEQUENCE=(
            \t1.67.0
            \t1.68.1
            \t1.69.0
            \t1.70.0-r1
            )
            """
        )
        rust_bootstrap_1_70.write_text(
            rust_bootstrap_contents, encoding="utf-8"
        )

        self.assertTrue(
            auto_update_rust_bootstrap.maybe_add_new_rust_bootstrap_version(
                tempdir, rust_bootstrap, dry_run=False, commit=False
            )
        )
        update_ebuild_manifest.assert_called_once()
        rust_bootstrap_1_71 = rust_bootstrap / "rust-bootstrap-1.71.0.ebuild"

        self.assertTrue(rust_bootstrap_1_70.is_symlink())
        self.assertEqual(
            os.readlink(rust_bootstrap_1_70),
            rust_bootstrap_1_71.name,
        )
        self.assertFalse(rust_bootstrap_1_71.is_symlink())
        self.assertEqual(
            rust_bootstrap_1_71.read_text(encoding="utf-8"),
            rust_bootstrap_contents,
        )

    def test_ensure_newest_version_breaks_if_prebuilt_is_not_available(self):
        tempdir = self.make_tempdir()
        rust = tempdir / "rust"
        rust.mkdir()
        (rust / "rust-1.71.0-r1.ebuild").touch()
        rust_bootstrap = tempdir / "rust-bootstrap"
        rust_bootstrap.mkdir()
        rust_bootstrap_1_70 = rust_bootstrap / "rust-bootstrap-1.70.0-r2.ebuild"

        rust_bootstrap_contents = textwrap.dedent(
            """\
            # Some copyright
            FOO=bar
            RUSTC_RAW_FULL_BOOTSTRAP_SEQUENCE=(
            \t1.67.0
            \t1.68.1
            \t1.69.0
            # Note: Missing 1.70.0 for rust-bootstrap-1.71.1
            )
            """
        )
        rust_bootstrap_1_70.write_text(
            rust_bootstrap_contents, encoding="utf-8"
        )

        with self.assertRaises(
            auto_update_rust_bootstrap.MissingRustBootstrapPrebuiltError
        ):
            auto_update_rust_bootstrap.maybe_add_new_rust_bootstrap_version(
                tempdir, rust_bootstrap, dry_run=True
            )

    def test_version_deletion_does_nothing_if_all_versions_are_needed(self):
        tempdir = self.make_tempdir()
        rust = tempdir / "rust"
        rust.mkdir()
        (rust / "rust-1.71.0-r1.ebuild").touch()
        rust_bootstrap = tempdir / "rust-bootstrap"
        rust_bootstrap.mkdir()
        (rust_bootstrap / "rust-bootstrap-1.70.0-r2.ebuild").touch()

        self.assertFalse(
            auto_update_rust_bootstrap.maybe_delete_old_rust_bootstrap_ebuilds(
                tempdir, rust_bootstrap, dry_run=True
            )
        )

    def test_version_deletion_ignores_newer_than_needed_versions(self):
        tempdir = self.make_tempdir()
        rust = tempdir / "rust"
        rust.mkdir()
        (rust / "rust-1.71.0-r1.ebuild").touch()
        rust_bootstrap = tempdir / "rust-bootstrap"
        rust_bootstrap.mkdir()
        (rust_bootstrap / "rust-bootstrap-1.70.0-r2.ebuild").touch()
        (rust_bootstrap / "rust-bootstrap-1.71.0-r1.ebuild").touch()
        (rust_bootstrap / "rust-bootstrap-1.72.0.ebuild").touch()

        self.assertFalse(
            auto_update_rust_bootstrap.maybe_delete_old_rust_bootstrap_ebuilds(
                tempdir, rust_bootstrap, dry_run=True
            )
        )

    @mock.patch.object(auto_update_rust_bootstrap, "update_ebuild_manifest")
    def test_version_deletion_deletes_old_files(self, update_ebuild_manifest):
        tempdir = self.make_tempdir()
        rust = tempdir / "rust"
        rust.mkdir()
        (rust / "rust-1.71.0-r1.ebuild").touch()
        rust_bootstrap = tempdir / "rust-bootstrap"
        rust_bootstrap.mkdir()
        needed_rust_bootstrap = (
            rust_bootstrap / "rust-bootstrap-1.70.0-r2.ebuild"
        )
        needed_rust_bootstrap.touch()

        # There are quite a few of these, so corner-cases are tested.

        # Symlink to outside of the group of files to delete.
        bootstrap_1_68_symlink = rust_bootstrap / "rust-bootstrap-1.68.0.ebuild"
        bootstrap_1_68_symlink.symlink_to(needed_rust_bootstrap.name)
        # Ensure that absolute symlinks are caught.
        bootstrap_1_68_symlink_abs = (
            rust_bootstrap / "rust-bootstrap-1.68.0-r1.ebuild"
        )
        bootstrap_1_68_symlink_abs.symlink_to(needed_rust_bootstrap)
        # Regular files should be no issue.
        bootstrap_1_69_regular = rust_bootstrap / "rust-bootstrap-1.69.0.ebuild"
        bootstrap_1_69_regular.touch()
        # Symlinks linking back into the set of files to delete should also be
        # no issue.
        bootstrap_1_69_symlink = (
            rust_bootstrap / "rust-bootstrap-1.69.0-r2.ebuild"
        )
        bootstrap_1_69_symlink.symlink_to(bootstrap_1_69_regular.name)

        self.assertTrue(
            auto_update_rust_bootstrap.maybe_delete_old_rust_bootstrap_ebuilds(
                tempdir,
                rust_bootstrap,
                dry_run=False,
                commit=False,
            )
        )
        update_ebuild_manifest.assert_called_once()

        self.assertFalse(bootstrap_1_68_symlink.exists())
        self.assertFalse(bootstrap_1_68_symlink_abs.exists())
        self.assertFalse(bootstrap_1_69_regular.exists())
        self.assertFalse(bootstrap_1_69_symlink.exists())
        self.assertTrue(needed_rust_bootstrap.exists())

    def test_version_deletion_raises_when_old_file_has_dep(self):
        tempdir = self.make_tempdir()
        rust = tempdir / "rust"
        rust.mkdir()
        (rust / "rust-1.71.0-r1.ebuild").touch()
        rust_bootstrap = tempdir / "rust-bootstrap"
        rust_bootstrap.mkdir()
        old_rust_bootstrap = rust_bootstrap / "rust-bootstrap-1.69.0-r1.ebuild"
        old_rust_bootstrap.touch()
        (rust_bootstrap / "rust-bootstrap-1.70.0-r2.ebuild").symlink_to(
            old_rust_bootstrap.name
        )

        with self.assertRaises(
            auto_update_rust_bootstrap.OldEbuildIsLinkedToError
        ):
            auto_update_rust_bootstrap.maybe_delete_old_rust_bootstrap_ebuilds(
                tempdir, rust_bootstrap, dry_run=True
            )


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