Path: blob/main_old/scripts/angle_presubmit_utils_unittest.py
1693 views
#!/usr/bin/env python1# Copyright 2020 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4"""5angle_presubmit_utils_unittest.py: Top-level unittest script for ANGLE presubmit checks.6"""78import imp9import os10import unittest11from angle_presubmit_utils import *121314def SetCWDToAngleFolder():15angle_folder = "angle"16cwd = os.path.dirname(os.path.abspath(__file__))17cwd = cwd.split(angle_folder)[0] + angle_folder18os.chdir(cwd)192021SetCWDToAngleFolder()2223PRESUBMIT = imp.load_source('PRESUBMIT', 'PRESUBMIT.py')242526class CommitMessageFormattingCheckTest(unittest.TestCase):2728def __init__(self, *args, **kwargs):29super(CommitMessageFormattingCheckTest, self).__init__(*args, **kwargs)30self.output_api = OutputAPI_mock()3132def run_check_commit_message_formatting(self, commit_msg):33input_api = InputAPI_mock(commit_msg)34return PRESUBMIT._CheckCommitMessageFormatting(input_api, self.output_api)3536def test_correct_commit_message(self):37commit_msg = """a3839b4041Bug: angleproject:466242Change-Id: I966c79d96175da9eee92ef6da20db50d488137b243"""44errors = self.run_check_commit_message_formatting(commit_msg)45self.assertEqual(len(errors), 0)4647def test_missing_description_body_and_description_summary(self):48commit_msg = """Change-Id: I966c79d96175da9eee92ef6da20db50d488137b2"""49errors = self.run_check_commit_message_formatting(commit_msg)50self.assertEqual(len(errors), 1)51self.assertEqual(52errors[0],53self.output_api.PresubmitError(54"Commit 1:Please ensure that your" +55" description summary and description body are not blank."))5657def test_missing_description_body(self):58commit_msg = """59a6061b: d62c: e63"""64errors = self.run_check_commit_message_formatting(commit_msg)65self.assertEqual(len(errors), 0)6667def test_missing_tag_paragraph(self):68commit_msg = """a6970bd71efgh"""72errors = self.run_check_commit_message_formatting(commit_msg)73self.assertEqual(len(errors), 1)74self.assertEqual(75errors[0],76self.output_api.PresubmitError(77"Commit 1:Please ensure that there are tags (e.g., Bug:, Test:) in your description."78))7980def test_missing_tag_paragraph_and_description_body(self):81commit_msg = "a"82errors = self.run_check_commit_message_formatting(commit_msg)83self.assertEqual(len(errors), 1)84self.assertEqual(85errors[0],86self.output_api.PresubmitError(87"Commit 1:Please ensure that there are tags (e.g., Bug:, Test:) in your description."88))8990def test_missing_blank_line_between_description_summary_and_description_body(self):91commit_msg = """a92b9394Change-Id: I925cdb45779a9cdebe4e14f9e81e4211ade37c12"""95errors = self.run_check_commit_message_formatting(commit_msg)96self.assertEqual(len(errors), 1)97self.assertEqual(errors[0], self.output_api.PresubmitError(98"Commit 1:Please ensure the summary is only 1 line and there is 1 blank line" + \99" between the summary and description body."))100101def test_missing_blank_line_between_description_body_and_tags_paragraph(self):102commit_msg = """a103104b105Change-Id: I925cdb45779a9cdebe4e14f9e81e4211ade37c12"""106errors = self.run_check_commit_message_formatting(commit_msg)107self.assertEqual(len(errors), 0)108109def test_multiple_blank_lines_before_and_after_commit_message(self):110commit_msg = """111112113a114115b116117Change-Id: I925cdb45779a9cdebe4e14f9e81e4211ade37c12118"""119errors = self.run_check_commit_message_formatting(commit_msg)120self.assertEqual(len(errors), 0)121122def test_newlines_within_description_body(self):123commit_msg = """a124125b126127d128129e130131for132133Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b"""134errors = self.run_check_commit_message_formatting(commit_msg)135self.assertEqual(len(errors), 0)136137# Summary description in warning threshold(at 65 characters)138def test_summmary_description_in_warning_thresholds(self):139commit_msg = """aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa140141b142143Change-Id: I925cdb45779a9cdebe4e14f9e81e4211ade37c12144"""145errors = self.run_check_commit_message_formatting(commit_msg)146self.assertEqual(len(errors), 1)147self.assertEqual(148errors[0],149self.output_api.PresubmitPromptWarning(150"Commit 1:Your description summary should be on one line of 64 or less characters."151))152153# Summary description in error threshold(at 71 characters)154def test_summary_description_in_error_threshold(self):155commit_msg = """aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa156157b158159Change-Id: I925cdb45779a9cdebe4e14f9e81e4211ade37c12"""160errors = self.run_check_commit_message_formatting(commit_msg)161self.assertEqual(len(errors), 1)162self.assertEqual(163errors[0],164self.output_api.PresubmitError(165"Commit 1:Please ensure that your description summary is on one line of 64 or less characters."166))167168def test_description_body_exceeds_line_count_limit(self):169commit_msg = """a170171bbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb172173174Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b"""175errors = self.run_check_commit_message_formatting(commit_msg)176self.assertEqual(len(errors), 2)177self.assertEqual(178errors[0],179self.output_api.PresubmitError(180"Commit 1:Please ensure that there exists only 1 blank line between tags and description body."181))182self.assertEqual(183errors[1],184self.output_api.PresubmitError("""Commit 1:Line 3 is too long.185"bbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"186Please wrap it to 72 characters. Lines without spaces or lines starting with 4 spaces are exempt."""187))188189def test_description_body_exceeds_line_count_limit_but_with_4_spaces_prefix(self):190commit_msg = """a191192cc193194dddd195196bbbbbbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb197198Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b"""199errors = self.run_check_commit_message_formatting(commit_msg)200self.assertEqual(len(errors), 0)201202def test_description_body_exceeds_line_count_limit_but_without_space(self):203commit_msg = """a204205cc206207dddd208209bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb210211a: d"""212errors = self.run_check_commit_message_formatting(commit_msg)213self.assertEqual(len(errors), 0)214215def test_tabs_in_commit_message(self):216commit_msg = """ a217218bbbbbbbbbbbbbbbbbbbb219220Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b"""221errors = self.run_check_commit_message_formatting(commit_msg)222self.assertEqual(len(errors), 1)223self.assertEqual(224errors[0],225self.output_api.PresubmitError("Commit 1:Tabs are not allowed in commit message."))226227def test_allowlist_revert(self):228commit_msg = """Revert "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssa229230bbbbbbbbbbbbbbbbbbbb231232Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b"""233errors = self.run_check_commit_message_formatting(commit_msg)234self.assertEqual(len(errors), 0)235236def test_allowlist_roll(self):237commit_msg = """Roll sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssadd238239bbbbbbbbbbbbbbbbbbbb240241Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b"""242errors = self.run_check_commit_message_formatting(commit_msg)243self.assertEqual(len(errors), 0)244245def test_allowlist_reland(self):246commit_msg = """Reland sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssadd247248bbbbbbbbbbbbbbbbbbbb249250Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b"""251errors = self.run_check_commit_message_formatting(commit_msg)252self.assertEqual(len(errors), 0)253254def test_multiple_commits_with_errors_in_multiple_commits(self):255commit_msg = """a256257bbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb258259Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b260261a262263cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccccccccccccccc264265Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b"""266errors = self.run_check_commit_message_formatting(commit_msg)267self.assertEqual(len(errors), 2)268self.assertEqual(269errors[0],270self.output_api.PresubmitError("""Commit 2:Line 3 is too long.271"bbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"272Please wrap it to 72 characters. Lines without spaces or lines starting with 4 spaces are exempt."""273))274self.assertEqual(275errors[1],276self.output_api.PresubmitError("""Commit 1:Line 4 is too long.277"cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccccccccccccccc"278Please wrap it to 72 characters. Lines without spaces or lines starting with 4 spaces are exempt."""279))280281def test_multiple_commits_with_error_in_one_commit(self):282commit_msg = """a283284bbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb285286Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b287288Roll sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssadd289290bbbbbbbbbbbbbbbbbbbb291292Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b"""293errors = self.run_check_commit_message_formatting(commit_msg)294self.assertEqual(len(errors), 1)295self.assertEqual(296errors[0],297self.output_api.PresubmitError("""Commit 2:Line 3 is too long.298"bbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"299Please wrap it to 72 characters. Lines without spaces or lines starting with 4 spaces are exempt."""300))301302def test_multiple_commits_with_no_error(self):303commit_msg = """Reland sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssadd304305bbbbbbbbbbbbbbbbbbbb306307Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b308309Roll sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssadd310311bbbbbbbbbbbbbbbbbbbb312313Change-Id: I443c36aaa8956c20da1abddf7aea613659e2cd5b"""314errors = self.run_check_commit_message_formatting(commit_msg)315self.assertEqual(len(errors), 0)316317318if __name__ == '__main__':319unittest.main()320321322