1import mock 2import unittest 3 4import presubmit 5 6 7class TestShouldSkipBuild(unittest.TestCase): 8 @mock.patch('presubmit.contains_bionicbb') 9 @mock.patch('presubmit.contains_cleanspec') 10 @mock.patch('gerrit.get_commit') 11 def test_accepts_googlers(self, mock_commit, *other_checks): 12 mock_commit.return_value = { 13 'committer': {'email': 'googler@google.com'} 14 } 15 16 for other_check in other_checks: 17 other_check.return_value = False 18 19 for message_type in ('newchange', 'newpatchset', 'comment'): 20 self.assertFalse(presubmit.should_skip_build({ 21 'MessageType': message_type, 22 'Change-Id': '', 23 'PatchSet': '', 24 })) 25 26 @mock.patch('presubmit.contains_bionicbb') 27 @mock.patch('presubmit.contains_cleanspec') 28 @mock.patch('gerrit.get_commit') 29 def test_rejects_googlish_domains(self, mock_commit, *other_checks): 30 mock_commit.return_value = { 31 'committer': {'email': 'fakegoogler@google.com.fake.com'} 32 } 33 34 for other_check in other_checks: 35 other_check.return_value = False 36 37 for message_type in ('newchange', 'newpatchset', 'comment'): 38 self.assertTrue(presubmit.should_skip_build({ 39 'MessageType': message_type, 40 'Change-Id': '', 41 'PatchSet': '', 42 })) 43 44 @mock.patch('presubmit.contains_bionicbb') 45 @mock.patch('presubmit.contains_cleanspec') 46 @mock.patch('gerrit.get_commit') 47 def test_rejects_non_googlers(self, mock_commit, *other_checks): 48 mock_commit.return_value = { 49 'committer': {'email': 'johndoe@example.com'} 50 } 51 52 for other_check in other_checks: 53 other_check.return_value = False 54 55 for message_type in ('newchange', 'newpatchset', 'comment'): 56 self.assertTrue(presubmit.should_skip_build({ 57 'MessageType': message_type, 58 'Change-Id': '', 59 'PatchSet': '', 60 })) 61 62 @mock.patch('presubmit.contains_bionicbb') 63 @mock.patch('presubmit.is_untrusted_committer') 64 @mock.patch('gerrit.get_files_for_revision') 65 def test_skips_cleanspecs(self, mock_files, *other_checks): 66 mock_files.return_value = ['foo/CleanSpec.mk'] 67 for other_check in other_checks: 68 other_check.return_value = False 69 70 for message_type in ('newchange', 'newpatchset', 'comment'): 71 self.assertTrue(presubmit.should_skip_build({ 72 'MessageType': message_type, 73 'Change-Id': '', 74 'PatchSet': '', 75 })) 76 77 @mock.patch('presubmit.contains_cleanspec') 78 @mock.patch('presubmit.is_untrusted_committer') 79 @mock.patch('gerrit.get_files_for_revision') 80 def test_skips_bionicbb(self, mock_files, *other_checks): 81 mock_files.return_value = ['tools/bionicbb/common.sh'] 82 for other_check in other_checks: 83 other_check.return_value = False 84 85 for message_type in ('newchange', 'newpatchset', 'comment'): 86 self.assertTrue(presubmit.should_skip_build({ 87 'MessageType': message_type, 88 'Change-Id': '', 89 'PatchSet': '', 90 })) 91 92 93if __name__ == '__main__': 94 unittest.main() 95