Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use expect_snapshot() for errors #200

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Check if R package can be loaded and fails loudly otherwise
#' @param pkg string
#' @param min_version optional minimum version needed
#' @param call Call to include in error message.
#' @export
#' @importFrom utils packageVersion compareVersion
#' @examples \dontrun{
Expand All @@ -9,8 +10,8 @@
#' g <- function() fail_on_missing_package("stats")
#' g()
#' }
fail_on_missing_package <- function(pkg, min_version) {
pc <- sys.call(which = 1)
fail_on_missing_package <- function(pkg, min_version, call = NULL) {
pc <- call %||% sys.call(which = 1)
if (!requireNamespace(pkg, quietly = TRUE)) {
stop(
sprintf(
Expand Down Expand Up @@ -100,3 +101,7 @@ catch_base_log <- function(level, namespace, .topcall = sys.call(-1), .topenv =
in_pkgdown <- function() {
identical(Sys.getenv("IN_PKGDOWN"), "true")
}

`%||%` <- function(x, y) {
if (is.null(x)) y else x
}
4 changes: 3 additions & 1 deletion man/fail_on_missing_package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions tests/testthat/_snaps/formatters.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# glue works

Code
formatter_glue("malformed {")
Condition
Error in `h()`:
! `glue` failed in `formatter_glue` on:

chr "malformed {"

Raw error message:

Expecting '}'

Please consider using another `log_formatter` or `skip_formatter` on strings with curly braces.

# glue gives informative error if message contains curlies

Code
Expand All @@ -14,3 +30,48 @@

Please consider using another `log_formatter` or `skip_formatter` on strings with curly braces.

# glue_safe works

Code
formatter_glue_safe("Hi {42}")
Condition
Error in `value[[3L]]()`:
! `glue_safe` failed in `formatter_glue_safe` on:

chr "Hi {42}"

Raw error message:

object '42' not found

Please consider using another `log_formatter` or `skip_formatter` on strings with curly braces.
Code
formatter_glue_safe("malformed {")
Condition
Error in `value[[3L]]()`:
! `glue_safe` failed in `formatter_glue_safe` on:

chr "malformed {"

Raw error message:

Expecting '}'

Please consider using another `log_formatter` or `skip_formatter` on strings with curly braces.

# sprintf works

Code
formatter_sprintf("%s and %i", 1)
Condition
Error in `sprintf()`:
! too few arguments

# skip formatter

Code
log_info(skip_formatter("hi {x}", x = 4))
Condition
Error in `skip_formatter()`:
! Cannot skip the formatter function if further arguments are passed besides the actual log message(s)

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/helpers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# log failure

Code
capture.output(log_failure(foobar))
Condition
Error:
! object 'foobar' not found

41 changes: 41 additions & 0 deletions tests/testthat/_snaps/layout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# must throw errors

Code
layout_simple(FOOBAR)
Condition
Error:
! object 'FOOBAR' not found
Code
layout_simple(42)
Condition
Error in `layout_simple()`:
! argument "msg" is missing, with no default
Code
layout_simple(msg = "foobar")
Condition
Error in `layout_simple()`:
! argument "level" is missing, with no default

---

Code
layout_glue(FOOBAR)
Condition
Error:
! object 'FOOBAR' not found
Code
layout_glue(42)
Condition
Error in `layout_glue()`:
! Invalid log level, see ?log_levels
Code
layout_glue(msg = "foobar")
Condition
Error in `layout_glue()`:
! argument "level" is missing, with no default
Code
layout_glue(level = 53, msg = "foobar")
Condition
Error in `layout_glue()`:
! Invalid log level, see ?log_levels

21 changes: 21 additions & 0 deletions tests/testthat/_snaps/utils.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# fail_on_missing_package

Code
fail_on_missing_package("logger", "9.9.9", call = quote(f()))
Condition
Error:
! Please install min. 9.9.9 version of logger to use f
Code
fail_on_missing_package("an.R.package-that-doesNotExists", call = quote(f()))
Condition
Error:
! Please install the 'an.R.package-that-doesNotExists' package to use f
daroczig marked this conversation as resolved.
Show resolved Hide resolved

# validate_log_level

Code
validate_log_level("FOOBAR")
Condition
Error in `validate_log_level()`:
! Invalid log level

19 changes: 10 additions & 9 deletions tests/testthat/test-formatters.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ test_that("glue works", {
formatter = formatter_glue,
appender = appender_void,
)
expect_error(formatter_glue("malformed {"))
expect_error(formatter_glue("malformed {{"), NA)
expect_snapshot(formatter_glue("malformed {"), error = TRUE)
expect_no_error(formatter_glue("malformed {{"))

## nolint start
## disabled for https://github.com/atalv/azlogr/issues/35
Expand Down Expand Up @@ -54,9 +54,11 @@ test_that("glue_safe works", {
f <- function() log_info("Hi {a}")
expect_output(f(), "43")

expect_error(formatter_glue_safe("Hi {42}"))
expect_error(formatter_glue_safe("malformed {"))
expect_error(formatter_glue_safe("malformed {{"), NA)
expect_snapshot(error = TRUE, {
formatter_glue_safe("Hi {42}")
formatter_glue_safe("malformed {")
})
expect_no_error(formatter_glue_safe("malformed {{"))
})

test_that("sprintf works", {
Expand All @@ -71,7 +73,7 @@ test_that("sprintf works", {
expect_equal(formatter_sprintf("pi is %s", round(pi, 2)), "pi is 3.14")
expect_equal(formatter_sprintf("pi is %1.2f", pi), "pi is 3.14")

expect_error(formatter_sprintf("%s and %i", 1))
expect_snapshot(formatter_sprintf("%s and %i", 1), error = TRUE)
expect_equal(formatter_sprintf("%s and %i", 1, 2), "1 and 2")

a <- 43
Expand Down Expand Up @@ -100,7 +102,7 @@ test_that("glue+sprintf works", {

for (fn in c(formatter_sprintf, formatter_glue_or_sprintf)) {
local_test_logger(formatter = fn, appender = appender_void)
expect_error(log_info(character(0)), NA)
expect_no_error(log_info(character(0)))

local_test_logger(formatter = fn)
expect_output(log_info(character(0)), "INFO")
Expand Down Expand Up @@ -152,8 +154,7 @@ test_that("paste formatter in actual logs", {
test_that("skip formatter", {
local_test_logger(formatter = formatter_glue)
expect_output(log_info(skip_formatter("hi {pi}")), "hi \\{pi\\}")
expect_error(log_info(skip_formatter(mtcars)))
expect_error(log_info(skip_formatter("hi {x}", x = 4)))
expect_snapshot(log_info(skip_formatter("hi {x}", x = 4)), error = TRUE)
})

test_that("skip formatter", {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ test_that("log failure", {
local_test_logger()
expect_output(log_failure("foobar"), NA)
expect_output(try(log_failure(foobar), silent = TRUE), "ERROR.*foobar")
expect_error(log_failure("foobar"), NA)
expect_match(capture.output(expect_error(log_failure(foobar))), "not found")
expect_no_error(log_failure("foobar"))
expect_snapshot(capture.output(log_failure(foobar)), error = TRUE)
})
20 changes: 12 additions & 8 deletions tests/testthat/test-layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ test_that("JSON parser layout", {
})

test_that("must throw errors", {
expect_error(layout_simple(FOOBAR))
expect_error(layout_simple(42))
expect_error(layout_simple(msg = "foobar"))

expect_error(layout_glue(FOOBAR))
expect_error(layout_glue(42))
expect_error(layout_glue(msg = "foobar"))
expect_error(layout_glue(level = 53, msg = "foobar"))
expect_snapshot(error = TRUE, {
layout_simple(FOOBAR)
layout_simple(42)
layout_simple(msg = "foobar")
})

expect_snapshot(error = TRUE, {
layout_glue(FOOBAR)
layout_glue(42)
layout_glue(msg = "foobar")
layout_glue(level = 53, msg = "foobar")
})
})

test_that("logging layout", {
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-logger.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,16 @@ test_that("config setter called from do.call", {
local_test_logger()

t <- withr::local_tempfile()
expect_error(do.call(log_appender, list(appender_file(t))), NA)
expect_no_error(do.call(log_appender, list(appender_file(t))))
log_info(42)
expect_length(readLines(t), 1)
expect_error(do.call(log_threshold, list(ERROR)), NA)
expect_no_error(do.call(log_threshold, list(ERROR)))
log_info(42)
expect_length(readLines(t), 1)
expect_error(do.call(log_threshold, list(INFO)), NA)
expect_no_error(do.call(log_threshold, list(INFO)))
log_info(42)
expect_length(readLines(t), 2)
expect_error(do.call(log_layout, list(formatter_paste)), NA)
expect_no_error(do.call(log_layout, list(formatter_paste)))
log_info(42)
expect_length(readLines(t), 3)
})
Expand Down
11 changes: 7 additions & 4 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
test_that("fail_on_missing_package", {
expect_error(fail_on_missing_package("logger"), NA)
expect_error(fail_on_missing_package("logger", "9.9.9"))
expect_error(fail_on_missing_package("an.R.package-that-doesNotExists"))
expect_no_error(fail_on_missing_package("logger"))

expect_snapshot(error = TRUE, {
fail_on_missing_package("logger", "9.9.9", call = quote(f()))
fail_on_missing_package("an.R.package-that-doesNotExists", call = quote(f()))
})
})

test_that("validate_log_level", {
expect_equal(validate_log_level(ERROR), ERROR)
expect_equal(validate_log_level("ERROR"), ERROR)
expect_error(validate_log_level("FOOBAR"), "log level")
expect_snapshot(validate_log_level("FOOBAR"), error = TRUE)
})

test_that("catch_base_log", {
Expand Down
Loading