Skip to content

Commit

Permalink
test: move coverage of R/config.R to 100%
Browse files Browse the repository at this point in the history
- notably: add package mockery to Suggests (no dependencies except for testthat and used for tests only)

- add tests for non-interactive usage of guess_where_config(): check that error is thrown correctly

- add tests for interactive usage of guess_where_config():
    - user says yes: test that necessary files are created
    - user says no: test that we return NULL

- add tests for encapsulated ask_golem_creation_upon_config()
    - shallow test as non-interactive menu() calls (inside the yesno()) are forbidden
    - but this makes code coverage 100% :)
  • Loading branch information
ilyaZar committed Aug 3, 2023
1 parent f3e6cb2 commit caf682a
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 16 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Suggests:
testthat,
tools,
withr,
mockery,
attachment (>= 0.2.5),
renv,
usethis (>= 1.6.0),
Expand Down
142 changes: 126 additions & 16 deletions tests/testthat/test-config.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ test_that("config works", {
})
})
test_that("golem-config.yml can be renamed and moved to another location", {

path_dummy_golem <- tempfile(pattern = "dummygolem")
create_golem(
path = path_dummy_golem,
Expand Down Expand Up @@ -128,12 +127,14 @@ test_that("golem-config.yml can be renamed and moved to another location", {
## IV.B app_config.R has a multi-line statement
## - > e.g. because of grkstyle formatting or long path names
tmp_app_config_test_02 <- tmp_app_config_default
tmp_max_lines_config <- length(tmp_app_config_test_02)
tmp_max_lines_config <- length(tmp_app_config_test_02)
tmp_app_config_test_02[36] <- " file = app_sys("
tmp_app_config_test_02 <- c(tmp_app_config_test_02[1:36],
"\"config/golem.yml\"",
")",
tmp_app_config_test_02[37:tmp_max_lines_config])
tmp_app_config_test_02 <- c(
tmp_app_config_test_02[1:36],
"\"config/golem.yml\"",
")",
tmp_app_config_test_02[37:tmp_max_lines_config]
)
writeLines(tmp_app_config_test_02, "R/app_config.R")
# IV.B The updated config with multi-line app_sys()-call is read correctly
expect_equal(
Expand Down Expand Up @@ -171,7 +172,6 @@ test_that("golem-config.yml can be renamed and moved to another location", {
})

test_that("golem-config.yml can be retrieved for some exotic corner cases", {

path_dummy_golem <- tempfile(pattern = "dummygolem")
create_golem(
path = path_dummy_golem,
Expand All @@ -181,15 +181,6 @@ test_that("golem-config.yml can be retrieved for some exotic corner cases", {
old_wd <- setwd(path_dummy_golem)
on.exit(setwd(old_wd))

# 0. The default config path is returned
expect_equal(
guess_where_config(),
fs_path_abs(file.path(
path_dummy_golem,
"inst/golem-config.yml"
))
)

# Test exotic case IV.A - for some reason wd is set to subdir "inst/"
# Change dir to subdir "inst"
setwd(file.path(getwd(), "inst"))
Expand All @@ -216,3 +207,122 @@ test_that("golem-config.yml can be retrieved for some exotic corner cases", {
# Cleanup
unlink(path_dummy_golem, TRUE, TRUE)
})

test_that("get_current_config() fails non-interactively with proper error", {
path_dummy_golem <- tempfile(pattern = "dummygolem")
create_golem(
path = path_dummy_golem,
open = FALSE
)

old_wd <- setwd(path_dummy_golem)
on.exit(setwd(old_wd))

pth_golem_ymlconf <- file.path(path_dummy_golem, "inst/golem-config.yml")

# Force the if-clause evaluation of get_current_config() for a NULL path
file.remove(pth_golem_ymlconf)
# Test that error is returned in NON-INTERACTIVE mode
expect_error(
rlang::with_interactive(
{
get_current_config("some/nonesense/path/to/test")
},
value = FALSE
),
"The golem-config.yml file doesn't exist"
)
# Cleanup
unlink(path_dummy_golem, TRUE, TRUE)
})

test_that("get_current_config() interactively recreates files upon user wish", {
path_dummy_golem <- tempfile(pattern = "dummygolem")
create_golem(
path = path_dummy_golem,
open = FALSE
)

old_wd <- setwd(path_dummy_golem)
on.exit(setwd(old_wd))

pth_golem_ymlconf <- file.path(path_dummy_golem, "inst/golem-config.yml")
pth_golem_appconf <- file.path(path_dummy_golem, "R/app_config.R")

# Test that golem-specific files are copied in INTERACTIVE mode
# I. remove files before they are copied
# 1. golem-config-yaml; remove and check if missing
expect_true(file.exists(pth_golem_ymlconf))
file.remove(pth_golem_ymlconf)
expect_false(file.exists(pth_golem_ymlconf))
# 2. R/app_config.R; remove and check if missing
expect_true(file.exists(pth_golem_appconf))
file.remove(pth_golem_appconf)
expect_false(file.exists(pth_golem_appconf))
# II. check that copying works: interactive is bypassed as-if user says "yes"
mockery::stub(
where = get_current_config,
what = "ask_golem_creation_upon_config",
how = TRUE
)
expect_equal(
rlang::with_interactive({
get_current_config(path_dummy_golem)
},
value = TRUE
),
fs_path_abs(pth_golem_ymlconf)
)
expect_true(file.exists(pth_golem_ymlconf))
expect_true(file.exists(pth_golem_appconf))

# Cleanup
unlink(path_dummy_golem, TRUE, TRUE)
})

test_that("get_current_config() interactively returns NULL upon user wish", {
path_dummy_golem <- tempfile(pattern = "dummygolem")
create_golem(
path = path_dummy_golem,
open = FALSE
)

old_wd <- setwd(path_dummy_golem)
on.exit(setwd(old_wd))

pth_golem_ymlconf <- file.path(path_dummy_golem, "inst/golem-config.yml")
pth_golem_appconf <- file.path(path_dummy_golem, "R/app_config.R")

# Test that golem-specific files are copied in INTERACTIVE mode
# I. remove files before they are copied
# 1. golem-config-yaml; remove and check if missing
expect_true(file.exists(pth_golem_ymlconf))
file.remove(pth_golem_ymlconf)
expect_false(file.exists(pth_golem_ymlconf))
# 2. R/app_config.R; remove and check if missing
expect_true(file.exists(pth_golem_appconf))
file.remove(pth_golem_appconf)
expect_false(file.exists(pth_golem_appconf))
# II. check that copying works: interactive is bypassed as-if user says "no"
mockery::stub(
where = get_current_config,
what = "ask_golem_creation_upon_config",
how = FALSE
)
expect_null(
rlang::with_interactive({
get_current_config(path_dummy_golem)
},
value = TRUE
)
)
expect_false(file.exists(pth_golem_ymlconf))
expect_false(file.exists(pth_golem_appconf))

# Cleanup
unlink(path_dummy_golem, TRUE, TRUE)
})

test_that("ask_golem_creation_upon_config() fails in non-interactive mode", {
expect_error(ask_golem_creation_upon_config("test/path"))
})

0 comments on commit caf682a

Please sign in to comment.