improve traceback logging and non-utf8 handling
This commit is contained in:
parent
c08288eefd
commit
fd322ba3b6
12 changed files with 101 additions and 68 deletions
|
@ -1,8 +1,10 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
from wakatime.compat import is_py3, u
|
||||
from wakatime.main import execute
|
||||
from wakatime.packages import requests
|
||||
from wakatime.packages.requests.models import Response
|
||||
|
||||
import logging
|
||||
import os
|
||||
|
@ -10,8 +12,6 @@ import tempfile
|
|||
import time
|
||||
import sys
|
||||
from testfixtures import log_capture
|
||||
from wakatime.compat import u
|
||||
from wakatime.packages.requests.models import Response
|
||||
from . import utils
|
||||
|
||||
|
||||
|
@ -139,9 +139,9 @@ class LoggingTestCase(utils.TestCase):
|
|||
self.assertEquals(sys.stdout.getvalue(), '')
|
||||
self.assertEquals(sys.stderr.getvalue(), '')
|
||||
|
||||
output = u("\n").join([u(' ').join(x) for x in logs.actual()])
|
||||
self.assertIn(u('WakaTime DEBUG Traceback (most recent call last):'), output)
|
||||
self.assertIn(u('Exception: FooBar'), output)
|
||||
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)
|
||||
|
||||
@log_capture()
|
||||
def test_exception_traceback_not_logged_normally(self, logs):
|
||||
|
@ -164,5 +164,23 @@ class LoggingTestCase(utils.TestCase):
|
|||
self.assertEquals(sys.stdout.getvalue(), '')
|
||||
self.assertEquals(sys.stderr.getvalue(), '')
|
||||
|
||||
output = u("\n").join([u(' ').join(x) for x in logs.actual()])
|
||||
self.assertEquals(u(''), output)
|
||||
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)
|
||||
|
|
|
@ -867,8 +867,12 @@ class MainTestCase(utils.TestCase):
|
|||
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
||||
entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt'))
|
||||
|
||||
timezone = tzlocal.get_localzone()
|
||||
timezone.zone = 'tz汉语' if is_py3 else 'tz\xe6\xb1\x89\xe8\xaf\xad'
|
||||
class TZ(object):
|
||||
@property
|
||||
def zone(self):
|
||||
return 'tz汉语' if is_py3 else 'tz\xe6\xb1\x89\xe8\xaf\xad'
|
||||
timezone = TZ()
|
||||
|
||||
with utils.mock.patch('wakatime.packages.tzlocal.get_localzone') as mock_getlocalzone:
|
||||
mock_getlocalzone.return_value = timezone
|
||||
|
||||
|
@ -887,6 +891,40 @@ class MainTestCase(utils.TestCase):
|
|||
headers = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].headers
|
||||
self.assertEquals(headers.get('TimeZone'), u(timezone.zone).encode('utf-8') if is_py3 else timezone.zone)
|
||||
|
||||
def test_timezone_with_invalid_encoding(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'))
|
||||
|
||||
class TZ(object):
|
||||
@property
|
||||
def zone(self):
|
||||
return bytes('\xab', 'utf-16') if is_py3 else '\xab'
|
||||
timezone = TZ()
|
||||
|
||||
with self.assertRaises(UnicodeDecodeError):
|
||||
timezone.zone.decode('utf8')
|
||||
|
||||
with utils.mock.patch('wakatime.packages.tzlocal.get_localzone') as mock_getlocalzone:
|
||||
mock_getlocalzone.return_value = timezone
|
||||
|
||||
config = 'tests/samples/configs/has_everything.cfg'
|
||||
args = ['--file', entity, '--config', config, '--timeout', '15']
|
||||
retval = execute(args)
|
||||
self.assertEquals(retval, SUCCESS)
|
||||
|
||||
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.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
||||
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
|
||||
|
||||
def test_tzlocal_exception(self):
|
||||
response = Response()
|
||||
response.status_code = 201
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue