1 #!/usr/bin/python 2 # -*- coding:utf-8 -*- 3 # Copyright 2016 The Android Open Source Project 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 """Unittests for the config module.""" 18 19 from __future__ import print_function 20 21 import os 22 import shutil 23 import sys 24 import tempfile 25 import unittest 26 27 _path = os.path.realpath(__file__ + '/../..') 28 if sys.path[0] != _path: 29 sys.path.insert(0, _path) 30 del _path 31 32 import rh.hooks 33 import rh.config 34 35 36 class PreSubmitConfigTests(unittest.TestCase): 37 """Tests for the PreSubmitConfig class.""" 38 39 def setUp(self): 40 self.tempdir = tempfile.mkdtemp() 41 42 def tearDown(self): 43 shutil.rmtree(self.tempdir) 44 45 def _write_config(self, data, filename=None): 46 """Helper to write out a config file for testing.""" 47 if filename is None: 48 filename = rh.config.PreSubmitConfig.FILENAME 49 path = os.path.join(self.tempdir, filename) 50 with open(path, 'w') as fp: 51 fp.write(data) 52 53 def _write_global_config(self, data): 54 """Helper to write out a global config file for testing.""" 55 self._write_config( 56 data, filename=rh.config.PreSubmitConfig.GLOBAL_FILENAME) 57 58 def testMissing(self): 59 """Instantiating a non-existent config file should be fine.""" 60 rh.config.PreSubmitConfig() 61 62 def testEmpty(self): 63 """Instantiating an empty config file should be fine.""" 64 self._write_config('') 65 rh.config.PreSubmitConfig(paths=(self.tempdir,)) 66 67 def testValid(self): 68 """Verify a fully valid file works.""" 69 self._write_config("""# This be a comment me matey. 70 [Hook Scripts] 71 name = script --with "some args" 72 73 [Builtin Hooks] 74 cpplint = true 75 76 [Builtin Hooks Options] 77 cpplint = --some 'more args' 78 79 [Options] 80 ignore_merged_commits = true 81 """) 82 rh.config.PreSubmitConfig(paths=(self.tempdir,)) 83 84 def testUnknownSection(self): 85 """Reject unknown sections.""" 86 self._write_config('[BOOGA]') 87 self.assertRaises(rh.config.ValidationError, rh.config.PreSubmitConfig, 88 paths=(self.tempdir,)) 89 90 def testUnknownBuiltin(self): 91 """Reject unknown builtin hooks.""" 92 self._write_config('[Builtin Hooks]\nbooga = borg!') 93 self.assertRaises(rh.config.ValidationError, rh.config.PreSubmitConfig, 94 paths=(self.tempdir,)) 95 96 def testEmptyCustomHook(self): 97 """Reject empty custom hooks.""" 98 self._write_config('[Hook Scripts]\nbooga = \t \n') 99 self.assertRaises(rh.config.ValidationError, rh.config.PreSubmitConfig, 100 paths=(self.tempdir,)) 101 102 def testInvalidIni(self): 103 """Reject invalid ini files.""" 104 self._write_config('[Hook Scripts]\n =') 105 self.assertRaises(rh.config.ValidationError, rh.config.PreSubmitConfig, 106 paths=(self.tempdir,)) 107 108 def testInvalidString(self): 109 """Catch invalid string quoting.""" 110 self._write_config("""[Hook Scripts] 111 name = script --'bad-quotes 112 """) 113 self.assertRaises(rh.config.ValidationError, rh.config.PreSubmitConfig, 114 paths=(self.tempdir,)) 115 116 def testGlobalConfigs(self): 117 """Verify global configs stack properly.""" 118 self._write_global_config("""[Builtin Hooks] 119 commit_msg_bug_field = true 120 commit_msg_changeid_field = true 121 commit_msg_test_field = false""") 122 self._write_config("""[Builtin Hooks] 123 commit_msg_bug_field = false 124 commit_msg_test_field = true""") 125 config = rh.config.PreSubmitConfig(paths=(self.tempdir,), 126 global_paths=(self.tempdir,)) 127 self.assertEqual(config.builtin_hooks, 128 ['commit_msg_changeid_field', 'commit_msg_test_field']) 129 130 131 if __name__ == '__main__': 132 unittest.main() 133