-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathruntests.jl
326 lines (278 loc) · 10 KB
/
runtests.jl
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using LoggingExtras
using Test
using Test: collect_test_logs, TestLogger
using Dates
using Base.CoreLogging
using Base.CoreLogging: BelowMinLevel, Debug, Info, Warn, Error
@testset "Tee" begin
@testset "contructor" begin
@testset "mixed types" begin
@test TeeLogger(TestLogger(), NullLogger()) isa TeeLogger
end
@testset "errors if given nonloggers" begin
@test_throws Exception TeeLogger(stdout, stderr)
end
end
@testset "basic use with compositional levels" begin
testlogger_info = TestLogger(min_level=Info)
testlogger_warn = TestLogger(min_level=Warn)
with_logger(TeeLogger(testlogger_warn, testlogger_info)) do
@info "info1"
@warn "warn1"
@info "info2"
end
@test length(testlogger_info.logs) == 3
@test length(testlogger_warn.logs) == 1
end
end
@testset "File" begin
mktempdir() do dir
for (filepath, sink) in [
(f = joinpath(dir, "log"); (f, f)), # Filepath
(f = joinpath(dir, "log_io"); (f, open(f, "w"))), # IOStream
]
with_logger(FileLogger(sink)) do
@info "first"
@warn "second"
@info "third"
end
sink isa IOStream && close(sink)
logtext = read(filepath, String)
@test occursin("first", logtext)
@test occursin("second", logtext)
@test occursin("third", logtext)
end
end
end
@testset "Active Filter" begin
testlogger = TestLogger()
yodawg_filter(logargs) = startswith(logargs.message, "Yo Dawg!")
filtered_logger = ActiveFilteredLogger(yodawg_filter, testlogger)
with_logger(filtered_logger) do
@info "info1"
@warn "Yo Dawg! It is a warning"
@info "info2"
@info "Yo Dawg! It's all good"
@info "info 3"
end
@test length(testlogger.logs) == 2
end
@testset "Early Filter" begin
testlogger = TestLogger()
filtered_logger = EarlyFilteredLogger(testlogger) do logargs
logargs.level == Info # Only exactly Info, nothing more nothing less
end
with_logger(filtered_logger) do
@info "info1"
@warn "Yo Dawg! It is a warning"
@info "info2"
@info "Yo Dawg! It's all good"
end
@test length(testlogger.logs) == 3
end
@testset "MinLevel Filter" begin
testlogger = TestLogger()
filtered_logger = MinLevelLogger(testlogger, Warn)
with_logger(filtered_logger) do
@info "info1"
@warn "Yo Dawg! It is a warning"
@info "info2"
@info "Yo Dawg! It's all good"
@info "info 3"
@error "MISTAKES WERE MADE"
end
@test length(testlogger.logs) == 2
end
@testset "Transformer" begin
testlogger = TestLogger(min_level=Error)
transformer_logger = TransformerLogger(testlogger) do log_msg
# We are going to transform all warnings into errors
if log_msg.level == Warn
return merge(log_msg, (; level=Error))
else
return log_msg
end
end
with_logger(transformer_logger) do
@info "info1"
@warn "Yo Dawg! It is a warning"
@info "info2"
@info "Yo Dawg! It's all good"
@info "info 3"
@error "MISTAKES WERE MADE"
end
@test length(testlogger.logs) == 2
end
@testset "DatetimeRotatingFileLogger" begin
mktempdir() do dir
drfl_min = DatetimeRotatingFileLogger(dir, raw"\m\i\n-YYYY-mm-dd-HH-MM.\l\o\g")
drfl_hour = DatetimeRotatingFileLogger(dir, raw"\h\o\u\r-YYYY-mm-dd-HH.\l\o\g")
func = (io, args) -> println(io, reverse(args.message))
drfl_fmt = DatetimeRotatingFileLogger(func, dir, raw"\f\m\t-YYYY-mm-dd-HH-MM.\l\o\g")
callback_record = []
callback(f) = filesize(f) > 0 && push!(callback_record, f)
drfl_cb = DatetimeRotatingFileLogger(dir, raw"\c\o\m\p-YYYY-mm-dd-HH-MM.\l\o\g";
rotation_callback=callback)
sink = TeeLogger(drfl_min, drfl_hour, drfl_fmt, drfl_cb)
with_logger(sink) do
# Make sure to trigger one minute-level rotation and no hour-level
# rotation by sleeping until HH:MM:55
n = now()
sleeptime = mod((55 - second(n)), 60)
minute(n) == 59 && (sleeptime += 60)
sleep(sleeptime)
@info "first"
@info "second"
sleep(10) # Should rotate to next minute
@info("third")
end
# Drop anything that's not a .log file or empty
files = sort(map(f -> joinpath(dir, f), readdir(dir)))
files = filter(f -> endswith(f, ".log") && filesize(f) > 0, files)
min_files = filter(f -> startswith(basename(f), "min-"), files)
@test length(min_files) == 2
hour_files = filter(f -> startswith(basename(f), "hour-"), files)
@test length(hour_files) == 1
fmt_files = filter(f -> startswith(basename(f), "fmt-"), files)
@test length(fmt_files) == 2
# Two files exist, but just one have been rotated
@test length(callback_record) == 1
@test occursin(r"comp-\d{4}(-\d{2}){4}\.log$", callback_record[1])
min1_data = String(read(min_files[1]))
@test occursin("first", min1_data)
@test occursin("second", min1_data)
min2_data = String(read(min_files[2]))
@test occursin("third", min2_data)
min_data = String(read(hour_files[1]))
@test occursin("first", min_data)
@test occursin("second", min_data)
@test occursin("third", min_data)
fmt_data = String(read(fmt_files[1]))
@test occursin("tsrif", fmt_data)
@test occursin("dnoces", fmt_data)
fmt_data = String(read(fmt_files[2]))
@test occursin("driht", fmt_data)
# Sub-minute resolution not allowed
@test_throws(ArgumentError("rotating the logger with sub-minute resolution not supported"),
DatetimeRotatingFileLogger(dir, "HH-MM-SS"))
# Test constructors with pattern as a DateFormat
l = DatetimeRotatingFileLogger(dir, raw"yyyy-mm-dd.\l\o\g")
l1 = DatetimeRotatingFileLogger(dir, dateformat"yyyy-mm-dd.\l\o\g")
l2 = DatetimeRotatingFileLogger(identity, dir, dateformat"yyyy-mm-dd.\l\o\g")
@test l.filename_pattern == l1.filename_pattern == l2.filename_pattern
# Test large resolutions, which have some weirdness related to ordering
# (see next_datetime_transition()). These should not throw.
DatetimeRotatingFileLogger(dir, raw"yyyy-mm.\l\o\g")
DatetimeRotatingFileLogger(dir, raw"yyyy.\l\o\g")
end
end
@testset "FormatLogger" begin
io = IOBuffer()
logger = FormatLogger(io) do io, args
# Put in some bogus sleep calls just to test that
# log records writes in one go
print(io, args.level)
sleep(rand())
print(io, ": ")
sleep(rand())
println(io, args.message)
end
with_logger(logger) do
@sync begin
@async @debug "debug message"
@async @info "info message"
@async @warn "warning message"
@async @error "error message"
end
end
str = String(take!(io))
@test occursin(r"^Debug: debug message$"m, str)
@test occursin(r"^Info: info message$"m, str)
@test occursin(r"^Warn: warning message$"m, str)
@test occursin(r"^Error: error message$"m, str)
@test logger.always_flush
# Test constructor with default io and kwarg
logger = FormatLogger(x -> x; always_flush=false)
@test logger.stream === stderr
@test !logger.always_flush
# test file arguments
mktempdir() do dir
f = joinpath(dir, "test.log")
logger = FormatLogger(f) do io, args
println(io, "log message")
end
with_logger(logger) do
@info "test message"
end
l = read(f, String)
@test startswith(l, "log message")
end
end
@testset "LevelOverrideLogger" begin
logger = TestLogger(min_level=Info)
with_logger(LevelOverrideLogger(Debug, logger)) do
@debug "debug message"
@info "info message"
end
@test length(logger.logs) == 2
@test map(x -> x.level, logger.logs) == [Debug, Info]
end
@testset "withlevel convenience" begin
logger = TestLogger(min_level=Info)
with_logger(logger) do
LoggingExtras.withlevel(Debug) do
@debug "debug message"
end
end
@test logger.logs[1].level == Debug
logger = TestLogger(min_level=Info)
with_logger(logger) do
LoggingExtras.withlevel(Debug; group=:foo) do
@debug "debug message" _group=:foo
@debug "debug message" _group=:bar
end
end
@test logger.logs[1].level == Debug
# test that we filtered on only foo group log messages
@test length(logger.logs) == 1
end
# Define a module for testing conditional debug messages
module JuliaDebugTest
function debug_message(str)
@debug str
end
end
@testset "JULIA_DEBUG" begin
expected_messages = 0
logger = TestLogger(min_level=Info)
with_logger(logger) do
JuliaDebugTest.debug_message("debug")
end
@test length(logger.logs) == expected_messages
extra_logger = TransformerLogger(logger) do log
merge(log, (; message="New message"))
end
with_logger(extra_logger) do
JuliaDebugTest.debug_message("debug")
end
@test length(logger.logs) == expected_messages
ENV["JULIA_DEBUG"] = "JuliaDebugTest"
with_logger(logger) do
JuliaDebugTest.debug_message("debug")
expected_messages += 1
end
@test length(logger.logs) == expected_messages
with_logger(extra_logger) do
JuliaDebugTest.debug_message("debug")
expected_messages += 1
end
@test length(logger.logs) == expected_messages
delete!(ENV, "JULIA_DEBUG")
with_logger(extra_logger) do
JuliaDebugTest.debug_message("debug")
end
@test length(logger.logs) == expected_messages
end
@testset "Deprecations" begin
# Nothing is currently deprecated
end