]> kaliko git repositories - mpd-sima.git/blob - tests/test_config.py
Remove bad heuristic to infer artist aliases
[mpd-sima.git] / tests / test_config.py
1 #!/usr/bin/env python3
2 # coding: utf-8
3
4 import unittest
5 import os
6
7 from unittest.mock import patch
8
9 from sima.utils.config import ConfMan, DIRNAME, CONF_FILE
10 # import set_logger to set TRACE_LEVEL_NUM
11 from sima.lib.logger import set_logger
12
13
14 class TestConfMan(unittest.TestCase):
15     """For some tests we don't care about file access check, then to ensure
16     checks are properly mocked run test forcing non existent locations:
17
18     XDG_DATA_HOME=/non/existent/ XDG_HOME_CONFIG=/non/existent/ python3 -m unittest -vf tests/test_config.py
19     """
20
21     @patch('sima.utils.config.makedirs')
22     @patch('sima.utils.config.chmod')
23     @patch('sima.utils.config.ConfMan.control_facc')
24     def test_XDG_var(self, *args):
25         config_home = '/foo/bar'
26         os.environ['XDG_CONFIG_HOME'] = config_home
27         conf_file = os.path.join(config_home, DIRNAME, CONF_FILE)
28         conf = ConfMan({})
29         self.assertEqual(conf.conf_file, conf_file)
30         data_home = '/bar/foo'
31         os.environ['XDG_DATA_HOME'] = data_home
32         var_dir = os.path.join(data_home, DIRNAME)
33         conf = ConfMan({})
34         self.assertEqual(conf.config['sima']['var_dir'], var_dir)
35
36     @patch('sima.utils.config.isdir')
37     @patch('sima.utils.config.ConfMan.control_facc')
38     def test_default_locations(self, mock_isdir, *args):
39         home = '/foo'
40         mock_isdir.return_value = True
41         os.environ.pop('XDG_CONFIG_HOME', None)
42         os.environ.pop('XDG_DATA_HOME', None)
43         os.environ['HOME'] = home
44         conf = ConfMan({})
45         # Test var dir construction
46         constructed_var_dir = conf.config['sima']['var_dir']
47         expected_var_dir = os.path.join(home, '.local', 'share', DIRNAME)
48         self.assertEqual(constructed_var_dir, expected_var_dir)
49         # Test config construction
50         constructed_config_location = conf.conf_file
51         expected_config = os.path.join(home, '.config', DIRNAME, CONF_FILE)
52         self.assertEqual(constructed_config_location, expected_config)
53
54     @patch('sima.utils.config.makedirs')
55     @patch('sima.utils.config.chmod')
56     @patch('sima.utils.config.ConfMan.control_facc')
57     def test_MPD_env_var(self, *args):
58         host = 'example.org'
59         passwd = 's2cr34!'
60         port = '6601'
61         os.environ.pop('MPD_HOST', None)
62         os.environ.pop('MPD_PORT', None)
63         # Test defaults
64         conf = ConfMan({})
65         self.assertEqual(dict(conf.config['MPD']),
66                          {'host': 'localhost', 'port': '6600'})
67         # Test provided env. var.
68         os.environ['MPD_HOST'] = host
69         conf = ConfMan({})
70         self.assertEqual(dict(conf.config['MPD']),
71                          {'host': host, 'port': '6600'})
72         os.environ['MPD_HOST'] = f'{passwd}@{host}'
73         conf = ConfMan({})
74         self.assertEqual(dict(conf.config['MPD']),
75                          {'host': host,
76                           'password': passwd,
77                           'port': '6600'})
78         # Test abstract unix socket support with password
79         os.environ['MPD_HOST'] = f'{passwd}@@/{host}'
80         conf = ConfMan({})
81         self.assertEqual(dict(conf.config['MPD']),
82                          {'host': f'@/{host}',
83                           'password': passwd,
84                           'port': '6600'})
85         # Test abstract unix socket support only
86         os.environ['MPD_HOST'] = f'@/{host}'
87         conf = ConfMan({})
88         self.assertEqual(dict(conf.config['MPD']),
89                          {'host': f'@/{host}',
90                           'port': '6600'})
91         # Test port
92         os.environ['MPD_PORT'] = f'{port}'
93         conf = ConfMan({})
94         self.assertEqual(conf.config['MPD']['port'], port)
95
96     @patch('sima.utils.config.makedirs')
97     @patch('sima.utils.config.chmod')
98     @patch('sima.utils.config.ConfMan.control_facc')
99     def test_config_origin_priority(self, *args):
100         # cli provided host overrides env. var.
101         os.environ['MPD_HOST'] = 'baz.foo'
102         conf = ConfMan({'host': 'cli.host'})
103         self.assertEqual(conf.config['MPD']['host'], 'cli.host')
104         # cli provided abstract socket overrides env. var.
105         conf = ConfMan({'host': '@/abstract'})
106         self.assertEqual(conf.config['MPD']['host'], '@/abstract')
107         # cli provided passord and abstract socket overrides env. var.
108         conf = ConfMan({'host': 'pass!@@/abstract'})
109         self.assertEqual(conf.config['MPD']['host'], '@/abstract')
110         self.assertEqual(conf.config['MPD']['password'], 'pass!')
111
112 # VIM MODLINE
113 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8