Back to index

unity  6.0.0
test_unity_logging.py
Go to the documentation of this file.
00001 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
00002 # Copyright 2012 Canonical
00003 # Author: Thomi Richards
00004 #
00005 # This program is free software: you can redistribute it and/or modify it
00006 # under the terms of the GNU General Public License version 3, as published
00007 # by the Free Software Foundation.
00008 
00009 from __future__ import absolute_import
00010 
00011 from os import remove
00012 from os.path import exists
00013 from tempfile import mktemp
00014 from testtools.matchers import Contains, Not
00015 from time import sleep
00016 
00017 
00018 from unity.emulators.unity import (
00019     start_log_to_file,
00020     reset_logging,
00021     set_log_severity,
00022     log_unity_message,
00023     )
00024 from unity.tests import UnityTestCase
00025 
00026 
00027 class UnityLoggingTests(UnityTestCase):
00028     """Tests for Unity's debug logging framework."""
00029 
00030     def start_new_log_file(self):
00031         fpath = mktemp()
00032         start_log_to_file(fpath)
00033         return fpath
00034 
00035     def test_new_file_created(self):
00036         """Unity must create log file when we call start_log_to_file.
00037         """
00038         fpath = self.start_new_log_file()
00039         self.addCleanup(remove, fpath)
00040         self.addCleanup(reset_logging)
00041         sleep(1)
00042         self.assertTrue(exists(fpath))
00043 
00044     def test_messages_arrive_in_file(self):
00045         fpath = self.start_new_log_file()
00046         log_unity_message("WARNING", "This is a warning of things to come")
00047         sleep(1)
00048         reset_logging()
00049 
00050         with open(fpath, 'r') as f:
00051             self.assertThat(f.read(), Contains("This is a warning of things to come"))
00052 
00053     def test_default_log_level_unchanged(self):
00054         fpath = self.start_new_log_file()
00055         log_unity_message("DEBUG", "This is some INFORMATION")
00056         sleep(1)
00057         reset_logging()
00058         with open(fpath, 'r') as f:
00059             self.assertThat(f.read(), Not(Contains("This is some INFORMATION")))
00060 
00061     def test_can_change_log_level(self):
00062         fpath = self.start_new_log_file()
00063         set_log_severity("", "DEBUG")
00064         self.addCleanup(set_log_severity, "", "INFO")
00065         log_unity_message("DEBUG", "This is some more INFORMATION")
00066         sleep(1)
00067         reset_logging()
00068         with open(fpath, 'r') as f:
00069             self.assertThat(f.read(), Contains("This is some more INFORMATION"))