rana-cli/tests/test_arguments.py

956 lines
37 KiB
Python
Raw Normal View History

2017-02-20 20:04:07 +00:00
# -*- coding: utf-8 -*-
from wakatime.main import execute
from wakatime.packages import requests
import logging
import os
import time
import shutil
import sys
import uuid
from testfixtures import log_capture
2017-11-09 06:54:33 +00:00
from wakatime.arguments import parse_arguments
2017-02-20 20:04:07 +00:00
from wakatime.compat import u
from wakatime.constants import (
AUTH_ERROR,
SUCCESS,
)
2017-11-09 06:54:33 +00:00
from wakatime.utils import get_user_agent
from .utils import mock, json, ANY, CustomResponse, TemporaryDirectory, TestCase
2017-02-20 20:04:07 +00:00
class ArgumentsTestCase(TestCase):
2017-02-20 20:04:07 +00:00
patch_these = [
'wakatime.packages.requests.adapters.HTTPAdapter.send',
'wakatime.offlinequeue.Queue.push',
['wakatime.offlinequeue.Queue.pop', None],
['wakatime.offlinequeue.Queue.connect', None],
'wakatime.session_cache.SessionCache.save',
'wakatime.session_cache.SessionCache.delete',
['wakatime.session_cache.SessionCache.get', requests.session],
['wakatime.session_cache.SessionCache.connect', None],
]
@log_capture()
def test_help_contents(self, logs):
logging.disable(logging.NOTSET)
2017-02-20 20:04:07 +00:00
args = ['--help']
with self.assertRaises(SystemExit) as e:
execute(args)
self.assertEquals(int(str(e.exception)), 0)
expected_stdout = open('tests/samples/output/test_help_contents').read()
self.assertEquals(sys.stdout.getvalue(), expected_stdout)
self.assertEquals(sys.stderr.getvalue(), '')
self.assertNothingLogged(logs)
2017-02-20 20:04:07 +00:00
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
@log_capture()
def test_argument_parsing(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
with TemporaryDirectory() as tempdir:
2017-02-20 20:04:07 +00:00
entity = 'tests/samples/codefiles/twolinefile.txt'
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
key = str(uuid.uuid4())
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--key', key, '--config', config]
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
self.assertNothingLogged(logs)
2017-02-20 20:04:07 +00:00
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()
2017-11-09 06:54:33 +00:00
@log_capture()
def test_argument_parsing_strips_quotes(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
entity = 'tests/samples/codefiles/python.py'
2017-11-09 06:54:33 +00:00
plugin = '"abc plugin\\"with quotes"'
args = ['--file', '"' + entity + '"', '--config', config, '--time', now, '--plugin', plugin]
retval = execute(args)
2017-11-09 06:54:33 +00:00
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
self.assertNothingLogged(logs)
2017-11-09 06:54:33 +00:00
ua = get_user_agent().replace('Unknown/0', 'abc plugin"with quotes')
heartbeat = {
'entity': os.path.realpath(entity),
'project': os.path.basename(os.path.abspath('.')),
'branch': ANY,
'time': float(now),
'type': 'file',
'cursorpos': None,
'dependencies': ['sqlalchemy', 'jinja', 'simplejson', 'flask', 'app', 'django', 'pygments', 'unittest', 'mock'],
'language': u('Python'),
'lineno': None,
'lines': 37,
'is_write': False,
'user_agent': ua,
}
self.assertHeartbeatSent(heartbeat)
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
@log_capture()
def test_lineno_and_cursorpos(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
entity = 'tests/samples/codefiles/twolinefile.txt'
config = 'tests/samples/configs/good_config.cfg'
now = u(int(time.time()))
heartbeat = {
'language': 'Text only',
'lines': 2,
'entity': os.path.realpath(entity),
'project': os.path.basename(os.path.abspath('.')),
'cursorpos': '4',
'lineno': '3',
2017-11-09 06:54:33 +00:00
'branch': ANY,
2017-02-20 20:04:07 +00:00
'time': float(now),
2017-11-09 06:54:33 +00:00
'is_write': False,
2017-02-20 20:04:07 +00:00
'type': 'file',
2017-11-09 06:54:33 +00:00
'dependencies': [],
'user_agent': ANY,
2017-02-20 20:04:07 +00:00
}
2017-11-09 06:54:33 +00:00
args = ['--entity', entity, '--config', config, '--time', now, '--lineno', '3', '--cursorpos', '4', '--verbose']
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
actual = self.getLogOutput(logs)
self.assertIn('WakaTime DEBUG Sending heartbeats to api', actual)
self.assertHeartbeatSent(heartbeat)
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
2017-02-20 20:04:07 +00:00
@log_capture()
def test_invalid_timeout_passed_via_command_line(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
with TemporaryDirectory() as tempdir:
2017-02-20 20:04:07 +00:00
entity = 'tests/samples/codefiles/twolinefile.txt'
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
config = 'tests/samples/configs/good_config.cfg'
key = str(uuid.uuid4())
args = ['--file', entity, '--key', key, '--config', config, '--timeout', 'abc']
with self.assertRaises(SystemExit) as e:
execute(args)
self.assertNothingLogged(logs)
2017-02-20 20:04:07 +00:00
self.assertEquals(int(str(e.exception)), 2)
self.assertEquals(sys.stdout.getvalue(), '')
expected_stderr = open('tests/samples/output/main_test_timeout_passed_via_command_line').read()
self.assertEquals(sys.stderr.getvalue(), expected_stderr)
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
@log_capture()
def test_missing_entity_file(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
entity = 'tests/samples/codefiles/missingfile.txt'
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--verbose']
retval = execute(args)
self.assertEquals(retval, SUCCESS)
2017-11-09 06:54:33 +00:00
self.assertNothingPrinted()
actual = self.getLogOutput(logs)
2017-02-20 20:04:07 +00:00
expected = 'WakaTime DEBUG File does not exist; ignoring this heartbeat.'
2017-11-09 06:54:33 +00:00
self.assertIn(expected, actual)
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
self.assertHeartbeatNotSent()
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheUntouched()
2017-02-20 20:04:07 +00:00
@log_capture()
def test_missing_entity_argument(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
config = 'tests/samples/configs/good_config.cfg'
args = ['--config', config]
with self.assertRaises(SystemExit) as e:
execute(args)
self.assertEquals(int(str(e.exception)), 2)
self.assertEquals(sys.stdout.getvalue(), '')
expected = 'error: argument --entity is required'
self.assertIn(expected, sys.stderr.getvalue())
log_output = u("\n").join([u(' ').join(x) for x in logs.actual()])
expected = ''
self.assertEquals(log_output, expected)
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
@log_capture()
def test_missing_api_key(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
config = 'tests/samples/configs/missing_api_key.cfg'
args = ['--config', config]
with self.assertRaises(SystemExit) as e:
execute(args)
self.assertEquals(int(str(e.exception)), AUTH_ERROR)
self.assertEquals(sys.stdout.getvalue(), '')
expected = 'error: Missing api key. Find your api key from wakatime.com/settings.'
self.assertIn(expected, sys.stderr.getvalue())
log_output = u("\n").join([u(' ').join(x) for x in logs.actual()])
expected = ''
self.assertEquals(log_output, expected)
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
@log_capture()
def test_invalid_api_key(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
key = 'an-invalid-key'
args = ['--key', key]
with self.assertRaises(SystemExit) as e:
execute(args)
self.assertEquals(int(str(e.exception)), AUTH_ERROR)
self.assertEquals(sys.stdout.getvalue(), '')
expected = 'error: Invalid api key. Find your api key from wakatime.com/settings.'
self.assertIn(expected, sys.stderr.getvalue())
log_output = u("\n").join([u(' ').join(x) for x in logs.actual()])
expected = ''
self.assertEquals(log_output, expected)
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
@log_capture()
def test_api_key_passed_via_command_line(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
with TemporaryDirectory() as tempdir:
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()))
key = str(uuid.uuid4())
args = ['--file', entity, '--key', key, '--time', now, '--config', 'fake-foobar']
retval = execute(args)
2017-11-09 06:54:33 +00:00
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
self.assertNothingLogged(logs)
heartbeat = {
'language': 'Text only',
'lines': 0,
'entity': os.path.realpath(entity),
2017-11-09 06:54:33 +00:00
'project': None,
'time': float(now),
'type': 'file',
2017-11-09 06:54:33 +00:00
'cursorpos': None,
'dependencies': [],
'lineno': None,
'is_write': False,
'user_agent': ANY,
}
2017-11-09 06:54:33 +00:00
self.assertHeartbeatSent(heartbeat)
2017-11-09 06:54:33 +00:00
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
@log_capture()
def test_proxy_argument(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
with TemporaryDirectory() as tempdir:
2017-02-20 20:04:07 +00:00
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'))
proxy = 'localhost:1337'
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--proxy', proxy]
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
self.assertNothingLogged(logs)
2017-02-20 20:04:07 +00:00
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()
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(ANY, cert=None, proxies={'https': proxy}, stream=False, timeout=60, verify=True)
@log_capture()
def test_disable_ssl_verify_argument(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
with 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/good_config.cfg'
args = ['--file', entity, '--config', config, '--no-ssl-verify']
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
self.assertNothingLogged(logs)
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()
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(ANY, cert=None, proxies=ANY, stream=False, timeout=60, verify=False)
2017-11-09 06:54:33 +00:00
@log_capture()
def test_write_argument(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
with TemporaryDirectory() as tempdir:
2017-02-20 20:04:07 +00:00
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'))
now = u(int(time.time()))
key = str(uuid.uuid4())
heartbeat = {
'language': 'Text only',
'lines': 0,
'entity': entity,
2017-11-09 06:54:33 +00:00
'project': None,
2017-02-20 20:04:07 +00:00
'time': float(now),
'type': 'file',
'is_write': True,
2017-11-09 06:54:33 +00:00
'dependencies': [],
'user_agent': ANY,
2017-02-20 20:04:07 +00:00
}
2017-11-09 06:54:33 +00:00
args = ['--file', entity, '--key', key, '--write', '--verbose',
'--config', 'tests/samples/configs/good_config.cfg', '--time', now]
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
actual = self.getLogOutput(logs)
self.assertIn('WakaTime DEBUG Sending heartbeats to api', actual)
self.assertHeartbeatSent(heartbeat)
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
@log_capture()
def test_entity_type_domain(self, logs):
logging.disable(logging.NOTSET)
2017-02-20 20:04:07 +00:00
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
entity = 'google.com'
config = 'tests/samples/configs/good_config.cfg'
now = u(int(time.time()))
args = ['--entity', entity, '--entity-type', 'domain', '--config', config, '--time', now]
retval = execute(args)
2017-11-09 06:54:33 +00:00
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
self.assertNothingLogged(logs)
2017-02-20 20:04:07 +00:00
heartbeat = {
'entity': u(entity),
'time': float(now),
'type': 'domain',
2017-11-09 06:54:33 +00:00
'cursorpos': None,
'language': None,
'lineno': None,
'lines': None,
'is_write': False,
'dependencies': [],
'user_agent': ANY,
2017-02-20 20:04:07 +00:00
}
2017-11-09 06:54:33 +00:00
self.assertHeartbeatSent(heartbeat)
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
@log_capture()
def test_entity_type_app(self, logs):
logging.disable(logging.NOTSET)
2017-02-20 20:04:07 +00:00
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
entity = 'Firefox'
config = 'tests/samples/configs/good_config.cfg'
now = u(int(time.time()))
args = ['--entity', entity, '--entity-type', 'app', '--config', config, '--time', now]
retval = execute(args)
2017-11-09 06:54:33 +00:00
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
self.assertNothingLogged(logs)
2017-02-20 20:04:07 +00:00
heartbeat = {
'entity': u(entity),
'time': float(now),
'type': 'app',
2017-11-09 06:54:33 +00:00
'cursorpos': None,
'dependencies': [],
'language': None,
'lineno': None,
'lines': None,
'is_write': False,
'user_agent': ANY,
2017-02-20 20:04:07 +00:00
}
2017-11-09 06:54:33 +00:00
self.assertHeartbeatSent(heartbeat)
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
@log_capture()
def test_old_alternate_language_argument_still_supported(self, logs):
logging.disable(logging.NOTSET)
2017-02-20 20:04:07 +00:00
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-11-09 06:54:33 +00:00
language = 'Java'
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
entity = 'tests/samples/codefiles/python.py'
2017-11-09 06:54:33 +00:00
args = ['--file', entity, '--config', config, '--time', now, '--alternate-language', language.upper()]
retval = execute(args)
2017-11-09 06:54:33 +00:00
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
self.assertNothingLogged(logs)
heartbeat = {
'entity': os.path.realpath(entity),
'project': os.path.basename(os.path.abspath('.')),
'branch': ANY,
'time': float(now),
'type': 'file',
'cursorpos': None,
'dependencies': [],
'language': u(language),
'lineno': None,
'lines': 37,
'is_write': False,
'user_agent': ANY,
}
self.assertHeartbeatSent(heartbeat)
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
@log_capture()
def test_extra_heartbeats_alternate_project_not_used(self, logs):
logging.disable(logging.NOTSET)
response = CustomResponse()
response.response_text = '[[{"id":1},201], [{"id":1},201]]'
2017-11-09 06:54:33 +00:00
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now1 = u(int(time.time()))
project1 = os.path.basename(os.path.abspath('.'))
project_not_used = 'xyz'
entity1 = os.path.abspath('tests/samples/codefiles/emptyfile.txt')
entity2 = os.path.abspath('tests/samples/codefiles/twolinefile.txt')
config = 'tests/samples/configs/good_config.cfg'
args = ['--time', now1, '--file', entity1, '--config', config, '--extra-heartbeats']
with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
2017-11-09 06:54:33 +00:00
now2 = int(time.time())
heartbeats = json.dumps([{
'timestamp': now2,
'entity': entity2,
'entity_type': 'file',
'alternate_project': project_not_used,
'is_write': True,
}])
mock_stdin.readline.return_value = heartbeats
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
self.assertNothingLogged(logs)
heartbeat = {
'language': 'Text only',
'lines': 0,
'entity': entity1,
'project': project1,
'branch': ANY,
'time': float(now1),
'is_write': False,
'type': 'file',
'dependencies': [],
'user_agent': ANY,
}
extra_heartbeats = [{
'language': 'Text only',
'lines': 2,
'entity': entity2,
'project': project1,
'branch': ANY,
'time': float(now2),
'is_write': True,
'type': 'file',
'dependencies': [],
'user_agent': ANY,
}]
self.assertHeartbeatSent(heartbeat, extra_heartbeats=extra_heartbeats)
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
@log_capture()
def test_extra_heartbeats_using_project_from_editor(self, logs):
logging.disable(logging.NOTSET)
response = CustomResponse()
response.response_text = '[[{"id":1},201], [{"id":1},201]]'
2017-11-09 06:54:33 +00:00
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now1 = u(int(time.time()))
project1 = os.path.basename(os.path.abspath('.'))
project2 = 'xyz'
entity1 = os.path.abspath('tests/samples/codefiles/emptyfile.txt')
entity2 = os.path.abspath('tests/samples/codefiles/twolinefile.txt')
config = 'tests/samples/configs/good_config.cfg'
args = ['--time', now1, '--file', entity1, '--config', config, '--extra-heartbeats']
with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
2017-11-09 06:54:33 +00:00
now2 = int(time.time())
heartbeats = json.dumps([{
'timestamp': now2,
'entity': entity2,
'entity_type': 'file',
'project': project2,
'is_write': True,
}])
mock_stdin.readline.return_value = heartbeats
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
self.assertNothingLogged(logs)
2017-11-09 06:54:33 +00:00
heartbeat = {
'language': 'Text only',
'lines': 0,
'entity': entity1,
'project': project1,
'branch': ANY,
'time': float(now1),
'is_write': False,
'type': 'file',
'dependencies': [],
'user_agent': ANY,
}
extra_heartbeats = [{
'language': 'Text only',
'lines': 2,
'entity': entity2,
'project': project2,
'branch': ANY,
'time': float(now2),
'is_write': True,
'type': 'file',
'dependencies': [],
'user_agent': ANY,
}]
self.assertHeartbeatSent(heartbeat, extra_heartbeats=extra_heartbeats)
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
@log_capture()
def test_extra_heartbeats_when_project_not_detected(self, logs):
logging.disable(logging.NOTSET)
response = CustomResponse()
response.response_text = '[[{"id":1},201], [{"id":1},201]]'
2017-02-20 20:04:07 +00:00
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with TemporaryDirectory() as tempdir:
2017-02-20 20:04:07 +00:00
entity = 'tests/samples/codefiles/twolinefile.txt'
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
2017-11-09 06:54:33 +00:00
now1 = u(int(time.time()))
2017-02-20 20:04:07 +00:00
project1 = os.path.basename(os.path.abspath('.'))
entity1 = os.path.abspath('tests/samples/codefiles/emptyfile.txt')
2017-11-09 06:54:33 +00:00
entity2 = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
2017-02-20 20:04:07 +00:00
config = 'tests/samples/configs/good_config.cfg'
2017-11-09 06:54:33 +00:00
args = ['--time', now1, '--file', entity1, '--config', config, '--extra-heartbeats']
2017-02-20 20:04:07 +00:00
with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
2017-11-09 06:54:33 +00:00
now2 = int(time.time())
2017-02-20 20:04:07 +00:00
heartbeats = json.dumps([{
2017-11-09 06:54:33 +00:00
'timestamp': now2,
2017-02-20 20:04:07 +00:00
'entity': entity2,
'entity_type': 'file',
'is_write': True,
}])
mock_stdin.readline.return_value = heartbeats
retval = execute(args)
self.assertEquals(retval, SUCCESS)
2017-11-09 06:54:33 +00:00
self.assertNothingPrinted()
self.assertNothingLogged(logs)
heartbeat = {
'language': 'Text only',
'lines': 0,
'entity': entity1,
'project': project1,
'branch': ANY,
'time': float(now1),
'is_write': False,
'type': 'file',
'dependencies': [],
'user_agent': ANY,
}
extra_heartbeats = [{
'language': 'Text only',
'lines': 2,
'entity': entity2,
'project': None,
'time': float(now2),
'is_write': True,
'type': 'file',
'dependencies': [],
'user_agent': ANY,
}]
self.assertHeartbeatSent(heartbeat, extra_heartbeats=extra_heartbeats)
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
@log_capture()
def test_extra_heartbeats_when_project_not_detected_alternate_project_used(self, logs):
logging.disable(logging.NOTSET)
2017-02-20 20:04:07 +00:00
response = CustomResponse()
response.response_text = '[[{"id":1},201], [{"id":1},201]]'
2017-11-09 06:54:33 +00:00
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
2017-02-20 20:04:07 +00:00
with TemporaryDirectory() as tempdir:
2017-11-09 06:54:33 +00:00
entity = 'tests/samples/codefiles/twolinefile.txt'
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
now1 = u(int(time.time()))
project1 = os.path.basename(os.path.abspath('.'))
project2 = 'xyz'
entity1 = os.path.abspath('tests/samples/codefiles/emptyfile.txt')
entity2 = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
config = 'tests/samples/configs/good_config.cfg'
args = ['--time', now1, '--file', entity1, '--config', config, '--extra-heartbeats']
with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
2017-11-09 06:54:33 +00:00
now2 = int(time.time())
heartbeats = json.dumps([{
'timestamp': now2,
'entity': entity2,
'alternate_project': project2,
'entity_type': 'file',
'is_write': True,
}])
mock_stdin.readline.return_value = heartbeats
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
self.assertNothingLogged(logs)
heartbeat = {
'language': 'Text only',
'lines': 0,
'entity': entity1,
'project': project1,
'branch': ANY,
'time': float(now1),
'is_write': False,
'type': 'file',
'dependencies': [],
'user_agent': ANY,
}
extra_heartbeats = [{
'language': 'Text only',
'lines': 2,
'entity': entity2,
'project': project2,
'time': float(now2),
'is_write': True,
'type': 'file',
'dependencies': [],
'user_agent': ANY,
}]
self.assertHeartbeatSent(heartbeat, extra_heartbeats=extra_heartbeats)
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
2017-02-20 20:04:07 +00:00
@log_capture()
def test_extra_heartbeats_with_malformed_json(self, logs):
logging.disable(logging.NOTSET)
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
with TemporaryDirectory() as tempdir:
2017-02-20 20:04:07 +00:00
entity = 'tests/samples/codefiles/twolinefile.txt'
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
entity = os.path.abspath('tests/samples/codefiles/emptyfile.txt')
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--extra-heartbeats']
with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
2017-02-20 20:04:07 +00:00
heartbeats = '[{foobar}]'
mock_stdin.readline.return_value = heartbeats
retval = execute(args)
2017-11-09 06:54:33 +00:00
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
actual = self.getLogOutput(logs)
self.assertIn('WakaTime WARNING Malformed extra heartbeats json', actual)
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
self.assertHeartbeatSent()
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
2017-11-23 17:25:30 +00:00
@log_capture()
def test_extra_heartbeats_with_null_heartbeat(self, logs):
logging.disable(logging.NOTSET)
response = CustomResponse()
response.response_text = '[[{"id":1},201], [{"id":1},201]]'
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now1 = u(int(time.time()))
project1 = os.path.basename(os.path.abspath('.'))
project_not_used = 'xyz'
entity1 = os.path.abspath('tests/samples/codefiles/emptyfile.txt')
entity2 = os.path.abspath('tests/samples/codefiles/twolinefile.txt')
config = 'tests/samples/configs/good_config.cfg'
args = ['--time', now1, '--file', entity1, '--config', config, '--extra-heartbeats']
with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
now2 = int(time.time())
heartbeats = json.dumps([
None,
{
'timestamp': now2,
'entity': entity2,
'entity_type': 'file',
'alternate_project': project_not_used,
'is_write': True,
},
])
mock_stdin.readline.return_value = heartbeats
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
self.assertNothingLogged(logs)
heartbeat = {
'language': 'Text only',
'lines': 0,
'entity': entity1,
'project': project1,
'branch': ANY,
'time': float(now1),
'is_write': False,
'type': 'file',
'dependencies': [],
'user_agent': ANY,
}
extra_heartbeats = [{
'language': 'Text only',
'lines': 2,
'entity': entity2,
'project': ANY,
'branch': ANY,
'time': float(now2),
'is_write': True,
'type': 'file',
'dependencies': [],
'user_agent': ANY,
}]
self.assertHeartbeatSent(heartbeat, extra_heartbeats=extra_heartbeats)
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
@log_capture()
def test_extra_heartbeats_with_skipped_heartbeat(self, logs):
logging.disable(logging.NOTSET)
response = CustomResponse()
response.response_text = '[[{"id":1},201], [{"id":1},201]]'
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now1 = u(int(time.time()))
project_not_used = 'xyz'
entity1 = os.path.abspath('tests/samples/codefiles/emptyfile.txt')
entity2 = os.path.abspath('tests/samples/codefiles/twolinefile.txt')
config = 'tests/samples/configs/good_config.cfg'
args = ['--time', now1, '--file', entity1, '--config', config, '--extra-heartbeats', '--exclude', 'twoline']
with mock.patch('wakatime.main.sys.stdin') as mock_stdin:
now2 = int(time.time())
heartbeats = json.dumps([
{
'timestamp': now2,
'entity': entity2,
'entity_type': 'file',
'alternate_project': project_not_used,
'is_write': True,
},
])
mock_stdin.readline.return_value = heartbeats
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
actual = self.getLogOutput(logs)
expected = 'WakaTime WARNING Results from api not matching heartbeats sent.'
self.assertIn(expected, actual)
heartbeat = {
'language': 'Text only',
'lines': 0,
'entity': entity1,
'project': ANY,
'branch': ANY,
'time': float(now1),
'is_write': False,
'type': 'file',
'dependencies': [],
'user_agent': ANY,
}
self.assertHeartbeatSent(heartbeat)
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
@log_capture()
def test_uses_wakatime_home_env_variable(self, logs):
logging.disable(logging.NOTSET)
with TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/twolinefile.txt'
shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt'))
entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt'))
key = str(uuid.uuid4())
config = 'tests/samples/configs/good_config.cfg'
logfile = os.path.realpath(os.path.join(tempdir, '.wakatime.log'))
args = ['--file', entity, '--key', key, '--config', config]
with mock.patch.object(sys, 'argv', ['wakatime'] + args):
2017-11-09 06:54:33 +00:00
args, configs = parse_arguments()
self.assertEquals(args.logfile, None)
with mock.patch('os.environ.get') as mock_env:
mock_env.return_value = os.path.realpath(tempdir)
2017-11-09 06:54:33 +00:00
args, configs = parse_arguments()
self.assertEquals(args.logfile, logfile)
self.assertNothingPrinted()
self.assertNothingLogged(logs)