| 1 |
# coding: utf-8 |
| 2 |
from __future__ import unicode_literals |
| 3 |
|
| 4 |
import os |
| 5 |
from six.moves import configparser |
| 6 |
from unittest import TestCase |
| 7 |
|
| 8 |
import bugwarrior.config as config |
| 9 |
|
| 10 |
from .base import ConfigTest |
| 11 |
|
| 12 |
|
| 13 |
class TestGetConfigPath(ConfigTest): |
| 14 |
|
| 15 |
def create(self, path): |
| 16 |
""" |
| 17 |
Create an empty file in the temporary directory, return the full path. |
| 18 |
""" |
| 19 |
fpath = os.path.join(self.tempdir, path) |
| 20 |
if not os.path.exists(os.path.dirname(fpath)): |
| 21 |
os.makedirs(os.path.dirname(fpath)) |
| 22 |
open(fpath, 'a').close() |
| 23 |
return fpath |
| 24 |
|
| 25 |
def test_default(self): |
| 26 |
""" |
| 27 |
If it exists, use the file at $XDG_CONFIG_HOME/bugwarrior/bugwarriorrc |
| 28 |
""" |
| 29 |
rc = self.create('.config/bugwarrior/bugwarriorrc') |
| 30 |
self.assertEqual(config.get_config_path(), rc) |
| 31 |
|
| 32 |
def test_legacy(self): |
| 33 |
""" |
| 34 |
Falls back on .bugwarriorrc if it exists |
| 35 |
""" |
| 36 |
rc = self.create('.bugwarriorrc') |
| 37 |
self.assertEqual(config.get_config_path(), rc) |
| 38 |
|
| 39 |
def test_xdg_first(self): |
| 40 |
""" |
| 41 |
If both files above exist, the one in $XDG_CONFIG_HOME takes precedence |
| 42 |
""" |
| 43 |
self.create('.bugwarriorrc') |
| 44 |
rc = self.create('.config/bugwarrior/bugwarriorrc') |
| 45 |
self.assertEqual(config.get_config_path(), rc) |
| 46 |
|
| 47 |
def test_no_file(self): |
| 48 |
""" |
| 49 |
If no bugwarriorrc exist anywhere, the path to the prefered one is |
| 50 |
returned. |
| 51 |
""" |
| 52 |
self.assertEqual( |
| 53 |
config.get_config_path(), |
| 54 |
os.path.join(self.tempdir, '.config/bugwarrior/bugwarriorrc')) |
| 55 |
|
| 56 |
def test_BUGWARRIORRC(self): |
| 57 |
""" |
| 58 |
If $BUGWARRIORRC is set, it takes precedence over everything else (even |
| 59 |
if the file doesn't exist). |
| 60 |
""" |
| 61 |
rc = os.path.join(self.tempdir, 'my-bugwarriorc') |
| 62 |
os.environ['BUGWARRIORRC'] = rc |
| 63 |
self.create('.bugwarriorrc') |
| 64 |
self.create('.config/bugwarrior/bugwarriorrc') |
| 65 |
self.assertEqual(config.get_config_path(), rc) |
| 66 |
|
| 67 |
def test_BUGWARRIORRC_empty(self): |
| 68 |
""" |
| 69 |
If $BUGWARRIORRC is set but emty, it is not used and the default file |
| 70 |
is used instead. |
| 71 |
""" |
| 72 |
os.environ['BUGWARRIORRC'] = '' |
| 73 |
rc = self.create('.config/bugwarrior/bugwarriorrc') |
| 74 |
self.assertEqual(config.get_config_path(), rc) |
| 75 |
|
| 76 |
|
| 77 |
class TestGetDataPath(ConfigTest): |
| 78 |
|
| 79 |
def setUp(self): |
| 80 |
super(TestGetDataPath, self).setUp() |
| 81 |
self.config = configparser.RawConfigParser() |
| 82 |
self.config.add_section('general') |
| 83 |
|
| 84 |
def assertDataPath(self, expected_datapath): |
| 85 |
self.assertEqual( |
| 86 |
expected_datapath, config.get_data_path(self.config, 'general')) |
| 87 |
|
| 88 |
def test_TASKDATA(self): |
| 89 |
""" |
| 90 |
TASKDATA should be respected, even when taskrc's data.location is set. |
| 91 |
""" |
| 92 |
datapath = os.environ['TASKDATA'] = os.path.join(self.tempdir, 'data') |
| 93 |
self.assertDataPath(datapath) |
| 94 |
|
| 95 |
def test_taskrc_datalocation(self): |
| 96 |
""" |
| 97 |
When TASKDATA is not set, data.location in taskrc should be respected. |
| 98 |
""" |
| 99 |
self.assertTrue('TASKDATA' not in os.environ) |
| 100 |
self.assertDataPath(self.lists_path) |
| 101 |
|
| 102 |
def test_unassigned(self): |
| 103 |
""" |
| 104 |
When data path is not assigned, use default location. |
| 105 |
""" |
| 106 |
# Empty taskrc. |
| 107 |
with open(self.taskrc, 'w'): |
| 108 |
pass |
| 109 |
|
| 110 |
self.assertTrue('TASKDATA' not in os.environ) |
| 111 |
|
| 112 |
self.assertDataPath(os.path.expanduser('~/.task')) |
| 113 |
|
| 114 |
|
| 115 |
class TestOracleEval(TestCase): |
| 116 |
|
| 117 |
def test_echo(self): |
| 118 |
self.assertEqual(config.oracle_eval("echo fööbår"), "fööbår") |
| 119 |
|
| 120 |
|
| 121 |
class TestBugwarriorConfigParser(TestCase): |
| 122 |
def setUp(self): |
| 123 |
self.config = config.BugwarriorConfigParser() |
| 124 |
self.config.add_section('general') |
| 125 |
self.config.set('general', 'someint', '4') |
| 126 |
self.config.set('general', 'somenone', '') |
| 127 |
self.config.set('general', 'somechar', 'somestring') |
| 128 |
|
| 129 |
def test_getint(self): |
| 130 |
self.assertEqual(self.config.getint('general', 'someint'), 4) |
| 131 |
|
| 132 |
def test_getint_none(self): |
| 133 |
self.assertEqual(self.config.getint('general', 'somenone'), None) |
| 134 |
|
| 135 |
def test_getint_valueerror(self): |
| 136 |
with self.assertRaises(ValueError): |
| 137 |
self.config.getint('general', 'somechar') |
| 138 |
|
| 139 |
|
| 140 |
class TestServiceConfig(TestCase): |
| 141 |
def setUp(self): |
| 142 |
self.target = 'someservice' |
| 143 |
|
| 144 |
self.config = config.BugwarriorConfigParser() |
| 145 |
self.config.add_section(self.target) |
| 146 |
self.config.set(self.target, 'someprefix.someint', '4') |
| 147 |
self.config.set(self.target, 'someprefix.somenone', '') |
| 148 |
self.config.set(self.target, 'someprefix.somechar', 'somestring') |
| 149 |
self.config.set(self.target, 'someprefix.somebool', 'true') |
| 150 |
|
| 151 |
self.service_config = config.ServiceConfig( |
| 152 |
'someprefix', self.config, self.target) |
| 153 |
|
| 154 |
def test_configparser_proxy(self): |
| 155 |
""" |
| 156 |
Methods not defined in ServiceConfig should be proxied to configparser. |
| 157 |
""" |
| 158 |
self.assertTrue( |
| 159 |
self.service_config.has_option(self.target, 'someprefix.someint')) |
| 160 |
|
| 161 |
def test__contains__(self): |
| 162 |
self.assertTrue('someint' in self.service_config) |
| 163 |
|
| 164 |
def test_get(self): |
| 165 |
self.assertEqual(self.service_config.get('someint'), '4') |
| 166 |
|
| 167 |
def test_get_default(self): |
| 168 |
self.assertEqual( |
| 169 |
self.service_config.get('someoption', default='somedefault'), |
| 170 |
'somedefault' |
| 171 |
) |
| 172 |
|
| 173 |
def test_get_default_none(self): |
| 174 |
self.assertIsNone(self.service_config.get('someoption')) |
| 175 |
|
| 176 |
def test_get_to_type(self): |
| 177 |
self.assertIs( |
| 178 |
self.service_config.get('somebool', to_type=config.asbool), |
| 179 |
True |
| 180 |
) |
| 181 |
|
| 182 |
|
| 183 |
class TestLoggingPath(TestCase): |
| 184 |
def setUp(self): |
| 185 |
self.config = config.BugwarriorConfigParser(allow_no_value=True) |
| 186 |
self.config.add_section('general') |
| 187 |
self.config.set('general', 'log.level', 'INFO') |
| 188 |
self.config.set('general', 'log.file', None) |
| 189 |
self.dir = os.getcwd() |
| 190 |
os.chdir(os.path.expanduser('~')) |
| 191 |
|
| 192 |
def test_log_stdout(self): |
| 193 |
self.assertIsNone(config.fix_logging_path(self.config, 'general')) |
| 194 |
|
| 195 |
def test_log_relative_path(self): |
| 196 |
self.config.set('general', 'log.file', 'bugwarrior.log') |
| 197 |
self.assertEqual( |
| 198 |
config.fix_logging_path(self.config, 'general'), |
| 199 |
'bugwarrior.log', |
| 200 |
) |
| 201 |
|
| 202 |
def test_log_absolute_path(self): |
| 203 |
filename = os.path.join(os.path.expandvars('$HOME'), 'bugwarrior.log') |
| 204 |
self.config.set('general', 'log.file', filename) |
| 205 |
self.assertEqual( |
| 206 |
config.fix_logging_path(self.config, 'general'), |
| 207 |
'bugwarrior.log', |
| 208 |
) |
| 209 |
|
| 210 |
def test_log_userhome(self): |
| 211 |
self.config.set('general', 'log.file', '~/bugwarrior.log') |
| 212 |
self.assertEqual( |
| 213 |
config.fix_logging_path(self.config, 'general'), |
| 214 |
'bugwarrior.log', |
| 215 |
) |
| 216 |
|
| 217 |
def test_log_envvar(self): |
| 218 |
self.config.set('general', 'log.file', '$HOME/bugwarrior.log') |
| 219 |
self.assertEqual( |
| 220 |
config.fix_logging_path(self.config, 'general'), |
| 221 |
'bugwarrior.log', |
| 222 |
) |
| 223 |
|
| 224 |
def tearDown(self): |
| 225 |
os.chdir(self.dir) |
| 226 |
|
| 227 |
|
| 228 |
class TestCasters(TestCase): |
| 229 |
def test_asbool(self): |
| 230 |
self.assertEqual(config.asbool('True'), True) |
| 231 |
self.assertEqual(config.asbool('False'), False) |
| 232 |
|
| 233 |
def test_aslist(self): |
| 234 |
self.assertEqual( |
| 235 |
config.aslist('project_bar,project_baz'), |
| 236 |
['project_bar', 'project_baz'] |
| 237 |
) |
| 238 |
|
| 239 |
def test_aslist_jinja(self): |
| 240 |
self.assertEqual( |
| 241 |
config.aslist("work, jira, {{jirastatus|lower|replace(' ','_')}}"), |
| 242 |
['work', 'jira', "{{jirastatus|lower|replace(' ','_')}}"] |
| 243 |
) |
| 244 |
|
| 245 |
def test_asint(self): |
| 246 |
self.assertEqual(config.asint(''), None) |
| 247 |
self.assertEqual(config.asint('42'), 42) |