force forward slash for file paths

This commit is contained in:
Alan Hamlett 2018-04-04 09:32:02 -07:00
parent b2c67deb19
commit 6022f1f531
2 changed files with 46 additions and 2 deletions

38
tests/test_utils.py Normal file
View file

@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
from wakatime.utils import format_file_path
import os
from .utils import TestCase, mock
class UtilsTestCase(TestCase):
def test_format_file_path_forces_forward_slashes(self):
path = 'some\\path////to\\\\\\a\\file.txt'
expected = os.path.realpath('some/path/to/a/file.txt')
result = format_file_path(path)
self.assertEquals(expected, result)
def test_format_file_path_uppercase_windows_drive(self):
path = 'c:\\some\\path////to\\\\\\a\\file.txt'
expected = 'C:/some/path/to/a/file.txt'
with mock.patch('os.path.realpath') as mock_realpath:
mock_realpath.return_value = path
with mock.patch('os.path.abspath') as mock_abspath:
mock_abspath.return_value = path
result = format_file_path(path)
self.assertEquals(expected, result)
def test_format_file_path_handles_exceptions(self):
path = 'c:\\some\\path////to\\\\\\a\\file.txt'
expected = path
with mock.patch('os.path.abspath') as mock_abspath:
mock_abspath.side_effect = Exception('foobar')
result = format_file_path(path)
self.assertEquals(expected, result)

View file

@ -24,6 +24,10 @@ from .compat import u
log = logging.getLogger('WakaTime') log = logging.getLogger('WakaTime')
BACKSLASH_REPLACE_PATTERN = re.compile(r'[\\/]+')
WINDOWS_DRIVE_PATTERN = re.compile(r'^[a-z]:/')
def should_exclude(entity, include, exclude): def should_exclude(entity, include, exclude):
if entity is not None and entity.strip() != '': if entity is not None and entity.strip() != '':
for pattern in include: for pattern in include:
@ -74,8 +78,10 @@ def format_file_path(filepath):
try: try:
filepath = os.path.realpath(os.path.abspath(filepath)) filepath = os.path.realpath(os.path.abspath(filepath))
filepath = re.sub(r'[/\\]', os.path.sep, filepath) filepath = re.sub(BACKSLASH_REPLACE_PATTERN, '/', filepath)
except: # pragma: nocover if WINDOWS_DRIVE_PATTERN.match(filepath):
filepath = filepath.capitalize()
except:
pass pass
return filepath return filepath