2015-09-07 20:25:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
from wakatime.main import execute
|
|
|
|
from wakatime.packages import requests
|
|
|
|
from wakatime.packages.requests.models import Response
|
|
|
|
|
2016-07-06 15:30:20 +00:00
|
|
|
import logging
|
2015-09-07 20:41:29 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
2016-07-06 15:31:24 +00:00
|
|
|
import sys
|
2015-09-07 20:41:29 +00:00
|
|
|
import tempfile
|
2015-09-07 20:25:08 +00:00
|
|
|
import time
|
2016-07-06 15:30:20 +00:00
|
|
|
from testfixtures import log_capture
|
2015-09-07 20:25:08 +00:00
|
|
|
from wakatime.compat import u
|
2015-09-27 02:23:38 +00:00
|
|
|
from wakatime.exceptions import NotYetImplemented
|
|
|
|
from wakatime.projects.base import BaseProject
|
2015-09-07 20:25:08 +00:00
|
|
|
from . import utils
|
|
|
|
|
|
|
|
|
|
|
|
class LanguagesTestCase(utils.TestCase):
|
|
|
|
patch_these = [
|
|
|
|
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
|
|
|
'wakatime.offlinequeue.Queue.push',
|
|
|
|
['wakatime.offlinequeue.Queue.pop', None],
|
2015-09-27 10:17:54 +00:00
|
|
|
['wakatime.offlinequeue.Queue.connect', None],
|
2015-09-07 20:25:08 +00:00
|
|
|
'wakatime.session_cache.SessionCache.save',
|
|
|
|
'wakatime.session_cache.SessionCache.delete',
|
|
|
|
['wakatime.session_cache.SessionCache.get', requests.session],
|
2015-09-27 10:17:54 +00:00
|
|
|
['wakatime.session_cache.SessionCache.connect', None],
|
2015-09-07 20:25:08 +00:00
|
|
|
]
|
|
|
|
|
2015-09-27 02:23:38 +00:00
|
|
|
def test_project_base(self):
|
|
|
|
path = 'tests/samples/codefiles/see.h'
|
|
|
|
project = BaseProject(path)
|
|
|
|
|
|
|
|
with self.assertRaises(NotYetImplemented):
|
|
|
|
project.process()
|
|
|
|
|
|
|
|
with self.assertRaises(NotYetImplemented):
|
|
|
|
project.name()
|
|
|
|
|
|
|
|
with self.assertRaises(NotYetImplemented):
|
|
|
|
project.branch()
|
|
|
|
|
2015-09-07 20:25:08 +00:00
|
|
|
def test_project_argument_overrides_detected_project(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/projects/git/emptyfile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-07 20:25:08 +00:00
|
|
|
|
|
|
|
args = ['--project', 'forced-project', '--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertEquals('forced-project', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
|
|
|
|
2016-04-28 17:25:52 +00:00
|
|
|
def test_alternate_project_argument_does_not_override_detected_project(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/projects/git/emptyfile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
project = os.path.basename(os.path.abspath('.'))
|
|
|
|
|
|
|
|
args = ['--alternate-project', 'alt-project', '--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertEquals(project, self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
|
|
|
|
|
|
|
def test_alternate_project_argument_does_not_override_project_argument(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/projects/git/emptyfile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--project', 'forced-project', '--alternate-project', 'alt-project', '--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertEquals('forced-project', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
|
|
|
|
|
|
|
def test_alternate_project_argument_used_when_project_not_detected(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
tempdir = tempfile.mkdtemp()
|
|
|
|
entity = 'tests/samples/projects/git/emptyfile.txt'
|
|
|
|
shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt'))
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = os.path.join(tempdir, 'emptyfile.txt')
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now, '--alternate-project', 'alt-project']
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
calls = self.patched['wakatime.offlinequeue.Queue.push'].call_args_list
|
|
|
|
self.assertEquals(None, calls[0][0][0].get('project'))
|
|
|
|
self.assertEquals('alt-project', calls[1][0][0]['project'])
|
|
|
|
|
2015-09-07 20:25:08 +00:00
|
|
|
def test_wakatime_project_file(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/projects/wakatime_project_file/emptyfile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-07 20:25:08 +00:00
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertEquals('waka-project-file', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
|
|
|
|
|
|
|
def test_git_project_detected(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2015-09-07 20:41:29 +00:00
|
|
|
tempdir = tempfile.mkdtemp()
|
|
|
|
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'))
|
|
|
|
|
2015-09-07 20:25:08 +00:00
|
|
|
now = u(int(time.time()))
|
2015-09-07 20:41:29 +00:00
|
|
|
entity = os.path.join(tempdir, 'git', 'emptyfile.txt')
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-07 20:25:08 +00:00
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertEquals('git', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
2016-05-21 12:21:55 +00:00
|
|
|
self.assertEquals('master', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['branch'])
|
|
|
|
|
|
|
|
def test_ioerror_when_reading_git_branch(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
tempdir = tempfile.mkdtemp()
|
|
|
|
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'))
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = os.path.join(tempdir, 'git', 'emptyfile.txt')
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.projects.git.open') as mock_open:
|
|
|
|
mock_open.side_effect = IOError('')
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertEquals('git', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
|
|
|
self.assertEquals('master', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('branch'))
|
2015-09-07 20:25:08 +00:00
|
|
|
|
2016-06-01 08:08:00 +00:00
|
|
|
def test_git_detached_head_not_used_as_branch(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
tempdir = tempfile.mkdtemp()
|
|
|
|
shutil.copytree('tests/samples/projects/git-with-detached-head', os.path.join(tempdir, 'git'))
|
|
|
|
shutil.move(os.path.join(tempdir, 'git', 'dot_git'), os.path.join(tempdir, 'git', '.git'))
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = os.path.join(tempdir, 'git', 'emptyfile.txt')
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertEquals('git', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
|
|
|
self.assertNotIn('branch', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0])
|
|
|
|
|
2015-09-07 20:25:08 +00:00
|
|
|
def test_svn_project_detected(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-05-21 12:21:55 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/projects/svn/afolder/emptyfile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
2015-09-07 20:25:08 +00:00
|
|
|
with utils.mock.patch('wakatime.projects.git.Git.process') as mock_git:
|
|
|
|
mock_git.return_value = False
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.projects.subversion.Popen.communicate') as mock_popen:
|
2015-09-08 22:48:28 +00:00
|
|
|
stdout = open('tests/samples/output/svn').read()
|
2015-09-07 20:25:08 +00:00
|
|
|
stderr = ''
|
|
|
|
mock_popen.return_value = (stdout, stderr)
|
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
2016-05-21 12:21:55 +00:00
|
|
|
self.assertEquals('svn', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
2015-09-08 03:43:06 +00:00
|
|
|
|
2015-09-08 04:25:21 +00:00
|
|
|
def test_svn_exception_handled(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.projects.git.Git.process') as mock_git:
|
|
|
|
mock_git.return_value = False
|
|
|
|
|
2016-05-21 12:21:55 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/projects/svn/afolder/emptyfile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
2015-09-08 04:25:21 +00:00
|
|
|
with utils.mock.patch('wakatime.projects.subversion.Popen') as mock_popen:
|
|
|
|
mock_popen.side_effect = OSError('')
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.projects.subversion.Popen.communicate') as mock_communicate:
|
|
|
|
mock_communicate.side_effect = OSError('')
|
|
|
|
|
2016-05-21 12:21:55 +00:00
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertNotIn('project', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0])
|
|
|
|
|
|
|
|
def test_svn_on_mac(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/projects/svn/afolder/emptyfile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.projects.git.Git.process') as mock_git:
|
|
|
|
mock_git.return_value = False
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.projects.subversion.platform.system') as mock_system:
|
|
|
|
mock_system.return_value = 'Darwin'
|
2015-09-08 04:25:21 +00:00
|
|
|
|
2016-05-21 12:21:55 +00:00
|
|
|
with utils.mock.patch('wakatime.projects.subversion.Popen') as mock_popen:
|
|
|
|
mock_popen.side_effect = OSError('')
|
2015-09-08 04:25:21 +00:00
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
2016-05-21 12:21:55 +00:00
|
|
|
self.assertNotIn('project', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0])
|
2015-09-08 04:25:21 +00:00
|
|
|
|
2015-09-08 04:40:01 +00:00
|
|
|
def test_mercurial_project_detected(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.projects.git.Git.process') as mock_git:
|
|
|
|
mock_git.return_value = False
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/projects/hg/emptyfile.txt'
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2015-09-08 04:40:01 +00:00
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertEquals('hg', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
|
|
|
self.assertEquals('test-hg-branch', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['branch'])
|
|
|
|
|
2016-05-21 12:21:55 +00:00
|
|
|
def test_ioerror_when_reading_mercurial_branch(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.projects.git.Git.process') as mock_git:
|
|
|
|
mock_git.return_value = False
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/projects/hg/emptyfile.txt'
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.projects.mercurial.open') as mock_open:
|
|
|
|
mock_open.side_effect = IOError('')
|
|
|
|
execute(args)
|
|
|
|
|
|
|
|
self.assertEquals('hg', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
|
|
|
self.assertEquals('default', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['branch'])
|
|
|
|
|
2015-09-08 03:43:06 +00:00
|
|
|
def test_project_map(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2016-04-18 19:34:00 +00:00
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/projects/project_map/emptyfile.txt'
|
|
|
|
config = 'tests/samples/configs/project_map.cfg'
|
2015-09-08 03:43:06 +00:00
|
|
|
|
2016-04-18 19:34:00 +00:00
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
2015-09-08 03:43:06 +00:00
|
|
|
|
2016-04-18 19:34:00 +00:00
|
|
|
execute(args)
|
2015-09-08 03:43:06 +00:00
|
|
|
|
2016-04-18 19:34:00 +00:00
|
|
|
self.assertEquals('proj-map', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
2015-09-08 03:43:06 +00:00
|
|
|
|
2016-04-18 19:34:00 +00:00
|
|
|
def test_project_map_group_usage(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/projects/project_map42/emptyfile.txt'
|
|
|
|
config = 'tests/samples/configs/project_map.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
execute(args)
|
2015-09-08 03:43:06 +00:00
|
|
|
|
2016-04-18 19:34:00 +00:00
|
|
|
self.assertEquals('proj-map42', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
2016-07-06 15:30:20 +00:00
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_project_map_with_invalid_regex(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
entity = 'tests/samples/projects/project_map42/emptyfile.txt'
|
|
|
|
config = 'tests/samples/configs/project_map_invalid.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
execute(args)
|
|
|
|
|
2016-07-06 15:31:24 +00:00
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
2016-07-06 15:30:20 +00:00
|
|
|
output = [u(' ').join(x) for x in logs.actual()]
|
|
|
|
expected = u('WakaTime WARNING Regex error (unexpected end of regular expression) for projectmap pattern: invalid[({regex')
|
|
|
|
if self.isPy35:
|
|
|
|
expected = u('WakaTime WARNING Regex error (unterminated character set at position 7) for projectmap pattern: invalid[({regex')
|
|
|
|
self.assertEquals(output[0], expected)
|