Skip to content

Commit

Permalink
Merge pull request #163 from hadley/test-polishing
Browse files Browse the repository at this point in the history
Testing improvements
  • Loading branch information
daroczig authored Aug 4, 2024
2 parents 8a793a3 + 35e5682 commit a0efcd3
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 37 deletions.
8 changes: 5 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Suggests:
slackr (>= 1.4.1),
RPushbullet,
telegram,
testthat,
testthat (>= 3.0.0),
covr,
knitr,
rmarkdown,
Expand All @@ -36,11 +36,13 @@ Suggests:
txtq,
botor,
R.utils,
syslognet
syslognet,
withr
Enhances:
logging,
futile.logger,
log4r
VignetteBuilder: knitr
Config/testthat/edition: 2
Config/testthat/edition: 3
Config/testthat/parallel: TRUE
Roxygen: list(markdown = TRUE)
14 changes: 9 additions & 5 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(logger)

if (identical(Sys.getenv("NOT_CRAN"), "true")) {
test_check('logger')
} else {
test_check('logger', filter = '^[a-z]*$')
}
test_check("logger")
2 changes: 0 additions & 2 deletions tests/testthat/test-CRANSKIP-appenders.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ threshold <- log_threshold()
layout <- log_layout()
appender <- log_appender()

context('CRAN skip: async appender')

test_that('async logging', {
t <- tempfile()
my_appender <- appender_async(appender_file(file = t))
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-CRANSKIP-helpers.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
library(logger)
library(testthat)

context('CRAN skip: helpers')

test_that('tictoc', {
expect_match(capture.output(log_tictoc(), type = 'message'), 'timer tic 0 secs')
## let time pass a bit
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-CRANSKIP-hooks.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
library(logger)
library(testthat)

context('hooks')

eval_outside <- function(expr) {
t <- tempfile()
on.exit(unlink(t))
Expand Down
19 changes: 11 additions & 8 deletions tests/testthat/test-CRANSKIP-logger-namespaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ library(testthat)
layout <- log_layout()
appender <- log_appender()

context('CRAN skip: logging from packages')

test_that('called from package', {
devtools::load_all(system.file('demo-packages/logger-tester-package', package = 'logger'))
log_layout(layout_simple)
Expand All @@ -15,6 +13,11 @@ test_that('called from package', {
expect_output(logger_info_tester_function('everything = '), 'INFO')
})

rscript <- function(path) {
out <- system(paste('$R_HOME/bin/Rscript', path, '2>&1'), intern = TRUE)
setdiff(out, "NULL")
}

test_that('namespace in a remote R session to avoid calling from testthat', {

t <- tempfile()
Expand All @@ -23,7 +26,7 @@ test_that('namespace in a remote R session to avoid calling from testthat', {
log_layout(layout_glue_generator("{ns} / {ans} / {topenv} / {fn} / {call}"))
log_info("foobar")', file = t)
expect_equal(
system(paste('$R_HOME/bin/Rscript', t, '2>&1'), intern = TRUE),
rscript(t),
'global / global / R_GlobalEnv / NA / NA')
unlink(t)

Expand All @@ -34,7 +37,7 @@ test_that('namespace in a remote R session to avoid calling from testthat', {
f <- function() log_info("foobar")
f()', file = t)
expect_equal(
system(paste('$R_HOME/bin/Rscript', t, '2>&1'), intern = TRUE),
rscript(t),
'global / global / R_GlobalEnv / f / f()')
unlink(t)

Expand All @@ -46,7 +49,7 @@ test_that('namespace in a remote R session to avoid calling from testthat', {
g <- function() f()
g()', file = t)
expect_equal(
system(paste('$R_HOME/bin/Rscript', t, '2>&1'), intern = TRUE),
rscript(t),
'global / global / R_GlobalEnv / f / f()')
unlink(t)

Expand All @@ -58,7 +61,7 @@ test_that('namespace in a remote R session to avoid calling from testthat', {
g <- f
g()', file = t)
expect_equal(
system(paste('$R_HOME/bin/Rscript', t, '2>&1'), intern = TRUE),
rscript(t),
'global / global / R_GlobalEnv / g / g()')
unlink(t)

Expand All @@ -71,7 +74,7 @@ test_that('namespace in a remote R session to avoid calling from testthat', {
logger_info_tester_function("foobar")',
file = t)
expect_equal(
system(paste('$R_HOME/bin/Rscript', t), intern = TRUE),
rscript(t),
'logger.tester / global / logger.tester / logger_info_tester_function / logger_info_tester_function("foobar")')
unlink(t)

Expand All @@ -85,7 +88,7 @@ test_that('namespace in a remote R session to avoid calling from testthat', {
logger_info_tester_function("foobar")',
file = t)
expect_equal(
system(paste('$R_HOME/bin/Rscript', t), intern = TRUE),
rscript(t),
'logger.tester / logger.tester / logger.tester / logger_info_tester_function / logger_info_tester_function("foobar")')
unlink(t)

Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-appender.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ threshold <- log_threshold()
layout <- log_layout()
appender <- log_appender()

context('appenders')

test_that('append to file', {
log_layout(layout_glue_generator('{level} {msg}'))
log_threshold(TRACE)
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ appender <- log_appender()
log_layout(layout_glue_generator('{level} {msg}'))
log_appender(appender_stdout)

context('log_eval')
test_that('single line', {
expect_output(log_eval(4, INFO), sprintf("INFO %s => %s", shQuote(4), shQuote(4)))
})
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test-formatters.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ library(jsonlite)
formatter <- log_formatter()
appender <- log_appender()

context('formatters')
everything <- 42
g <- function() {
log_info("Hi {everything}")
Expand Down Expand Up @@ -137,8 +136,11 @@ test_that('special chars in the text work', {
expect_output(log_info('JSON: {toJSON(iris[1:2, ], auto_unbox = TRUE)}'), '[{"Sepal.Length":5.1,"Sepal.Width":3.5,"Petal.Length":1.4,"Petal.Width":0.2,"Species":"setosa"},{"Sepal.Length":4.9,"Sepal.Width":3,"Petal.Length":1.4,"Petal.Width":0.2,"Species":"setosa"}]') # nolint
})

log_formatter(formatter_pander)
test_that('pander formatter', {
log_formatter(formatter_pander)
# pander partially matches coef to coefficient
withr::local_options(warnPartialMatchDollar = FALSE)

expect_output(log_info(42), '_42_')
expect_output(log_info('42'), '42')
expect_output(log_info(head(iris)), 'Sepal.Length')
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ appender <- log_appender()

log_appender(appender_stdout)

context('helpers')

test_that('separator', {
original_layout <- log_layout()
log_layout(layout_blank)
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-layout.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ appender <- log_appender()

log_appender(appender_stdout)

context('layouts')

log_layout(layout_blank)
test_that('blank layout', {
expect_output(log_info('foobar'), 'foobar')
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-logger.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ threshold <- log_threshold()
appender <- log_appender()
layout <- log_layout()

context('loggers')

log_appender(appender_stdout)

log_threshold(WARN)
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-return.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ library(testthat)
formatter <- log_formatter()
appender <- log_appender()

context('return value')

t <- tempfile()
log_appender(appender_file(t))

Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ library(testthat)
appender <- log_appender()
log_appender(appender_stdout)

context('utils')

test_that('fail_on_missing_package', {
expect_error(fail_on_missing_package('logger'), NA)
expect_error(fail_on_missing_package('logger', '9.9.9'))
Expand Down

0 comments on commit a0efcd3

Please sign in to comment.