2015-08-11 18:19:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
2015-08-29 23:16:14 +00:00
|
|
|
from wakatime.main import execute
|
2015-08-12 21:21:32 +00:00
|
|
|
from wakatime.packages import requests
|
|
|
|
|
|
|
|
import os
|
|
|
|
import time
|
2015-08-12 01:59:29 +00:00
|
|
|
import sys
|
|
|
|
from wakatime.compat import u
|
2015-08-11 19:15:35 +00:00
|
|
|
from wakatime.packages.requests.models import Response
|
2015-08-12 01:59:29 +00:00
|
|
|
from . import utils
|
2015-08-11 18:19:22 +00:00
|
|
|
|
2015-08-30 00:48:42 +00:00
|
|
|
try:
|
|
|
|
from .packages import simplejson as json
|
|
|
|
except (ImportError, SyntaxError):
|
|
|
|
import json
|
2015-08-11 18:19:22 +00:00
|
|
|
try:
|
2015-08-12 21:21:32 +00:00
|
|
|
from mock import ANY
|
2015-08-11 21:53:35 +00:00
|
|
|
except ImportError:
|
2015-08-12 21:21:32 +00:00
|
|
|
from unittest.mock import ANY
|
2015-08-11 18:19:22 +00:00
|
|
|
|
|
|
|
|
2015-08-12 01:59:29 +00:00
|
|
|
class BaseTestCase(utils.TestCase):
|
2015-08-12 21:21:32 +00:00
|
|
|
patch_these = [
|
|
|
|
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
|
|
|
'wakatime.offlinequeue.Queue.push',
|
|
|
|
['wakatime.offlinequeue.Queue.pop', None],
|
|
|
|
'wakatime.session_cache.SessionCache.save',
|
|
|
|
'wakatime.session_cache.SessionCache.delete',
|
|
|
|
['wakatime.session_cache.SessionCache.get', requests.session],
|
|
|
|
]
|
2015-08-11 18:19:22 +00:00
|
|
|
|
2015-08-12 21:21:32 +00:00
|
|
|
def test_help_contents(self):
|
2015-08-12 17:28:01 +00:00
|
|
|
args = ['--help']
|
2015-08-11 18:19:22 +00:00
|
|
|
with self.assertRaises(SystemExit):
|
2015-08-29 23:16:14 +00:00
|
|
|
execute(args)
|
2015-08-12 01:59:29 +00:00
|
|
|
expected_stdout = open('tests/samples/output/test_help_contents').read()
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), expected_stdout)
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
2015-08-11 18:19:22 +00:00
|
|
|
|
2015-08-12 21:21:32 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called()
|
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
|
|
|
|
def test_argument_parsing(self):
|
2015-08-11 19:15:35 +00:00
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
2015-08-12 21:21:32 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2015-09-07 03:06:36 +00:00
|
|
|
args = ['--file', 'tests/samples/codefiles/twolinefile.txt', '--key', '123', '--config', 'tests/samples/sample.cfg']
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2015-08-29 23:16:14 +00:00
|
|
|
retval = execute(args)
|
2015-08-11 18:19:22 +00:00
|
|
|
self.assertEquals(retval, 0)
|
2015-08-12 21:21:32 +00:00
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
2015-08-12 01:59:29 +00:00
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
2015-08-12 21:21:32 +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()
|
|
|
|
|
|
|
|
def test_missing_config_file(self):
|
2015-09-07 03:06:36 +00:00
|
|
|
args = ['--file', 'tests/samples/codefiles/emptyfile.txt', '--config', 'foo']
|
2015-08-12 01:59:29 +00:00
|
|
|
with self.assertRaises(SystemExit):
|
2015-08-29 23:16:14 +00:00
|
|
|
execute(args)
|
2015-08-12 01:59:29 +00:00
|
|
|
expected_stdout = u("Error: Could not read from config file foo\n")
|
|
|
|
expected_stderr = open('tests/samples/output/test_missing_config_file').read()
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), expected_stdout)
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), expected_stderr)
|
|
|
|
|
2015-08-12 21:21:32 +00:00
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called()
|
|
|
|
|
|
|
|
def test_config_file(self):
|
2015-08-12 01:59:29 +00:00
|
|
|
response = Response()
|
|
|
|
response.status_code = 201
|
2015-08-12 21:21:32 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
2015-09-07 03:06:36 +00:00
|
|
|
args = ['--file', 'tests/samples/codefiles/emptyfile.txt', '--config', 'tests/samples/sample.cfg']
|
2015-08-29 23:16:14 +00:00
|
|
|
retval = execute(args)
|
2015-08-12 01:59:29 +00:00
|
|
|
self.assertEquals(retval, 0)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
2015-08-12 21:21:32 +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()
|
|
|
|
|
|
|
|
def test_bad_config_file(self):
|
2015-09-07 03:06:36 +00:00
|
|
|
args = ['--file', 'tests/samples/codefiles/emptyfile.txt', '--config', 'tests/samples/bad_config.cfg']
|
2015-08-29 23:16:14 +00:00
|
|
|
retval = execute(args)
|
2015-08-12 01:59:29 +00:00
|
|
|
self.assertEquals(retval, 103)
|
2015-08-12 02:06:01 +00:00
|
|
|
self.assertIn('ParsingError', sys.stdout.getvalue())
|
2015-08-12 01:59:29 +00:00
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
2015-08-12 21:21:32 +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()
|
|
|
|
|
|
|
|
def test_non_hidden_filename(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
2015-09-07 03:06:36 +00:00
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-08-12 21:21:32 +00:00
|
|
|
config = 'tests/samples/sample.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--key', '123', '--config', config, '--time', now]
|
|
|
|
|
2015-08-29 23:16:14 +00:00
|
|
|
retval = execute(args)
|
2015-08-12 21:21:32 +00:00
|
|
|
self.assertEquals(retval, 102)
|
|
|
|
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_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'language': 'Text only',
|
|
|
|
'lines': 2,
|
|
|
|
'entity': os.path.abspath(entity),
|
2015-08-12 21:28:21 +00:00
|
|
|
'project': os.path.basename(os.path.abspath('.')),
|
2015-08-12 21:36:35 +00:00
|
|
|
'branch': os.environ.get('TRAVIS_COMMIT', ANY),
|
2015-08-12 21:21:32 +00:00
|
|
|
'time': float(now),
|
|
|
|
'type': 'file',
|
|
|
|
}
|
2015-08-30 00:48:42 +00:00
|
|
|
stats = {
|
|
|
|
u('cursorpos'): None,
|
|
|
|
u('dependencies'): [],
|
|
|
|
u('language'): u('Text only'),
|
|
|
|
u('lineno'): None,
|
|
|
|
u('lines'): 2,
|
|
|
|
}
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2015-08-30 00:48:42 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
|
|
|
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
2015-08-12 21:21:32 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
|
|
|
|
def test_hidden_filename(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
2015-09-07 03:06:36 +00:00
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-08-12 21:21:32 +00:00
|
|
|
config = 'tests/samples/paranoid.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--key', '123', '--config', config, '--time', now]
|
|
|
|
|
2015-08-29 23:16:14 +00:00
|
|
|
retval = execute(args)
|
2015-08-12 21:21:32 +00:00
|
|
|
self.assertEquals(retval, 102)
|
|
|
|
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_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'language': 'Text only',
|
|
|
|
'lines': 2,
|
|
|
|
'entity': 'HIDDEN.txt',
|
2015-08-12 21:28:21 +00:00
|
|
|
'project': os.path.basename(os.path.abspath('.')),
|
2015-08-12 21:36:35 +00:00
|
|
|
'branch': os.environ.get('TRAVIS_COMMIT', ANY),
|
2015-08-12 21:21:32 +00:00
|
|
|
'time': float(now),
|
|
|
|
'type': 'file',
|
|
|
|
}
|
2015-08-30 00:48:42 +00:00
|
|
|
stats = {
|
|
|
|
u('cursorpos'): None,
|
|
|
|
u('dependencies'): [],
|
|
|
|
u('language'): u('Text only'),
|
|
|
|
u('lineno'): None,
|
|
|
|
u('lines'): 2,
|
|
|
|
}
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2015-08-30 00:48:42 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
|
|
|
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
2015-08-12 21:21:32 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
|
|
|
|
def test_500_response(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 500
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
|
2015-09-07 03:06:36 +00:00
|
|
|
args = ['--file', 'tests/samples/codefiles/twolinefile.txt', '--key', '123',
|
2015-08-12 21:21:32 +00:00
|
|
|
'--config', 'tests/samples/paranoid.cfg', '--time', now]
|
|
|
|
|
|
|
|
|
2015-08-29 23:16:14 +00:00
|
|
|
retval = execute(args)
|
2015-08-12 21:21:32 +00:00
|
|
|
self.assertEquals(retval, 102)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'language': 'Text only',
|
|
|
|
'lines': 2,
|
|
|
|
'entity': 'HIDDEN.txt',
|
2015-08-12 21:28:21 +00:00
|
|
|
'project': os.path.basename(os.path.abspath('.')),
|
2015-08-12 21:36:35 +00:00
|
|
|
'branch': os.environ.get('TRAVIS_COMMIT', ANY),
|
2015-08-12 21:21:32 +00:00
|
|
|
'time': float(now),
|
|
|
|
'type': 'file',
|
|
|
|
}
|
2015-08-30 00:48:42 +00:00
|
|
|
stats = {
|
|
|
|
u('cursorpos'): None,
|
|
|
|
u('dependencies'): [],
|
|
|
|
u('language'): u('Text only'),
|
|
|
|
u('lineno'): None,
|
|
|
|
u('lines'): 2,
|
|
|
|
}
|
2015-08-12 21:21:32 +00:00
|
|
|
|
2015-08-30 00:48:42 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
|
|
|
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
2015-08-12 21:21:32 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
|
|
|
|
def test_400_response(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 400
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
|
2015-09-07 03:06:36 +00:00
|
|
|
args = ['--file', 'tests/samples/codefiles/twolinefile.txt', '--key', '123',
|
2015-08-12 21:21:32 +00:00
|
|
|
'--config', 'tests/samples/paranoid.cfg', '--time', now]
|
|
|
|
|
|
|
|
|
2015-08-29 23:16:14 +00:00
|
|
|
retval = execute(args)
|
2015-08-12 21:21:32 +00:00
|
|
|
self.assertEquals(retval, 102)
|
|
|
|
self.assertEquals(sys.stdout.getvalue(), '')
|
|
|
|
self.assertEquals(sys.stderr.getvalue(), '')
|
|
|
|
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
|
|
|
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()
|
2015-08-12 22:03:39 +00:00
|
|
|
|
|
|
|
def test_alternate_project(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
2015-09-07 03:06:36 +00:00
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-08-12 22:03:39 +00:00
|
|
|
config = 'tests/samples/sample.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--alternate-project', 'xyz', '--config', config, '--time', now]
|
|
|
|
|
2015-08-29 23:16:14 +00:00
|
|
|
retval = execute(args)
|
2015-08-12 22:03:39 +00:00
|
|
|
self.assertEquals(retval, 102)
|
|
|
|
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_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'language': 'Text only',
|
|
|
|
'lines': 2,
|
|
|
|
'entity': os.path.abspath(entity),
|
|
|
|
'project': os.path.basename(os.path.abspath('.')),
|
|
|
|
'branch': os.environ.get('TRAVIS_COMMIT', ANY),
|
|
|
|
'time': float(now),
|
|
|
|
'type': 'file',
|
|
|
|
}
|
2015-08-30 00:48:42 +00:00
|
|
|
stats = {
|
|
|
|
u('cursorpos'): None,
|
|
|
|
u('dependencies'): [],
|
|
|
|
u('language'): u('Text only'),
|
|
|
|
u('lineno'): None,
|
|
|
|
u('lines'): 2,
|
|
|
|
}
|
2015-08-12 22:03:39 +00:00
|
|
|
|
2015-08-30 00:48:42 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
|
|
|
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
2015-08-12 22:03:39 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
|
|
|
|
|
|
|
def test_set_project_from_command_line(self):
|
|
|
|
response = Response()
|
|
|
|
response.status_code = 0
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
2015-09-07 03:06:36 +00:00
|
|
|
entity = 'tests/samples/codefiles/twolinefile.txt'
|
2015-08-12 22:03:39 +00:00
|
|
|
config = 'tests/samples/sample.cfg'
|
|
|
|
|
|
|
|
args = ['--file', entity, '--project', 'xyz', '--config', config, '--time', now]
|
|
|
|
|
2015-08-29 23:16:14 +00:00
|
|
|
retval = execute(args)
|
2015-08-12 22:03:39 +00:00
|
|
|
self.assertEquals(retval, 102)
|
|
|
|
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_called_once_with()
|
|
|
|
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'language': 'Text only',
|
|
|
|
'lines': 2,
|
|
|
|
'entity': os.path.abspath(entity),
|
|
|
|
'project': 'xyz',
|
|
|
|
'branch': os.environ.get('TRAVIS_COMMIT', ANY),
|
|
|
|
'time': float(now),
|
|
|
|
'type': 'file',
|
|
|
|
}
|
2015-08-30 00:48:42 +00:00
|
|
|
stats = {
|
|
|
|
u('cursorpos'): None,
|
|
|
|
u('dependencies'): [],
|
|
|
|
u('language'): u('Text only'),
|
|
|
|
u('lineno'): None,
|
|
|
|
u('lines'): 2,
|
|
|
|
}
|
2015-08-12 22:03:39 +00:00
|
|
|
|
2015-08-30 00:48:42 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
|
|
|
|
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
2015-08-12 22:03:39 +00:00
|
|
|
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|