]> kaliko git repositories - mpd-sima.git/commitdiff
Fixed crash when setting XDG_CONFIG_HOME (closes #50)
authorkaliko <kaliko@azylum.org>
Wed, 5 May 2021 14:33:28 +0000 (16:33 +0200)
committerkaliko <kaliko@azylum.org>
Wed, 5 May 2021 14:33:28 +0000 (16:33 +0200)
Also add unittests for XDG env. var. management.

doc/Changelog
sima/utils/config.py
tests/test_config.py [new file with mode: 0644]

index d4b9f281cdc4eff989547d1fedf5dc9ea93ba4db..c487b0ef66422f091ce90abcccc5bdabefd7b1c7 100644 (file)
@@ -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 <kaliko@azylum.org>  Wed, 28 Apr 2021 17:21:39 +0200
 
index 616a01f5bf16d26cac971d97b352cf5dd2138698..299c7c34cadfe8c932fa0a95f814a9afa6754236 100644 (file)
@@ -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 (file)
index 0000000..99a89f8
--- /dev/null
@@ -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