test for index error on project map replacement regex groups

This commit is contained in:
Alan Hamlett 2016-08-31 14:49:01 +02:00
parent c4d6bb38a1
commit 8ef87d0393
2 changed files with 41 additions and 13 deletions

View file

@ -13,6 +13,7 @@ import tempfile
import time import time
from testfixtures import log_capture from testfixtures import log_capture
from wakatime.compat import u from wakatime.compat import u
from wakatime.constants import API_ERROR
from wakatime.exceptions import NotYetImplemented from wakatime.exceptions import NotYetImplemented
from wakatime.projects.base import BaseProject from wakatime.projects.base import BaseProject
from . import utils from . import utils
@ -354,3 +355,27 @@ class ProjectTestCase(utils.TestCase):
if self.isPy35: if self.isPy35:
expected = u('WakaTime WARNING Regex error (unterminated character set at position 7) for projectmap pattern: invalid[({regex') expected = u('WakaTime WARNING Regex error (unterminated character set at position 7) for projectmap pattern: invalid[({regex')
self.assertEquals(output[0], expected) self.assertEquals(output[0], expected)
@log_capture()
def test_project_map_with_replacement_group_index_error(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_malformed.cfg'
args = ['--file', entity, '--config', config, '--time', now]
retval = execute(args)
self.assertEquals(retval, API_ERROR)
self.assertEquals(sys.stdout.getvalue(), '')
self.assertEquals(sys.stderr.getvalue(), '')
log_output = "\n".join([u(' ').join(x) for x in logs.actual()])
expected = u('WakaTime WARNING Regex error (tuple index out of range) for projectmap pattern: proj-map{3}')
self.assertEquals(log_output, expected)

View file

@ -44,20 +44,23 @@ class ProjectMap(BaseProject):
def _find_project(self, path): def _find_project(self, path):
path = os.path.realpath(path) path = os.path.realpath(path)
try:
for pattern, new_proj_name in self._configs.items(): for pattern, new_proj_name in self._configs.items():
try: try:
compiled = re.compile(pattern, re.IGNORECASE) compiled = re.compile(pattern, re.IGNORECASE)
match = compiled.search(path) match = compiled.search(path)
if match: if match:
try:
return new_proj_name.format(*match.groups()) return new_proj_name.format(*match.groups())
except IndexError as ex:
log.warning(u('Regex error ({msg}) for projectmap pattern: {pattern}').format(
msg=u(ex),
pattern=u(new_proj_name),
))
except re.error as ex: except re.error as ex:
log.warning(u('Regex error ({msg}) for projectmap pattern: {pattern}').format( log.warning(u('Regex error ({msg}) for projectmap pattern: {pattern}').format(
msg=u(ex), msg=u(ex),
pattern=u(pattern), pattern=u(pattern),
)) ))
except TypeError:
pass
return None return None