2015-09-07 01:22:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
from wakatime.main import execute
|
|
|
|
from wakatime.offlinequeue import Queue
|
|
|
|
from wakatime.packages import requests
|
|
|
|
|
2016-07-06 21:07:55 +00:00
|
|
|
import logging
|
2015-09-07 01:22:05 +00:00
|
|
|
import os
|
2015-09-27 10:17:54 +00:00
|
|
|
import sqlite3
|
2017-11-09 17:03:27 +00:00
|
|
|
import shutil
|
2015-09-07 01:22:05 +00:00
|
|
|
import time
|
2017-11-09 17:03:27 +00:00
|
|
|
import uuid
|
2016-07-06 21:07:55 +00:00
|
|
|
from testfixtures import log_capture
|
2015-09-07 01:22:05 +00:00
|
|
|
from wakatime.compat import u
|
2017-11-23 02:34:15 +00:00
|
|
|
from wakatime.constants import API_ERROR, AUTH_ERROR, SUCCESS
|
2015-09-07 01:22:05 +00:00
|
|
|
from wakatime.packages.requests.models import Response
|
2017-11-23 17:25:30 +00:00
|
|
|
from .utils import mock, json, ANY, CustomResponse, NamedTemporaryFile, TemporaryDirectory, TestCase
|
2015-09-07 01:22:05 +00:00
|
|
|
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
class OfflineQueueTestCase(TestCase):
|
2015-09-07 01:22:05 +00:00
|
|
|
patch_these = [
|
2018-10-05 06:59:23 +00:00
|
|
|
'time.sleep',
|
2015-09-07 01:22:05 +00:00
|
|
|
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
|
|
|
'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-07 01:22:05 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def test_heartbeat_saved_from_error_response(self):
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2015-09-27 10:17:54 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
2015-09-27 10:17:54 +00:00
|
|
|
saved_heartbeat = queue.pop()
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(os.path.realpath(entity), saved_heartbeat['entity'])
|
2015-09-07 01:22:05 +00:00
|
|
|
|
|
|
|
def test_heartbeat_discarded_from_400_response(self):
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2015-09-27 10:17:54 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 400
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
2015-09-27 10:17:54 +00:00
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
2015-09-07 01:22:05 +00:00
|
|
|
|
|
|
|
def test_offline_heartbeat_sent_after_success_response(self):
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2015-09-27 10:17:54 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
response = CustomResponse()
|
2017-11-25 00:10:15 +00:00
|
|
|
response.response_text = '{"responses": [[null,201], [null,201], [null,201]]}'
|
2017-11-23 02:34:15 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
2015-09-27 10:17:54 +00:00
|
|
|
execute(args)
|
2015-09-07 01:22:05 +00:00
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
2015-09-27 10:17:54 +00:00
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
2015-09-07 01:47:17 +00:00
|
|
|
|
2016-07-28 07:00:58 +00:00
|
|
|
def test_all_offline_heartbeats_sent_after_success_response(self):
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2016-07-28 07:00:58 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
now1 = u(int(time.time()))
|
|
|
|
entity1 = 'tests/samples/codefiles/emptyfile.txt'
|
|
|
|
project1 = 'proj1'
|
|
|
|
|
|
|
|
args = ['--file', entity1, '--config', config, '--time', now1, '--project', project1]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
now2 = u(int(time.time()))
|
|
|
|
entity2 = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
project2 = 'proj2'
|
|
|
|
|
|
|
|
args = ['--file', entity2, '--config', config, '--time', now2, '--project', project2]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
# send heartbeats from offline queue after 201 response
|
|
|
|
now3 = u(int(time.time()))
|
|
|
|
entity3 = 'tests/samples/codefiles/python.py'
|
|
|
|
project3 = 'proj3'
|
|
|
|
args = ['--file', entity3, '--config', config, '--time', now3, '--project', project3]
|
2017-11-23 02:34:15 +00:00
|
|
|
|
|
|
|
response = CustomResponse()
|
2017-11-25 00:10:15 +00:00
|
|
|
response.response_text = '{"responses": [[null,201], [null,201], [null,201]]}'
|
2017-11-23 02:34:15 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
2016-07-28 07:00:58 +00:00
|
|
|
execute(args)
|
|
|
|
|
|
|
|
# offline queue should be empty
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
2016-07-28 07:00:58 +00:00
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
|
|
|
|
|
|
|
calls = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args_list
|
|
|
|
|
|
|
|
body = calls[0][0][0].body
|
2017-11-09 06:54:33 +00:00
|
|
|
data = json.loads(body)[0]
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(data.get('entity'), os.path.abspath(entity1))
|
2016-07-28 07:00:58 +00:00
|
|
|
self.assertEquals(data.get('project'), project1)
|
|
|
|
self.assertEquals(u(int(data.get('time'))), now1)
|
|
|
|
|
|
|
|
body = calls[1][0][0].body
|
2017-11-09 06:54:33 +00:00
|
|
|
data = json.loads(body)[0]
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(data.get('entity'), os.path.abspath(entity2))
|
2016-07-28 07:00:58 +00:00
|
|
|
self.assertEquals(data.get('project'), project2)
|
|
|
|
self.assertEquals(u(int(data.get('time'))), now2)
|
|
|
|
|
|
|
|
body = calls[2][0][0].body
|
2017-11-09 06:54:33 +00:00
|
|
|
data = json.loads(body)[0]
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(data.get('entity'), os.path.abspath(entity3))
|
2016-07-28 07:00:58 +00:00
|
|
|
self.assertEquals(data.get('project'), project3)
|
|
|
|
self.assertEquals(u(int(data.get('time'))), now3)
|
|
|
|
|
|
|
|
body = calls[3][0][0].body
|
2017-11-09 06:54:33 +00:00
|
|
|
data = json.loads(body)[0]
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(data.get('entity'), os.path.abspath(entity1))
|
2016-07-28 07:00:58 +00:00
|
|
|
self.assertEquals(data.get('project'), project1)
|
|
|
|
self.assertEquals(u(int(data.get('time'))), now1)
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
body = calls[3][0][0].body
|
|
|
|
data = json.loads(body)[1]
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(data.get('entity'), os.path.abspath(entity2))
|
2016-07-28 07:00:58 +00:00
|
|
|
self.assertEquals(data.get('project'), project2)
|
|
|
|
self.assertEquals(u(int(data.get('time'))), now2)
|
|
|
|
|
2017-11-09 17:03:27 +00:00
|
|
|
@log_capture()
|
|
|
|
def test_heartbeats_sent_not_saved_from_bulk_response(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2017-11-09 17:03:27 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
entities = [
|
|
|
|
'emptyfile.txt',
|
|
|
|
'twolinefile.txt',
|
|
|
|
'python.py',
|
|
|
|
'go.go',
|
|
|
|
]
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with TemporaryDirectory() as tempdir:
|
2017-11-09 17:03:27 +00:00
|
|
|
for entity in entities:
|
|
|
|
shutil.copy(os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, entity))
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
key = str(uuid.uuid4())
|
|
|
|
args = ['--file', os.path.join(tempdir, entities[0]), '--key', key, '--config', 'tests/samples/configs/good_config.cfg', '--time', now, '--extra-heartbeats']
|
|
|
|
|
|
|
|
response = CustomResponse()
|
2017-11-23 02:34:15 +00:00
|
|
|
response.response_code = 202
|
2017-11-25 00:10:15 +00:00
|
|
|
response.response_text = '{"responses": [[null,201], [null,500], [null,201], [null, 500]]}'
|
2017-11-09 17:03:27 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
|
2017-11-09 17:03:27 +00:00
|
|
|
heartbeats = json.dumps([{
|
|
|
|
'timestamp': now,
|
|
|
|
'entity': os.path.join(tempdir, entity),
|
|
|
|
'entity_type': 'file',
|
|
|
|
'is_write': False,
|
|
|
|
} for entity in entities[1:]])
|
|
|
|
mock_stdin.readline.return_value = heartbeats
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with mock.patch('wakatime.offlinequeue.Queue.pop') as mock_pop:
|
2017-11-09 17:03:27 +00:00
|
|
|
mock_pop.return_value = None
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'entity': os.path.realpath(os.path.join(tempdir, entities[0])),
|
|
|
|
'language': ANY,
|
|
|
|
'lines': ANY,
|
|
|
|
'project': ANY,
|
|
|
|
'time': ANY,
|
|
|
|
'type': 'file',
|
|
|
|
'is_write': ANY,
|
|
|
|
'user_agent': ANY,
|
|
|
|
'dependencies': ANY,
|
|
|
|
}
|
|
|
|
extra_heartbeats = [{
|
|
|
|
'entity': os.path.realpath(os.path.join(tempdir, entity)),
|
|
|
|
'language': ANY,
|
|
|
|
'lines': ANY,
|
|
|
|
'project': ANY,
|
|
|
|
'branch': ANY,
|
|
|
|
'time': ANY,
|
|
|
|
'is_write': ANY,
|
|
|
|
'type': 'file',
|
|
|
|
'dependencies': ANY,
|
|
|
|
'user_agent': ANY,
|
|
|
|
} for entity in entities[1:]]
|
|
|
|
self.assertHeartbeatSent(heartbeat, extra_heartbeats=extra_heartbeats)
|
|
|
|
|
|
|
|
self.assertSessionCacheSaved()
|
|
|
|
|
|
|
|
queue = Queue(None, None)
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(queue._get_db_file(), fh.name)
|
2018-09-20 17:14:25 +00:00
|
|
|
saved_heartbeats = next(queue.pop_many())
|
2017-11-09 17:03:27 +00:00
|
|
|
self.assertNothingPrinted()
|
|
|
|
self.assertNothingLogged(logs)
|
|
|
|
|
|
|
|
# make sure only heartbeats with error code responses were saved
|
2018-09-20 17:14:25 +00:00
|
|
|
self.assertEquals(sum(1 for x in saved_heartbeats), 2)
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(saved_heartbeats[0].entity, os.path.realpath(os.path.join(tempdir, entities[1])))
|
|
|
|
self.assertPathsEqual(saved_heartbeats[1].entity, os.path.realpath(os.path.join(tempdir, entities[3])))
|
2017-11-09 17:03:27 +00:00
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_offline_heartbeats_sent_after_partial_success_from_bulk_response(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2017-11-09 17:03:27 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
entities = [
|
|
|
|
'emptyfile.txt',
|
|
|
|
'twolinefile.txt',
|
|
|
|
'python.py',
|
|
|
|
'go.go',
|
|
|
|
]
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with TemporaryDirectory() as tempdir:
|
2017-11-09 17:03:27 +00:00
|
|
|
for entity in entities:
|
|
|
|
shutil.copy(os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, entity))
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
key = str(uuid.uuid4())
|
|
|
|
args = ['--file', os.path.join(tempdir, entities[0]), '--key', key, '--config', 'tests/samples/configs/good_config.cfg', '--time', now, '--extra-heartbeats']
|
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
response = CustomResponse()
|
|
|
|
response.response_code = 202
|
2017-11-25 00:10:15 +00:00
|
|
|
response.response_text = '{"responses": [[null,201], [null,500], [null,201], [null, 500]]}'
|
2017-11-23 02:34:15 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
2017-11-09 17:03:27 +00:00
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
|
2017-11-23 02:34:15 +00:00
|
|
|
heartbeats = json.dumps([{
|
|
|
|
'timestamp': now,
|
|
|
|
'entity': os.path.join(tempdir, entity),
|
|
|
|
'entity_type': 'file',
|
|
|
|
'is_write': False,
|
|
|
|
} for entity in entities[1:]])
|
|
|
|
mock_stdin.readline.return_value = heartbeats
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
|
|
|
|
expected = 'WakaTime WARNING Results from api not matching heartbeats sent.'
|
|
|
|
actual = self.getLogOutput(logs)
|
|
|
|
self.assertEquals(actual, expected)
|
|
|
|
|
|
|
|
queue = Queue(None, None)
|
|
|
|
self.assertEquals(queue._get_db_file(), fh.name)
|
2018-09-20 17:14:25 +00:00
|
|
|
saved_heartbeats = next(queue.pop_many())
|
2017-11-23 02:34:15 +00:00
|
|
|
self.assertNothingPrinted()
|
2017-11-09 17:03:27 +00:00
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
# make sure all offline heartbeats were sent, so queue should only have 1 heartbeat left from the second 500 response
|
|
|
|
self.assertEquals(len(saved_heartbeats), 1)
|
2017-11-09 17:03:27 +00:00
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
@log_capture()
|
|
|
|
def test_leftover_heartbeats_saved_when_bulk_response_not_matching_length(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2017-11-23 02:34:15 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
entities = [
|
|
|
|
'emptyfile.txt',
|
|
|
|
'twolinefile.txt',
|
|
|
|
'python.py',
|
|
|
|
'go.go',
|
|
|
|
'java.java',
|
|
|
|
'php.php',
|
|
|
|
]
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with TemporaryDirectory() as tempdir:
|
2017-11-23 02:34:15 +00:00
|
|
|
for entity in entities:
|
|
|
|
shutil.copy(os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, entity))
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
key = str(uuid.uuid4())
|
|
|
|
args = ['--file', os.path.join(tempdir, entities[0]), '--key', key, '--config', 'tests/samples/configs/good_config.cfg', '--time', now, '--extra-heartbeats']
|
2017-11-09 17:03:27 +00:00
|
|
|
|
|
|
|
response = CustomResponse()
|
2017-11-23 02:34:15 +00:00
|
|
|
response.response_code = 202
|
2017-11-25 00:10:15 +00:00
|
|
|
response.response_text = '{"responses": [[null,201], [null,201]]}'
|
2017-11-09 17:03:27 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
|
2017-11-09 17:03:27 +00:00
|
|
|
heartbeats = json.dumps([{
|
|
|
|
'timestamp': now,
|
|
|
|
'entity': os.path.join(tempdir, entity),
|
|
|
|
'entity_type': 'file',
|
|
|
|
'is_write': False,
|
|
|
|
} for entity in entities[1:]])
|
|
|
|
mock_stdin.readline.return_value = heartbeats
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
|
|
|
|
queue = Queue(None, None)
|
|
|
|
self.assertEquals(queue._get_db_file(), fh.name)
|
2018-09-20 17:14:25 +00:00
|
|
|
saved_heartbeats = next(queue.pop_many())
|
2017-11-09 17:03:27 +00:00
|
|
|
self.assertNothingPrinted()
|
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
expected = "WakaTime WARNING Missing 4 results from api.\nWakaTime WARNING Missing 2 results from api."
|
|
|
|
actual = self.getLogOutput(logs)
|
|
|
|
self.assertEquals(actual, expected)
|
|
|
|
|
|
|
|
# make sure extra heartbeats not matching server response were saved
|
|
|
|
self.assertEquals(len(saved_heartbeats), 2)
|
2017-11-09 17:03:27 +00:00
|
|
|
|
2016-08-31 20:37:02 +00:00
|
|
|
def test_auth_error_when_sending_offline_heartbeats(self):
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2016-08-31 20:37:02 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
now1 = u(int(time.time()))
|
|
|
|
entity1 = 'tests/samples/codefiles/emptyfile.txt'
|
|
|
|
project1 = 'proj1'
|
|
|
|
|
|
|
|
args = ['--file', entity1, '--config', config, '--time', now1, '--project', project1]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
now2 = u(int(time.time()))
|
|
|
|
entity2 = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
project2 = 'proj2'
|
|
|
|
|
|
|
|
args = ['--file', entity2, '--config', config, '--time', now2, '--project', project2]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
# send heartbeats from offline queue after 201 response
|
|
|
|
now3 = u(int(time.time()))
|
|
|
|
entity3 = 'tests/samples/codefiles/python.py'
|
|
|
|
project3 = 'proj3'
|
|
|
|
args = ['--file', entity3, '--config', config, '--time', now3, '--project', project3]
|
|
|
|
|
|
|
|
response = CustomResponse()
|
2017-11-23 02:34:15 +00:00
|
|
|
response.second_response_code = 401
|
|
|
|
response.limit = 2
|
2017-11-25 00:10:15 +00:00
|
|
|
response.response_text = '{"responses": [[null,201], [null,201], [null,201]]}'
|
2016-08-31 20:37:02 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-08-31 22:08:21 +00:00
|
|
|
retval = execute(args)
|
2017-11-23 02:34:15 +00:00
|
|
|
self.assertEquals(retval, AUTH_ERROR)
|
2016-08-31 22:08:21 +00:00
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
# offline queue should still have saved heartbeats
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
2018-09-20 17:14:25 +00:00
|
|
|
saved_heartbeats = next(queue.pop_many())
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2017-11-23 02:34:15 +00:00
|
|
|
self.assertEquals(len(saved_heartbeats), 2)
|
2016-08-31 22:08:21 +00:00
|
|
|
|
|
|
|
def test_500_error_when_sending_offline_heartbeats(self):
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2016-08-31 22:08:21 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
now1 = u(int(time.time()))
|
|
|
|
entity1 = 'tests/samples/codefiles/emptyfile.txt'
|
|
|
|
project1 = 'proj1'
|
|
|
|
|
|
|
|
args = ['--file', entity1, '--config', config, '--time', now1, '--project', project1]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
now2 = u(int(time.time()))
|
|
|
|
entity2 = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
project2 = 'proj2'
|
|
|
|
|
|
|
|
args = ['--file', entity2, '--config', config, '--time', now2, '--project', project2]
|
2016-08-31 20:37:02 +00:00
|
|
|
execute(args)
|
|
|
|
|
2016-08-31 22:08:21 +00:00
|
|
|
# send heartbeats from offline queue after 201 response
|
|
|
|
now3 = u(int(time.time()))
|
|
|
|
entity3 = 'tests/samples/codefiles/python.py'
|
|
|
|
project3 = 'proj3'
|
|
|
|
args = ['--file', entity3, '--config', config, '--time', now3, '--project', project3]
|
|
|
|
|
|
|
|
response = CustomResponse()
|
2017-11-23 02:34:15 +00:00
|
|
|
response.second_response_code = 500
|
|
|
|
response.limit = 2
|
2017-11-25 00:10:15 +00:00
|
|
|
response.response_text = '{"responses": [[null,201], [null,201], [null,201]]}'
|
2016-08-31 22:08:21 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
retval = execute(args)
|
2017-11-23 02:34:15 +00:00
|
|
|
self.assertEquals(retval, API_ERROR)
|
2016-08-31 22:08:21 +00:00
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
# offline queue should still have saved heartbeats
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
2018-09-20 17:14:25 +00:00
|
|
|
saved_heartbeats = next(queue.pop_many())
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2017-11-23 02:34:15 +00:00
|
|
|
self.assertEquals(len(saved_heartbeats), 2)
|
2016-08-31 20:37:02 +00:00
|
|
|
|
2015-09-07 01:47:17 +00:00
|
|
|
def test_empty_project_can_be_saved(self):
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2015-09-27 10:17:54 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/emptyfile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-27 10:17:54 +00:00
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
2015-09-27 10:17:54 +00:00
|
|
|
saved_heartbeat = queue.pop()
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(os.path.realpath(entity), saved_heartbeat['entity'])
|
2015-09-27 10:17:54 +00:00
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
def test_get_handles_exception_on_connect(self):
|
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2015-09-27 10:17:54 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-27 10:17:54 +00:00
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
2015-09-07 01:47:17 +00:00
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with mock.patch('wakatime.offlinequeue.Queue.connect') as mock_connect:
|
2015-09-27 10:17:54 +00:00
|
|
|
mock_connect.side_effect = sqlite3.Error('')
|
2015-09-07 01:47:17 +00:00
|
|
|
|
2015-09-27 10:17:54 +00:00
|
|
|
response.status_code = 201
|
|
|
|
execute(args)
|
2015-09-07 01:47:17 +00:00
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
2015-09-27 10:17:54 +00:00
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
2015-09-07 01:47:17 +00:00
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
2015-09-27 10:17:54 +00:00
|
|
|
saved_heartbeat = queue.pop()
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(os.path.realpath(entity), saved_heartbeat['entity'])
|
2015-09-27 10:32:28 +00:00
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
def test_push_handles_exception_on_connect(self):
|
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2015-09-27 10:32:28 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-27 10:32:28 +00:00
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with mock.patch('wakatime.offlinequeue.Queue.connect') as mock_connect:
|
2015-09-27 10:32:28 +00:00
|
|
|
mock_connect.side_effect = sqlite3.Error('')
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
response.status_code = 201
|
|
|
|
execute(args)
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
2015-09-27 10:32:28 +00:00
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
@log_capture()
|
2017-11-23 18:00:01 +00:00
|
|
|
def test_push_handles_exception_on_execute(self, logs):
|
2017-11-23 17:25:30 +00:00
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
2017-11-23 18:00:01 +00:00
|
|
|
mock_conn = mock.MagicMock()
|
|
|
|
|
|
|
|
mock_c = mock.MagicMock()
|
|
|
|
mock_c.execute.side_effect = sqlite3.Error('')
|
|
|
|
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue.connect') as mock_connect:
|
|
|
|
mock_connect.return_value = (mock_conn, mock_c)
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
response.status_code = 201
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue(None, None)
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
|
|
|
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_push_handles_exception_on_commit(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
mock_conn = mock.MagicMock()
|
|
|
|
mock_conn.commit.side_effect = sqlite3.Error('')
|
|
|
|
|
|
|
|
mock_c = mock.MagicMock()
|
|
|
|
mock_c.fetchone.return_value = None
|
|
|
|
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue.connect') as mock_connect:
|
|
|
|
mock_connect.return_value = (mock_conn, mock_c)
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
response.status_code = 201
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue(None, None)
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
|
|
|
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_push_handles_exception_on_close(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2017-11-23 17:25:30 +00:00
|
|
|
|
|
|
|
mock_conn = mock.MagicMock()
|
2017-11-23 18:00:01 +00:00
|
|
|
mock_conn.close.side_effect = sqlite3.Error('')
|
|
|
|
|
|
|
|
mock_c = mock.MagicMock()
|
|
|
|
mock_c.fetchone.return_value = None
|
2017-11-23 17:25:30 +00:00
|
|
|
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue.connect') as mock_connect:
|
2017-11-23 18:00:01 +00:00
|
|
|
mock_connect.return_value = (mock_conn, mock_c)
|
2017-11-23 17:25:30 +00:00
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
response.status_code = 201
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue(None, None)
|
|
|
|
saved_heartbeat = queue.pop()
|
|
|
|
self.assertEquals(None, saved_heartbeat)
|
|
|
|
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
|
2017-10-29 18:16:23 +00:00
|
|
|
def test_uses_home_folder_by_default(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
|
|
|
db_file = queue._get_db_file()
|
2015-09-27 10:32:28 +00:00
|
|
|
expected = os.path.join(os.path.expanduser('~'), '.wakatime.db')
|
|
|
|
self.assertEquals(db_file, expected)
|
2016-07-06 21:07:55 +00:00
|
|
|
|
2017-10-29 18:16:23 +00:00
|
|
|
def test_uses_wakatime_home_env_variable(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
2017-10-29 18:16:23 +00:00
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with TemporaryDirectory() as tempdir:
|
2017-10-29 18:16:23 +00:00
|
|
|
expected = os.path.realpath(os.path.join(tempdir, '.wakatime.db'))
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with mock.patch('os.environ.get') as mock_env:
|
2017-10-29 18:16:23 +00:00
|
|
|
mock_env.return_value = os.path.realpath(tempdir)
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
actual = queue._get_db_file()
|
2017-10-29 18:16:23 +00:00
|
|
|
self.assertEquals(actual, expected)
|
|
|
|
|
2016-07-06 21:07:55 +00:00
|
|
|
@log_capture()
|
|
|
|
def test_heartbeat_saved_when_requests_raises_exception(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2016-07-06 21:07:55 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
2016-08-31 10:23:38 +00:00
|
|
|
exception_msg = u("Oops, requests raised an exception. This is a test.")
|
2016-07-20 18:32:08 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].side_effect = AttributeError(exception_msg)
|
2016-07-06 21:07:55 +00:00
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
queue = Queue(None, None)
|
2016-07-06 21:07:55 +00:00
|
|
|
saved_heartbeat = queue.pop()
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(os.path.realpath(entity), saved_heartbeat['entity'])
|
2016-07-06 21:07:55 +00:00
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2016-07-06 21:07:55 +00:00
|
|
|
|
|
|
|
output = [u(' ').join(x) for x in logs.actual()]
|
2016-07-20 18:32:08 +00:00
|
|
|
self.assertIn(exception_msg, output[0])
|
2016-07-06 21:07:55 +00:00
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
2017-05-25 05:54:58 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
2016-07-06 21:07:55 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
2017-11-22 20:58:29 +00:00
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_nonascii_heartbeat_saved(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2017-11-22 20:58:29 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with TemporaryDirectory() as tempdir:
|
2017-11-22 20:58:29 +00:00
|
|
|
filename = list(filter(lambda x: x.endswith('.txt'), os.listdir(u('tests/samples/codefiles/unicode'))))[0]
|
|
|
|
entity = os.path.join('tests/samples/codefiles/unicode', filename)
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, filename))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, filename))
|
|
|
|
now = u(int(time.time()))
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
key = str(uuid.uuid4())
|
|
|
|
language = 'lang汉语' if self.isPy35OrNewer else 'lang\xe6\xb1\x89\xe8\xaf\xad'
|
|
|
|
project = 'proj汉语' if self.isPy35OrNewer else 'proj\xe6\xb1\x89\xe8\xaf\xad'
|
|
|
|
branch = 'branch汉语' if self.isPy35OrNewer else 'branch\xe6\xb1\x89\xe8\xaf\xad'
|
|
|
|
heartbeat = {
|
|
|
|
'language': u(language),
|
|
|
|
'lines': 0,
|
|
|
|
'entity': os.path.realpath(entity),
|
|
|
|
'project': u(project),
|
|
|
|
'branch': u(branch),
|
|
|
|
'time': float(now),
|
|
|
|
'type': 'file',
|
|
|
|
'is_write': False,
|
|
|
|
'user_agent': ANY,
|
|
|
|
'dependencies': [],
|
|
|
|
}
|
|
|
|
|
|
|
|
args = ['--file', entity, '--key', key, '--config', config, '--time', now]
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with mock.patch('wakatime.stats.standardize_language') as mock_language:
|
2018-12-06 05:32:53 +00:00
|
|
|
mock_language.return_value = language
|
2017-11-22 20:58:29 +00:00
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with mock.patch('wakatime.heartbeat.get_project_info') as mock_project:
|
2017-11-22 20:58:29 +00:00
|
|
|
mock_project.return_value = (project, branch)
|
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
self.assertNothingLogged(logs)
|
|
|
|
|
|
|
|
self.assertHeartbeatSent(heartbeat)
|
|
|
|
|
|
|
|
queue = Queue(None, None)
|
|
|
|
saved_heartbeat = queue.pop()
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(os.path.realpath(entity), saved_heartbeat['entity'])
|
2017-11-22 20:58:29 +00:00
|
|
|
self.assertEquals(u(language), saved_heartbeat['language'])
|
|
|
|
self.assertEquals(u(project), saved_heartbeat['project'])
|
|
|
|
self.assertEquals(u(branch), saved_heartbeat['branch'])
|
2017-11-23 02:54:47 +00:00
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_heartbeat_saved_when_bulk_result_json_decode_error(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2017-11-23 02:54:47 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = CustomResponse()
|
2017-11-25 00:10:15 +00:00
|
|
|
response.response_text = '{"responses": [[{id":1},201]]}'
|
2017-11-23 02:54:47 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue(None, None)
|
|
|
|
saved_heartbeat = queue.pop()
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(os.path.realpath(entity), saved_heartbeat['entity'])
|
2017-11-23 02:54:47 +00:00
|
|
|
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
expected = 'JSONDecodeError'
|
|
|
|
actual = self.getLogOutput(logs)
|
|
|
|
self.assertIn(expected, actual)
|
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_heartbeat_saved_from_result_type_error(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2017-11-23 02:54:47 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = CustomResponse()
|
2017-11-25 00:10:15 +00:00
|
|
|
response.response_text = '{"responses": [1]}'
|
2017-11-23 02:54:47 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue(None, None)
|
|
|
|
saved_heartbeat = queue.pop()
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(os.path.realpath(entity), saved_heartbeat['entity'])
|
2017-11-23 02:54:47 +00:00
|
|
|
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
expected = 'TypeError'
|
|
|
|
actual = self.getLogOutput(logs)
|
|
|
|
self.assertIn(expected, actual)
|
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_heartbeat_saved_from_result_index_error(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
2017-11-23 17:25:30 +00:00
|
|
|
with NamedTemporaryFile() as fh:
|
|
|
|
with mock.patch('wakatime.offlinequeue.Queue._get_db_file') as mock_db_file:
|
2017-11-23 02:54:47 +00:00
|
|
|
mock_db_file.return_value = fh.name
|
|
|
|
|
|
|
|
response = CustomResponse()
|
2017-11-25 00:10:15 +00:00
|
|
|
response.response_text = '{"responses": [[{"id":1}]]}'
|
2017-11-23 02:54:47 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
queue = Queue(None, None)
|
|
|
|
saved_heartbeat = queue.pop()
|
2018-10-01 02:53:52 +00:00
|
|
|
self.assertPathsEqual(os.path.realpath(entity), saved_heartbeat['entity'])
|
2017-11-23 02:54:47 +00:00
|
|
|
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
expected = 'IndexError'
|
|
|
|
actual = self.getLogOutput(logs)
|
|
|
|
self.assertIn(expected, actual)
|