File: test_config.py

package info (click to toggle)
bugwarrior 1.8.0-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,280 kB
  • sloc: python: 9,226; makefile: 147
file content (247 lines) | stat: -rw-r--r-- 7,899 bytes parent folder | download | duplicates (4)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# coding: utf-8
from __future__ import unicode_literals

import os
from six.moves import configparser
from unittest import TestCase

import bugwarrior.config as config

from .base import ConfigTest


class TestGetConfigPath(ConfigTest):

    def create(self, path):
        """
        Create an empty file in the temporary directory, return the full path.
        """
        fpath = os.path.join(self.tempdir, path)
        if not os.path.exists(os.path.dirname(fpath)):
            os.makedirs(os.path.dirname(fpath))
        open(fpath, 'a').close()
        return fpath

    def test_default(self):
        """
        If it exists, use the file at $XDG_CONFIG_HOME/bugwarrior/bugwarriorrc
        """
        rc = self.create('.config/bugwarrior/bugwarriorrc')
        self.assertEqual(config.get_config_path(), rc)

    def test_legacy(self):
        """
        Falls back on .bugwarriorrc if it exists
        """
        rc = self.create('.bugwarriorrc')
        self.assertEqual(config.get_config_path(), rc)

    def test_xdg_first(self):
        """
        If both files above exist, the one in $XDG_CONFIG_HOME takes precedence
        """
        self.create('.bugwarriorrc')
        rc = self.create('.config/bugwarrior/bugwarriorrc')
        self.assertEqual(config.get_config_path(), rc)

    def test_no_file(self):
        """
        If no bugwarriorrc exist anywhere, the path to the prefered one is
        returned.
        """
        self.assertEqual(
            config.get_config_path(),
            os.path.join(self.tempdir, '.config/bugwarrior/bugwarriorrc'))

    def test_BUGWARRIORRC(self):
        """
        If $BUGWARRIORRC is set, it takes precedence over everything else (even
        if the file doesn't exist).
        """
        rc = os.path.join(self.tempdir, 'my-bugwarriorc')
        os.environ['BUGWARRIORRC'] = rc
        self.create('.bugwarriorrc')
        self.create('.config/bugwarrior/bugwarriorrc')
        self.assertEqual(config.get_config_path(), rc)

    def test_BUGWARRIORRC_empty(self):
        """
        If $BUGWARRIORRC is set but emty, it is not used and the default file
        is used instead.
        """
        os.environ['BUGWARRIORRC'] = ''
        rc = self.create('.config/bugwarrior/bugwarriorrc')
        self.assertEqual(config.get_config_path(), rc)


class TestGetDataPath(ConfigTest):

    def setUp(self):
        super(TestGetDataPath, self).setUp()
        self.config = configparser.RawConfigParser()
        self.config.add_section('general')

    def assertDataPath(self, expected_datapath):
        self.assertEqual(
            expected_datapath, config.get_data_path(self.config, 'general'))

    def test_TASKDATA(self):
        """
        TASKDATA should be respected, even when taskrc's data.location is set.
        """
        datapath = os.environ['TASKDATA'] = os.path.join(self.tempdir, 'data')
        self.assertDataPath(datapath)

    def test_taskrc_datalocation(self):
        """
        When TASKDATA is not set, data.location in taskrc should be respected.
        """
        self.assertTrue('TASKDATA' not in os.environ)
        self.assertDataPath(self.lists_path)

    def test_unassigned(self):
        """
        When data path is not assigned, use default location.
        """
        # Empty taskrc.
        with open(self.taskrc, 'w'):
            pass

        self.assertTrue('TASKDATA' not in os.environ)

        self.assertDataPath(os.path.expanduser('~/.task'))


class TestOracleEval(TestCase):

    def test_echo(self):
        self.assertEqual(config.oracle_eval("echo fööbår"), "fööbår")


