From b12a42ff3189529c0c9e6622ba9be1af08d3e684 Mon Sep 17 00:00:00 2001 From: Siddhesh Deodhar Date: Sat, 14 Sep 2024 22:22:33 -0400 Subject: [PATCH] Refactor to reuse start_server --- NAMESPACE | 2 ++ R/utilities.r | 26 ++++++++++++++++++++++++++ tests/testthat/helper-functions.R | 20 +++----------------- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index c6dc730fa..39c5cc327 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -446,6 +446,7 @@ export(scale_y_log10) export(scale_y_reverse) export(scale_y_sqrt) export(should_stop) +export(start_servr) export(stat_bin) export(stat_bin2d) export(stat_bin_2d) @@ -471,6 +472,7 @@ export(stat_summary_bin) export(stat_summary_hex) export(stat_unique) export(stat_ydensity) +export(stop_server) export(theme) export(theme_animint) export(theme_bw) diff --git a/R/utilities.r b/R/utilities.r index 8e100e78f..acc4905fd 100644 --- a/R/utilities.r +++ b/R/utilities.r @@ -280,3 +280,29 @@ dispatch_args <- function(f, ...) { formals(f) <- formals f } + +start_servr <- function(serverDirectory = ".", port = 4848, + code = "servr::httd(dir='%s', port=%d)", + tmpPath = ".") { + dir <- normalizePath(serverDirectory, winslash = "/", mustWork = TRUE) + cmd <- sprintf( + paste("write.table(Sys.getpid(), file='%s', append=T, row.name=F, col.names=F);", code), + file.path(tmpPath, "pids.txt"), dir, port + ) + system2("Rscript", c("-e", shQuote(cmd)), wait = FALSE) +} + +stop_server <- function(tmpPath = ".") { + res <- TRUE + f <- file.path(tmpPath, "pids.txt") + if (file.exists(f)) { + e <- try(readLines(con <- file(f), warn = FALSE), silent = TRUE) + if (!inherits(e, "try-error")) { + pids <- as.integer(e) + res <- c(res, tools::pskill(pids)) + } + close(con) + unlink(f) + } + res +} diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 9e26dfc08..604d5da17 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -351,18 +351,9 @@ tests_run <- function(dir = ".", filter = NULL) { #' @seealso \link{tests_run} #' @export tests_exit <- function() { - res <- stop_binary() + stop_binary() Sys.unsetenv("ANIMINT_BROWSER") - f <- file.path(find_test_path(), "pids.txt") - if (file.exists(f)) { - e <- try(readLines(con <- file(f), warn = FALSE), silent = TRUE) - if (!inherits(e, "try-error")) { - pids <- as.integer(e) - res <- c(res, tools::pskill(pids)) - } - close(con) - unlink(f) - } + res <- stop_server(tmpPath = find_test_path()) invisible(all(res)) } @@ -376,12 +367,7 @@ tests_exit <- function() { #' @return port number of the successful attempt run_servr <- function(directory = ".", port = 4848, code = "servr::httd(dir='%s', port=%d)") { - dir <- normalizePath(directory, winslash = "/", mustWork = TRUE) - cmd <- sprintf( - paste("write.table(Sys.getpid(), file='%s', append=T, row.name=F, col.names=F);", code), - file.path(find_test_path(), "pids.txt"), dir, port - ) - system2("Rscript", c("-e", shQuote(cmd)), wait = FALSE) + start_servr(directory, port, code, tmpPath = find_test_path()) } # --------------------------