|
| 1 | +test_that("options quarto.log.debug and env var R_QUARTO_LOG_DEBUG", { |
| 2 | + # clean state |
| 3 | + withr::local_options(quarto.log.debug = NULL) |
| 4 | + withr::local_envvar(R_QUARTO_LOG_DEBUG = NA) |
| 5 | + expect_false(is_quarto_r_debug()) |
| 6 | + withr::with_envvar(list(R_QUARTO_LOG_DEBUG = TRUE), { |
| 7 | + expect_true(is_quarto_r_debug()) |
| 8 | + # option takes precedence over env var |
| 9 | + withr::with_options(list(quarto.log.debug = FALSE), { |
| 10 | + expect_false(is_quarto_r_debug()) |
| 11 | + }) |
| 12 | + }) |
| 13 | + withr::with_options(list(quarto.log.debug = TRUE), { |
| 14 | + expect_true(is_quarto_r_debug()) |
| 15 | + }) |
| 16 | + withr::with_envvar(list(R_QUARTO_LOG_DEBUG = FALSE), { |
| 17 | + expect_false(is_quarto_r_debug()) |
| 18 | + # option takes precedence over env var |
| 19 | + withr::with_options(list(quarto.log.debug = TRUE), { |
| 20 | + expect_true(is_quarto_r_debug()) |
| 21 | + }) |
| 22 | + }) |
| 23 | +}) |
| 24 | + |
| 25 | +test_that("quarto_log_level respects Quarto env var", { |
| 26 | + withr::local_envvar(QUARTO_LOG_LEVEL = NA) |
| 27 | + expect_true(is.na(quarto_log_level())) |
| 28 | + expect_false(quarto_log_level("DEBUG")) |
| 29 | + withr::with_envvar(list(QUARTO_LOG_LEVEL = "DEBUG"), { |
| 30 | + expect_true(quarto_log_level("DEBUG")) |
| 31 | + expect_false(quarto_log_level("INFO")) |
| 32 | + expect_false(quarto_log_level("ERROR")) |
| 33 | + }) |
| 34 | + withr::with_envvar(list(QUARTO_LOG_LEVEL = "INFO"), { |
| 35 | + expect_false(quarto_log_level("DEBUG")) |
| 36 | + expect_true(quarto_log_level("INFO")) |
| 37 | + expect_false(quarto_log_level("ERROR")) |
| 38 | + }) |
| 39 | + withr::with_envvar(list(QUARTO_LOG_LEVEL = "ERROR"), { |
| 40 | + expect_false(quarto_log_level("DEBUG")) |
| 41 | + expect_false(quarto_log_level("INFO")) |
| 42 | + expect_true(quarto_log_level("ERROR")) |
| 43 | + }) |
| 44 | +}) |
| 45 | + |
| 46 | +test_that("in_debug_mode respects GHA CI env var", { |
| 47 | + withr::local_envvar(ACTIONS_RUNNER_DEBUG = NA, ACTIONS_STEP_DEBUG = NA) |
| 48 | + expect_false(in_debug_mode()) |
| 49 | + |
| 50 | + withr::with_envvar(list(ACTIONS_RUNNER_DEBUG = "true"), { |
| 51 | + expect_true(in_debug_mode()) |
| 52 | + }) |
| 53 | + |
| 54 | + withr::with_envvar(list(ACTIONS_STEP_DEBUG = "true"), { |
| 55 | + expect_true(in_debug_mode()) |
| 56 | + }) |
| 57 | + |
| 58 | + withr::with_envvar( |
| 59 | + list(ACTIONS_RUNNER_DEBUG = "false", ACTIONS_STEP_DEBUG = "false"), |
| 60 | + { |
| 61 | + expect_false(in_debug_mode()) |
| 62 | + } |
| 63 | + ) |
| 64 | +}) |
| 65 | + |
| 66 | +test_that("in_debug mode respects quarto_log_level", { |
| 67 | + # clean state |
| 68 | + withr::local_envvar(ACTIONS_RUNNER_DEBUG = NA, ACTIONS_STEP_DEBUG = NA) |
| 69 | + withr::local_envvar(QUARTO_LOG_LEVEL = NA) |
| 70 | + expect_false(in_debug_mode()) |
| 71 | + |
| 72 | + withr::with_envvar(list(QUARTO_LOG_LEVEL = "DEBUG"), { |
| 73 | + expect_true(in_debug_mode()) |
| 74 | + }) |
| 75 | + |
| 76 | + withr::with_envvar(list(QUARTO_LOG_LEVEL = "INFO"), { |
| 77 | + expect_false(in_debug_mode()) |
| 78 | + }) |
| 79 | + |
| 80 | + withr::with_envvar(list(QUARTO_LOG_LEVEL = "ERROR"), { |
| 81 | + expect_false(in_debug_mode()) |
| 82 | + }) |
| 83 | +}) |
| 84 | + |
| 85 | +test_that("in_debug_mode respects R_QUARTO_LOG_DEBUG and quarto.log.debug", { |
| 86 | + # 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) |
| 91 | + expect_false(in_debug_mode()) |
| 92 | + |
| 93 | + withr::with_envvar(list(R_QUARTO_LOG_DEBUG = TRUE), { |
| 94 | + expect_true(in_debug_mode()) |
| 95 | + withr::with_options(list(quarto.log.debug = FALSE), { |
| 96 | + expect_false(in_debug_mode()) |
| 97 | + }) |
| 98 | + withr::with_options(list(quarto.log.debug = TRUE), { |
| 99 | + expect_true(in_debug_mode()) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + withr::with_envvar(list(R_QUARTO_LOG_DEBUG = FALSE), { |
| 104 | + expect_false(in_debug_mode()) |
| 105 | + withr::with_options(list(quarto.log.debug = TRUE), { |
| 106 | + expect_true(in_debug_mode()) |
| 107 | + }) |
| 108 | + withr::with_options(list(quarto.log.debug = FALSE), { |
| 109 | + expect_false(in_debug_mode()) |
| 110 | + }) |
| 111 | + }) |
| 112 | +}) |
| 113 | + |
| 114 | +test_that("quarto_log only logs when in debug mode", { |
| 115 | + temp_file <- tempfile(fileext = ".log") |
| 116 | + on.exit(unlink(temp_file)) |
| 117 | + |
| 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) |
| 123 | + |
| 124 | + result <- quarto_log("test message", file = temp_file) |
| 125 | + expect_false(result) |
| 126 | + expect_false(file.exists(temp_file)) |
| 127 | + |
| 128 | + # Test with debug mode on |
| 129 | + withr::with_options(list(quarto.log.debug = TRUE), { |
| 130 | + result <- quarto_log("test message", file = temp_file) |
| 131 | + expect_true(result) |
| 132 | + expect_true(file.exists(temp_file)) |
| 133 | + |
| 134 | + content <- readLines(temp_file) |
| 135 | + expect_true(grepl("DEBUG: test message", content[1])) |
| 136 | + expect_true(grepl( |
| 137 | + "\\[\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\]", |
| 138 | + content[1] |
| 139 | + )) |
| 140 | + }) |
| 141 | +}) |
| 142 | + |
| 143 | +test_that("quarto_log respects log file configuration", { |
| 144 | + temp_file <- tempfile(fileext = ".log") |
| 145 | + on.exit(unlink(temp_file)) |
| 146 | + |
| 147 | + # Enable debug mode |
| 148 | + withr::local_options(quarto.log.debug = TRUE) |
| 149 | + |
| 150 | + # Test with file parameter |
| 151 | + result <- quarto_log("direct file", file = temp_file) |
| 152 | + expect_true(result) |
| 153 | + expect_true(file.exists(temp_file)) |
| 154 | + |
| 155 | + # Test with option |
| 156 | + unlink(temp_file) |
| 157 | + withr::with_options(list(quarto.log.file = temp_file), { |
| 158 | + result <- quarto_log("via option") |
| 159 | + expect_true(result) |
| 160 | + expect_true(file.exists(temp_file)) |
| 161 | + }) |
| 162 | + |
| 163 | + # Test with environment variable (R_QUARTO_LOG_FILE) |
| 164 | + unlink(temp_file) |
| 165 | + withr::with_envvar(list(R_QUARTO_LOG_FILE = temp_file), { |
| 166 | + result <- quarto_log("via env var") |
| 167 | + expect_true(result) |
| 168 | + expect_true(file.exists(temp_file)) |
| 169 | + }) |
| 170 | + |
| 171 | + # Test with no file configured |
| 172 | + result <- quarto_log("no file configured") |
| 173 | + expect_false(result) |
| 174 | +}) |
| 175 | + |
| 176 | +test_that("quarto_log handles custom formatting", { |
| 177 | + temp_file <- tempfile(fileext = ".log") |
| 178 | + on.exit(unlink(temp_file)) |
| 179 | + |
| 180 | + withr::local_options(quarto.log.debug = TRUE) |
| 181 | + |
| 182 | + # Test without timestamp |
| 183 | + quarto_log("no timestamp", file = temp_file, timestamp = FALSE) |
| 184 | + content <- readLines(temp_file) |
| 185 | + expect_equal(content[1], "DEBUG: no timestamp") |
| 186 | + |
| 187 | + # Test without prefix |
| 188 | + quarto_log("no prefix", file = temp_file, prefix = "", append = FALSE) |
| 189 | + content <- readLines(temp_file) |
| 190 | + expect_true(grepl( |
| 191 | + "^\\[\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\] no prefix$", |
| 192 | + content[1] |
| 193 | + )) |
| 194 | + |
| 195 | + # Test custom prefix |
| 196 | + quarto_log( |
| 197 | + "custom prefix", |
| 198 | + file = temp_file, |
| 199 | + prefix = "CUSTOM: ", |
| 200 | + append = FALSE |
| 201 | + ) |
| 202 | + content <- readLines(temp_file) |
| 203 | + expect_true(grepl("CUSTOM: custom prefix", content[1])) |
| 204 | +}) |
0 commit comments