assertPathsEqual test helper

This commit is contained in:
Alan Hamlett 2018-09-30 19:30:11 -07:00
parent 96ac604c74
commit 49ff066a12
2 changed files with 22 additions and 5 deletions

View file

@ -13,7 +13,7 @@ class UtilsTestCase(TestCase):
path = 'some\\path////to\\\\\\a\\file.txt' path = 'some\\path////to\\\\\\a\\file.txt'
expected = os.path.realpath('some/path/to/a/file.txt') expected = os.path.realpath('some/path/to/a/file.txt')
result = format_file_path(path) result = format_file_path(path)
self.assertEquals(expected, result) self.assertPathsEqual(expected, result)
def test_format_file_path_uppercase_windows_drive(self): def test_format_file_path_uppercase_windows_drive(self):
path = 'c:\\some\\path////to\\\\\\a\\file.txt' path = 'c:\\some\\path////to\\\\\\a\\file.txt'
@ -25,7 +25,7 @@ class UtilsTestCase(TestCase):
mock_abspath.return_value = path mock_abspath.return_value = path
result = format_file_path(path) result = format_file_path(path)
self.assertEquals(expected, result) self.assertPathsEqual(expected, result)
def test_format_file_path_handles_exceptions(self): def test_format_file_path_handles_exceptions(self):
path = 'c:\\some\\path////to\\\\\\a\\file.txt' path = 'c:\\some\\path////to\\\\\\a\\file.txt'
@ -35,4 +35,4 @@ class UtilsTestCase(TestCase):
mock_abspath.side_effect = Exception('foobar') mock_abspath.side_effect = Exception('foobar')
result = format_file_path(path) result = format_file_path(path)
self.assertEquals(expected, result) self.assertPathsEqual(expected, result)

View file

@ -2,11 +2,13 @@
import logging import logging
import os import os
import re
import sys import sys
import tempfile import tempfile
from wakatime.compat import u from wakatime.compat import u
from wakatime.packages.requests.models import Response from wakatime.packages.requests.models import Response
from wakatime.utils import BACKSLASH_REPLACE_PATTERN
try: try:
@ -70,6 +72,15 @@ class TestCase(unittest.TestCase):
else: else:
self.assertEquals(first_list, second_list) self.assertEquals(first_list, second_list)
def normalize_path(self, path):
return re.sub(BACKSLASH_REPLACE_PATTERN, '/', path)
def assertPathsEqual(self, first_path, second_path, message=None):
if message:
self.assertEquals(self.normalize_path(first_path), self.normalize_path(second_path), message)
else:
self.assertEquals(self.normalize_path(first_path), self.normalize_path(second_path))
def assertHeartbeatNotSent(self): def assertHeartbeatNotSent(self):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_not_called() self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_not_called()
@ -92,7 +103,10 @@ class TestCase(unittest.TestCase):
if isinstance(heartbeat.get(key), list): if isinstance(heartbeat.get(key), list):
self.assertListsEqual(heartbeat.get(key), body[0].get(key), u('Expected heartbeat to be sent with {0}={1}, instead {0}={2}').format(u(key), u(heartbeat.get(key)), u(body[0].get(key)))) self.assertListsEqual(heartbeat.get(key), body[0].get(key), u('Expected heartbeat to be sent with {0}={1}, instead {0}={2}').format(u(key), u(heartbeat.get(key)), u(body[0].get(key))))
else: else:
self.assertEquals(heartbeat.get(key), body[0].get(key), u('Expected heartbeat to be sent with {1} {0}={2}, instead {3} {0}={4}').format(u(key), type(heartbeat.get(key)).__name__, u(heartbeat.get(key)), type(body[0].get(key)).__name__, u(body[0].get(key)))) if key == 'entity':
self.assertPathsEqual(heartbeat.get(key), body[0].get(key), u('Expected heartbeat to be sent with {1} {0}={2}, instead {3} {0}={4}').format(u(key), type(heartbeat.get(key)).__name__, u(heartbeat.get(key)), type(body[0].get(key)).__name__, u(body[0].get(key))))
else:
self.assertEquals(heartbeat.get(key), body[0].get(key), u('Expected heartbeat to be sent with {1} {0}={2}, instead {3} {0}={4}').format(u(key), type(heartbeat.get(key)).__name__, u(heartbeat.get(key)), type(body[0].get(key)).__name__, u(body[0].get(key))))
if extra_heartbeats: if extra_heartbeats:
for i in range(len(extra_heartbeats)): for i in range(len(extra_heartbeats)):
@ -101,7 +115,10 @@ class TestCase(unittest.TestCase):
if isinstance(extra_heartbeats[i].get(key), list): if isinstance(extra_heartbeats[i].get(key), list):
self.assertListsEqual(extra_heartbeats[i].get(key), body[i + 1].get(key), u('Expected extra heartbeat {3} to be sent with {0}={1}, instead {0}={2}').format(u(key), u(extra_heartbeats[i].get(key)), u(body[i + 1].get(key)), i)) self.assertListsEqual(extra_heartbeats[i].get(key), body[i + 1].get(key), u('Expected extra heartbeat {3} to be sent with {0}={1}, instead {0}={2}').format(u(key), u(extra_heartbeats[i].get(key)), u(body[i + 1].get(key)), i))
else: else:
self.assertEquals(extra_heartbeats[i].get(key), body[i + 1].get(key), u('Expected extra heartbeat {5} to be sent with {1} {0}={2}, instead {3} {0}={4}').format(u(key), type(extra_heartbeats[i].get(key)).__name__, u(extra_heartbeats[i].get(key)), type(body[i + 1].get(key)).__name__, u(body[i + 1].get(key)), i)) if key == 'entity':
self.assertPathsEqual(extra_heartbeats[i].get(key), body[i + 1].get(key), u('Expected extra heartbeat {5} to be sent with {1} {0}={2}, instead {3} {0}={4}').format(u(key), type(extra_heartbeats[i].get(key)).__name__, u(extra_heartbeats[i].get(key)), type(body[i + 1].get(key)).__name__, u(body[i + 1].get(key)), i))
else:
self.assertEquals(extra_heartbeats[i].get(key), body[i + 1].get(key), u('Expected extra heartbeat {5} to be sent with {1} {0}={2}, instead {3} {0}={4}').format(u(key), type(extra_heartbeats[i].get(key)).__name__, u(extra_heartbeats[i].get(key)), type(body[i + 1].get(key)).__name__, u(body[i + 1].get(key)), i))
def assertSessionCacheUntouched(self): def assertSessionCacheUntouched(self):
self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called()