mirror of
https://github.com/gryf/weechat-replacer.git
synced 2025-12-18 03:50:23 +01:00
Fixed path resolving implementation, added tests.
This commit is contained in:
15
replacer.py
15
replacer.py
@@ -79,14 +79,13 @@ class Replacer(object):
|
|||||||
|
|
||||||
# nothing found. so there is no replacement file. let's assume the
|
# nothing found. so there is no replacement file. let's assume the
|
||||||
# right file path.
|
# right file path.
|
||||||
if not path:
|
version = weechat.info_get("version_number", "") or 0
|
||||||
version = weechat.info_get("version_number", "") or 0
|
if version < 0x3020000: # < 3.2.0
|
||||||
if version < 0x3020000: # < 3.2.0
|
path = '%h/' + map_file
|
||||||
path = '%h/' + map_file
|
return weechat.string_eval_path_home(path, {}, {}, {})
|
||||||
return weechat.string_eval_path_home(path, {}, {}, {})
|
else:
|
||||||
else:
|
return os.path.join(weechat.info_get("weechat_data_dir", ""),
|
||||||
return os.path.join(weechat.info_get("weechat_data_dir", ""),
|
map_file)
|
||||||
map_file)
|
|
||||||
|
|
||||||
def _get_replacement_map(self):
|
def _get_replacement_map(self):
|
||||||
"""Read json file, and assign it to the replacement_map attr"""
|
"""Read json file, and assign it to the replacement_map attr"""
|
||||||
|
|||||||
@@ -252,5 +252,73 @@ class TestFunctions(unittest.TestCase):
|
|||||||
self.assertEqual(weechat.line, 'quis cursus')
|
self.assertEqual(weechat.line, 'quis cursus')
|
||||||
|
|
||||||
|
|
||||||
|
class TestLocateWeeHome(unittest.TestCase):
|
||||||
|
|
||||||
|
@mock.patch('os.path.exists')
|
||||||
|
@mock.patch('weechat.string_eval_path_home')
|
||||||
|
@mock.patch('weechat.info_get')
|
||||||
|
def test_locate_replacement_file_data_dir(self, info_get, eval_path_home,
|
||||||
|
path_exists):
|
||||||
|
info_get.side_effect = ('foo', 'bar')
|
||||||
|
eval_path_home.side_effect = ('baz', )
|
||||||
|
path_exists.side_effect = (True, )
|
||||||
|
|
||||||
|
result = replacer.Replacer._locate_replacement_file(object)
|
||||||
|
|
||||||
|
self.assertEqual(result, 'foo/replacement_map.json')
|
||||||
|
|
||||||
|
@mock.patch('os.path.exists')
|
||||||
|
@mock.patch('weechat.string_eval_path_home')
|
||||||
|
@mock.patch('weechat.info_get')
|
||||||
|
def test_locate_replacement_file_config_dir(self, info_get, eval_path_home,
|
||||||
|
path_exists):
|
||||||
|
info_get.side_effect = ('foo', 'bar')
|
||||||
|
eval_path_home.side_effect = ('baz', )
|
||||||
|
path_exists.side_effect = (False, True)
|
||||||
|
|
||||||
|
result = replacer.Replacer._locate_replacement_file(object)
|
||||||
|
|
||||||
|
self.assertEqual(result, 'bar/replacement_map.json')
|
||||||
|
|
||||||
|
@mock.patch('os.path.exists')
|
||||||
|
@mock.patch('weechat.string_eval_path_home')
|
||||||
|
@mock.patch('weechat.info_get')
|
||||||
|
def test_locate_replacement_file_old_home(self, info_get, eval_path_home,
|
||||||
|
path_exists):
|
||||||
|
info_get.side_effect = ('foo', 'bar')
|
||||||
|
eval_path_home.side_effect = ('baz', )
|
||||||
|
path_exists.side_effect = (False, False, True)
|
||||||
|
|
||||||
|
result = replacer.Replacer._locate_replacement_file(object)
|
||||||
|
|
||||||
|
self.assertEqual(result, 'baz/replacement_map.json')
|
||||||
|
|
||||||
|
@mock.patch('os.path.exists')
|
||||||
|
@mock.patch('weechat.string_eval_path_home')
|
||||||
|
@mock.patch('weechat.info_get')
|
||||||
|
def test_locate_replacement_default_home_31(self, info_get, eval_path_home,
|
||||||
|
path_exists):
|
||||||
|
info_get.side_effect = ('foo', 'bar', 0x3010000)
|
||||||
|
eval_path_home.side_effect = ('baz', 'old/replacement_map.json')
|
||||||
|
path_exists.side_effect = (False, False, False)
|
||||||
|
|
||||||
|
result = replacer.Replacer._locate_replacement_file(object)
|
||||||
|
|
||||||
|
self.assertEqual(result, 'old/replacement_map.json')
|
||||||
|
|
||||||
|
@mock.patch('os.path.exists')
|
||||||
|
@mock.patch('weechat.string_eval_path_home')
|
||||||
|
@mock.patch('weechat.info_get')
|
||||||
|
def test_locate_replacement_default_home_32(self, info_get, eval_path_home,
|
||||||
|
path_exists):
|
||||||
|
info_get.side_effect = ('foo', 'bar', 0x3020000, 'new')
|
||||||
|
eval_path_home.side_effect = ('baz', )
|
||||||
|
path_exists.side_effect = (False, False, False)
|
||||||
|
|
||||||
|
result = replacer.Replacer._locate_replacement_file(object)
|
||||||
|
|
||||||
|
self.assertEqual(result, 'new/replacement_map.json')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user