1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
# encoding: UTF-8
require File.expand_path('../setup', File.dirname(__FILE__))
module TestLogging
module TestAppenders
class TestFile < Test::Unit::TestCase
include LoggingTestCase
NAME = 'logfile'
def setup
super
Logging.init
FileUtils.mkdir [File.join(@tmpdir, 'dir'), File.join(@tmpdir, 'uw_dir')]
FileUtils.chmod 0555, File.join(@tmpdir, 'uw_dir')
FileUtils.touch File.join(@tmpdir, 'uw_file')
FileUtils.chmod 0444, File.join(@tmpdir, 'uw_file')
end
def test_factory_method_validates_input
assert_raise(ArgumentError) do
Logging.appenders.file
end
end
def test_class_assert_valid_logfile
log = File.join(@tmpdir, 'uw_dir', 'file.log')
assert_raise(ArgumentError) do
Logging.appenders.file(log).class.assert_valid_logfile(log)
end if Process.euid != 0
log = File.join(@tmpdir, 'dir')
assert_raise(ArgumentError) do
Logging.appenders.file(log).class.assert_valid_logfile(log)
end
log = File.join(@tmpdir, 'uw_file')
assert_raise(ArgumentError) do
Logging.appenders.file(log).class.assert_valid_logfile(log)
end if Process.euid != 0
log = File.join(@tmpdir, 'file.log')
assert Logging.appenders.file(log).class.assert_valid_logfile(log)
end
def test_initialize
log = File.join(@tmpdir, 'file.log')
appender = Logging.appenders.file(NAME, :filename => log)
assert_equal 'logfile', appender.name
assert_equal ::File.expand_path(log), appender.filename
appender << "This will be the first line\n"
appender << "This will be the second line\n"
appender.flush
File.open(log, 'r') do |file|
assert_equal "This will be the first line\n", file.readline
assert_equal "This will be the second line\n", file.readline
assert_raise(EOFError) {file.readline}
end
cleanup
appender = Logging.appenders.file(NAME, :filename => log)
assert_equal 'logfile', appender.name
assert_equal ::File.expand_path(log), appender.filename
appender << "This will be the third line\n"
appender.flush
File.open(log, 'r') do |file|
assert_equal "This will be the first line\n", file.readline
assert_equal "This will be the second line\n", file.readline
assert_equal "This will be the third line\n", file.readline
assert_raise(EOFError) {file.readline}
end
cleanup
appender = Logging.appenders.file(NAME, :filename => log,
:truncate => true)
assert_equal 'logfile', appender.name
appender << "The file was truncated\n"
appender.flush
File.open(log, 'r') do |file|
assert_equal "The file was truncated\n", file.readline
assert_raise(EOFError) {file.readline}
end
cleanup
end
def test_changing_directories
log = File.join(@tmpdir, 'file.log')
appender = Logging.appenders.file(NAME, :filename => log)
assert_equal 'logfile', appender.name
assert_equal ::File.expand_path(log), appender.filename
begin
pwd = Dir.pwd
Dir.chdir @tmpdir
assert_nothing_raised { appender.reopen }
ensure
Dir.chdir pwd
end
end
def test_encoding
log = File.join(@tmpdir, 'file-encoding.log')
appender = Logging.appenders.file(NAME, :filename => log, :encoding => 'ASCII')
appender << "A normal line of text\n"
appender << "ümlaut\n"
appender.close
lines = File.readlines(log, :encoding => 'UTF-8')
assert_equal "A normal line of text\n", lines[0]
assert_equal "ümlaut\n", lines[1]
cleanup
end
def test_reopening_should_not_truncate_the_file
log = File.join(@tmpdir, 'truncate.log')
appender = Logging.appenders.file(NAME, filename: log, truncate: true)
appender << "This will be the first line\n"
appender << "This will be the second line\n"
appender << "This will be the third line\n"
appender.reopen
File.open(log, 'r') do |file|
assert_equal "This will be the first line\n", file.readline
assert_equal "This will be the second line\n", file.readline
assert_equal "This will be the third line\n", file.readline
assert_raise(EOFError) {file.readline}
end
cleanup
end
private
def cleanup
unless Logging.appenders[NAME].nil?
Logging.appenders[NAME].close false
Logging.appenders[NAME] = nil
end
end
end # TestFile
end # TestAppenders
end # TestLogging
|