import os
import shutil
from nose.tools import (assert_equal, assert_true)
from qiutil import logging
from qiutil.logging import logger

ROOT = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
"""The test parent directory."""

FIXTURE = os.path.join(ROOT, 'fixtures', 'logging', 'logging.yaml')
"""The test fixture logging configuration file."""

RESULTS = os.path.join(ROOT, 'results', 'fixtures', 'logging')
"""The test result parent directory."""

RESULT = os.path.join(RESULTS, 'log', 'qiutil.log')
"""The resulting test log."""


class TestLoggingHelper(object):

    """The logging unit tests."""

    def setUp(self):
        shutil.rmtree(RESULTS, True)

    def tearDown(self):
        shutil.rmtree(RESULTS, True)

    def test_filename(self):
        logging.configure('test', filename=RESULT)
        logger('test').info("Test info log message.")
        logger('test').debug("Test debug log message.")
        assert_true(os.path.exists(RESULT),
                    "The log file was not created: %s" % RESULT)
        with open(RESULT) as fs:
            msgs = fs.readlines()
        assert_true(not not msgs, "No log messages in %s" % RESULT)
        assert_equal(len(msgs), 1, "Extraneous log messages in %s" % RESULT)

    def test_level(self):
        logging.configure('test', filename=RESULT, level='DEBUG')
        logger('test').info("Test info log message.")
        logger('test').debug("Test debug log message.")
        assert_true(os.path.exists(RESULT),
                    "The log file was not created: %s" % RESULT)
        with open(RESULT) as fs:
            msgs = fs.readlines()
        assert_true(not not msgs, "No log messages in %s" % RESULT)
        assert_equal(len(msgs), 2, "Extraneous log messages in %s" % RESULT)

if __name__ == "__main__":
    import nose

    nose.main(defaultTest=__name__)

@@ -54,16 +54,34 @@
 t_equal,
+ assert_not_equal,
  assert_
@@ -151,112 +151,52 @@
 ger%0A
-%0AROOT = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))%0A%22%22%22The test parent directory.%22%22%22
+from qiutil.cd import cd%0Afrom .. import ROOT
 %0A%0AFI
@@ -352,28 +352,16 @@
 esults',
- 'fixtures',
  'loggin
@@ -444,22 +444,20 @@
 'log', '
-qiutil
+test
 .log')%0A%22
@@ -508,14 +508,8 @@
 ging
-Helper
 (obj
@@ -514,17 +514,16 @@
 bject):%0A
-%0A
     %22%22%22T
@@ -1163,34 +1163,34 @@
 messages
- in
+:
  %25s%22 %25 
-RESULT)%0A
+msgs)%0A    
 %0A    def
@@ -1647,68 +1647,929 @@
 ert_
-equal(len(msgs), 2, %22Extraneous log messages in %25s%22 %25 RESULT
+not_equal(len(msgs), 1, %22Missing log messages in %25s%22 %25 RESULT)%0A        assert_equal(len(msgs), 2, %22Extraneous log messages in %25s%22 %25 RESULT)%0A%0A    def test_config_file(self):%0A        os.makedirs(RESULTS)%0A        # Since the config fixture specifies a relative log file 'log/test.log',%0A        # run this test in the context of the results directory.%0A        with cd(RESULTS):%0A            logging.configure('test', FIXTURE)%0A            logger('test').info(%22Test info log message.%22)%0A        assert_true(os.path.exists(RESULT), %22The log file was not created: %25s%22 %25%0A                                            RESULT)%0A        with open(RESULT) as fs:%0A          msgs = fs.readlines()%0A          assert_true(not not msgs, %22No log messages in %25s%22 %25 RESULT)%0A          msg = msgs%5B0%5D%0A          assert_true(msg.startswith('Custom: '), %22The log message format is%22%0A                                                  %22 incorrect: %25s%22 %25 msg
 )%0A%0Ai

Test logging config file.