class TestBugwarriorConfigParser(TestCase):
    def setUp(self):
        self.config = config.BugwarriorConfigParser()
        self.config.add_section('general')
        self.config.set('general', 'someint', '4')
        self.config.set('general', 'somenone', '')
        self.config.set('general', 'somechar', 'somestring')

    def test_getint(self):
        self.assertEqual(self.config.getint('general', 'someint'), 4)

    def test_getint_none(self):
        self.assertEqual(self.config.getint('general', 'somenone'), None)

    def test_getint_valueerror(self):
        with self.assertRaises(ValueError):
            self.config.getint('general', 'somechar')


class TestServiceConfig(TestCase):
    def setUp(self):
        self.target = 'someservice'

        self.config = config.BugwarriorConfigParser()
        self.config.add_section(self.target)
        self.config.set(self.target, 'someprefix.someint', '4')
        self.config.set(self.target, 'someprefix.somenone', '')
        self.config.set(self.target, 'someprefix.somechar', 'somestring')
        self.config.set(self.target, 'someprefix.somebool', 'true')

        self.service_config = config.ServiceConfig(
            'someprefix', self.config, self.target)

    def test_configparser_proxy(self):
        """
        Methods not defined in ServiceConfig should be proxied to configparser.
        """
        self.assertTrue(
            self.service_config.has_option(self.target, 'someprefix.someint'))

    def test__contains__(self):
        self.assertTrue('someint' in self.service_config)

    def test_get(self):
        self.assertEqual(self.service_config.get('someint'), '4')

    def test_get_default(self):
        self.assertEqual(
            self.service_config.get('someoption', default='somedefault'),
            'somedefault'
        )

    def test_get_default_none(self):
        self.assertIsNone(self.service_config.get('someoption'))

    def test_get_to_type(self):
        self.assertIs(
            self.service_config.get('somebool', to_type=config.asbool),
            True
        )


class TestLoggingPath(TestCase):
    def setUp(self):
        self.config = config.BugwarriorConfigParser(allow_no_value=True)
        self.config.add_section('general')
        self.config.set('general', 'log.level', 'INFO')
        self.config.set('general', 'log.file', None)
        self.dir = os.getcwd()
        os.chdir(os.path.expanduser('~'))

    def test_log_stdout(self):
        self.assertIsNone(config.fix_logging_path(self.config, 'general'))

    def test_log_relative_path(self):
        self.config.set('general', 'log.file', 'bugwarrior.log')
        self.assertEqual(
            config.fix_logging_path(self.config, 'general'),
            'bugwarrior.log',
        )

    def test_log_absolute_path(self):
        filename = os.path.join(os.path.expandvars('$HOME'), 'bugwarrior.log')
        self.config.set('general', 'log.file', filename)
        self.assertEqual(
            config.fix_logging_path(self.config, 'general'),
            'bugwarrior.log',
        )

    def test_log_userhome(self):
        self.config.set('general', 'log.file', '~/bugwarrior.log')
        self.assertEqual(
            config.fix_logging_path(self.config, 'general'),
            'bugwarrior.log',
        )

    def test_log_envvar(self):
        self.config.set('general', 'log.file', '$HOME/bugwarrior.log')
        self.assertEqual(
            config.fix_logging_path(self.config, 'general'),
            'bugwarrior.log',
        )

    def tearDown(self):
        os.chdir(self.dir)


class TestCasters(TestCase):
    def test_asbool(self):
        self.assertEqual(config.asbool('True'), True)
        self.assertEqual(config.asbool('False'), False)

    def test_aslist(self):
        self.assertEqual(
            config.aslist('project_bar,project_baz'),
            ['project_bar', 'project_baz']
        )

    def test_aslist_jinja(self):
        self.assertEqual(
            config.aslist("work, jira, {{jirastatus|lower|replace(' ','_')}}"),
            ['work', 'jira', "{{jirastatus|lower|replace(' ','_')}}"]
        )

    def test_asint(self):
        self.assertEqual(config.asint(''), None)
        self.assertEqual(config.asint('42'), 42)