Debian Codegraph @

tests/testthat/test_logger_config.R

raw
1 context("logger_config")
2
3
4 test_that("logger_config() works as expected", {
5 cfg <- logger_config(
6 appenders = list("Appender" = list()),
7 propagate = FALSE,
8 exception_handler = default_exception_handler,
9 threshold = NA,
10 filters = list("FilterForceLevel" = list(level = "info"))
11 )
12
13 expect_s3_class(cfg, "logger_config")
14
15 tl <- get_logger("test")$config(cfg)
16 expect_true(inherits(tl$appenders[[1]], "Appender"))
17 expect_false(is_virgin_Logger(tl))
18
19 expect_silent(tl$fatal("test")) # propagate
20 expect_equal(tl$last_event$level, 400) # force level filter
21
22 tl <- get_logger("test")$config(logger_config())
23 expect_true(is_virgin_Logger(tl))
24 })
25
26
27
28
29 test_that("as_logger_config() works as expected with YAML and JSON files", {
30 skip_if_not_installed("yaml")
31 # files work...
32 cy <- as_logger_config(rprojroot::find_testthat_root_file("testdata", "lg_full.yaml"))
33 cj <- as_logger_config(rprojroot::find_testthat_root_file("testdata", "lg_full.json"))
34 expect_identical(cj, cy)
35 expect_s3_class(cj, "logger_config")
36 expect_identical(cy$appenders[[1]]$layout$LayoutFormat$fmt, "%L %t - %m")
37
38 # so do text vectors (and scalars with newlines)
39 cy2 <- as_logger_config(
40 readLines(rprojroot::find_testthat_root_file("testdata", "lg_full.yaml"))
41 )
42 cj2 <- as_logger_config(
43 paste(readLines(rprojroot::find_testthat_root_file("testdata", "lg_full.json")), collapse = "\n")
44 )
45 expect_identical(cj2, cj)
46 expect_identical(cy2, cy)
47 })
48
49
50
51
52 test_that("as_logger_config() works for simplified yaml logger config", {
53 skip_if_not_installed("yaml")
54 cy <- as_logger_config(rprojroot::find_testthat_root_file("testdata", "lg_simple.yaml"))
55 cj <- as_logger_config(rprojroot::find_testthat_root_file("testdata", "lg_simple.json"))
56 expect_identical(cj, cy)
57 expect_s3_class(cj, "logger_config")
58
59 expect_identical(cy$appenders[[1]]$layout$LayoutFormat$fmt, "%L %t - %m")
60 expect_s3_class(cy, "logger_config")
61 })
62
63
64
65
66 test_that("logger$config() fails if yaml file is passed to `text` instead of `file`", {
67 ty <- rprojroot::find_testthat_root_file("testdata", "lg_full.yaml")
68 lg <- get_logger("test")
69 expect_error(lg$config(text = ty), "YAML")
70 lg$config(NULL)
71 })
72
73
74
75
76 test_that("resolve_r6_ctors() works as expected", {
77 tf <- tempfile()
78 on.exit(unlink(tf))
79 x <- logger_config(
80 appenders = list("AppenderFile" = list(file = tf))
81 )
82
83 res <- resolve_r6_ctors(x)
84 expect_s3_class(as_logger_config(res), "logger_config")
85 expect_identical(res$appenders[[1]]$file, tf)
86
87
88 tf2 <- tempfile()
89 tf3 <- tempfile()
90 on.exit(unlink(c(tf2, tf3)), add = TRUE)
91 x <- list(
92 "Logger" = list(
93 name = "test2",
94 appenders = list(
95 "AppenderBuffer" = list(
96 threshold = NA,
97 appenders = list(
98 "AppenderJson" = list(threshold = 100, file = tf2),
99 "AppenderFile" = list(file = tf3),
100 "Appender" = list()
101 )
102 )
103 )
104 )
105 )
106
107 res <- resolve_r6_ctors(x)[[1]]
108 expect_true(is_Logger(res))
109 expect_identical(res$appenders[[1]]$appenders[[1]]$file, tf2)
110 expect_s3_class(res$appenders[[1]]$appenders[[2]], "AppenderFile")
111 expect_identical(res$appenders[[1]]$appenders[[2]]$file, tf3)
112 expect_s3_class(res$appenders[[1]]$appenders[[3]], "Appender")
113
114 res$config(NULL)
115 })
116
117
118
119 test_that("parse_logger_config() works", {
120 skip_if_not_installed("yaml")
121 # parse_logger_config turns logger_configs into lists of R6 objects
122 # that cann direclty be applied to a logger
123 full <- as_logger_config(rprojroot::find_testthat_root_file("testdata", "lg_full.yaml"))
124 simple <- as_logger_config(rprojroot::find_testthat_root_file("testdata", "lg_simple.json"))
125
126 pf <- parse_logger_config(full)
127 ps <- parse_logger_config(simple)
128
129 lg <- get_logger("test")$config(full)
130 on.exit(lg$config(NULL))
131
132 expect_length(lg$appenders, 2)
133 expect_identical(lg$appenders$AppenderConsole$threshold, 200L)
134 expect_identical(lg$appenders$AppenderBuffer$appenders[[1]]$threshold, 123L)
135 expect_identical(lg$threshold, 400L)
136
137 lg <- get_logger("test")$config(simple)
138 expect_identical(lg$appenders$AppenderConsole$threshold, 200L)
139 expect_identical(lg$propagate, FALSE)
140 expect_identical(lg$threshold, 400L)
141 })