Skip to content

Commit

Permalink
Merge 0941327 into 2935968
Browse files Browse the repository at this point in the history
  • Loading branch information
facundobatista committed May 19, 2016
2 parents 2935968 + 0941327 commit e61a047
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
33 changes: 21 additions & 12 deletions fades/helpers.py
Expand Up @@ -75,26 +75,35 @@ def logged_exec(cmd):
return stdout


def get_basedir():
"""Get the base fades directory, from xdg or kinda hardcoded."""
def _get_basedirectory():
from xdg import BaseDirectory
return BaseDirectory


def _get_specific_dir(xdg_attrib):
"""Get a specific directory, using some XDG base, with sensible default."""
try:
from xdg import BaseDirectory # NOQA
return os.path.join(BaseDirectory.xdg_data_home, 'fades')
basedirectory = _get_basedirectory()
base = getattr(basedirectory, xdg_attrib)
direct = os.path.join(base, 'fades')
except ImportError:
logger.debug("Package xdg not installed; using ~/.fades folder")
from os.path import expanduser
return expanduser("~/.fades")
direct = os.path.join(expanduser("~"), ".fades")

if not os.path.exists(direct):
os.makedirs(direct)
return direct


def get_basedir():
"""Get the base fades directory, from xdg or kinda hardcoded."""
return _get_specific_dir('xdg_data_home')


def get_confdir():
"""Get the config fades directory, from xdg or kinda hardcoded."""
try:
from xdg import BaseDirectory # NOQA
return os.path.join(BaseDirectory.xdg_config_home, 'fades')
except ImportError:
logger.debug("Package xdg not installed; using ~/.fades folder")
from os.path import expanduser
return expanduser("~/.fades")
return _get_specific_dir('xdg_config_home')


def _get_interpreter_info(interpreter=None):
Expand Down
44 changes: 44 additions & 0 deletions tests/test_helpers.py
Expand Up @@ -19,12 +19,16 @@
import io
import os
import sys
import tempfile
import unittest

from unittest.mock import patch
from urllib.error import HTTPError

import logassert

from xdg import BaseDirectory

from fades import helpers
from fades import parsing

Expand Down Expand Up @@ -208,3 +212,43 @@ def test_check_pypi_updates_with_the_latest_version_of_a_package(self):
helpers.check_pypi_updates(self.deps_same_than_latest)
self.assertLoggedInfo(
"The requested version for django is the latest one in PyPI: 1.9")


class GetDirsTestCase(unittest.TestCase):
"""Utilities to get dir."""

_home = os.path.expanduser("~")

def test_basedir_xdg(self):
direct = helpers.get_basedir()
self.assertEqual(direct, os.path.join(BaseDirectory.xdg_data_home, 'fades'))

def test_basedir_default(self):
with patch.object(helpers, "_get_basedirectory") as mock:
mock.side_effect = ImportError()
direct = helpers.get_basedir()
self.assertEqual(direct, os.path.join(self._home, '.fades'))

def test_basedir_nonexistant(self):
with patch("xdg.BaseDirectory") as mock:
with tempfile.TemporaryDirectory() as dirname:
mock.xdg_data_home = dirname
direct = helpers.get_basedir()
self.assertTrue(os.path.exists(direct))

def test_confdir_xdg(self):
direct = helpers.get_confdir()
self.assertEqual(direct, os.path.join(BaseDirectory.xdg_config_home, 'fades'))

def test_confdir_default(self):
with patch.object(helpers, "_get_basedirectory") as mock:
mock.side_effect = ImportError()
direct = helpers.get_confdir()
self.assertEqual(direct, os.path.join(self._home, '.fades'))

def test_confdir_nonexistant(self):
with patch("xdg.BaseDirectory") as mock:
with tempfile.TemporaryDirectory() as dirname:
mock.xdg_config_home = dirname
direct = helpers.get_confdir()
self.assertTrue(os.path.exists(direct))

0 comments on commit e61a047

Please sign in to comment.