-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_build_log_output.R
208 lines (116 loc) · 7.89 KB
/
test_build_log_output.R
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
library(tryCatchLog)
library(testthat)
context("test_build_log_output.R")
source("init_unit_test.R")
options("width" = 129) # default value in R is 129
# suppress logging of errors and warnings to avoid overly output
source("disable_logging_output.R")
test_that("log output is correct", {
# The example stack trace was produced and saved with:
# tryLog(log("abc")) # with breakpoint in the function "tryCatchLog" to save the internal variable "call.stack"
# save(stack.trace, file = "stack_trace.RData")
# load("tests/testthat/stack_trace.RData")
load("stack_trace.RData") # creates variable "stack.trace"
log.entry <- tryCatchLog:::build.log.entry(Sys.time(), "ERROR", "msg", "", stack.trace, "", 0)
out1 <- tryCatchLog::build.log.output(log.entry, include.full.call.stack = FALSE)
# Due to a OSX bug R emits a warning in versions below 3.4.3 here:
# In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'default/Europe/Berlin'
# The reason is that the default timezone cannot be recognized by R anymore due to an OSX update.
# Symptom: Sys.timezone() # returns NA instead of a timezone string
# See https://stackoverflow.com/questions/47709061/sys-date-showing-the-wrong-date
# cat(out1) # how it looks like
expected1 <- "[ERROR] msg\n\nCompact call stack:\n 1 tryLog(log(\"abc\"))\n 2 tryLog.R#49: tryCatchLog(expr = expr, dump.errors.to.file = dump.errors.to.file, error = function(e) {\n 3 tryCatchLog.R#135: tryCatch(withCallingHandlers(expr, error = function(e) {\n\n"
expect_equal(out1, expected1, info = "include.full.call.stack = FALSE")
out2 <- tryCatchLog::build.log.output(log.entry, include.full.call.stack = TRUE)
# expected2 <- paste(readLines("tests/testthat/build_log_output_test_data_2.txt"), collapse = "\n")
expected2 <- paste(readLines("build_log_output_test_data_2.txt"), collapse = "\n")
# writeLines(out2, "build_log_output_test_data_2.txt") # to write the expected result after checking it manually
expect_equal(out2, expected2, info = "include.full.call.stack = TRUE")
out3 <- tryCatchLog::build.log.output(log.entry) # default is include.full.call.stack = TRUE
expect_equal(out3, expected2, info = "default value of include.full.call.stack") # same expected result as before
log.entry$dump.file.name <- "my.dump.file123.rda"
out4 <- tryCatchLog::build.log.output(log.entry)
expect_match(out4, "Created dump file: my.dump.file123.rda", fixed = TRUE, info = "dump file name is in output")
expect_equal(tryCatchLog::build.log.output(data.frame()), "", info = "empty log -> empty output")
expect_error(tryCatchLog::build.log.output(""), "\"data.frame\" %in% class(log.results) is not TRUE", fixed = TRUE)
})
test_that("multiple log entry rows work", {
expect_error(expect_warning(tryCatchLog({
log(-1); log("abc")
})))
log.entries <- last.tryCatchLog.result()
expect_equal(NROW(log.entries), 2)
out1 <- build.log.output(log.entries)
# out1a <- build.log.output(log.entries, include.full.call.stack = FALSE)
expect_match(out1, ".*\\[WARN\\] NaNs produced.*\\[ERROR\\] non-numeric argument to mathematical function.*")
})
test_that("include args do work", {
timestamp <- as.POSIXct("12/31/2010 9:00", format = "%m/%d/%Y %H:%M")
log.entry <- tryCatchLog:::build.log.entry(timestamp, "ERROR", "MESSAGE", "", NULL, "dump_123.rda", 0)
out <- build.log.output(log.entry, include.severity = TRUE, include.timestamp = TRUE)
expect_equal(out, "2010-12-31 09:00:00 [ERROR] MESSAGE\n\nCreated dump file: dump_123.rda\n\nCompact call stack:\n\n\nFull call stack:\n\n\n")
out <- build.log.output(log.entry, include.severity = FALSE, include.timestamp = TRUE)
expect_equal(out, "2010-12-31 09:00:00 MESSAGE\n\nCreated dump file: dump_123.rda\n\nCompact call stack:\n\n\nFull call stack:\n\n\n")
out <- build.log.output(log.entry)
expect_equal(out, "[ERROR] MESSAGE\n\nCreated dump file: dump_123.rda\n\nCompact call stack:\n\n\nFull call stack:\n\n\n")
out <- build.log.output(log.entry, include.severity = FALSE)
expect_false(grepl("ERROR", out, fixed = TRUE))
out <- build.log.output(log.entry, include.timestamp = TRUE)
expect_true(grepl("2010-12-31 09:00:00", out, fixed = TRUE))
})
test_that("platform-specific newline works", {
log.entry <- tryCatchLog:::build.log.entry(Sys.time(), "ERROR", "MESSAGE", "stack.trace", "dump.file.name", 0)
out <- build.log.output(log.entry)
expect_true(grepl("\n", out, fixed = TRUE))
with_mock(
platform.NewLine = function() return("<platform_newline>"),
# print(tryCatchLog::platform.NewLine()),
out <- build.log.output(log.entry, use.platform.newline = TRUE),
# print(out),
expect_false(grepl("\n", out, fixed = TRUE)),
expect_true(grepl("<platform_newline>", out, fixed = TRUE))
)
})
test_that("log output includes execution.context.msg", {
# The example stack trace was produced and saved with:
# tryLog(log("abc")) # with breakpoint in the function "tryCatchLog" to save the internal variable "call.stack"
# save(stack.trace, file = "stack_trace.RData")
# load("tests/testthat/stack_trace.RData")
load("stack_trace.RData") # creates variable "stack.trace"
log.entry <- tryCatchLog:::build.log.entry(Sys.time(), "ERROR", "msg", "ctx1", stack.trace, "", 0)
out1 <- tryCatchLog::build.log.output(log.entry, include.full.call.stack = FALSE)
# Due to a OSX bug R emits a warning in versions below 3.4.3 here:
# In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'default/Europe/Berlin'
# The reason is that the default timezone cannot be recognized by R anymore due to an OSX update.
# Symptom: Sys.timezone() # returns NA instead of a timezone string
# See https://stackoverflow.com/questions/47709061/sys-date-showing-the-wrong-date
# cat(out1) # how it looks like
expected1 <- "[ERROR] msg {execution.context.msg: ctx1}\n\nCompact call stack:\n 1 tryLog(log(\"abc\"))\n 2 tryLog.R#49: tryCatchLog(expr = expr, dump.errors.to.file = dump.errors.to.file, error = function(e) {\n 3 tryCatchLog.R#135: tryCatch(withCallingHandlers(expr, error = function(e) {\n\n"
expect_equal(out1, expected1)
log.entry <- tryCatchLog:::build.log.entry(Sys.time(), "ERROR", "msg", NA_character_, stack.trace, "", 0)
out2 <- tryCatchLog::build.log.output(log.entry, include.full.call.stack = FALSE)
# Due to a OSX bug R emits a warning in versions below 3.4.3 here:
# In as.POSIXlt.POSIXct(x, tz) : unknown timezone 'default/Europe/Berlin'
# The reason is that the default timezone cannot be recognized by R anymore due to an OSX update.
# Symptom: Sys.timezone() # returns NA instead of a timezone string
# See https://stackoverflow.com/questions/47709061/sys-date-showing-the-wrong-date
# cat(out2) # how it looks like
expected2 <- "[ERROR] msg\n\nCompact call stack:\n 1 tryLog(log(\"abc\"))\n 2 tryLog.R#49: tryCatchLog(expr = expr, dump.errors.to.file = dump.errors.to.file, error = function(e) {\n 3 tryCatchLog.R#135: tryCatch(withCallingHandlers(expr, error = function(e) {\n\n"
expect_equal(out2, expected2)
})
test_that("log output contains execution.context.msg", {
expect_error(expect_warning(tryCatchLog({
log(-1); log("abc")
}, execution.context.msg = "my context")
)
)
log.entries <- last.tryCatchLog.result()
expect_equal(NROW(log.entries), 2)
out1 <- build.log.output(log.entries)
# Expect:
# [WARN] NaNs produced {execution.context.msg: my context}
# [ERROR] non-numeric argument to mathematical function {execution.context.msg: my context}
expect_match(out1, paste0(".*\\[WARN\\] NaNs produced \\{execution\\.context\\.msg: my context\\}",
".*",
"\\[ERROR\\] non-numeric argument to mathematical function \\{execution\\.context\\.msg: my context\\}.*"))
})