aboutsummaryrefslogtreecommitdiff
path: root/rh/config_unittest.py
blob: 9547b74239b8f0ceb6ba10db9eeb3556fdf5632a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/python
# -*- coding:utf-8 -*-
# Copyright 2016 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unittests for the config module."""

from __future__ import print_function

import os
import shutil
import sys
import tempfile
import unittest

_path = os.path.realpath(__file__ + '/../..')
if sys.path[0] != _path:
    sys.path.insert(0, _path)
del _path

import rh.hooks
import rh.config


class PreSubmitConfigTests(unittest.TestCase):
    """Tests for the PreSubmitConfig class."""

    def setUp(self):
        self.tempdir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.tempdir)

    def _write_config(self, data, filename=None):
        """Helper to write out a config file for testing."""
        if filename is None:
            filename = rh.config.PreSubmitConfig.FILENAME
        path = os.path.join(self.tempdir, filename)
        with open(path, 'w') as fp:
            fp.write(data)

    def _write_global_config(self, data):
        """Helper to write out a global config file for testing."""
        self._write_config(
            data, filename=rh.config.PreSubmitConfig.GLOBAL_FILENAME)

    def testMissing(self):
        """Instantiating a non-existent config file should be fine."""
        rh.config.PreSubmitConfig()

    def testEmpty(self):
        """Instantiating an empty config file should be fine."""
        self._write_config('')
        rh.config.PreSubmitConfig(paths=(self.tempdir,))

    def testValid(self):
        """Verify a fully valid file works."""
        self._write_config("""# This be a comment me matey.
[Hook Scripts]
name = script --with "some args"

[Builtin Hooks]
cpplint = true

[Builtin Hooks Options]
cpplint = --some 'more args'

[Options]
ignore_merged_commits = true
""")
        rh.config.PreSubmitConfig(paths=(self.tempdir,))

    def testUnknownSection(self):
        """Reject unknown sections."""
        self._write_config('[BOOGA]')
        self.assertRaises(rh.config.ValidationError, rh.config.PreSubmitConfig,
                          paths=(self.tempdir,))

    def testUnknownBuiltin(self):
        """Reject unknown builtin hooks."""
        self._write_config('[Builtin Hooks]\nbooga = borg!')
        self.assertRaises(rh.config.ValidationError, rh.config.PreSubmitConfig,
                          paths=(self.tempdir,))

    def testEmptyCustomHook(self):
        """Reject empty custom hooks."""
        self._write_config('[Hook Scripts]\nbooga = \t \n')
        self.assertRaises(rh.config.ValidationError, rh.config.PreSubmitConfig,
                          paths=(self.tempdir,))

    def testInvalidIni(self):
        """Reject invalid ini files."""
        self._write_config('[Hook Scripts]\n =')
        self.assertRaises(rh.config.ValidationError, rh.config.PreSubmitConfig,
                          paths=(self.tempdir,))

    def testInvalidString(self):
        """Catch invalid string quoting."""
        self._write_config("""[Hook Scripts]
name = script --'bad-quotes
""")
        self.assertRaises(rh.config.ValidationError, rh.config.PreSubmitConfig,
                          paths=(self.tempdir,))

    def testGlobalConfigs(self):
        """Verify global configs stack properly."""
        self._write_global_config("""[Builtin Hooks]
commit_msg_bug_field = true
commit_msg_changeid_field = true
commit_msg_test_field = false""")
        self._write_config("""[Builtin Hooks]
commit_msg_bug_field = false
commit_msg_test_field = true""")
        config = rh.config.PreSubmitConfig(paths=(self.tempdir,),
                                           global_paths=(self.tempdir,))
        self.assertEqual(config.builtin_hooks,
                         ['commit_msg_changeid_field', 'commit_msg_test_field'])


if __name__ == '__main__':
    unittest.main()