Skip to content

Commit 8011354

Browse files
committed
improve test running in clean state first
1 parent 8539d9c commit 8011354

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

tests/testthat/helper.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,22 @@ install_dev_package <- function(.local_envir = parent.frame()) {
285285
)
286286
}
287287
}
288+
289+
local_clean_state <- function(env = parent.frame()) {
290+
withr::local_envvar(
291+
# gha debug env variables
292+
ACTIONS_RUNNER_DEBUG = NA,
293+
ACTIONS_STEP_DEBUG = NA,
294+
# quarto R env variables
295+
R_QUARTO_LOG_DEBUG = NA,
296+
R_QUARTO_LOG_FILE = NA,
297+
# quarto CLI env variables
298+
QUARTO_LOG_LEVEL = NA,
299+
.local_envir = env
300+
)
301+
withr::local_options(
302+
quarto.log.debug = NULL,
303+
quarto.log.file = NULL,
304+
.local_envir = env
305+
)
306+
}

tests/testthat/test-r-logging.R

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
test_that("options quarto.log.debug and env var R_QUARTO_LOG_DEBUG", {
22
# clean state
3-
withr::local_options(quarto.log.debug = NULL)
4-
withr::local_envvar(R_QUARTO_LOG_DEBUG = NA)
3+
local_clean_state()
54
expect_false(is_quarto_r_debug())
65
withr::with_envvar(list(R_QUARTO_LOG_DEBUG = TRUE), {
76
expect_true(is_quarto_r_debug())
@@ -23,7 +22,7 @@ test_that("options quarto.log.debug and env var R_QUARTO_LOG_DEBUG", {
2322
})
2423

2524
test_that("quarto_log_level respects Quarto env var", {
26-
withr::local_envvar(QUARTO_LOG_LEVEL = NA)
25+
local_clean_state()
2726
expect_true(is.na(quarto_log_level()))
2827
expect_false(quarto_log_level("DEBUG"))
2928
withr::with_envvar(list(QUARTO_LOG_LEVEL = "DEBUG"), {
@@ -44,7 +43,7 @@ test_that("quarto_log_level respects Quarto env var", {
4443
})
4544

4645
test_that("in_debug_mode respects GHA CI env var", {
47-
withr::local_envvar(ACTIONS_RUNNER_DEBUG = NA, ACTIONS_STEP_DEBUG = NA)
46+
local_clean_state()
4847
expect_false(in_debug_mode())
4948

5049
withr::with_envvar(list(ACTIONS_RUNNER_DEBUG = "true"), {
@@ -65,8 +64,7 @@ test_that("in_debug_mode respects GHA CI env var", {
6564

6665
test_that("in_debug mode respects quarto_log_level", {
6766
# clean state
68-
withr::local_envvar(ACTIONS_RUNNER_DEBUG = NA, ACTIONS_STEP_DEBUG = NA)
69-
withr::local_envvar(QUARTO_LOG_LEVEL = NA)
67+
local_clean_state()
7068
expect_false(in_debug_mode())
7169

7270
withr::with_envvar(list(QUARTO_LOG_LEVEL = "DEBUG"), {
@@ -84,10 +82,7 @@ test_that("in_debug mode respects quarto_log_level", {
8482

8583
test_that("in_debug_mode respects R_QUARTO_LOG_DEBUG and quarto.log.debug", {
8684
# clean state
87-
withr::local_envvar(ACTIONS_RUNNER_DEBUG = NA, ACTIONS_STEP_DEBUG = NA)
88-
withr::local_envvar(QUARTO_LOG_LEVEL = NA)
89-
withr::local_envvar(R_QUARTO_LOG_DEBUG = NA)
90-
withr::local_options(quarto.log.debug = NULL)
85+
local_clean_state()
9186
expect_false(in_debug_mode())
9287

9388
withr::with_envvar(list(R_QUARTO_LOG_DEBUG = TRUE), {
@@ -112,14 +107,9 @@ test_that("in_debug_mode respects R_QUARTO_LOG_DEBUG and quarto.log.debug", {
112107
})
113108

114109
test_that("quarto_log only logs when in debug mode", {
115-
temp_file <- tempfile(fileext = ".log")
116-
on.exit(unlink(temp_file))
110+
temp_file <- withr::local_tempfile(fileext = ".log")
117111

118-
# Test with debug mode off
119-
withr::local_envvar(ACTIONS_RUNNER_DEBUG = NA, ACTIONS_STEP_DEBUG = NA)
120-
withr::local_envvar(QUARTO_LOG_LEVEL = NA)
121-
withr::local_envvar(R_QUARTO_LOG_DEBUG = NA)
122-
withr::local_options(quarto.log.debug = NULL)
112+
local_clean_state()
123113

124114
result <- quarto_log("test message", file = temp_file)
125115
expect_false(result)
@@ -141,10 +131,10 @@ test_that("quarto_log only logs when in debug mode", {
141131
})
142132

143133
test_that("quarto_log respects log file configuration", {
144-
temp_file <- tempfile(fileext = ".log")
145-
on.exit(unlink(temp_file))
134+
temp_file <- withr::local_tempfile(fileext = ".log")
135+
136+
local_clean_state()
146137

147-
# Enable debug mode
148138
withr::local_options(quarto.log.debug = TRUE)
149139

150140
# Test with file parameter
@@ -169,13 +159,16 @@ test_that("quarto_log respects log file configuration", {
169159
})
170160

171161
# Test with no file configured
162+
unlink(temp_file)
172163
result <- quarto_log("no file configured")
173164
expect_false(result)
165+
expect_false(file.exists(temp_file))
174166
})
175167

176168
test_that("quarto_log handles custom formatting", {
177-
temp_file <- tempfile(fileext = ".log")
178-
on.exit(unlink(temp_file))
169+
temp_file <- withr::local_tempfile(fileext = ".log")
170+
171+
local_clean_state()
179172

180173
withr::local_options(quarto.log.debug = TRUE)
181174

0 commit comments

Comments
 (0)