llvm-project
133 строки · 4.4 Кб
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# ===----------------------------------------------------------------------===##
4#
5# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6# See https://llvm.org/LICENSE.txt for license information.
7# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8#
9# ===----------------------------------------------------------------------===##
10"""Tests for revert_checker.
11
12Note that these tests require having LLVM's git history available, since our
13repository has a few interesting instances of edge-cases.
14"""
15
16import os
17import logging
18import unittest
19from typing import List
20
21import revert_checker
22
23# pylint: disable=protected-access
24
25
26def get_llvm_project_path() -> str:
27"""Returns the path to llvm-project's root."""
28my_dir = os.path.dirname(__file__)
29return os.path.realpath(os.path.join(my_dir, "..", ".."))
30
31
32class _SilencingFilter(logging.Filter):
33"""Silences all log messages.
34
35Also collects info about log messages that would've been emitted.
36"""
37
38def __init__(self) -> None:
39self.messages: List[str] = []
40
41def filter(self, record: logging.LogRecord) -> bool:
42self.messages.append(record.getMessage())
43return False
44
45
46class Test(unittest.TestCase):
47"""Tests for revert_checker."""
48
49def silence_logging(self) -> _SilencingFilter:
50root = logging.getLogger()
51filt = _SilencingFilter()
52root.addFilter(filt)
53self.addCleanup(root.removeFilter, filt)
54return filt
55
56def test_log_stream_with_known_sha_range(self) -> None:
57start_sha = "e241573d5972d34a323fa5c64774c4207340beb3"
58end_sha = "a7a37517751ffb0f5529011b4ba96e67fcb27510"
59commits = [
60revert_checker._LogEntry(
61"e241573d5972d34a323fa5c64774c4207340beb3",
62"\n".join(
63(
64"[mlir] NFC: remove IntegerValueSet / MutableIntegerSet",
65"",
66"Summary:",
67"- these are unused and really not needed now given flat "
68"affine",
69" constraints",
70"",
71"Differential Revision: https://reviews.llvm.org/D75792",
72)
73),
74),
75revert_checker._LogEntry(
76"97572fa6e9daecd648873496fd11f7d1e25a55f0",
77"[NFC] use hasAnyOperatorName and hasAnyOverloadedOperatorName "
78"functions in clang-tidy matchers",
79),
80]
81
82logs = list(
83revert_checker._log_stream(
84get_llvm_project_path(),
85root_sha=start_sha,
86end_at_sha=end_sha,
87)
88)
89self.assertEqual(commits, logs)
90
91def test_reverted_noncommit_object_is_a_nop(self) -> None:
92log_filter = self.silence_logging()
93# c9944df916e41b1014dff5f6f75d52297b48ecdc mentions reverting a non-commit
94# object. It sits between the given base_ref and root.
95reverts = revert_checker.find_reverts(
96git_dir=get_llvm_project_path(),
97across_ref="c9944df916e41b1014dff5f6f75d52297b48ecdc~",
98root="c9944df916e41b1014dff5f6f75d52297b48ecdc",
99)
100self.assertEqual(reverts, [])
101
102complaint = (
103"Failed to resolve reverted object "
104"edd18355be574122aaa9abf58c15d8c50fb085a1"
105)
106self.assertTrue(
107any(x.startswith(complaint) for x in log_filter.messages),
108log_filter.messages,
109)
110
111def test_known_reverts_across_arbitrary_llvm_rev(self) -> None:
112reverts = revert_checker.find_reverts(
113git_dir=get_llvm_project_path(),
114across_ref="c47f971694be0159ffddfee8a75ae515eba91439",
115root="9f981e9adf9c8d29bb80306daf08d2770263ade6",
116)
117self.assertEqual(
118reverts,
119[
120revert_checker.Revert(
121sha="4e0fe038f438ae1679eae9e156e1f248595b2373",
122reverted_sha="65b21282c710afe9c275778820c6e3c1cf46734b",
123),
124revert_checker.Revert(
125sha="9f981e9adf9c8d29bb80306daf08d2770263ade6",
126reverted_sha="4060016fce3e6a0b926ee9fc59e440a612d3a2ec",
127),
128],
129)
130
131
132if __name__ == "__main__":
133unittest.main()
134