7 from unittest.mock import patch
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
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:
18 XDG_DATA_HOME=/non/existent/ XDG_HOME_CONFIG=/non/existent/ python3 -m unittest -vf tests/test_config.py
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)
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)
34 self.assertEqual(conf.config['sima']['var_dir'], var_dir)
36 @patch('sima.utils.config.isdir')
37 @patch('sima.utils.config.ConfMan.control_facc')
38 def test_default_locations(self, mock_isdir, *args):
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
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)
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):
61 os.environ.pop('MPD_HOST', None)
62 os.environ.pop('MPD_PORT', None)
65 self.assertEqual(dict(conf.config['MPD']),
66 {'host': 'localhost', 'port': '6600'})
67 # Test provided env. var.
68 os.environ['MPD_HOST'] = host
70 self.assertEqual(dict(conf.config['MPD']),
71 {'host': host, 'port': '6600'})
72 os.environ['MPD_HOST'] = f'{passwd}@{host}'
74 self.assertEqual(dict(conf.config['MPD']),
78 # Test abstract unix socket support with password
79 os.environ['MPD_HOST'] = f'{passwd}@@/{host}'
81 self.assertEqual(dict(conf.config['MPD']),
85 # Test abstract unix socket support only
86 os.environ['MPD_HOST'] = f'@/{host}'
88 self.assertEqual(dict(conf.config['MPD']),
92 os.environ['MPD_PORT'] = f'{port}'
94 self.assertEqual(conf.config['MPD']['port'], port)
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!')
113 # vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8