Repository: srsudar/eg

Issue:
No recognize config path: $HOME/.config/eg/egrc 
Version: 1.2.0
Installed from pip

I follow the [information](https://github.com/srsudar/eg#configuration-and-extension) on README of the repository. For custom config file I create a file called `egrc` on the next path: `skabit/.config/eg`, but nothing happens. In this file I only add a custom directory with my examples:

```
[eg-config]
# Lines starting with # are treated as comments
custom-dir = ~/docu/cheats
```



Discussion:

Patch:
diff --git a/eg/config.py b/eg/config.py
index f448d28..661057a 100644
--- a/eg/config.py
+++ b/eg/config.py
@@ -173,12 +173,16 @@ def get_egrc_config(cli_egrc_path):
 
     if config_path == '':
       # Try for the xdg config.
-      xdg_home_dir = os.getenv('XDG_CONFIG_HOME')
-      if xdg_home_dir:
-        xdg_config_path = os.path.join(xdg_home_dir, 'eg', 'egrc')
-        xdg_config_path = get_expanded_path(xdg_config_path)
-        if os.path.isfile(xdg_config_path):
-          config_path = xdg_config_path
+      # The freedesktop.org spec says '$HOME/.config' should be used if the
+      # environment variable is not set or is empty. os.getenv() will be falsey
+      # if it is either not set (None) or an empty string.
+      default_xdg_dir = os.path.join('~', '.config')
+      xdg_home_dir = os.getenv('XDG_CONFIG_HOME') or default_xdg_dir
+
+      xdg_config_path = os.path.join(xdg_home_dir, 'eg', 'egrc')
+      xdg_config_path = get_expanded_path(xdg_config_path)
+      if os.path.isfile(xdg_config_path):
+        config_path = xdg_config_path
 
     if config_path == '':
       # Fall back to our home directory.


Test patch:
diff --git a/test/config_test.py b/test/config_test.py
index 4277230..c7ea4cb 100644
--- a/test/config_test.py
+++ b/test/config_test.py
@@ -200,6 +200,50 @@ def test_get_egrc_config_uses_home_dir_default_no_xdg_dir():
     )
 
 
+def test_get_egrc_config_uses_default_xdg_dir_if_env_var_not_set():
+    """
+    If the XDG environment variable isn't set, but the file exists at the
+    default XDG location, it should be used. Note that here we don't have
+    'egrc', not as a hidden file by default.
+
+    See: https://github.com/srsudar/eg/issues/87
+    """
+    expected = 'mock config from default position'
+
+    _assert_about_get_egrc_config(
+        cli_path=None,
+        cli_config_exists=False,
+        winning_config_path=os.path.join(
+          '~', '.config', 'eg', 'egrc_expanded'),
+        home_config_exists=True,
+        expected_config=expected,
+        xdg_dir_variable=None,
+        xdg_config_exists=True,
+    )
+
+
+def test_get_egrc_config_uses_default_xdg_dir_if_env_var_empty():
+    """
+    If the XDG environment variable is empty, but the file exists at the default
+    XDG location, it should be used. Note that here we don't have 'egrc', not as
+    a hidden file by default.
+
+    See: https://github.com/srsudar/eg/issues/87
+    """
+    expected = 'mock config from default position'
+
+    _assert_about_get_egrc_config(
+        cli_path=None,
+        cli_config_exists=False,
+        winning_config_path=os.path.join(
+          '~', '.config', 'eg', 'egrc_expanded'),
+        home_config_exists=True,
+        expected_config=expected,
+        xdg_dir_variable='',
+        xdg_config_exists=True,
+    )
+
+
 def test_get_egrc_config_uses_home_dir_default_xdg_dir_file_not_present():
     """
     Fallback to the home directory config if XDG directory is set but the XDG
@@ -217,7 +261,7 @@ def test_get_egrc_config_uses_home_dir_default_xdg_dir_file_not_present():
         xdg_config_exists=False,
     )
 
-def test_get_egrc_config_uses_xdg_dir_default_if_present():
+def test_get_egrc_config_uses_xdg_dir_default_if_both_present():
     """
     The XDG config should win over the home directory.
     """
@@ -278,8 +322,13 @@ def _assert_about_get_egrc_config(
         return cli_config_exists
       if file_name == os.path.join('~', '.egrc_expanded'):
         return home_config_exists
-      if file_name == os.path.join(xdg_dir_variable, 'eg', 'egrc_expanded'):
-        # ${XDG_DIR_HOME}/eg/egrc
+      if (xdg_dir_variable and
+          file_name == os.path.join(xdg_dir_variable, 'eg', 'egrc_expanded')):
+        # ${XDG_CONFIG_HOME}/eg/egrc
+        return xdg_config_exists
+      if file_name == os.path.join('~', '.config', 'eg', 'egrc_expanded'):
+        # This is the default location of XDG_CONFIG_HOME, which we may fall
+        # back to under some test cases.
         return xdg_config_exists
       return False
     mock_isfile.side_effect = isfile_side_effect