xref: /aosp_15_r20/external/toolchain-utils/llvm_tools/git_llvm_rev_test.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1#!/usr/bin/env python3
2# Copyright 2019 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Tests for git_llvm_rev."""
7
8import unittest
9
10import git_llvm_rev
11import llvm_project
12
13
14def get_llvm_config() -> git_llvm_rev.LLVMConfig:
15    return git_llvm_rev.LLVMConfig(
16        dir=llvm_project.get_location(), remote="origin"
17    )
18
19
20class Test(unittest.TestCase):
21    """Test cases for git_llvm_rev."""
22
23    def rev_to_sha_with_round_trip(self, rev: git_llvm_rev.Rev) -> str:
24        config = get_llvm_config()
25        sha = git_llvm_rev.translate_rev_to_sha(config, rev)
26        roundtrip_rev = git_llvm_rev.translate_sha_to_rev(config, sha)
27        self.assertEqual(roundtrip_rev, rev)
28        return sha
29
30    def test_sha_to_rev_on_base_sha_works(self) -> None:
31        sha = self.rev_to_sha_with_round_trip(
32            git_llvm_rev.Rev(
33                branch=git_llvm_rev.MAIN_BRANCH,
34                number=git_llvm_rev.base_llvm_revision,
35            )
36        )
37        self.assertEqual(sha, git_llvm_rev.base_llvm_sha)
38
39    def test_sha_to_rev_prior_to_base_rev_works(self) -> None:
40        sha = self.rev_to_sha_with_round_trip(
41            git_llvm_rev.Rev(branch=git_llvm_rev.MAIN_BRANCH, number=375000)
42        )
43        self.assertEqual(sha, "2f6da767f13b8fd81f840c211d405fea32ac9db7")
44
45    def test_sha_to_rev_after_base_rev_works(self) -> None:
46        sha = self.rev_to_sha_with_round_trip(
47            git_llvm_rev.Rev(branch=git_llvm_rev.MAIN_BRANCH, number=375506)
48        )
49        self.assertEqual(sha, "3bf7fddeb05655d9baed4cc69e13535c677ed1dd")
50
51    def test_llvm_svn_parsing_runs_ignore_reverts(self) -> None:
52        # This commit has a revert that mentions the reverted llvm-svn in the
53        # commit message.
54
55        # Commit which performed the revert
56        sha = self.rev_to_sha_with_round_trip(
57            git_llvm_rev.Rev(branch=git_llvm_rev.MAIN_BRANCH, number=374895)
58        )
59        self.assertEqual(sha, "1731fc88d1fa1fa55edd056db73a339b415dd5d6")
60
61        # Commit that was reverted
62        sha = self.rev_to_sha_with_round_trip(
63            git_llvm_rev.Rev(branch=git_llvm_rev.MAIN_BRANCH, number=374841)
64        )
65        self.assertEqual(sha, "2a1386c81de504b5bda44fbecf3f7b4cdfd748fc")
66
67    def test_imaginary_revs_raise(self) -> None:
68        with self.assertRaises(ValueError) as r:
69            git_llvm_rev.translate_rev_to_sha(
70                get_llvm_config(),
71                git_llvm_rev.Rev(
72                    branch=git_llvm_rev.MAIN_BRANCH, number=9999999
73                ),
74            )
75
76        self.assertIn("Try updating your tree?", str(r.exception))
77
78    def test_merge_commits_count_as_one_commit_crbug1041079(self) -> None:
79        # This CL merged _a lot_ of commits in. Verify a few hand-computed
80        # properties about it.
81        merge_sha_rev_number = 4496 + git_llvm_rev.base_llvm_revision
82        sha = self.rev_to_sha_with_round_trip(
83            git_llvm_rev.Rev(
84                branch=git_llvm_rev.MAIN_BRANCH, number=merge_sha_rev_number
85            )
86        )
87        self.assertEqual(sha, "0f0d0ed1c78f1a80139a1f2133fad5284691a121")
88
89        sha = self.rev_to_sha_with_round_trip(
90            git_llvm_rev.Rev(
91                branch=git_llvm_rev.MAIN_BRANCH, number=merge_sha_rev_number - 1
92            )
93        )
94        self.assertEqual(sha, "6f635f90929da9545dd696071a829a1a42f84b30")
95
96        sha = self.rev_to_sha_with_round_trip(
97            git_llvm_rev.Rev(
98                branch=git_llvm_rev.MAIN_BRANCH, number=merge_sha_rev_number + 1
99            )
100        )
101        self.assertEqual(sha, "199700a5cfeedf227619f966aa3125cef18bc958")
102
103    def test_known_rev_sha_pairs_are_sorted(self):
104        revs = [rev for rev, _ in git_llvm_rev.known_llvm_rev_sha_pairs]
105        self.assertEqual(revs, sorted(revs))
106
107    # NOTE: The below tests have _zz_ in their name as an optimization.
108    # Iterating on a quick test is painful when these larger tests come before
109    # it and take 7secs to run. Python's unittest module guarantees tests are
110    # run in alphabetical order by their method name, so...
111    #
112    # If you're wondering, the slow part is `git branch -r --contains`. I
113    # imagine it's going to be very cold code, so I'm not inclined to optimize
114    # it much.
115
116    def test_zz_non_base_rev_sha_pairs_are_correct(self):
117        base_rev_sha_pairs = git_llvm_rev.known_llvm_rev_sha_pairs
118
119        def restore_rev_sha_pairs():
120            git_llvm_rev.known_llvm_rev_sha_pairs = base_rev_sha_pairs
121
122        self.addCleanup(restore_rev_sha_pairs)
123        git_llvm_rev.known_llvm_rev_sha_pairs = (
124            (git_llvm_rev.base_llvm_revision, git_llvm_rev.base_llvm_sha),
125        )
126
127        for rev, cached_sha in base_rev_sha_pairs:
128            got_sha = self.rev_to_sha_with_round_trip(
129                git_llvm_rev.Rev(branch=git_llvm_rev.MAIN_BRANCH, number=rev)
130            )
131            self.assertEqual(cached_sha, got_sha)
132
133    def test_zz_branch_revs_work_after_merge_points_and_svn_cutoff(
134        self,
135    ) -> None:
136        # Arbitrary 9.x commit without an attached llvm-svn: value.
137        sha = self.rev_to_sha_with_round_trip(
138            git_llvm_rev.Rev(branch="upstream/release/9.x", number=366670)
139        )
140        self.assertEqual(sha, "4e858e4ac00b59f064da4e1f7e276916e7d296aa")
141
142    def test_zz_branch_revs_work_at_merge_points(self) -> None:
143        rev_number = 366426
144        backing_sha = "c89a3d78f43d81b9cff7b9248772ddf14d21b749"
145
146        sha = self.rev_to_sha_with_round_trip(
147            git_llvm_rev.Rev(branch=git_llvm_rev.MAIN_BRANCH, number=rev_number)
148        )
149        self.assertEqual(sha, backing_sha)
150
151        # Note that this won't round-trip: since this commit is on the main
152        # branch, we'll pick main for this. That's fine.
153        sha = git_llvm_rev.translate_rev_to_sha(
154            get_llvm_config(),
155            git_llvm_rev.Rev(branch="upstream/release/9.x", number=rev_number),
156        )
157        self.assertEqual(sha, backing_sha)
158
159    def test_zz_branch_revs_work_after_merge_points(self) -> None:
160        # Picking the commit on the 9.x branch after the merge-base for that +
161        # main. Note that this is where llvm-svn numbers should diverge from
162        # ours, and are therefore untrustworthy. The commit for this *does*
163        # have a different `llvm-svn:` string than we should have.
164        sha = self.rev_to_sha_with_round_trip(
165            git_llvm_rev.Rev(branch="upstream/release/9.x", number=366427)
166        )
167        self.assertEqual(sha, "2cf681a11aea459b50d712abc7136f7129e4d57f")
168
169
170# FIXME: When release/10.x happens, it may be nice to have a test-case
171# generally covering that, since it's the first branch that we have to travel
172# back to the base commit for.
173
174if __name__ == "__main__":
175    llvm_project.ensure_up_to_date()
176    unittest.main()
177