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