blob: 475dc22933cb8a95c623e1244c063c965db4d260 [file] [log] [blame]
Mike Frysinger0ac3ce42016-09-26 11:41:16 -04001#!/usr/bin/env python3
Mike Frysinger2e65c542016-03-08 16:17:00 -05002# 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 Frysinger2e65c542016-03-08 16:17:00 -050018import os
19import shutil
20import sys
21import tempfile
22import unittest
23
24_path = os.path.realpath(__file__ + '/../..')
25if sys.path[0] != _path:
26 sys.path.insert(0, _path)
27del _path
28
Mike Frysinger2ef213c2017-11-10 15:41:56 -050029# 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 Frysinger2e65c542016-03-08 16:17:00 -050032import rh.hooks
33import rh.config
34
35
Mike Frysinger828a0ee2019-08-05 17:42:04 -040036class PreUploadConfigTests(unittest.TestCase):
37 """Tests for the PreUploadConfig class."""
Mike Frysinger2e65c542016-03-08 16:17:00 -050038
Mike Frysinger1baec122020-08-25 00:27:52 -040039 def testMissing(self):
40 """Instantiating a non-existent config file should be fine."""
41 rh.config.PreUploadConfig()
42
43
Thiébaud Weksteen324296b2020-08-28 16:32:07 +020044class FileTestCase(unittest.TestCase):
45 """Helper class for tests cases to setup configuration files."""
Mike Frysinger1baec122020-08-25 00:27:52 -040046
Mike Frysinger2e65c542016-03-08 16:17:00 -050047 def setUp(self):
48 self.tempdir = tempfile.mkdtemp()
49
50 def tearDown(self):
51 shutil.rmtree(self.tempdir)
52
Thiébaud Weksteen324296b2020-08-28 16:32:07 +020053 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 Frysinger05229222022-04-28 00:45:01 -040060 with open(path, 'w', encoding='utf-8') as fp:
Mike Frysinger2e65c542016-03-08 16:17:00 -050061 fp.write(data)
Mike Frysinger1baec122020-08-25 00:27:52 -040062 return path
Mike Frysinger2e65c542016-03-08 16:17:00 -050063
Thiébaud Weksteen324296b2020-08-28 16:32:07 +020064 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
75class PreUploadFileTests(FileTestCase):
76 """Tests for the PreUploadFile class."""
77
Mike Frysinger2e65c542016-03-08 16:17:00 -050078 def testEmpty(self):
79 """Instantiating an empty config file should be fine."""
Mike Frysinger1baec122020-08-25 00:27:52 -040080 path = self._write_config('')
81 rh.config.PreUploadFile(path)
Mike Frysinger2e65c542016-03-08 16:17:00 -050082
83 def testValid(self):
84 """Verify a fully valid file works."""
Mike Frysinger1baec122020-08-25 00:27:52 -040085 path = self._write_config("""# This be a comment me matey.
Mike Frysinger2e65c542016-03-08 16:17:00 -050086[Hook Scripts]
87name = script --with "some args"
88
89[Builtin Hooks]
90cpplint = true
91
92[Builtin Hooks Options]
93cpplint = --some 'more args'
Luis Hector Chavez5c4c2932016-10-16 21:56:58 -070094
95[Options]
96ignore_merged_commits = true
Mike Frysinger2e65c542016-03-08 16:17:00 -050097""")
Mike Frysinger1baec122020-08-25 00:27:52 -040098 rh.config.PreUploadFile(path)
Mike Frysinger2e65c542016-03-08 16:17:00 -050099
100 def testUnknownSection(self):
101 """Reject unknown sections."""
Mike Frysinger1baec122020-08-25 00:27:52 -0400102 path = self._write_config('[BOOGA]')
103 self.assertRaises(rh.config.ValidationError, rh.config.PreUploadFile,
104 path)
Mike Frysinger2e65c542016-03-08 16:17:00 -0500105
106 def testUnknownBuiltin(self):
107 """Reject unknown builtin hooks."""
Mike Frysinger1baec122020-08-25 00:27:52 -0400108 path = self._write_config('[Builtin Hooks]\nbooga = borg!')
109 self.assertRaises(rh.config.ValidationError, rh.config.PreUploadFile,
110 path)
Mike Frysinger2e65c542016-03-08 16:17:00 -0500111
112 def testEmptyCustomHook(self):
113 """Reject empty custom hooks."""
Mike Frysinger1baec122020-08-25 00:27:52 -0400114 path = self._write_config('[Hook Scripts]\nbooga = \t \n')
115 self.assertRaises(rh.config.ValidationError, rh.config.PreUploadFile,
116 path)
Mike Frysinger2e65c542016-03-08 16:17:00 -0500117
118 def testInvalidIni(self):
119 """Reject invalid ini files."""
Mike Frysinger1baec122020-08-25 00:27:52 -0400120 path = self._write_config('[Hook Scripts]\n =')
121 self.assertRaises(rh.config.ValidationError, rh.config.PreUploadFile,
122 path)
Mike Frysinger2e65c542016-03-08 16:17:00 -0500123
124 def testInvalidString(self):
125 """Catch invalid string quoting."""
Mike Frysinger1baec122020-08-25 00:27:52 -0400126 path = self._write_config("""[Hook Scripts]
Mike Frysinger2e65c542016-03-08 16:17:00 -0500127name = script --'bad-quotes
128""")
Mike Frysinger1baec122020-08-25 00:27:52 -0400129 self.assertRaises(rh.config.ValidationError, rh.config.PreUploadFile,
130 path)
131
132
Thiébaud Weksteenea528202020-08-28 15:54:29 +0200133class 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]
139cpplint = external/ 'test directory' ^vendor/(?!google/)
140""")
141 self.assertRaises(rh.config.ValidationError,
142 rh.config.LocalPreUploadFile,
143 path)
144
145
Thiébaud Weksteen324296b2020-08-28 16:32:07 +0200146class PreUploadSettingsTests(FileTestCase):
Mike Frysinger1baec122020-08-25 00:27:52 -0400147 """Tests for the PreUploadSettings class."""
148
Mike Frysingerca797702016-09-03 02:00:55 -0400149 def testGlobalConfigs(self):
150 """Verify global configs stack properly."""
151 self._write_global_config("""[Builtin Hooks]
152commit_msg_bug_field = true
153commit_msg_changeid_field = true
154commit_msg_test_field = false""")
Thiébaud Weksteen324296b2020-08-28 16:32:07 +0200155 self._write_local_config("""[Builtin Hooks]
Mike Frysingerca797702016-09-03 02:00:55 -0400156commit_msg_bug_field = false
157commit_msg_test_field = true""")
Mike Frysinger1baec122020-08-25 00:27:52 -0400158 config = rh.config.PreUploadSettings(paths=(self.tempdir,),
159 global_paths=(self.tempdir,))
Mike Frysingerca797702016-09-03 02:00:55 -0400160 self.assertEqual(config.builtin_hooks,
161 ['commit_msg_changeid_field', 'commit_msg_test_field'])
Mike Frysinger2e65c542016-03-08 16:17:00 -0500162
Thiébaud Weksteenea528202020-08-28 15:54:29 +0200163 def testGlobalExcludeScope(self):
164 """Verify exclude scope is valid for global config."""
165 self._write_global_config("""[Builtin Hooks Exclude Paths]
166cpplint = external/ 'test directory' ^vendor/(?!google/)
167""")
168 rh.config.PreUploadSettings(global_paths=(self.tempdir,))
169
Mike Frysinger2e65c542016-03-08 16:17:00 -0500170
171if __name__ == '__main__':
172 unittest.main()