rana-cli/tests/test_configs.py

674 lines
28 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
2017-11-08 02:52:30 +00:00
import base64
2017-02-20 20:04:07 +00:00
import logging
import os
import time
import re
import shutil
import sys
import uuid
from testfixtures import log_capture
from wakatime.compat import u, is_py3
from wakatime.constants import (
AUTH_ERROR,
2017-02-20 20:04:07 +00:00
CONFIG_FILE_PARSE_ERROR,
SUCCESS,
)
from wakatime.packages.requests.models import Response
from .utils import mock, ANY, CustomResponse, TemporaryDirectory, TestCase
2017-02-20 20:04:07 +00:00
class ConfigsTestCase(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],
]
def test_config_file_not_passed_in_command_line_args(self):
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'))
args = ['--file', entity, '--log-file', '~/.wakatime.log']
2017-02-20 20:04:07 +00:00
with mock.patch('wakatime.configs.os.environ.get') as mock_env:
2017-02-20 20:04:07 +00:00
mock_env.return_value = None
with mock.patch('wakatime.configs.open') as mock_open:
2017-02-20 20:04:07 +00:00
mock_open.side_effect = IOError('')
with self.assertRaises(SystemExit) as e:
execute(args)
self.assertEquals(int(str(e.exception)), AUTH_ERROR)
expected_stdout = u('')
expected_stderr = open('tests/samples/output/common_usage_header').read()
expected_stderr += open('tests/samples/output/configs_test_config_file_not_passed_in_command_line_args').read()
self.assertEquals(sys.stdout.getvalue(), expected_stdout)
self.assertEquals(sys.stderr.getvalue(), expected_stderr)
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
@log_capture()
def test_config_file_from_env(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'))
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 mock.patch('wakatime.configs.os.environ.get') as mock_env:
2017-02-20 20:04:07 +00:00
mock_env.return_value = tempdir
args = ['--file', entity, '--log-file', '~/.wakatime.log']
2017-02-20 20:04:07 +00:00
retval = execute(args)
self.assertEquals(retval, SUCCESS)
2017-11-09 06:54:33 +00:00
expected_stdout = open('tests/samples/output/configs_test_good_config_file').read()
2017-02-20 20:04:07 +00:00
traceback_file = os.path.realpath('wakatime/arguments.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(), '')
2017-11-09 06:54:33 +00:00
self.assertHeartbeatSent(proxies=ANY, verify=ANY)
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
def test_missing_config_file(self):
config = 'foo'
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'))
args = ['--file', entity, '--config', config, '--log-file', '~/.wakatime.log']
2017-02-20 20:04:07 +00:00
with self.assertRaises(SystemExit) as e:
execute(args)
self.assertEquals(int(str(e.exception)), AUTH_ERROR)
2017-02-20 20:04:07 +00:00
expected_stdout = u('')
expected_stderr = open('tests/samples/output/common_usage_header').read()
expected_stderr += open('tests/samples/output/configs_test_missing_config_file').read()
self.assertEquals(sys.stdout.getvalue(), expected_stdout)
self.assertEquals(sys.stderr.getvalue(), expected_stderr)
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
2017-02-20 20:04:07 +00:00
def test_good_config_file(self):
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'))
config = 'tests/samples/configs/has_everything.cfg'
args = ['--file', entity, '--config', config, '--log-file', '~/.wakatime.log']
2017-02-20 20:04:07 +00:00
retval = execute(args)
self.assertEquals(retval, SUCCESS)
2017-11-09 06:54:33 +00:00
expected_stdout = open('tests/samples/output/configs_test_good_config_file').read()
2017-02-20 20:04:07 +00:00
traceback_file = os.path.realpath('wakatime/arguments.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(), '')
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_api_key_setting_without_underscore_accepted(self):
"""Api key in wakatime.cfg should also work without an underscore:
apikey = XXX
"""
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'))
config = 'tests/samples/configs/sample_alternate_apikey.cfg'
args = ['--file', entity, '--config', config, '--log-file', '~/.wakatime.log']
2017-02-20 20:04:07 +00:00
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertEquals(sys.stdout.getvalue(), '')
self.assertEquals(sys.stderr.getvalue(), '')
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()
@log_capture()
def test_bad_config_file(self, logs):
logging.disable(logging.NOTSET)
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'))
config = 'tests/samples/configs/bad_config.cfg'
args = ['--file', entity, '--config', config, '--log-file', '~/.wakatime.log']
with self.assertRaises(SystemExit) as e:
execute(args)
self.assertEquals(int(str(e.exception)), CONFIG_FILE_PARSE_ERROR)
2017-02-20 20:04:07 +00:00
self.assertIn('ParsingError', sys.stdout.getvalue())
expected_stderr = ''
self.assertEquals(sys.stderr.getvalue(), expected_stderr)
log_output = u("\n").join([u(' ').join(x) for x in logs.actual()])
expected = ''
self.assertEquals(log_output, expected)
2017-02-20 20:04:07 +00:00
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
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()
2017-11-09 06:54:33 +00:00
@log_capture()
def test_non_hidden_filename(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'))
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
key = str(uuid.uuid4())
args = ['--file', entity, '--key', key, '--config', config, '--time', now, '--log-file', '~/.wakatime.log']
2017-02-20 20:04:07 +00:00
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': os.path.realpath(entity),
2017-11-09 06:54:33 +00:00
'project': None,
'branch': None,
2017-02-20 20:04:07 +00:00
'time': float(now),
'type': 'file',
2017-11-09 06:54:33 +00:00
'cursorpos': None,
'dependencies': [],
'language': u('Text only'),
'lineno': None,
'lines': 2,
'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()
2017-02-20 20:04:07 +00:00
def test_hide_all_filenames(self):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
with 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'))
2017-02-20 20:04:07 +00:00
now = u(int(time.time()))
config = 'tests/samples/configs/paranoid.cfg'
2017-11-09 06:54:33 +00:00
key = u(uuid.uuid4())
project = 'abcxyz'
2017-02-20 20:04:07 +00:00
args = ['--file', entity, '--key', key, '--config', config, '--time', now, '--log-file', '~/.wakatime.log', '--alternate-project', project]
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
heartbeat = {
'language': 'Python',
'lines': None,
'entity': 'HIDDEN.py',
'project': project,
'time': float(now),
'is_write': False,
'type': 'file',
'dependencies': None,
'user_agent': ANY,
}
self.assertHeartbeatSent(heartbeat)
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
def test_legacy_hidefilenames_config_supported(self):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
with 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'))
now = u(int(time.time()))
config = 'tests/samples/configs/paranoid_legacy.cfg'
key = u(uuid.uuid4())
project = 'abcxyz'
args = ['--file', entity, '--key', key, '--config', config, '--time', now, '--log-file', '~/.wakatime.log', '--alternate-project', project]
2017-02-20 20:04:07 +00:00
retval = execute(args)
2017-11-09 06:54:33 +00:00
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
2017-02-20 20:04:07 +00:00
heartbeat = {
'language': 'Python',
2017-11-09 06:54:33 +00:00
'lines': None,
'entity': 'HIDDEN.py',
2017-11-09 06:54:33 +00:00
'project': project,
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': None,
'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()
2017-02-20 20:04:07 +00:00
def test_hide_all_filenames_from_cli_arg(self):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
with 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'))
2017-02-20 20:04:07 +00:00
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
key = str(uuid.uuid4())
2017-11-09 06:54:33 +00:00
project = 'abcxyz'
2017-02-20 20:04:07 +00:00
args = ['--file', entity, '--key', key, '--config', config, '--time', now, '--hide-filenames', '--log-file', '~/.wakatime.log', '--alternate-project', project]
2017-02-20 20:04:07 +00:00
retval = execute(args)
2017-11-09 06:54:33 +00:00
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
2017-02-20 20:04:07 +00:00
heartbeat = {
'language': 'Python',
2017-11-09 06:54:33 +00:00
'lines': None,
'entity': 'HIDDEN.py',
2017-11-09 06:54:33 +00:00
'project': project,
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': None,
'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()
2017-02-20 20:04:07 +00:00
def test_hide_matching_filenames(self):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
with 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'))
2017-02-20 20:04:07 +00:00
now = u(int(time.time()))
config = 'tests/samples/configs/hide_file_names.cfg'
2017-11-09 06:54:33 +00:00
key = '033c47c9-0441-4eb5-8b3f-b51f27b31049'
project = 'abcxyz'
2017-02-20 20:04:07 +00:00
args = ['--file', entity, '--key', key, '--config', config, '--time', now, '--log-file', '~/.wakatime.log', '--alternate-project', project]
2017-02-20 20:04:07 +00:00
retval = execute(args)
2017-11-09 06:54:33 +00:00
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
2017-02-20 20:04:07 +00:00
heartbeat = {
'language': 'Python',
2017-11-09 06:54:33 +00:00
'lines': None,
'entity': 'HIDDEN.py',
2017-11-09 06:54:33 +00:00
'project': project,
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': None,
'user_agent': ANY,
2017-02-20 20:04:07 +00:00
}
2017-11-09 06:54:33 +00:00
headers = {
'Authorization': u('Basic {0}').format(u(base64.b64encode(str.encode(key) if is_py3 else key))),
}
self.assertHeartbeatSent(heartbeat, headers=headers)
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
2017-11-08 02:52:30 +00:00
2017-02-20 20:04:07 +00:00
def test_does_not_hide_unmatching_filenames(self):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
2017-02-20 20:04:07 +00:00
with 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'))
2017-02-20 20:04:07 +00:00
now = u(int(time.time()))
config = 'tests/samples/configs/hide_file_names_not_python.cfg'
2017-11-09 06:54:33 +00:00
key = u(uuid.uuid4())
dependencies = ['sqlalchemy', 'jinja', 'simplejson', 'flask', 'app', 'django', 'pygments', 'unittest', 'mock']
2017-11-09 06:54:33 +00:00
project = 'abcxyz'
2017-02-20 20:04:07 +00:00
args = ['--file', entity, '--key', key, '--config', config, '--time', now, '--log-file', '~/.wakatime.log', '--alternate-project', project]
2017-02-20 20:04:07 +00:00
retval = execute(args)
2017-11-09 06:54:33 +00:00
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
2017-02-20 20:04:07 +00:00
heartbeat = {
'language': 'Python',
'lines': 37,
2017-02-20 20:04:07 +00:00
'entity': entity,
2017-11-09 06:54:33 +00:00
'project': project,
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': 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()
2017-02-20 20:04:07 +00:00
@log_capture()
def test_does_not_hide_file_names_from_invalid_regex(self, logs):
2017-02-20 20:04:07 +00:00
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()))
config = 'tests/samples/configs/invalid_hide_file_names.cfg'
key = str(uuid.uuid4())
args = ['--file', entity, '--key', key, '--config', config, '--time', now, '--log-file', '~/.wakatime.log']
2017-02-20 20:04:07 +00:00
retval = execute(args)
2017-11-09 06:54:33 +00:00
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
actual = self.getLogOutput(logs)
expected = u('WakaTime WARNING Regex error (unbalanced parenthesis) for hide_file_names pattern: invalid(regex')
2017-02-20 20:04:07 +00:00
if self.isPy35OrNewer:
expected = 'WakaTime WARNING Regex error (missing ), unterminated subpattern at position 7) for hide_file_names pattern: invalid(regex'
2017-11-09 06:54:33 +00:00
self.assertEquals(expected, actual)
2017-02-20 20:04:07 +00:00
heartbeat = {
'language': 'Text only',
'lines': 0,
2017-11-09 06:54:33 +00:00
'entity': os.path.realpath(entity),
'project': None,
'cursorpos': None,
'lineno': None,
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
self.assertHeartbeatSent(heartbeat)
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
2017-02-20 20:04:07 +00:00
2018-07-18 08:01:31 +00:00
def test_obfuscte_project_names(self):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
with TemporaryDirectory() as tempdir:
shutil.copytree('tests/samples/projects/git', os.path.join(tempdir, 'git'))
shutil.move(os.path.join(tempdir, 'git', 'dot_git'), os.path.join(tempdir, 'git', '.git'))
entity = os.path.join(tempdir, 'git', 'emptyfile.txt')
now = u(int(time.time()))
config = 'tests/samples/configs/paranoid_projects.cfg'
key = u(uuid.uuid4())
dependencies = []
generated_proj = 'Icy Bridge 42'
args = ['--file', entity, '--key', key, '--config', config, '--time', now, '--log-file', '~/.wakatime.log']
with mock.patch('wakatime.project.generate_project_name') as mock_proj:
mock_proj.return_value = generated_proj
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
heartbeat = {
'language': 'Text only',
'lines': 0,
'entity': os.path.realpath(entity),
'project': generated_proj,
'time': float(now),
'is_write': False,
'type': 'file',
'dependencies': dependencies,
'user_agent': ANY,
}
self.assertHeartbeatSent(heartbeat)
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
2017-02-20 20:04:07 +00:00
@log_capture()
def test_exclude_file(self, logs):
logging.disable(logging.NOTSET)
response = Response()
response.status_code = 0
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/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, '--exclude', 'empty', '--verbose', '--log-file', '~/.wakatime.log']
2017-02-20 20:04:07 +00:00
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 Skipping because matches exclude pattern: empty'
2017-11-09 06:54:33 +00:00
self.assertEquals(actual, expected)
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_exclude_file_without_project_file(self, logs):
logging.disable(logging.NOTSET)
response = Response()
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
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/include_only_with_project_file.cfg'
args = ['--file', entity, '--config', config, '--verbose', '--log-file', '~/.wakatime.log']
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
actual = self.getLogOutput(logs)
expected = 'WakaTime DEBUG Skipping because missing .wakatime-project file in parent path.'
self.assertEquals(actual, expected)
self.assertHeartbeatNotSent()
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheUntouched()
@log_capture()
def test_exclude_file_because_project_unknown(self, logs):
logging.disable(logging.NOTSET)
response = Response()
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
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/exclude_unknown_project.cfg'
args = ['--file', entity, '--config', config, '--verbose', '--log-file', '~/.wakatime.log']
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
actual = self.getLogOutput(logs)
expected = 'WakaTime DEBUG Skipping because project unknown.'
self.assertEquals(actual, expected)
self.assertHeartbeatNotSent()
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheUntouched()
@log_capture()
def test_include_file_with_project_file(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/include_only_with_project_file.cfg'
project = 'abcxyz'
now = u(int(time.time()))
with open(os.path.join(tempdir, '.wakatime-project'), 'w'):
pass
args = ['--file', entity, '--config', config, '--time', now, '--verbose', '--log-file', '~/.wakatime.log', '--project', project]
retval = execute(args)
self.assertEquals(retval, SUCCESS)
self.assertNothingPrinted()
heartbeat = {
'language': 'Text only',
'lines': 0,
'entity': os.path.realpath(entity),
'project': project,
'branch': ANY,
'cursorpos': None,
'lineno': None,
'time': float(now),
'is_write': False,
'type': 'file',
'dependencies': [],
'user_agent': ANY,
}
self.assertHeartbeatSent(heartbeat)
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
2017-02-20 20:04:07 +00:00
def test_hostname_set_from_config_file(self):
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'))
hostname = 'fromcfgfile'
config = 'tests/samples/configs/has_everything.cfg'
args = ['--file', entity, '--config', config, '--timeout', '15', '--log-file', '~/.wakatime.log']
2017-02-20 20:04:07 +00:00
retval = execute(args)
self.assertEquals(retval, SUCCESS)
2017-11-09 06:54:33 +00:00
self.assertNothingPrinted()
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
headers = {
'X-Machine-Name': hostname.encode('utf-8') if is_py3 else hostname,
}
self.assertHeartbeatSent(headers=headers, proxies=ANY, timeout=15)
2017-02-20 20:04:07 +00:00
2017-11-09 06:54:33 +00:00
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()
def test_no_ssl_verify_from_config_file(self):
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/has_ssl_verify_disabled.cfg'
args = ['--file', entity, '--config', config, '--timeout', '15', '--log-file', '~/.wakatime.log']
retval = execute(args)
self.assertEquals(retval, SUCCESS)
2017-11-09 06:54:33 +00:00
self.assertNothingPrinted()
2017-11-09 06:54:33 +00:00
self.assertHeartbeatSent(proxies=ANY, timeout=15, verify=False)
2017-11-09 06:54:33 +00:00
self.assertHeartbeatNotSavedOffline()
self.assertOfflineHeartbeatsSynced()
self.assertSessionCacheSaved()