use temp directory for all code files in tests

This commit is contained in:
Alan Hamlett 2016-06-08 20:33:36 +02:00
parent e918bb0997
commit 429c61967a
3 changed files with 890 additions and 749 deletions

View file

@ -6,6 +6,7 @@ from wakatime.packages import requests
import os
import time
import shutil
import sys
from wakatime.compat import u
from wakatime.exceptions import NotYetImplemented
@ -52,8 +53,12 @@ class DependenciesTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.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/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
@ -100,8 +105,12 @@ class DependenciesTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.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/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
@ -157,8 +166,12 @@ class DependenciesTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/bower.json'
shutil.copy(entity, os.path.join(tempdir, 'bower.json'))
entity = os.path.realpath(os.path.join(tempdir, 'bower.json'))
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
@ -199,8 +212,12 @@ class DependenciesTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/java.java'
shutil.copy(entity, os.path.join(tempdir, 'java.java'))
entity = os.path.realpath(os.path.join(tempdir, 'java.java'))
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
@ -249,8 +266,12 @@ class DependenciesTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/see.c'
shutil.copy(entity, os.path.join(tempdir, 'see.c'))
entity = os.path.realpath(os.path.join(tempdir, 'see.c'))
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
@ -297,8 +318,12 @@ class DependenciesTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/seeplusplus.cpp'
shutil.copy(entity, os.path.join(tempdir, 'seeplusplus.cpp'))
entity = os.path.realpath(os.path.join(tempdir, 'seeplusplus.cpp'))
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
@ -345,8 +370,12 @@ class DependenciesTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/seesharp.cs'
shutil.copy(entity, os.path.join(tempdir, 'seesharp.cs'))
entity = os.path.realpath(os.path.join(tempdir, 'seesharp.cs'))
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
@ -396,8 +425,12 @@ class DependenciesTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/php.php'
shutil.copy(entity, os.path.join(tempdir, 'php.php'))
entity = os.path.realpath(os.path.join(tempdir, 'php.php'))
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
@ -453,8 +486,12 @@ class DependenciesTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/html-with-php.html'
shutil.copy(entity, os.path.join(tempdir, 'html-with-php.html'))
entity = os.path.realpath(os.path.join(tempdir, 'html-with-php.html'))
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
@ -501,8 +538,12 @@ class DependenciesTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/html-django.html'
shutil.copy(entity, os.path.join(tempdir, 'html-django.html'))
entity = os.path.realpath(os.path.join(tempdir, 'html-django.html'))
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
@ -549,8 +590,12 @@ class DependenciesTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.TemporaryDirectory() as tempdir:
entity = 'tests/samples/codefiles/go.go'
shutil.copy(entity, os.path.join(tempdir, 'go.go'))
entity = os.path.realpath(os.path.join(tempdir, 'go.go'))
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]

View file

