Skip to content

Commit

Permalink
Merge 21135e6 into c4c9885
Browse files Browse the repository at this point in the history
  • Loading branch information
elbenfreund committed Apr 25, 2016
2 parents c4c9885 + 21135e6 commit d4bc7ae
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
8 changes: 5 additions & 3 deletions hamster_cli/hamster_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from __future__ import absolute_import, unicode_literals

import codecs
import datetime
import logging
import os
Expand All @@ -35,6 +36,7 @@
from backports.configparser import SafeConfigParser
from hamsterlib import Fact, HamsterControl, helpers, reports
from tabulate import tabulate
from six import text_type

from . import help_strings

Expand Down Expand Up @@ -756,10 +758,10 @@ def _write_config_file(file_path):
# factory settings easily.

def get_db_path():
return os.path.join(str(AppDirs.user_data_dir), 'hamster_cli.sqlite')
return os.path.join(text_type(AppDirs.user_data_dir), 'hamster_cli.sqlite')

def get_tmp_file_path():
return os.path.join(str(AppDirs.user_data_dir), 'hamster_cli.fact')
return os.path.join(text_type(AppDirs.user_data_dir), 'hamster_cli.fact')

config = SafeConfigParser()

Expand All @@ -786,7 +788,7 @@ def get_tmp_file_path():
configfile_path = os.path.dirname(file_path)
if not os.path.lexists(configfile_path):
os.makedirs(configfile_path)
with open(file_path, 'w') as fobj:
with codecs.open(file_path, 'w', encoding='utf-8') as fobj:
config.write(fobj)

return config
Expand Down
24 changes: 15 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,25 @@ def filepath(tmpdir, filename):

@pytest.fixture
def appdirs(mocker, tmpdir):
"""Provide mocked version specific user dirs using a tmpdir."""
"""
Provide mocked version specific user dirs using a tmpdir.
We add a utf8-subdir to our paths to make sure our consuming methods can cope with it.
"""
def ensure_directory_exists(directory):
if not os.path.lexists(directory):
os.makedirs(directory)
return directory

hamster_cli.AppDirs = mocker.MagicMock()
hamster_cli.AppDirs.user_config_dir = ensure_directory_exists(os.path.join(
tmpdir.mkdir('config').strpath, 'hamster_cli/'))
tmpdir.mkdir('config').mkdir(fauxfactory.gen_utf8()).strpath, 'hamster_cli/'))
hamster_cli.AppDirs.user_data_dir = ensure_directory_exists(os.path.join(
tmpdir.mkdir('data').strpath, 'hamster_cli/'))
tmpdir.mkdir('data').mkdir(fauxfactory.gen_utf8()).strpath, 'hamster_cli/'))
hamster_cli.AppDirs.user_cache_dir = ensure_directory_exists(os.path.join(
tmpdir.mkdir('cache').strpath, 'hamster_cli/'))
tmpdir.mkdir('cache').mkdir(fauxfactory.gen_utf8()).strpath, 'hamster_cli/'))
hamster_cli.AppDirs.user_log_dir = ensure_directory_exists(os.path.join(
tmpdir.mkdir('log').strpath, 'hamster_cli/'))
tmpdir.mkdir('log').mkdir(fauxfactory.gen_utf8()).strpath, 'hamster_cli/'))
return hamster_cli.AppDirs


Expand Down Expand Up @@ -92,7 +96,7 @@ def lib_config(tmpdir):
'day_start': datetime.time(hour=0, minute=0, second=0),
'db_engine': 'sqlite',
'db_path': ':memory:',
'tmpfile_path': os.path.join(tmpdir.mkdir('cache2').strpath, 'test.pickle'),
'tmpfile_path': os.path.join(tmpdir.mkdir(fauxfactory.gen_utf8()).strpath, 'test.pickle'),
'fact_min_delta': 60,
}

Expand All @@ -110,8 +114,10 @@ def client_config(tmpdir):
'log_level': 10,
'log_console': False,
'logfile_path': False,
'export_path': os.path.join(tmpdir.mkdir('export').strpath, 'export'),
'logging_path': os.path.join(tmpdir.mkdir('log2').strpath, 'hamster_cli.log'),
'export_path': os.path.join(
tmpdir.mkdir('export').mkdir(fauxfactory.gen_utf8()).strpath, 'export'),
'logging_path': os.path.join(
tmpdir.mkdir('log2').mkdir(fauxfactory.gen_utf8()).strpath, 'hamster_cli.log'),
}


Expand All @@ -127,7 +133,7 @@ def generate_config(**kwargs):
config.set('Backend', 'fact_min_delta', kwargs.get('fact_min_delta', '60'))
config.set('Backend', 'db_engine', kwargs.get('db_engine', 'sqlite'))
config.set('Backend', 'db_path', kwargs.get('db_path', os.path.join(
tmpdir.strpath, 'hamster_db.sqlite')))
tmpdir.mkdir(fauxfactory.gen_utf8()).strpath, 'hamster_db.sqlite')))
config.set('Backend', 'db_host', kwargs.get('db_host', ''))
config.set('Backend', 'db_name', kwargs.get('db_name', ''))
config.set('Backend', 'db_port', kwargs.get('db_port', ''))
Expand Down

0 comments on commit d4bc7ae

Please sign in to comment.