Skip to content

Commit

Permalink
Merge branch '140-feature-request-create-unit-test-for-rmd-files' int…
Browse files Browse the repository at this point in the history
…o dev
  • Loading branch information
nicholas-masel authored Jul 3, 2023
2 parents 9ae9814 + ce6a9d0 commit df6eb4d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
7 changes: 7 additions & 0 deletions R/get.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ get_masked_functions <- function(){
#'
get_used_functions <- function(file){

if (grepl("*.Rmd$", file, ignore.case = TRUE)){
tmpfile <- tempfile(fileext = ".R")
on.exit(unlink(tmpfile))
knitr::purl(file, tmpfile)
file <- tmpfile
}

# catch error
retfun <- safely(parse,
quiet = FALSE,
Expand Down
15 changes: 14 additions & 1 deletion R/interact.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ set_log_name_path <- function(log_name = NA, log_path = NA) {
#' @noRd
run_safely <- function(file) "dummy"

#' Is this a R Markdown file#'
#' @param file String. Path to file to execute
#' @noRd
is_rmarkdown <- function(file) {
grepl("*.Rmd$", file, ignore.case = TRUE)
}

#' Dummy function for running a file
#' @noRd
run_file <- function(file){
Expand All @@ -118,7 +125,13 @@ run_file <- function(file){
} else{
exec_env <- getOption("log.rx.exec.env")
}
source(file, local = exec_env)

if (is_rmarkdown(file)) {
rmarkdown::render(file, envir = exec_env)
} else (
source(file, local = exec_env)
)

}

#' Safely run an R script and record results, outputs, messages, errors, warnings
Expand Down
2 changes: 1 addition & 1 deletion R/log.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ log_write <- function(file = NA,
}
if ("result" %in% to_report){
cleaned_log_vec <- c(cleaned_log_vec,
write_result())
write_result(file))
}

cleaned_log_vec <- c(cleaned_log_vec,
Expand Down
9 changes: 7 additions & 2 deletions R/writer.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,19 @@ write_output <- function() {

#' Format result attribute for writing
#'
#' @param file String. Path to file to execute
#' @return A formatted vector of results
#'
#' @noRd
#'
write_result <- function() {
write_result <- function(file) {
result <- get_log_element("result")

c("\nResult:", paste0("\t", capture.output(result$value)))
if (is_rmarkdown(file)) {
c("\nResult:", paste0("\t", capture.output(result)))
} else {
c("\nResult:", paste0("\t", capture.output(result$value)))
}
}

#' Format lint results for writing
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-writer.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ test_that("write_result will return a formatted log result element", {

run_safely_loudly(fp)

expect_identical(write_result(),
expect_identical(write_result(fp),
c("\nResult:", paste0("\t", capture.output(data.frame(test = c(8, 6, 7, 5, 3, 0, 9))))))

log_remove()
Expand Down

0 comments on commit df6eb4d

Please sign in to comment.