allow setting home folder from WAKATIME_HOME env variable

This commit is contained in:
Alan Hamlett 2016-10-24 11:10:36 +02:00
parent 5380d24007
commit d8f0968c1a
3 changed files with 92 additions and 11 deletions

View file

@ -10,6 +10,7 @@ import logging
import os
import tempfile
import time
import shutil
import sys
from testfixtures import log_capture
from . import utils
@ -84,6 +85,42 @@ class LoggingTestCase(utils.TestCase):
self.assertEquals(logfile, logging.getLogger('WakaTime').handlers[0].baseFilename)
logs.check()
@log_capture()
def test_log_file_location_can_be_set_from_env_variable(self, logs):
logging.disable(logging.NOTSET)
response = Response()
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/python.py'
shutil.copy(entity, os.path.join(tempdir, 'python.py'))
entity = os.path.realpath(os.path.join(tempdir, 'python.py'))
config = 'tests/samples/configs/good_config.cfg'
shutil.copy(config, os.path.join(tempdir, '.wakatime.cfg'))
config = os.path.realpath(os.path.join(tempdir, '.wakatime.cfg'))
logfile = os.path.realpath(os.path.join(tempdir, '.wakatime.log'))
with utils.mock.patch('wakatime.main.os.environ.get') as mock_env:
mock_env.return_value = tempdir
args = ['--file', entity, '--config', config, '--time', now]
execute(args)
retval = execute(args)
self.assertEquals(retval, 102)
self.assertEquals(sys.stdout.getvalue(), '')
self.assertEquals(sys.stderr.getvalue(), '')
self.assertEquals(logging.WARNING, logging.getLogger('WakaTime').level)
actual_logfile = os.path.realpath(logging.getLogger('WakaTime').handlers[0].baseFilename)
self.assertEquals(logfile, actual_logfile)
logs.check()
@log_capture()
def test_verbose_flag_enables_verbose_logging(self, logs):
logging.disable(logging.NOTSET)

View file

@ -94,21 +94,56 @@ class MainTestCase(utils.TestCase):
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
with utils.mock.patch('wakatime.main.open') as mock_open:
mock_open.side_effect = IOError('')
with utils.mock.patch('wakatime.main.os.environ.get') as mock_env:
mock_env.return_value = None
with utils.mock.patch('wakatime.main.open') as mock_open:
mock_open.side_effect = IOError('')
config = os.path.join(os.path.expanduser('~'), '.wakatime.cfg')
args = ['--file', entity]
with self.assertRaises(SystemExit) as e:
execute(args)
self.assertEquals(int(str(e.exception)), CONFIG_FILE_PARSE_ERROR)
expected_stdout = u('')
expected_stderr = u("Error: Could not read from config file {0}\n").format(u(config))
self.assertEquals(sys.stdout.getvalue(), expected_stdout)
self.assertEquals(sys.stderr.getvalue(), expected_stderr)
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
def test_config_file_from_env(self):
response = Response()
response.status_code = 201
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/emptyfile.txt'
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
config = 'tests/samples/configs/has_everything.cfg'
shutil.copy(config, os.path.join(tempdir, '.wakatime.cfg'))
config = os.path.realpath(os.path.join(tempdir, '.wakatime.cfg'))
with utils.mock.patch('wakatime.main.os.environ.get') as mock_env:
mock_env.return_value = tempdir
config = os.path.join(os.path.expanduser('~'), '.wakatime.cfg')
args = ['--file', entity]
retval = execute(args)
self.assertEquals(retval, SUCCESS)
expected_stdout = open('tests/samples/output/main_test_good_config_file').read()
traceback_file = os.path.realpath('wakatime/main.py')
lineno = int(re.search(r' line (\d+),', sys.stdout.getvalue()).group(1))
self.assertEquals(sys.stdout.getvalue(), expected_stdout.format(file=traceback_file, lineno=lineno))
self.assertEquals(sys.stderr.getvalue(), '')
with self.assertRaises(SystemExit) as e:
execute(args)
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY)
self.assertEquals(int(str(e.exception)), CONFIG_FILE_PARSE_ERROR)
expected_stdout = u('')
expected_stderr = u("Error: Could not read from config file {0}\n").format(u(config))
self.assertEquals(sys.stdout.getvalue(), expected_stdout)
self.assertEquals(sys.stderr.getvalue(), expected_stderr)
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
def test_missing_config_file(self):
config = 'foo'

View file

@ -74,6 +74,12 @@ def parseConfigFile(configFile=None):
at ~/.wakatime.cfg.
"""
# get config file location from ENV
home = os.environ.get('WAKATIME_HOME')
if not configFile and home:
configFile = os.path.join(os.path.expanduser(home), '.wakatime.cfg')
# use default config file location
if not configFile:
configFile = os.path.join(os.path.expanduser('~'), '.wakatime.cfg')
@ -244,6 +250,9 @@ def parseArguments():
args.verbose = configs.getboolean('settings', 'debug')
if not args.logfile and configs.has_option('settings', 'logfile'):
args.logfile = configs.get('settings', 'logfile')
if not args.logfile and os.environ.get('WAKATIME_HOME'):
home = os.environ.get('WAKATIME_HOME')
args.logfile = os.path.join(os.path.expanduser(home), '.wakatime.log')
if not args.api_url and configs.has_option('settings', 'api_url'):
args.api_url = configs.get('settings', 'api_url')
if not args.timeout and configs.has_option('settings', 'timeout'):