Skip to content

Commit

Permalink
Refactor to reuse start_server
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhesh195 committed Sep 15, 2024
1 parent c7dca84 commit b12a42f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions R/utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 3 additions & 17 deletions tests/testthat/helper-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand All @@ -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())
}

# --------------------------
Expand Down

0 comments on commit b12a42f

Please sign in to comment.