| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Unit tests for verification/presubmit_check.py.""" | |
| 7 | |
| 8 import logging | |
| 9 import os | |
| 10 import sys | |
| 11 import unittest | |
| 12 | |
| 13 ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| 14 sys.path.insert(0, os.path.join(ROOT_DIR, '..')) | |
| 15 | |
| 16 from verification import base | |
| 17 from verification import presubmit_check | |
| 18 | |
| 19 import find_depot_tools # pylint: disable=W0611 | |
| 20 from testing_support import trial_dir | |
| 21 | |
| 22 # From tests/ | |
| 23 import mocks | |
| 24 | |
| 25 | |
| 26 class PresubmitTest(mocks.TestCase, trial_dir.TrialDirMixIn): | |
| 27 def setUp(self): | |
| 28 mocks.TestCase.setUp(self) | |
| 29 trial_dir.TrialDirMixIn.setUp(self) | |
| 30 | |
| 31 # The presubmit check assumes PWD is set accordingly. | |
| 32 self._old_cwd = os.getcwd() | |
| 33 os.chdir(self.root_dir) | |
| 34 with open(os.path.join(self.root_dir, 'hello.txt'), 'wb') as f: | |
| 35 f.write('allo') | |
| 36 self.pending.files = ['hello.txt'] | |
| 37 | |
| 38 def tearDown(self): | |
| 39 try: | |
| 40 os.chdir(self._old_cwd) | |
| 41 trial_dir.TrialDirMixIn.tearDown(self) | |
| 42 finally: | |
| 43 mocks.TestCase.tearDown(self) | |
| 44 | |
| 45 def _presubmit(self, content): | |
| 46 # Creates the presubmit check. | |
| 47 with open(os.path.join(self.root_dir, 'PRESUBMIT.py'), 'wb') as f: | |
| 48 f.write(content) | |
| 49 | |
| 50 def testPresubmitBuggy(self): | |
| 51 self._presubmit('symbol_not_defined\n') | |
| 52 self._check(error_message='symbol_not_defined') | |
| 53 | |
| 54 def testPresubmitHangs(self): | |
| 55 self._presubmit('import time\ntime.sleep(5)') | |
| 56 self._check(error_message='The presubmit check was hung.', expiration=0.2) | |
| 57 | |
| 58 def testSuccess(self): | |
| 59 self._presubmit('') | |
| 60 self._check() | |
| 61 | |
| 62 def testSuccessNotify(self): | |
| 63 self._presubmit( | |
| 64 'def CheckChangeOnCommit(input_api, output_api):\n' | |
| 65 ' return [output_api.PresubmitNotifyResult("There is no problem")]\n') | |
| 66 self._check() | |
| 67 | |
| 68 def testFailWarning(self): | |
| 69 self._presubmit( | |
| 70 'def CheckChangeOnCommit(input_api, output_api):\n' | |
| 71 ' return [output_api.PresubmitPromptWarning(\n' | |
| 72 ' "There is some problems")]\n') | |
| 73 self._check(error_message='There is some problems\n') | |
| 74 | |
| 75 def testFailError(self): | |
| 76 self._presubmit( | |
| 77 'def CheckChangeOnCommit(input_api, output_api):\n' | |
| 78 ' return [output_api.PresubmitError("Die die please die")]\n') | |
| 79 self._check(error_message='Die die please die') | |
| 80 | |
| 81 def _check(self, error_message=None, expiration=None): | |
| 82 # checkout is not used yet. To be used to get the list of modified files. | |
| 83 ver = presubmit_check.PresubmitCheckVerifier(self.context) | |
| 84 if expiration: | |
| 85 ver.execution_timeout = expiration | |
| 86 ver.verify(self.pending) | |
| 87 ver.update_status(None) | |
| 88 name = presubmit_check.PresubmitCheckVerifier.name | |
| 89 self.assertEqual(self.pending.verifications.keys(), [name]) | |
| 90 if error_message: | |
| 91 self.assertIn( | |
| 92 error_message, self.pending.verifications[name].error_message) | |
| 93 self.assertEqual( | |
| 94 self.pending.verifications[name].get_state(), base.FAILED) | |
| 95 self.assertIn(error_message, self.pending.error_message()) | |
| 96 else: | |
| 97 self.assertEqual(None, self.pending.verifications[name].error_message) | |
| 98 self.assertEqual( | |
| 99 self.pending.verifications[name].get_state(), base.SUCCEEDED) | |
| 100 self.assertEqual('', self.pending.error_message()) | |
| 101 self.context.status.check_names(['presubmit'] * 2) | |
| 102 | |
| 103 def testPresubmitTryJob(self): | |
| 104 self._presubmit( | |
| 105 'def CheckChangeOnCommit(input_api, output_api):\n' | |
| 106 ' out = input_api.canned_checks.CheckRietveldTryJobExecution(\n' | |
| 107 ' 1, 2, 3, 4, 5, 6, absurd=True)\n' | |
| 108 ' assert [] == out\n' | |
| 109 ' return out\n') | |
| 110 self._check() | |
| 111 | |
| 112 def testPresubmitTreeOpen(self): | |
| 113 self._presubmit( | |
| 114 'def CheckChangeOnCommit(input_api, output_api):\n' | |
| 115 ' out = input_api.canned_checks.CheckTreeIsOpen(\n' | |
| 116 ' 1, 2, 3, 4, 5, 6, absurd=True)\n' | |
| 117 ' assert [] == out\n' | |
| 118 ' return out\n') | |
| 119 self._check() | |
| 120 | |
| 121 def testPresubmitPendingBuilds(self): | |
| 122 self._presubmit( | |
| 123 'def CheckChangeOnCommit(input_api, output_api):\n' | |
| 124 ' out = input_api.canned_checks.CheckBuildbotPendingBuilds(\n' | |
| 125 ' 1, 2, 3, 4, 5, 6, absurd=True)\n' | |
| 126 ' assert [] == out\n' | |
| 127 ' return out\n') | |
| 128 self._check() | |
| 129 | |
| 130 def testPresubmitRietveld(self): | |
| 131 self._presubmit( | |
| 132 ('def CheckChangeOnCommit(input_api, output_api):\n' | |
| 133 ' out = []\n' | |
| 134 ' if input_api.rietveld.email != %r:\n' | |
| 135 ' out.append(output_api.PresubmitError(\n' | |
| 136 ' "email: %%r" %% input_api.rietveld.email))\n' | |
| 137 # TODO(maruel): Bad! Remove me. | |
| 138 ' if input_api.rietveld.password != %r:\n' | |
| 139 ' out.append(output_api.PresubmitError(\n' | |
| 140 ' "password: %%r" %% input_api.rietveld.password))\n' | |
| 141 ' if input_api.rietveld.url != %r:\n' | |
| 142 ' out.append(output_api.PresubmitError(\n' | |
| 143 ' "url: %%r" %% input_api.rietveld.url))\n' | |
| 144 ' return out\n') % ( | |
| 145 self.context.rietveld.email, | |
| 146 self.context.rietveld.password, | |
| 147 self.context.rietveld.url)) | |
| 148 | |
| 149 self._check() | |
| 150 | |
| 151 def testPresubmitNoFiles(self): | |
| 152 self.pending.files = [] | |
| 153 self._presubmit( | |
| 154 'def CheckChangeOnCommit(input_api, output_api):\n' | |
| 155 ' return []\n') | |
| 156 # TODO(maruel): Would make sense to have a more helpful error message. | |
| 157 self._check( | |
| 158 'Presubmit check for 42-23 failed and returned exit status 2.\n\n' | |
| 159 'Usage: presubmit_shim.py [options] <files...>\n\n' | |
| 160 'presubmit_shim.py: error: For unversioned directory, <files> is not ' | |
| 161 'optional.\n') | |
| 162 | |
| 163 | |
| 164 if __name__ == '__main__': | |
| 165 if '-v' in sys.argv: | |
| 166 logging.basicConfig(level=logging.DEBUG) | |
| 167 else: | |
| 168 logging.basicConfig(level=logging.ERROR) | |
| 169 unittest.main() | |
| OLD | NEW |