You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
input-remapper/tests/unit/test_logger.py

155 lines
4.9 KiB
Python

4 years ago
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# input-remapper - GUI for device specific keyboard mappings
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
4 years ago
#
# This file is part of input-remapper.
4 years ago
#
# input-remapper is free software: you can redistribute it and/or modify
4 years ago
# 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.
#
# input-remapper is distributed in the hope that it will be useful,
4 years ago
# 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
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
4 years ago
from tests.test import tmp
4 years ago
import os
import shutil
import unittest
import logging
from inputremapper.logger import logger, add_filehandler, update_verbosity, log_info
from inputremapper.configs.paths import remove
4 years ago
class TestLogger(unittest.TestCase):
def tearDown(self):
update_verbosity(debug=True)
4 years ago
# remove the file handler
logger.handlers = [
3 years ago
handler
for handler in logger.handlers
4 years ago
if not isinstance(logger.handlers, logging.FileHandler)
]
3 years ago
path = os.path.join(tmp, "logger-test")
remove(path)
4 years ago
def test_key_debug(self):
3 years ago
path = os.path.join(tmp, "logger-test")
add_filehandler(path)
logger.debug_key(((1, 2, 1),), "foo %s bar", 1234)
logger.debug_key(((1, 200, -1), (1, 5, 1)), "foo %s", (1, 2))
3 years ago
with open(path, "r") as f:
content = f.read().lower()
self.assertIn(
"foo 1234 bar ·················· ((1, 2, 1))",
content,
)
self.assertIn(
"foo (1, 2) ···················· ((1, 200, -1), (1, 5, 1))",
content,
)
4 years ago
def test_log_info(self):
update_verbosity(debug=False)
3 years ago
path = os.path.join(tmp, "logger-test")
add_filehandler(path)
4 years ago
log_info()
3 years ago
with open(path, "r") as f:
4 years ago
content = f.read().lower()
self.assertIn("input-remapper", content)
4 years ago
def test_makes_path(self):
3 years ago
path = os.path.join(tmp, "logger-test")
4 years ago
if os.path.exists(path):
shutil.rmtree(path)
3 years ago
new_path = os.path.join(tmp, "logger-test", "a", "b", "c")
4 years ago
add_filehandler(new_path)
self.assertTrue(os.path.exists(new_path))
def test_clears_log(self):
3 years ago
path = os.path.join(tmp, "logger-test")
4 years ago
os.makedirs(os.path.dirname(path), exist_ok=True)
os.mknod(path)
3 years ago
with open(path, "w") as f:
f.write("aaaa\n" * 2000 + "end")
3 years ago
add_filehandler(os.path.join(tmp, "logger-test"))
with open(path, "r") as f:
# it only keeps the newest information
content = f.readlines()
self.assertLess(abs(len(content) - 1000), 10)
# whatever the logging module decides to log into that file
self.assertNotIn("aaaa", content[-1])
4 years ago
def test_debug(self):
3 years ago
path = os.path.join(tmp, "logger-test")
add_filehandler(path)
3 years ago
logger.error("abc")
logger.warning("foo")
logger.info("123")
logger.debug("456")
logger.debug("789")
3 years ago
with open(path, "r") as f:
4 years ago
content = f.read().lower()
3 years ago
self.assertIn("logger.py", content)
4 years ago
3 years ago
self.assertIn("error", content)
self.assertIn("abc", content)
4 years ago
3 years ago
self.assertIn("warn", content)
self.assertIn("foo", content)
4 years ago
3 years ago
self.assertIn("info", content)
self.assertIn("123", content)
4 years ago
3 years ago
self.assertIn("debug", content)
self.assertIn("456", content)
4 years ago
self.assertIn("debug", content)
3 years ago
self.assertIn("789", content)
4 years ago
def test_default(self):
3 years ago
path = os.path.join(tmp, "logger-test")
4 years ago
update_verbosity(debug=False)
add_filehandler(path)
3 years ago
logger.error("abc")
logger.warning("foo")
logger.info("123")
logger.debug("456")
logger.debug("789")
3 years ago
with open(path, "r") as f:
4 years ago
content = f.read().lower()
3 years ago
self.assertNotIn("logger.py", content)
self.assertNotIn("line", content)
4 years ago
3 years ago
self.assertIn("error", content)
self.assertIn("abc", content)
4 years ago
3 years ago
self.assertIn("warn", content)
self.assertIn("foo", content)
4 years ago
3 years ago
self.assertNotIn("info", content)
self.assertIn("123", content)
4 years ago
3 years ago
self.assertNotIn("debug", content)
self.assertNotIn("456", content)
4 years ago
self.assertNotIn("debug", content)
3 years ago
self.assertNotIn("789", content)
4 years ago
if __name__ == "__main__":
unittest.main()