Skip to content

Commit

Permalink
Fix 1074 to improve code coverage of R/utils.R to 100% (#1075)
Browse files Browse the repository at this point in the history
* feature: improve is_exisiting_module()

    - make it self-check if is called inside an R package (by checking for existence of an R-directory)
    - improve docs: add information that it checks for the existence of a module /!\ file /!\ name which is not the same as a module
    - improve tests:
        - proper cleanup of a golem
        - check failure/error if not inside a R-package (in the above sense)

* tests: test create_if_need() function non-interactively

- must produce error when called non-interactively for argyment type="file"

* refactor: add clearer logic for create_if_needed() and test interactively 'no'

- allow mocking of user interaction with "no"-response for testing purposes
    - requires encapsulation of yesno() to a separate function
- restyle the file with grkstyler
- add a test that mimicks user interation with "no" response of the yesno()-function

=> improve test coverage to 60.09 %

* tests: add test for user interactive behavior "yes"

whereby improving coverage to 62.62 %

* refactor:

- shallow-test encapsulated function ask_golem_creation_file()
- remove redundant check_file_exist() which is never used inside `{golem}`!
- improve formatting of some comments inside create_if_needed()

=> improve test coverage to 67.79%

* tests: increaset R/utils.R coverage to 88.98%

- snapshot tests save the output of cli-messages and check options
    - if cli:: package changes we see a different output and the snapshots will inform about this
    - additionally check do_if_unquiet() feature of these message functions
  • Loading branch information
ilyaZar authored Aug 8, 2023
1 parent 7fed26f commit cf3eb8b
Show file tree
Hide file tree
Showing 3 changed files with 286 additions and 63 deletions.
103 changes: 48 additions & 55 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ golem_sys <- function(
...,
lib.loc = NULL,
mustWork = FALSE
) {
) {
system.file(
...,
package = "golem",
Expand All @@ -17,7 +17,7 @@ create_if_needed <- function(
path,
type = c("file", "directory"),
content = NULL
) {
) {
type <- match.arg(type)

# Check if file or dir already exist
Expand All @@ -26,28 +26,20 @@ create_if_needed <- function(
} else if (type == "directory") {
dont_exist <- Negate(fs_dir_exists)(path)
}
# If it doesn't exist, ask if we are allowed
# to create it
# If it doesn't exist, ask if we are allowed to create it
if (dont_exist) {
if (interactive()) {
ask <- yesno(
sprintf(
"The %s %s doesn't exist, create?",
basename(path),
type
)
)
if (rlang::is_interactive()) {
ask <- ask_golem_creation_file(path, type)
# Return early if the user doesn't allow
if (!ask) {
return(FALSE)
} else {
# Create the file
if (type == "file") {
fs_file_create(path)
write(content, path, append = TRUE)
} else if (type == "directory") {
fs_dir_create(path, recurse = TRUE)
}
}
# Create the file
if (type == "file") {
fs_file_create(path)
write(content, path, append = TRUE)
} else if (type == "directory") {
fs_dir_create(path, recurse = TRUE)
}
} else {
stop(
Expand All @@ -59,30 +51,25 @@ create_if_needed <- function(
)
}
}

# TRUE means that file exists (either
# created or already there)
# TRUE means that file exists (either created or already there)
return(TRUE)
}

check_file_exist <- function(file) {
res <- TRUE
if (fs_file_exists(file)) {
if (interactive()) {
res <- yesno("This file already exists, override?")
} else {
res <- TRUE
}
}
return(res)
ask_golem_creation_file <- function(path, type) {
yesno(
sprintf(
"The %s %s doesn't exist, create?",
basename(path),
type
)
)
}

# internal
replace_word <- function(
file,
pattern,
replace
) {
) {
suppressWarnings(tx <- readLines(file))
tx2 <- gsub(
pattern = pattern,
Expand Down Expand Up @@ -170,7 +157,7 @@ cat_start_download <- function() {
cat_downloaded <- function(
where,
file = "File"
) {
) {
cat_green_tick(
sprintf(
"%s downloaded at %s",
Expand All @@ -190,7 +177,7 @@ cat_start_copy <- function() {
cat_copied <- function(
where,
file = "File"
) {
) {
cat_green_tick(
sprintf(
"%s copied to %s",
Expand All @@ -203,7 +190,7 @@ cat_copied <- function(
cat_created <- function(
where,
file = "File"
) {
) {
cat_green_tick(
sprintf(
"%s created at %s",
Expand All @@ -224,7 +211,7 @@ cat_automatically_linked <- function() {
open_or_go_to <- function(
where,
open_file
) {
) {
if (
open_file
) {
Expand All @@ -250,7 +237,7 @@ after_creation_message_js <- function(
pkg,
dir,
name
) {
) {
if (
desc_exist(pkg)
) {
Expand All @@ -273,7 +260,7 @@ after_creation_message_css <- function(
pkg,
dir,
name
) {
) {
if (
desc_exist(pkg)
) {
Expand All @@ -296,7 +283,7 @@ after_creation_message_sass <- function(
pkg,
dir,
name
) {
) {
if (
desc_exist(pkg)
) {
Expand All @@ -316,7 +303,7 @@ after_creation_message_html_template <- function(
pkg,
dir,
name
) {
) {
do_if_unquiet({
cli_cat_line("")
cli_cat_line("To use this html file as a template, add the following code in your UI:")
Expand All @@ -337,7 +324,7 @@ file_created_dance <- function(
open_file,
open_or_go_to = TRUE,
catfun = cat_created
) {
) {
catfun(where)

fun(pkg, dir, name)
Expand All @@ -355,7 +342,7 @@ file_created_dance <- function(
file_already_there_dance <- function(
where,
open_file
) {
) {
cat_green_tick("File already exists.")
open_or_go_to(
where = where,
Expand Down Expand Up @@ -394,9 +381,9 @@ yesno <- function(...) {

# Checking that a package is installed
check_is_installed <- function(
pak,
...
) {
pak,
...
) {
if (
!requireNamespace(pak, ..., quietly = TRUE)
) {
Expand All @@ -412,9 +399,9 @@ check_is_installed <- function(
}

required_version <- function(
pak,
version
) {
pak,
version
) {
if (
utils::packageVersion(pak) < version
) {
Expand Down Expand Up @@ -464,14 +451,20 @@ add_sass_code <- function(where, dir, name) {
}
}

#' Check if a module already exists
#' Check if a module (`R`-file) already exists
#'
#' Assumes it is called at the root of a golem project.
#' Should be called at the root of a `{golem}` project; but an error is thrown
#' only if one is not inside an R package (as the checks of `golem:::is_golem()`
#' are rather strict, specifically only the presence of a "R/" directory is
#' checked for the moment).
#'
#' @param module A character string. The name of a potentially existing module
#' @return Boolean. Does the module exist or not ?
#' @param module a character string giving the name of a potentially existing
#' module `R`-file
#' @return boolean; `TRUE` if the module (`R`-file) exists and `FALSE` else
#' @noRd
is_existing_module <- function(module) {
stopifnot(`Cannot be called when not inside a R-package` = dir.exists("R"))
# stopifnot(`Cannot be called when not inside a golem-project` = is.golem())
existing_module_files <- list.files("R/", pattern = "^mod_")
existing_module_names <- sub(
"^mod_([[:alnum:]_]+)\\.R$",
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/_snaps/utils.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# simple cat-messaging functions keep output style when printing

> Rebuild Berlin from scratch.

---



---



---


Initiating file download

---


Copying file

---



---



Loading

0 comments on commit cf3eb8b

Please sign in to comment.