@ -7,6 +7,7 @@ from wakatime.packages import requests
import os
import time
import re
import shutil
import sys
from wakatime.compat import u, is_py3
from wakatime.constants import (
@ -57,7 +58,11 @@ class BaseTestCase(utils.TestCase):
response.status_code = 201
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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'))
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--key', '123', '--config', config]
@ -78,11 +83,15 @@ class BaseTestCase(utils.TestCase):
response.status_code = 201
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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'))
with utils.mock.patch('wakatime.main.open') as mock_open:
mock_open.side_effect = IOError('')
config = os.path.join(os.path.expanduser('~'), '.wakatime.cfg')
entity = 'tests/samples/codefiles/emptyfile.txt'
args = ['--file', entity]
with self.assertRaises(SystemExit):
@ -95,7 +104,12 @@ class BaseTestCase(utils.TestCase):
def test_missing_config_file(self):
config = 'foo'
with utils.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'))
args = ['--file', entity, '--config', config]
with self.assertRaises(SystemExit):
execute(args)
@ -111,7 +125,11 @@ class BaseTestCase(utils.TestCase):
response.status_code = 201
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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_everything.cfg'
args = ['--file', entity, '--config', config]
retval = execute(args)
@ -134,7 +152,11 @@ class BaseTestCase(utils.TestCase):
response.status_code = 201
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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/sample_alternate_apikey.cfg'
args = ['--file', entity, '--config', config]
retval = execute(args)
@ -150,7 +172,11 @@ class BaseTestCase(utils.TestCase):
self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with()
def test_bad_config_file(self):
with utils.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/bad_config.cfg'
args = ['--file', entity, '--config', config]
retval = execute(args)
@ -167,8 +193,12 @@ class BaseTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.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'))
now = u(int(time.time()))
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--key', '123', '--config', config, '--time', now]
@ -185,7 +215,7 @@ class BaseTestCase(utils.TestCase):
heartbeat = {
'language': 'Text only',
'lines': 2,
'entity': os.path.abspath(entity),
'entity': os.path.realpath(entity),
'project': os.path.basename(os.path.abspath('.')),
'time': float(now),
'type': 'file',
@ -209,8 +239,12 @@ class BaseTestCase(utils.TestCase):
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
now = u(int(time.time()))
with utils.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'))
now = u(int(time.time()))
config = 'tests/samples/configs/paranoid.cfg'
args = ['--file', entity, '--key', '123', '--config', config, '--time', now]
@ -251,7 +285,11 @@ class BaseTestCase(utils.TestCase):
response.status_code = 201
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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'))
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--key', '123', '--config', config, '--timeout', 'abc']
@ -270,9 +308,14 @@ class BaseTestCase(utils.TestCase):
response.status_code = 500
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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'))
now = u(int(time.time()))
args = ['--file', 'tests/samples/codefiles/twolinefile.txt', '--key', '123',
args = ['--file', entity, '--key', '123',
'--config', 'tests/samples/configs/paranoid.cfg', '--time', now]
@ -312,9 +355,14 @@ class BaseTestCase(utils.TestCase):
response.status_code = 400
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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'))
now = u(int(time.time()))
args = ['--file', 'tests/samples/codefiles/twolinefile.txt', '--key', '123',
args = ['--file', entity, '--key', '123',
'--config', 'tests/samples/configs/paranoid.cfg', '--time', now]
@ -335,9 +383,14 @@ class BaseTestCase(utils.TestCase):
response.status_code = 401
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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'))
now = u(int(time.time()))
args = ['--file', 'tests/samples/codefiles/twolinefile.txt', '--key', '123',
args = ['--file', entity, '--key', '123',
'--config', 'tests/samples/configs/paranoid.cfg', '--time', now]
@ -378,6 +431,7 @@ class BaseTestCase(utils.TestCase):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
entity = 'tests/samples/codefiles/missingfile.txt'
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config]
retval = execute(args)
@ -397,7 +451,11 @@ class BaseTestCase(utils.TestCase):
response.status_code = 201
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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, '--proxy', 'localhost:1234']
retval = execute(args)
@ -493,12 +551,16 @@ class BaseTestCase(utils.TestCase):
response.status_code = 201
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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'))
hostname = 'test汉语' if is_py3 else 'test\xe6\xb1\x89\xe8\xaf\xad'
with utils.mock.patch('socket.gethostname') as mock_gethostname:
mock_gethostname.return_value = hostname
self.assertEquals(type(hostname).__name__, 'str')
entity = 'tests/samples/codefiles/emptyfile.txt'
config = 'tests/samples/configs/has_everything.cfg'
args = ['--file', entity, '--config', config]
retval = execute(args)
@ -524,13 +586,17 @@ class BaseTestCase(utils.TestCase):
response.status_code = 201
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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'))
package_path = 'wakatime.packages.py3.tzlocal.get_localzone' if is_py3 else 'wakatime.packages.py2.tzlocal.get_localzone'
timezone = tzlocal.get_localzone()
timezone.zone = 'tz汉语' if is_py3 else 'tz\xe6\xb1\x89\xe8\xaf\xad'
with utils.mock.patch(package_path) as mock_getlocalzone:
mock_getlocalzone.return_value = timezone
entity = 'tests/samples/codefiles/emptyfile.txt'
config = 'tests/samples/configs/has_everything.cfg'
args = ['--file', entity, '--config', config, '--timeout', '15']
retval = execute(args)
@ -551,7 +617,11 @@ class BaseTestCase(utils.TestCase):
response.status_code = 201
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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]
retval = execute(args)
@ -575,6 +645,11 @@ class BaseTestCase(utils.TestCase):
response.status_code = 201
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
with utils.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'))
project1 = os.path.basename(os.path.abspath('.'))
project2 = 'xyz'
entity1 = os.path.abspath('tests/samples/codefiles/emptyfile.txt')

View file

@ -52,3 +52,24 @@ class TestCase(unittest.TestCase):
@property
def isPy35(self):
return (sys.version_info[0] == 3 and sys.version_info[1] == 5)
try:
# Python >= 3
from tempfile import TemporaryDirectory
except ImportError:
# Python < 3
import shutil
import tempfile
class TemporaryDirectory(object):
"""Context manager for tempfile.mkdtemp().
Adds the ability to use with a `with` statement.
"""
def __enter__(self):
self.name = tempfile.mkdtemp()
return self.name
def __exit__(self, exc_type, exc_value, traceback):
shutil.rmtree(self.name)