From: kaliko Date: Wed, 5 May 2021 14:33:28 +0000 (+0200) Subject: Fixed crash when setting XDG_CONFIG_HOME (closes #50) X-Git-Tag: 0.18.0~74 X-Git-Url: https://git.kaliko.me/?a=commitdiff_plain;h=69c5f0029e140e4471f38e8cae5d07b649263a08;p=mpd-sima.git Fixed crash when setting XDG_CONFIG_HOME (closes #50) Also add unittests for XDG env. var. management. --- diff --git a/doc/Changelog b/doc/Changelog index d4b9f28..c487b0e 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,6 +1,7 @@ MPD_sima v0.18.0.dev0 * Removed obsolete --create-db and --generate-config options + * Fixed crash when setting XDG_CONFIG_HOME (closes #50) -- kaliko Wed, 28 Apr 2021 17:21:39 +0200 diff --git a/sima/utils/config.py b/sima/utils/config.py index 616a01f..299c7c3 100644 --- a/sima/utils/config.py +++ b/sima/utils/config.py @@ -245,6 +245,7 @@ class ConfMan: # CONFIG MANAGER CLASS pass elif environ.get('XDG_CONFIG_HOME'): conf_dir = join(environ.get('XDG_CONFIG_HOME'), DIRNAME) + self.conf_file = join(conf_dir, CONF_FILE) elif homedir and isdir(homedir) and homedir not in ['/']: conf_dir = join(homedir, '.config', DIRNAME) self.conf_file = join(conf_dir, CONF_FILE) @@ -253,7 +254,7 @@ class ConfMan: # CONFIG MANAGER CLASS self.log.critical('Please use "--config" to locate the conf file') sys.exit(1) - ## Sima sqlite DB + # Sima sqlite DB self.config['sima']['var_dir'] = join(data_dir) # If no conf file present, uses defaults diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..99a89f8 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +# coding: utf-8 + +import unittest +import os + +from unittest.mock import patch + +from sima.utils.config import ConfMan, DIRNAME, CONF_FILE +# import set_logger to set TRACE_LEVEL_NUM +from sima.lib.logger import set_logger + + +class TestConfMan(unittest.TestCase): + + @patch('sima.utils.config.makedirs') + @patch('sima.utils.config.chmod') + def test_XDG_var(self, mock_makedirs, mock_chmod): + config_home = '/foo/bar' + os.environ['XDG_CONFIG_HOME'] = config_home + conf_file = os.path.join(config_home, DIRNAME, CONF_FILE) + conf = ConfMan({}) + self.assertEqual(conf.conf_file, conf_file) + data_home = '/bar/foo' + os.environ['XDG_DATA_HOME'] = data_home + var_dir = os.path.join(data_home, DIRNAME) + conf = ConfMan({}) + self.assertEqual(conf.config['sima']['var_dir'], var_dir) + + @patch('sima.utils.config.isdir') + def test_default_locations(self, mock_isdir): + home = '/foo' + mock_isdir.return_value = True + os.environ.pop('XDG_CONFIG_HOME', None) + os.environ.pop('XDG_DATA_HOME', None) + os.environ['HOME'] = home + conf = ConfMan({}) + # Test var dir construction + constructed_var_dir = conf.config['sima']['var_dir'] + expected_var_dir = os.path.join(home, '.local', 'share', DIRNAME) + self.assertEqual(constructed_var_dir, expected_var_dir) + # Test config construction + constructed_config_location = conf.conf_file + expected_config = os.path.join(home, '.config', DIRNAME, CONF_FILE) + self.assertEqual(constructed_config_location, expected_config) + +# VIM MODLINE +# vim: ai ts=4 sw=4 sts=4 expandtab fileencoding=utf8