Mike Frysinger | 0ac3ce4 | 2016-09-26 11:41:16 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 2 | # Copyright 2016 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | """Unittests for the config module.""" |
| 17 | |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 18 | import os |
| 19 | import shutil |
| 20 | import sys |
| 21 | import tempfile |
| 22 | import unittest |
| 23 | |
| 24 | _path = os.path.realpath(__file__ + '/../..') |
| 25 | if sys.path[0] != _path: |
| 26 | sys.path.insert(0, _path) |
| 27 | del _path |
| 28 | |
Mike Frysinger | 2ef213c | 2017-11-10 15:41:56 -0500 | [diff] [blame] | 29 | # We have to import our local modules after the sys.path tweak. We can't use |
| 30 | # relative imports because this is an executable program, not a module. |
| 31 | # pylint: disable=wrong-import-position |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 32 | import rh.hooks |
| 33 | import rh.config |
| 34 | |
| 35 | |
Mike Frysinger | 828a0ee | 2019-08-05 17:42:04 -0400 | [diff] [blame] | 36 | class PreUploadConfigTests(unittest.TestCase): |
| 37 | """Tests for the PreUploadConfig class.""" |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 38 | |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 39 | def testMissing(self): |
| 40 | """Instantiating a non-existent config file should be fine.""" |
| 41 | rh.config.PreUploadConfig() |
| 42 | |
| 43 | |
Thiébaud Weksteen | 324296b | 2020-08-28 16:32:07 +0200 | [diff] [blame] | 44 | class FileTestCase(unittest.TestCase): |
| 45 | """Helper class for tests cases to setup configuration files.""" |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 46 | |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 47 | def setUp(self): |
| 48 | self.tempdir = tempfile.mkdtemp() |
| 49 | |
| 50 | def tearDown(self): |
| 51 | shutil.rmtree(self.tempdir) |
| 52 | |
Thiébaud Weksteen | 324296b | 2020-08-28 16:32:07 +0200 | [diff] [blame] | 53 | def _write_config(self, data, filename='temp.cfg'): |
| 54 | """Helper to write out a config file for testing. |
| 55 | |
| 56 | Returns: |
| 57 | Path to the file where the configuration was written. |
| 58 | """ |
| 59 | path = os.path.join(self.tempdir, filename) |
Mike Frysinger | 0522922 | 2022-04-28 00:45:01 -0400 | [diff] [blame] | 60 | with open(path, 'w', encoding='utf-8') as fp: |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 61 | fp.write(data) |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 62 | return path |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 63 | |
Thiébaud Weksteen | 324296b | 2020-08-28 16:32:07 +0200 | [diff] [blame] | 64 | def _write_local_config(self, data): |
| 65 | """Helper to write out a local config file for testing.""" |
| 66 | return self._write_config( |
| 67 | data, filename=rh.config.LocalPreUploadFile.FILENAME) |
| 68 | |
| 69 | def _write_global_config(self, data): |
| 70 | """Helper to write out a global config file for testing.""" |
| 71 | return self._write_config( |
| 72 | data, filename=rh.config.GlobalPreUploadFile.FILENAME) |
| 73 | |
| 74 | |
| 75 | class PreUploadFileTests(FileTestCase): |
| 76 | """Tests for the PreUploadFile class.""" |
| 77 | |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 78 | def testEmpty(self): |
| 79 | """Instantiating an empty config file should be fine.""" |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 80 | path = self._write_config('') |
| 81 | rh.config.PreUploadFile(path) |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 82 | |
| 83 | def testValid(self): |
| 84 | """Verify a fully valid file works.""" |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 85 | path = self._write_config("""# This be a comment me matey. |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 86 | [Hook Scripts] |
| 87 | name = script --with "some args" |
| 88 | |
| 89 | [Builtin Hooks] |
| 90 | cpplint = true |
| 91 | |
| 92 | [Builtin Hooks Options] |
| 93 | cpplint = --some 'more args' |
Luis Hector Chavez | 5c4c293 | 2016-10-16 21:56:58 -0700 | [diff] [blame] | 94 | |
| 95 | [Options] |
| 96 | ignore_merged_commits = true |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 97 | """) |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 98 | rh.config.PreUploadFile(path) |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 99 | |
| 100 | def testUnknownSection(self): |
| 101 | """Reject unknown sections.""" |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 102 | path = self._write_config('[BOOGA]') |
| 103 | self.assertRaises(rh.config.ValidationError, rh.config.PreUploadFile, |
| 104 | path) |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 105 | |
| 106 | def testUnknownBuiltin(self): |
| 107 | """Reject unknown builtin hooks.""" |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 108 | path = self._write_config('[Builtin Hooks]\nbooga = borg!') |
| 109 | self.assertRaises(rh.config.ValidationError, rh.config.PreUploadFile, |
| 110 | path) |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 111 | |
| 112 | def testEmptyCustomHook(self): |
| 113 | """Reject empty custom hooks.""" |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 114 | path = self._write_config('[Hook Scripts]\nbooga = \t \n') |
| 115 | self.assertRaises(rh.config.ValidationError, rh.config.PreUploadFile, |
| 116 | path) |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 117 | |
| 118 | def testInvalidIni(self): |
| 119 | """Reject invalid ini files.""" |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 120 | path = self._write_config('[Hook Scripts]\n =') |
| 121 | self.assertRaises(rh.config.ValidationError, rh.config.PreUploadFile, |
| 122 | path) |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 123 | |
| 124 | def testInvalidString(self): |
| 125 | """Catch invalid string quoting.""" |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 126 | path = self._write_config("""[Hook Scripts] |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 127 | name = script --'bad-quotes |
| 128 | """) |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 129 | self.assertRaises(rh.config.ValidationError, rh.config.PreUploadFile, |
| 130 | path) |
| 131 | |
| 132 | |
Thiébaud Weksteen | ea52820 | 2020-08-28 15:54:29 +0200 | [diff] [blame] | 133 | class LocalPreUploadFileTests(FileTestCase): |
| 134 | """Test for the LocalPreUploadFile class.""" |
| 135 | |
| 136 | def testInvalidSectionConfig(self): |
| 137 | """Reject local config that uses invalid sections.""" |
| 138 | path = self._write_config("""[Builtin Hooks Exclude Paths] |
| 139 | cpplint = external/ 'test directory' ^vendor/(?!google/) |
| 140 | """) |
| 141 | self.assertRaises(rh.config.ValidationError, |
| 142 | rh.config.LocalPreUploadFile, |
| 143 | path) |
| 144 | |
| 145 | |
Thiébaud Weksteen | 324296b | 2020-08-28 16:32:07 +0200 | [diff] [blame] | 146 | class PreUploadSettingsTests(FileTestCase): |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 147 | """Tests for the PreUploadSettings class.""" |
| 148 | |
Mike Frysinger | ca79770 | 2016-09-03 02:00:55 -0400 | [diff] [blame] | 149 | def testGlobalConfigs(self): |
| 150 | """Verify global configs stack properly.""" |
| 151 | self._write_global_config("""[Builtin Hooks] |
| 152 | commit_msg_bug_field = true |
| 153 | commit_msg_changeid_field = true |
| 154 | commit_msg_test_field = false""") |
Thiébaud Weksteen | 324296b | 2020-08-28 16:32:07 +0200 | [diff] [blame] | 155 | self._write_local_config("""[Builtin Hooks] |
Mike Frysinger | ca79770 | 2016-09-03 02:00:55 -0400 | [diff] [blame] | 156 | commit_msg_bug_field = false |
| 157 | commit_msg_test_field = true""") |
Mike Frysinger | 1baec12 | 2020-08-25 00:27:52 -0400 | [diff] [blame] | 158 | config = rh.config.PreUploadSettings(paths=(self.tempdir,), |
| 159 | global_paths=(self.tempdir,)) |
Mike Frysinger | ca79770 | 2016-09-03 02:00:55 -0400 | [diff] [blame] | 160 | self.assertEqual(config.builtin_hooks, |
| 161 | ['commit_msg_changeid_field', 'commit_msg_test_field']) |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 162 | |
Thiébaud Weksteen | ea52820 | 2020-08-28 15:54:29 +0200 | [diff] [blame] | 163 | def testGlobalExcludeScope(self): |
| 164 | """Verify exclude scope is valid for global config.""" |
| 165 | self._write_global_config("""[Builtin Hooks Exclude Paths] |
| 166 | cpplint = external/ 'test directory' ^vendor/(?!google/) |
| 167 | """) |
| 168 | rh.config.PreUploadSettings(global_paths=(self.tempdir,)) |
| 169 | |
Mike Frysinger | 2e65c54 | 2016-03-08 16:17:00 -0500 | [diff] [blame] | 170 | |
| 171 | if __name__ == '__main__': |
| 172 | unittest.main() |