|
| 1 | +# Runs the tests from project directory and writes results JSON to the root of the project |
| 2 | +# as .results.json. |
| 3 | +# |
| 4 | +# Args: |
| 5 | +# project_path: The absolute path to the root of the project being tested. |
| 6 | +# print: If TRUE, prints results; if not, not. DEFAULT is FALSE. |
| 7 | +# |
| 8 | +# Returns: |
| 9 | +# Run results list containing: runStatus (string), backtrace (list), test_results (list) |
| 10 | +run_tests <- function(project_path = getwd(), print = FALSE) { |
| 11 | + #Runs tests for project and returns the results. |
| 12 | + #If sourcing_error occurs, .sourcing_error_run_results returns the results. |
| 13 | + run_results <- tryCatch({.run_tests_project(project_path)}, |
| 14 | + sourcing_error = .sourcing_error_run_result, |
| 15 | + run_error = .run_error_run_result) |
| 16 | + |
| 17 | + json_run_results <- .create_json_run_results(run_results) |
| 18 | + .write_json(json_run_results, file.path(project_path, ".results.json")) |
| 19 | + |
| 20 | + if (print) { |
| 21 | + .print_results_from_json(json_run_results) |
| 22 | + } |
| 23 | + |
| 24 | + invisible(run_results) |
| 25 | +} |
| 26 | + |
| 27 | +.run_tests_project <- function(project_path) { |
| 28 | + test_results <- list() |
| 29 | + #Lists all the files in the path beginning with "test" and ending in ".R" |
| 30 | + test_files <- list.files(path = file.path(project_path, "tests", "testthat"), pattern = "test.*\\.R", |
| 31 | + full.names = TRUE, recursive = FALSE) |
| 32 | + |
| 33 | + for (test_file in test_files) { |
| 34 | + file_results <- .run_tests_file(test_file, project_path) |
| 35 | + test_results <- c(test_results, file_results) |
| 36 | + } |
| 37 | + return(list("run_status" = "success", "backtrace" = list(), "test_results" = test_results)) |
| 38 | +} |
| 39 | + |
| 40 | +.run_tests_file <- function(file_path, project_path) { |
| 41 | + .GlobalEnv$points <- list() |
| 42 | + .GlobalEnv$points_for_all_tests <- list() |
| 43 | + |
| 44 | + test_env = .create_test_env(project_path) |
| 45 | + test_file_output <- tryCatch({test_file(file_path, reporter = "silent", env = test_env)}, |
| 46 | + error = .signal_run_error) |
| 47 | + |
| 48 | + test_file_results <- .create_file_results(test_file_output, points, .GlobalEnv$points_for_all_tests) |
| 49 | + |
| 50 | + return(test_file_results) |
| 51 | +} |
| 52 | + |
| 53 | +.signal_sourcing_error <- function(error) { |
| 54 | + sourcing_error <- simpleError(message = error$message, call = error$call) |
| 55 | + class(sourcing_error) <- c("sourcing_error", class(sourcing_error)) |
| 56 | + signalCondition(sourcing_error) |
| 57 | +} |
| 58 | + |
| 59 | +.sourcing_error_run_result <- function(sourcing_error) { |
| 60 | + split_message <- strsplit(sourcing_error$message, split = "\n") |
| 61 | + backtrace <- lapply(split_message[[1]], unbox) |
| 62 | + return(list("run_status" = "sourcing_failed", "backtrace" = backtrace, "test_results" = list())) |
| 63 | +} |
| 64 | + |
| 65 | +.signal_run_error <- function(error) { |
| 66 | + run_error <- simpleError(message = error$message, call = error$call) |
| 67 | + class(run_error) <- c("run_error", class(run_error)) |
| 68 | + signalCondition(run_error) |
| 69 | +} |
| 70 | + |
| 71 | +.run_error_run_result <- function(run_error) { |
| 72 | + split_message <- strsplit(run_error$message, split = "\n") |
| 73 | + backtrace <- lapply(split_message[[1]], unbox) |
| 74 | + return(list("run_status" = "run_failed", "backtrace" = backtrace, "test_results" = list())) |
| 75 | +} |
0 commit comments