import os
from MuEnvironment import MuLogging
import tempfile
import unittest
import logging


class TestMuLogging(unittest.TestCase):

    def test_can_create_console_logger(self):
        console_logger = MuLogging.setup_console_logging(False, False)
        self.assertIsNot(console_logger, None, "We created a console logger")
        MuLogging.stop_logging(console_logger)

    def test_can_create_txt_logger(self):
        test_dir = tempfile.mkdtemp()
        location, txt_logger = MuLogging.setup_txt_logger(test_dir, "test_txt")
        logging.info("Testing")
        self.assertTrue(os.path.isfile(location), "We should have created a file")
        self.assertIsNot(txt_logger, None, "We created a txt logger")
        MuLogging.stop_logging(txt_logger)

    def test_can_create_md_logger(self):
        test_dir = tempfile.mkdtemp()
        location, txt_logger = MuLogging.setup_markdown_logger(test_dir, "test_md")
        logging.info("Testing")
        self.assertTrue(os.path.isfile(location), "We should have created a file")
        self.assertIsNot(txt_logger, None, "We created a txt logger")
        MuLogging.stop_logging(txt_logger)

    def test_none_to_close(self):
        MuLogging.stop_logging(None)

    def test_can_close_logger(self):
        test_dir = tempfile.mkdtemp()
        location, txt_logger = MuLogging.setup_txt_logger(test_dir, "test_close")
        logging.critical("Testing")
        self.assertTrue(os.path.isfile(location), "We should have created a file")
        file = open(location, "r")
        num_lines = len(file.readlines())
        file.close()
        self.assertEqual(num_lines, 1, "We should only have one line")
        MuLogging.stop_logging(txt_logger)
        logging.critical("Test 2")
        file = open(location, "r")
        num_lines2 = len(file.readlines())
        file.close()
        self.assertEqual(num_lines, num_lines2, "We should only have one line")


if __name__ == '__main__':
    unittest.main()
