input-remapper/tests/unit/test_logger.py

145 lines
4.4 KiB
Python
Raw Normal View History

2020-11-29 15:21:34 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
2022-01-01 12:00:49 +00:00
# input-remapper - GUI for device specific keyboard mappings
2023-02-27 16:07:42 +00:00
# Copyright (C) 2023 sezanzeb <proxima@sezanzeb.de>
2020-11-29 15:21:34 +00:00
#
2022-01-01 12:00:49 +00:00
# This file is part of input-remapper.
2020-11-29 15:21:34 +00:00
#
2022-01-01 12:00:49 +00:00
# input-remapper is free software: you can redistribute it and/or modify
2020-11-29 15:21:34 +00:00
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
2022-01-01 12:00:49 +00:00
# input-remapper is distributed in the hope that it will be useful,
2020-11-29 15:21:34 +00:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2022-01-01 12:00:49 +00:00
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
2020-11-29 15:21:34 +00:00
import evdev
2020-11-29 15:21:34 +00:00
import os
import shutil
import unittest
import logging
from tests.lib.tmp import tmp
2022-06-25 10:23:56 +00:00
from inputremapper.logger import logger, update_verbosity, log_info, ColorfulFormatter
2022-01-31 19:58:37 +00:00
from inputremapper.configs.paths import remove
2020-11-29 15:21:34 +00:00
2022-06-25 10:23:56 +00:00
def add_filehandler(log_path):
"""Start logging to a file."""
log_path = os.path.expanduser(log_path)
os.makedirs(os.path.dirname(log_path), exist_ok=True)
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(ColorfulFormatter())
logger.addHandler(file_handler)
logger.info('Starting logging to "%s"', log_path)
2020-11-29 15:21:34 +00:00
class TestLogger(unittest.TestCase):
def tearDown(self):
2021-03-21 18:15:20 +00:00
update_verbosity(debug=True)
2020-11-29 15:21:34 +00:00
# remove the file handler
logger.handlers = [
2021-09-26 10:44:56 +00:00
handler
for handler in logger.handlers
2020-11-29 15:21:34 +00:00
if not isinstance(logger.handlers, logging.FileHandler)
]
2021-09-26 10:44:56 +00:00
path = os.path.join(tmp, "logger-test")
2021-03-21 18:15:20 +00:00
remove(path)
2020-11-29 15:21:34 +00:00
def test_write(self):
uinput = evdev.UInput(name="foo")
2021-09-26 10:44:56 +00:00
path = os.path.join(tmp, "logger-test")
2021-03-21 18:15:20 +00:00
add_filehandler(path)
logger.write((evdev.ecodes.EV_KEY, evdev.ecodes.KEY_A, 1), uinput)
2021-09-26 10:44:56 +00:00
with open(path, "r") as f:
content = f.read()
2022-02-27 14:43:01 +00:00
self.assertIn(
f'Writing (1, 30, 1) to "foo"',
2022-02-27 14:43:01 +00:00
content,
)
2021-01-05 18:33:47 +00:00
2020-11-29 15:21:34 +00:00
def test_log_info(self):
update_verbosity(debug=False)
2021-09-26 10:44:56 +00:00
path = os.path.join(tmp, "logger-test")
2021-03-21 18:15:20 +00:00
add_filehandler(path)
2020-11-29 15:21:34 +00:00
log_info()
2021-09-26 10:44:56 +00:00
with open(path, "r") as f:
2020-11-29 15:21:34 +00:00
content = f.read().lower()
2022-01-01 12:00:49 +00:00
self.assertIn("input-remapper", content)
2020-11-29 15:21:34 +00:00
def test_makes_path(self):
2021-09-26 10:44:56 +00:00
path = os.path.join(tmp, "logger-test")
2020-11-29 15:21:34 +00:00
if os.path.exists(path):
shutil.rmtree(path)
2021-09-26 10:44:56 +00:00
new_path = os.path.join(tmp, "logger-test", "a", "b", "c")
2020-11-29 15:21:34 +00:00
add_filehandler(new_path)
self.assertTrue(os.path.exists(new_path))
def test_debug(self):
2021-09-26 10:44:56 +00:00
path = os.path.join(tmp, "logger-test")
2021-03-21 18:15:20 +00:00
add_filehandler(path)
2021-09-26 10:44:56 +00:00
logger.error("abc")
logger.warning("foo")
logger.info("123")
logger.debug("456")
logger.debug("789")
2021-09-26 10:44:56 +00:00
with open(path, "r") as f:
2020-11-29 15:21:34 +00:00
content = f.read().lower()
2021-09-26 10:44:56 +00:00
self.assertIn("logger.py", content)
2020-11-29 15:21:34 +00:00
2021-09-26 10:44:56 +00:00
self.assertIn("error", content)
self.assertIn("abc", content)
2020-11-29 15:21:34 +00:00
2021-09-26 10:44:56 +00:00
self.assertIn("warn", content)
self.assertIn("foo", content)
2020-11-29 15:21:34 +00:00
2021-09-26 10:44:56 +00:00
self.assertIn("info", content)
self.assertIn("123", content)
2020-11-29 15:21:34 +00:00
2021-09-26 10:44:56 +00:00
self.assertIn("debug", content)
self.assertIn("456", content)
2020-11-29 15:21:34 +00:00
self.assertIn("debug", content)
2021-09-26 10:44:56 +00:00
self.assertIn("789", content)
2020-11-29 15:21:34 +00:00
def test_default(self):
2021-09-26 10:44:56 +00:00
path = os.path.join(tmp, "logger-test")
2020-11-29 15:21:34 +00:00
update_verbosity(debug=False)
add_filehandler(path)
2021-09-26 10:44:56 +00:00
logger.error("abc")
logger.warning("foo")
logger.info("123")
logger.debug("456")
logger.debug("789")
2021-09-26 10:44:56 +00:00
with open(path, "r") as f:
2020-11-29 15:21:34 +00:00
content = f.read().lower()
2021-09-26 10:44:56 +00:00
self.assertNotIn("logger.py", content)
self.assertNotIn("line", content)
2020-11-29 15:21:34 +00:00
2021-09-26 10:44:56 +00:00
self.assertIn("error", content)
self.assertIn("abc", content)
2020-11-29 15:21:34 +00:00
2021-09-26 10:44:56 +00:00
self.assertIn("warn", content)
self.assertIn("foo", content)
2020-11-29 15:21:34 +00:00
2021-09-26 10:44:56 +00:00
self.assertNotIn("info", content)
self.assertIn("123", content)
2020-11-29 15:21:34 +00:00
2021-09-26 10:44:56 +00:00
self.assertNotIn("debug", content)
self.assertNotIn("456", content)
2020-11-29 15:21:34 +00:00
self.assertNotIn("debug", content)
2021-09-26 10:44:56 +00:00
self.assertNotIn("789", content)
2020-11-29 15:21:34 +00:00
if __name__ == "__main__":
unittest.main()