aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/clean_up_old_llvm_patches_test.py
blob: 02100c8fd7c1a0dd2bf41cfbec18fb8f1f2669bd (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
#!/usr/bin/env python3
# Copyright 2024 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 clean_up_old_llvm_patches"""

from pathlib import Path
import shutil
import tempfile
import unittest

import clean_up_old_llvm_patches


ANDROID_VERSION_PY_EXAMPLE = """
def get_svn_revision():
    return "r654321"
"""


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

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

    def test_android_version_autodetection(self):
        android_root = self.make_tempdir()
        android_version_py = (
            android_root / "toolchain" / "llvm_android" / "android_version.py"
        )
        android_version_py.parent.mkdir(parents=True)
        android_version_py.write_text(
            ANDROID_VERSION_PY_EXAMPLE, encoding="utf-8"
        )

        self.assertEqual(
            clean_up_old_llvm_patches.find_android_llvm_version(android_root),
            654321,
        )

    def test_chromeos_version_autodetection(self):
        chromiumos_overlay = self.make_tempdir()
        llvm = chromiumos_overlay / "sys-devel" / "llvm"
        llvm.mkdir(parents=True)

        file_names = (
            "Manifest",
            "llvm-12.0-r1.ebuild",
            "llvm-18.0_pre123456-r90.ebuild",
            "llvm-9999.ebuild",
        )
        for f in file_names:
            (llvm / f).touch()

        self.assertEqual(
            clean_up_old_llvm_patches.find_chromeos_llvm_version(
                chromiumos_overlay
            ),
            123456,
        )


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