summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Mehne <julian.mehne@posteo.de>2018-01-21 19:08:19 +0100
committerJulian Mehne <julian.mehne@posteo.de>2018-01-21 20:10:09 +0100
commit0935ccfd5c8cd00dd7b55a06133c6b5cf6368668 (patch)
treef6278deddadc2813a226f818eeb3c61f154a0daf
parent6f1a8b687dde23458b141c36f3044cf10fa008af (diff)
Fix empty XDG_* environment variables.
Use fallback, if an enviroment variable is unset *or* empty. Bug: - XDG_CONFIG_HOME='' alot Problem: Does not find the configuration file (among others), because os.environ.get('XDG_CONFIG_HOME', '~/.config') returns '', instead of '~/.config'.
-rw-r--r--alot/__main__.py6
-rw-r--r--alot/helper.py6
-rw-r--r--alot/settings/manager.py10
-rw-r--r--alot/ui.py3
-rw-r--r--tests/helper_test.py26
5 files changed, 42 insertions, 9 deletions
diff --git a/alot/__main__.py b/alot/__main__.py
index ee5e442a..207c8b92 100644
--- a/alot/__main__.py
+++ b/alot/__main__.py
@@ -11,6 +11,7 @@ import sys
import alot
from alot.settings.const import settings
from alot.settings.errors import ConfigError
+from alot.helper import get_env
from alot.db.manager import DBManager
from alot.ui import UI
from alot.commands import *
@@ -92,9 +93,8 @@ def main():
# locate alot config files
if options.config is None:
- alotconfig = os.path.join(
- os.environ.get('XDG_CONFIG_HOME', os.path.expanduser('~/.config')),
- 'alot', 'config')
+ xdg_dir = get_env('XDG_CONFIG_HOME', os.path.expanduser('~/.config'))
+ alotconfig = os.path.join(xdg_dir, 'alot', 'config')
if os.path.exists(alotconfig):
settings.alot_rc_path = alotconfig
else:
diff --git a/alot/helper.py b/alot/helper.py
index be5191d8..06362e06 100644
--- a/alot/helper.py
+++ b/alot/helper.py
@@ -625,3 +625,9 @@ def email_as_string(mail):
as_string, flags=re.MULTILINE)
return as_string
+
+
+def get_env(env_name, fallback):
+ """ Gets environment variable and returns fallback if unset or empty """
+ env = os.environ.get(env_name)
+ return env if env else fallback
diff --git a/alot/settings/manager.py b/alot/settings/manager.py
index 296ef22b..99954639 100644
--- a/alot/settings/manager.py
+++ b/alot/settings/manager.py
@@ -15,7 +15,7 @@ from configobj import ConfigObj, Section
from ..account import SendmailAccount
from ..addressbook.abook import AbookAddressBook
from ..addressbook.external import ExternalAddressbook
-from ..helper import pretty_datetime, string_decode
+from ..helper import pretty_datetime, string_decode, get_env
from ..utils import configobj as checks
from .errors import ConfigError, NoMatchingAccount
@@ -25,8 +25,8 @@ from .theme import Theme
DEFAULTSPATH = os.path.join(os.path.dirname(__file__), '..', 'defaults')
-DATA_DIRS = os.environ.get('XDG_DATA_DIRS',
- '/usr/local/share:/usr/share').split(':')
+DATA_DIRS = get_env('XDG_DATA_DIRS',
+ '/usr/local/share:/usr/share').split(':')
class SettingsManager(object):
@@ -157,8 +157,8 @@ class SettingsManager(object):
if path:
path = os.path.expanduser(path)
else:
- xdgdir = os.environ.get('XDG_CONFIG_HOME',
- os.path.expanduser('~/.config'))
+ xdgdir = get_env('XDG_CONFIG_HOME',
+ os.path.expanduser('~/.config'))
path = os.path.join(xdgdir, fallback)
self._config[setting_name] = path
diff --git a/alot/ui.py b/alot/ui.py
index c97f2fc1..6c0dea6e 100644
--- a/alot/ui.py
+++ b/alot/ui.py
@@ -18,6 +18,7 @@ from .commands import CommandCanceled
from .commands import CommandParseError
from .helper import split_commandline
from .helper import string_decode
+from .helper import get_env
from .widgets.globals import CompleteEdit
from .widgets.globals import ChoiceWidget
@@ -83,7 +84,7 @@ class UI(object):
# load histories
self._cache = os.path.join(
- os.environ.get('XDG_CACHE_HOME', os.path.expanduser('~/.cache')),
+ get_env('XDG_CACHE_HOME', os.path.expanduser('~/.cache')),
'alot', 'history')
self._cmd_hist_file = os.path.join(self._cache, 'commands')
self._sender_hist_file = os.path.join(self._cache, 'senders')
diff --git a/tests/helper_test.py b/tests/helper_test.py
index c2927725..02105a29 100644
--- a/tests/helper_test.py
+++ b/tests/helper_test.py
@@ -464,3 +464,29 @@ class TestCallCmdAsync(unittest.TestCase):
yield helper.call_cmd_async(['_____better_not_exist'])
self.assertEqual(cm.exception.exitCode, 1)
self.assertTrue(cm.exception.stderr)
+
+
+class TestGetEnv(unittest.TestCase):
+ env_name = 'XDG_CONFIG_HOME'
+ default = '~/.config'
+
+ def test_env_not_set(self):
+ with mock.patch.dict('os.environ'):
+ if self.env_name in os.environ:
+ del os.environ[self.env_name]
+ self.assertEqual(helper.get_env(self.env_name, self.default),
+ self.default)
+
+ def test_env_empty(self):
+ with mock.patch.dict('os.environ'):
+ os.environ[self.env_name] = ''
+ self.assertEqual(helper.get_env(self.env_name, self.default),
+ self.default)
+
+ def test_env_not_empty(self):
+ custom_path = '/my/personal/config/home'
+
+ with mock.patch.dict('os.environ'):
+ os.environ[self.env_name] = custom_path
+ self.assertEqual(helper.get_env(self.env_name, self.default),
+ custom_path)