Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maksymiuks committed Sep 27, 2024
2 parents ddae74b + d409357 commit 8d4149e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

* Expose `...` allowing customization of check subprocesses when creating checks df.

* Force garbage collection before scheduling task, to make sure any already
finished processes are removed from the memory.

# checked 0.2.3

* Use custom `checked` `finisher`'s instead of the `processx` `finalizer`'s
Expand Down
51 changes: 31 additions & 20 deletions R/check_design.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,7 @@ check_design <- R6::R6Class( # nolint cyclocomp_linter
!any(uulist(drlapply(df$custom, `[[`, "alias")) %in% df$alias)
)

if (dir.exists(output)) {
if (is.na(restore)) {
restore <- if (interactive()) {
switch(
menu(
c("Yes", "No"),
title = "Do you want to restore previous results?"
),
"1" = TRUE,
"2" = FALSE
)
} else {
FALSE
}

if (!restore) {
unlink(output, recursive = TRUE, force = TRUE)
}
}
}
check_past_output(output, restore, ask = interactive())

dir_create(output)

Expand Down Expand Up @@ -182,6 +163,9 @@ check_design <- R6::R6Class( # nolint cyclocomp_linter
if (self$is_done()) {
return(-1L)
}

# force garbage collection to free memory from terminated processes
gc(verbose = FALSE, reset = FALSE, full = TRUE)

# if all available processes are in use, terminate early
n_active <- length(private$active)
Expand Down Expand Up @@ -272,3 +256,30 @@ print.check_design <- function(x, ...) {
}
invisible(x)
}

check_past_output <- function(output, restore, ask = interactive()) {
if (dir.exists(output)) {
if (is.na(restore)) {
restore <- if (ask) {
switch(
restore_menu(),
"1" = TRUE,
"2" = FALSE
)
} else {
FALSE
}
}

if (!restore) {
unlink(output, recursive = TRUE, force = TRUE)
}
}
}

restore_menu <- function() {
menu(
c("Yes", "No"),
title = "Do you want to restore previous results?"
)
}
27 changes: 27 additions & 0 deletions tests/testthat/test-check.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,30 @@ test_that("check_pkgs works as expected", {
reporter = checked:::reporter_ansi_tty()
))
})

test_that("check design restore dialog test", {

dir_create(output <- tempfile())
expect_true(dir.exists(output))
with_mocked_bindings({
check_past_output(output, options::opt("restore"), ask = TRUE)
}, restore_menu = function(...) "1") # Yes
expect_true(dir.exists(output))
with_mocked_bindings({
check_past_output(output, options::opt("restore"), ask = TRUE)
}, restore_menu = function(...) "2") # No
expect_true(!dir.exists(output))

dir_create(output <- tempfile())
expect_true(dir.exists(output))
check_past_output(output, options::opt("restore"), ask = FALSE)
expect_true(!dir.exists(output))

dir_create(output <- tempfile())
expect_true(dir.exists(output))
check_past_output(output, TRUE, ask = TRUE)
expect_true(dir.exists(output))
check_past_output(output, FALSE, ask = TRUE)
expect_true(!dir.exists(output))

})
10 changes: 6 additions & 4 deletions tests/testthat/test-checks_df.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
examples_path <- system.file("example_packages", package = "checked")
names <- c("DALEXtra_new", "rd2markdown_new", "exampleGood_new", "exampleOkay_new", "exampleBad_new")
path <- path_broken <- c(
path <- c(
test_path("testing_pkgs", "DALEXtra"),
test_path("testing_pkgs", "rd2markdown"),
file.path(examples_path, "exampleGood"),
file.path(examples_path, "exampleOkay"),
file.path(examples_path, "exampleBad")
)
names(path_broken) <- names

test_that("rev_dep_check_tasks_df works with deafult params", {
expect_silent(
Expand Down Expand Up @@ -155,12 +153,16 @@ test_that("source_check_tasks_df works as expected", {
})

test_that("source_check_tasks_df aliases are properly handled", {
broken_names <- c("DALEXtra_new", "rd2markdown_new", "exampleGood_new", "exampleOkay_new", "exampleBad_new")
path_broken <- path
names(path_broken) <- broken_names

expect_silent(
df <- source_check_tasks_df(path_broken)
)

expect_true(all(endsWith(df$alias, "_new")))
expect_equal(df$alias, names)
expect_equal(df$alias, broken_names)

expect_silent(
df <- source_check_tasks_df(c(
Expand Down

0 comments on commit 8d4149e

Please sign in to comment.