test/test_logging.rb in logging-0.3.1 vs test/test_logging.rb in logging-0.4.0

- old
+ new

@@ -1,20 +1,34 @@ -# $Id: test_logging.rb 22 2007-01-29 16:20:54Z tim_pease $ +# $Id: test_logging.rb 34 2007-02-28 21:42:41Z tim_pease $ require 'test/setup.rb' +require 'fileutils' module TestLogging class TestLogging < Test::Unit::TestCase include LoggingTestCase + TMP = 'tmp' + def setup super @levels = ::Logging::LEVELS @lnames = ::Logging::LNAMES + + FileUtils.rm_rf TMP + FileUtils.mkdir(TMP) + @fn = File.join(TMP, 'test.log') + @glob = File.join(TMP, '*.log') end + def teardown + h = ::Logging::Repository.instance.instance_variable_get :@h + h.values.each {|l| l.close if l.respond_to? :close} + FileUtils.rm_rf TMP + end + def test_configure assert_raise(ArgumentError) {::Logging.configure 'blah.txt'} ::Logging.configure 'examples/logging.yaml' @@ -74,9 +88,49 @@ assert_equal 2, appenders.length assert_equal ['logfile', 'stderr'], appenders.map {|a| a.name}.sort # cleanup File.delete('temp.log') if File.exist?('temp.log') + end + + def test_logger + assert_raise(TypeError) {::Logging.logger []} + + logger = ::Logging.logger STDOUT + assert_match %r/\A-?\d+\z/, logger.name + assert_same logger, ::Logging.logger(STDOUT) + + logger.close + assert !STDOUT.closed? + + assert !File.exist?(@fn) + fd = File.new @fn, 'w' + logger = ::Logging.logger fd, 2, 100 + assert_equal @fn, logger.name + logger.debug 'this is a debug message' + logger.warn 'this is a warning message' + logger.error 'and now we should have over 100 bytes of data ' + + 'in the log file' + logger.info 'but the log file should not roll since we provided ' + + 'a file descriptor -- not a file name' + logger.close + assert fd.closed? + assert File.exist?(@fn) + assert_equal 1, Dir.glob(@glob).length + + FileUtils.rm_f @fn + assert !File.exist?(@fn) + logger = ::Logging.logger @fn, 2, 100 + assert File.exist?(@fn) + assert_equal @fn, logger.name + logger.debug 'this is a debug message' + logger.warn 'this is a warning message' + logger.error 'and now we should have over 100 bytes of data ' + + 'in the log file' + logger.info 'but the log file should not roll since we provided ' + + 'a file descriptor -- not a file name' + logger.close + assert_equal 3, Dir.glob(@glob).length end def test_define_levels_default empty = {} assert_equal empty, @levels