2015-09-08 22:15:16 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
2016-09-01 09:49:12 +00:00
|
|
|
from wakatime.compat import is_py3, u
|
2015-09-08 22:15:16 +00:00
|
|
|
from wakatime.main import execute
|
|
|
|
from wakatime.packages import requests
|
2016-09-01 09:49:12 +00:00
|
|
|
from wakatime.packages.requests.models import Response
|
2015-09-08 22:15:16 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import time
|
2016-10-24 09:10:36 +00:00
|
|
|
import shutil
|
2015-09-08 22:15:16 +00:00
|
|
|
from testfixtures import log_capture
|
|
|
|
from . import utils
|
|
|
|
|
|
|
|
|
2015-09-30 05:34:14 +00:00
|
|
|
class LoggingTestCase(utils.TestCase):
|
2015-09-08 22:15:16 +00:00
|
|
|
patch_these = [
|
|
|
|
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
|
|
|
'wakatime.offlinequeue.Queue.push',
|
|
|
|
['wakatime.offlinequeue.Queue.pop', None],
|
2015-09-27 10:17:54 +00:00
|
|
|
['wakatime.offlinequeue.Queue.connect', None],
|
2015-09-08 22:15:16 +00:00
|
|
|
'wakatime.session_cache.SessionCache.save',
|
|
|
|
'wakatime.session_cache.SessionCache.delete',
|
|
|
|
['wakatime.session_cache.SessionCache.get', requests.session],
|
2015-09-27 10:17:54 +00:00
|
|
|
['wakatime.session_cache.SessionCache.connect', None],
|
2015-09-08 22:15:16 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_default_log_file_used(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()))
|
|
|
|
entity = 'tests/samples/codefiles/python.py'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/has_regex_errors.cfg'
|
2015-09-08 22:15:16 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, 102)
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2015-09-08 22:15:16 +00:00
|
|
|
|
|
|
|
self.assertEquals(logging.WARNING, logging.getLogger('WakaTime').level)
|
|
|
|
logfile = os.path.realpath(os.path.expanduser('~/.wakatime.log'))
|
|
|
|
self.assertEquals(logfile, logging.getLogger('WakaTime').handlers[0].baseFilename)
|
2015-11-20 07:34:32 +00:00
|
|
|
output = [u(' ').join(x) for x in logs.actual()]
|
|
|
|
expected = u('WakaTime WARNING Regex error (unbalanced parenthesis) for include pattern: \\(invalid regex)')
|
2017-02-16 20:46:04 +00:00
|
|
|
if self.isPy35OrNewer:
|
2015-11-20 07:39:32 +00:00
|
|
|
expected = u('WakaTime WARNING Regex error (unbalanced parenthesis at position 15) for include pattern: \\(invalid regex)')
|
2015-11-20 07:34:32 +00:00
|
|
|
self.assertEquals(output[0], expected)
|
|
|
|
expected = u('WakaTime WARNING Regex error (unbalanced parenthesis) for exclude pattern: \\(invalid regex)')
|
2017-02-16 20:46:04 +00:00
|
|
|
if self.isPy35OrNewer:
|
2015-11-20 07:39:32 +00:00
|
|
|
expected = u('WakaTime WARNING Regex error (unbalanced parenthesis at position 15) for exclude pattern: \\(invalid regex)')
|
2015-11-20 07:34:32 +00:00
|
|
|
self.assertEquals(output[1], expected)
|
2015-09-08 22:15:16 +00:00
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_log_file_location_can_be_changed(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2017-05-21 22:58:48 +00:00
|
|
|
with utils.NamedTemporaryFile() as fh:
|
2015-09-08 22:15:16 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/python.py'
|
2015-09-08 22:48:28 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-08 22:15:16 +00:00
|
|
|
logfile = os.path.realpath(fh.name)
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now, '--logfile', logfile]
|
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, 102)
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2015-09-08 22:15:16 +00:00
|
|
|
|
|
|
|
self.assertEquals(logging.WARNING, logging.getLogger('WakaTime').level)
|
|
|
|
self.assertEquals(logfile, logging.getLogger('WakaTime').handlers[0].baseFilename)
|
|
|
|
logs.check()
|
2015-09-27 01:56:33 +00:00
|
|
|
|
2016-10-24 09:10:36 +00:00
|
|
|
@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'))
|
2017-10-29 18:16:23 +00:00
|
|
|
expected_logfile = os.path.realpath(os.path.join(tempdir, '.wakatime.log'))
|
2016-10-24 09:10:36 +00:00
|
|
|
|
|
|
|
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)
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2016-10-24 09:10:36 +00:00
|
|
|
|
|
|
|
self.assertEquals(logging.WARNING, logging.getLogger('WakaTime').level)
|
2017-10-29 18:16:23 +00:00
|
|
|
logfile = os.path.realpath(logging.getLogger('WakaTime').handlers[0].baseFilename)
|
|
|
|
self.assertEquals(logfile, expected_logfile)
|
2016-10-24 09:10:36 +00:00
|
|
|
logs.check()
|
|
|
|
|
2015-09-27 01:56:33 +00:00
|
|
|
@log_capture()
|
|
|
|
def test_verbose_flag_enables_verbose_logging(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()))
|
|
|
|
entity = 'tests/samples/codefiles/python.py'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/has_regex_errors.cfg'
|
2015-09-27 01:56:33 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now, '--verbose']
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, 102)
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2015-09-27 01:56:33 +00:00
|
|
|
|
|
|
|
self.assertEquals(logging.DEBUG, logging.getLogger('WakaTime').level)
|
|
|
|
logfile = os.path.realpath(os.path.expanduser('~/.wakatime.log'))
|
|
|
|
self.assertEquals(logfile, logging.getLogger('WakaTime').handlers[0].baseFilename)
|
|
|
|
output = [u(' ').join(x) for x in logs.actual()]
|
2015-11-20 07:34:32 +00:00
|
|
|
expected = u('WakaTime WARNING Regex error (unbalanced parenthesis) for include pattern: \\(invalid regex)')
|
2017-02-16 20:46:04 +00:00
|
|
|
if self.isPy35OrNewer:
|
2015-11-20 07:39:32 +00:00
|
|
|
expected = u('WakaTime WARNING Regex error (unbalanced parenthesis at position 15) for include pattern: \\(invalid regex)')
|
2015-11-20 07:34:32 +00:00
|
|
|
self.assertEquals(output[0], expected)
|
|
|
|
expected = u('WakaTime WARNING Regex error (unbalanced parenthesis) for exclude pattern: \\(invalid regex)')
|
2017-02-16 20:46:04 +00:00
|
|
|
if self.isPy35OrNewer:
|
2015-11-20 07:39:32 +00:00
|
|
|
expected = u('WakaTime WARNING Regex error (unbalanced parenthesis at position 15) for exclude pattern: \\(invalid regex)')
|
2015-11-20 07:34:32 +00:00
|
|
|
self.assertEquals(output[1], expected)
|
2017-11-09 15:31:28 +00:00
|
|
|
self.assertEquals(output[2], u('WakaTime DEBUG Sending heartbeats to api at https://api.wakatime.com/api/v1/users/current/heartbeats.bulk'))
|
2015-09-27 01:56:33 +00:00
|
|
|
self.assertIn('Python', output[3])
|
2017-11-09 17:03:27 +00:00
|
|
|
self.assertIn('response_code', output[5])
|
2015-10-23 06:15:57 +00:00
|
|
|
|
|
|
|
@log_capture()
|
2016-06-16 06:02:56 +00:00
|
|
|
def test_exception_traceback_logged_in_debug_mode(self, logs):
|
2015-10-23 06:15:57 +00:00
|
|
|
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()))
|
|
|
|
entity = 'tests/samples/codefiles/python.py'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2016-06-16 06:02:56 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now, '--verbose']
|
2015-10-23 06:15:57 +00:00
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.stats.open') as mock_open:
|
2015-10-23 06:21:32 +00:00
|
|
|
mock_open.side_effect = Exception('FooBar')
|
2015-10-23 06:15:57 +00:00
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, 102)
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2015-10-23 06:15:57 +00:00
|
|
|
|
2016-09-01 09:49:12 +00:00
|
|
|
log_output = u("\n").join([u(' ').join(x) for x in logs.actual()])
|
|
|
|
self.assertIn(u('WakaTime DEBUG Traceback (most recent call last):'), log_output)
|
|
|
|
self.assertIn(u('Exception: FooBar'), log_output)
|
2016-06-16 06:02:56 +00:00
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_exception_traceback_not_logged_normally(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()))
|
|
|
|
entity = 'tests/samples/codefiles/python.py'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.stats.open') as mock_open:
|
|
|
|
mock_open.side_effect = Exception('FooBar')
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, 102)
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2016-06-16 06:02:56 +00:00
|
|
|
|
2016-09-01 09:49:12 +00:00
|
|
|
log_output = u("\n").join([u(' ').join(x) for x in logs.actual()])
|
|
|
|
self.assertEquals(u(''), log_output)
|
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_can_log_invalid_utf8(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
data = bytes('\xab', 'utf-16') if is_py3 else '\xab'
|
|
|
|
|
|
|
|
with self.assertRaises(UnicodeDecodeError):
|
|
|
|
data.decode('utf8')
|
|
|
|
|
|
|
|
logger = logging.getLogger('WakaTime')
|
|
|
|
logger.error(data)
|
|
|
|
|
|
|
|
found = False
|
|
|
|
for msg in list(logs.actual())[0]:
|
|
|
|
if u(msg) == u(data):
|
|
|
|
found = True
|
|
|
|
self.assertTrue(found)
|