mirror of
https://github.com/gryf/weechat-replacer.git
synced 2025-12-19 12:28:16 +01:00
Refactoring, fix tests
This commit is contained in:
17
replacer.py
17
replacer.py
@@ -64,8 +64,10 @@ class Replacer(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Initialize plugin"""
|
"""Initialize plugin"""
|
||||||
self.replacement_map = {}
|
self.replacement_map = {}
|
||||||
self._path = None
|
self._path = self._locate_replacement_file()
|
||||||
|
self._get_replacement_map()
|
||||||
|
|
||||||
|
def _locate_replacement_file(self):
|
||||||
map_file = "replacement_map.json"
|
map_file = "replacement_map.json"
|
||||||
data_dirs = (weechat.info_get("weechat_data_dir", ""),
|
data_dirs = (weechat.info_get("weechat_data_dir", ""),
|
||||||
weechat.info_get("weechat_config_dir", ""),
|
weechat.info_get("weechat_config_dir", ""),
|
||||||
@@ -73,21 +75,18 @@ class Replacer(object):
|
|||||||
|
|
||||||
for path in data_dirs:
|
for path in data_dirs:
|
||||||
if os.path.exists(os.path.join(path, map_file)):
|
if os.path.exists(os.path.join(path, map_file)):
|
||||||
self._path = os.path.join(path, map_file)
|
return os.path.join(path, map_file)
|
||||||
break
|
|
||||||
|
|
||||||
version = weechat.info_get("version_number", "") or 0
|
|
||||||
|
|
||||||
# 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:
|
if not path:
|
||||||
|
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
|
||||||
self.path = weechat.string_eval_path_home(path, {}, {}, {})
|
return weechat.string_eval_path_home(path, {}, {}, {})
|
||||||
else:
|
else:
|
||||||
self.path = os.path.join(weechat.info_get("weechat_data_dir",
|
return os.path.join(weechat.info_get("weechat_data_dir", ""),
|
||||||
""), map_file)
|
map_file)
|
||||||
self._get_replacement_map()
|
|
||||||
|
|
||||||
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"""
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
from unittest import mock
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
|
||||||
@@ -69,6 +70,12 @@ class Weechat(object):
|
|||||||
def string_eval_path_home(self, path, pointers, extra_vars, options):
|
def string_eval_path_home(self, path, pointers, extra_vars, options):
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
def info_get(self, key, args):
|
||||||
|
_map = {'weechat_data_dir': None,
|
||||||
|
'weechat_config_dir': None,
|
||||||
|
'version_number': 0x3020000}
|
||||||
|
return _map.get(key)
|
||||||
|
|
||||||
|
|
||||||
weechat = Weechat()
|
weechat = Weechat()
|
||||||
sys.modules['weechat'] = weechat
|
sys.modules['weechat'] = weechat
|
||||||
@@ -78,11 +85,14 @@ import replacer
|
|||||||
|
|
||||||
|
|
||||||
class TestReplacer(unittest.TestCase):
|
class TestReplacer(unittest.TestCase):
|
||||||
def setUp(self):
|
|
||||||
|
@mock.patch('replacer.Replacer._locate_replacement_file')
|
||||||
|
def setUp(self, rfile):
|
||||||
fd, fname = tempfile.mkstemp()
|
fd, fname = tempfile.mkstemp()
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
|
rfile.return_value = fname
|
||||||
self._path = fname
|
self._path = fname
|
||||||
self.repl = replacer.Replacer(self._path)
|
self.repl = replacer.Replacer()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.repl = None
|
self.repl = None
|
||||||
@@ -104,6 +114,7 @@ class TestReplacer(unittest.TestCase):
|
|||||||
self.assertTrue(self.repl.delete('foo'))
|
self.assertTrue(self.repl.delete('foo'))
|
||||||
self.assertDictEqual(self.repl.replacement_map, {})
|
self.assertDictEqual(self.repl.replacement_map, {})
|
||||||
|
|
||||||
|
|
||||||
class TestDummyTests(unittest.TestCase):
|
class TestDummyTests(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
This, somehow stupid test ensures, that process of reading default
|
This, somehow stupid test ensures, that process of reading default
|
||||||
@@ -112,15 +123,22 @@ class TestDummyTests(unittest.TestCase):
|
|||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
replacer.Replacer.self_object = None
|
replacer.Replacer.self_object = None
|
||||||
|
|
||||||
def test_init(self):
|
@mock.patch('replacer.Replacer._locate_replacement_file')
|
||||||
|
def test_init(self, rfile):
|
||||||
|
rfile.return_value = 'dummy_path'
|
||||||
repl = replacer.Replacer()
|
repl = replacer.Replacer()
|
||||||
self.assertIsInstance(repl.replacement_map, dict)
|
self.assertIsInstance(repl.replacement_map, dict)
|
||||||
|
|
||||||
def test_main(self):
|
@mock.patch('replacer.Replacer._locate_replacement_file')
|
||||||
|
def test_main(self, rfile):
|
||||||
|
rfile.return_value = 'dummy_path'
|
||||||
replacer.Replacer.self_object = replacer.Replacer()
|
replacer.Replacer.self_object = replacer.Replacer()
|
||||||
replacer.main()
|
replacer.main()
|
||||||
|
|
||||||
def test_injector(self):
|
@mock.patch('replacer.Replacer._locate_replacement_file')
|
||||||
|
def test_injector(self, rfile):
|
||||||
|
|
||||||
|
rfile.return_value = 'dummy_path'
|
||||||
|
|
||||||
def fun(first, *args, **kwargs):
|
def fun(first, *args, **kwargs):
|
||||||
return first
|
return first
|
||||||
@@ -132,11 +150,14 @@ class TestDummyTests(unittest.TestCase):
|
|||||||
|
|
||||||
|
|
||||||
class TestFunctions(unittest.TestCase):
|
class TestFunctions(unittest.TestCase):
|
||||||
def setUp(self):
|
|
||||||
|
@mock.patch('replacer.Replacer._locate_replacement_file')
|
||||||
|
def setUp(self, rfile):
|
||||||
fd, fname = tempfile.mkstemp()
|
fd, fname = tempfile.mkstemp()
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
self._path = fname
|
self._path = fname
|
||||||
replacer.Replacer.self_object = replacer.Replacer(self._path)
|
rfile.return_value = fname
|
||||||
|
replacer.Replacer.self_object = replacer.Replacer()
|
||||||
self.rc = replacer.Replacer
|
self.rc = replacer.Replacer
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user