Skip to content

Commit

Permalink
Fix 1058, 1060, 1062 and improve code coverage in use_files.R from …
Browse files Browse the repository at this point in the history
…0 to 100% (#1059)

* tests: add dummy files for use_{external,internal}_XXX_files() testing

- add some dummy files to the inst/utils/ directory

- for later: add tests that download exactly from that directory

Refs: #1058

* tests: add testing for external-funcs standard cases

* tests: add testing for internal-funcs standard cases

- fix typo in testfile_template_html
- use styler::style_file(..., style = grkstyle::grk_style_transformer)

* fix: set default value for argument name before using it

- the order needs a change at some places: first set a default value when name argument is missing
    - only then call check_name_length() to avoid errors of the type:
    "argument "name" is missing, with no default"
- also, to avoid RStudio and other IDEs linting a missing argument, provide a default value "NULL" for argument  'name'

Refs: #1060

* feat: improve error handling and allow codecovr. to be 100%

- instead of throwing an error, return 'FALSE' is passed and cat_dir_necessary() prints an informative message to the user (with the same effect i.e. not creating a directory)
- this behavior is a bit more userfriendly and also allows for a code coverage of 100%; the latter is not possible to implement since tests are run non-interactively

* tests: add corner cases for tests to make covr. 100%
  • Loading branch information
ilyaZar authored Aug 8, 2023
1 parent ebcfe13 commit 6d9bb1c
Show file tree
Hide file tree
Showing 6 changed files with 543 additions and 51 deletions.
158 changes: 107 additions & 51 deletions R/use_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
#' @return The path to the file, invisibly.
use_external_js_file <- function(
url,
name,
name = NULL,
pkg = get_golem_wd(),
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
) {
) {
old <- setwd(fs_path_abs(pkg))
on.exit(setwd(old))

Expand All @@ -36,12 +36,19 @@ use_external_js_file <- function(
name <- file_path_sans_ext(name)
new_file <- sprintf("%s.js", name)

dir_created <- create_if_needed(
dir,
type = "directory"
dir_created <- tryCatch(
create_if_needed(
dir,
type = "directory"
),
error = function(e) {
out <- FALSE
names(out) <- e[[1]]
return(out)
}
)

if (!dir_created) {
if (isFALSE(dir_created)) {
cat_dir_necessary()
return(invisible(FALSE))
}
Expand Down Expand Up @@ -85,12 +92,12 @@ use_external_js_file <- function(
#' @rdname use_files
use_external_css_file <- function(
url,
name,
name = NULL,
pkg = get_golem_wd(),
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
) {
) {
old <- setwd(fs_path_abs(pkg))
on.exit(setwd(old))

Expand All @@ -103,12 +110,19 @@ use_external_css_file <- function(
name <- file_path_sans_ext(name)
new_file <- sprintf("%s.css", name)

dir_created <- create_if_needed(
dir,
type = "directory"
dir_created <- tryCatch(
create_if_needed(
dir,
type = "directory"
),
error = function(e) {
out <- FALSE
names(out) <- e[[1]]
return(out)
}
)

if (!dir_created) {
if (isFALSE(dir_created)) {
cat_dir_necessary()
return(invisible(FALSE))
}
Expand Down Expand Up @@ -157,7 +171,7 @@ use_external_html_template <- function(
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
) {
) {
old <- setwd(fs_path_abs(pkg))
on.exit(setwd(old))

Expand All @@ -168,12 +182,19 @@ use_external_html_template <- function(

check_name_length(name)

dir_created <- create_if_needed(
dir,
type = "directory"
dir_created <- tryCatch(
create_if_needed(
dir,
type = "directory"
),
error = function(e) {
out <- FALSE
names(out) <- e[[1]]
return(out)
}
)

if (!dir_created) {
if (isFALSE(dir_created)) {
cat_dir_necessary()
return(invisible(FALSE))
}
Expand Down Expand Up @@ -211,28 +232,34 @@ use_external_html_template <- function(
#' @rdname use_files
use_external_file <- function(
url,
name,
name = NULL,
pkg = get_golem_wd(),
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
) {
check_name_length(name)

) {
if (missing(name)) {
name <- basename(url)
}

check_name_length(name)

old <- setwd(fs_path_abs(pkg))
on.exit(setwd(old))

dir_created <- create_if_needed(
dir,
type = "directory"
dir_created <- tryCatch(
create_if_needed(
dir,
type = "directory"
),
error = function(e) {
out <- FALSE
names(out) <- e[[1]]
return(out)
}
)

if (!dir_created) {
if (isFALSE(dir_created)) {
cat_dir_necessary()
return(invisible(FALSE))
}
Expand Down Expand Up @@ -260,29 +287,37 @@ use_external_file <- function(
#' @rdname use_files
use_internal_js_file <- function(
path,
name,
name = NULL,
pkg = get_golem_wd(),
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
) {
check_name_length(name)
) {
old <- setwd(fs_path_abs(pkg))
on.exit(setwd(old))

if (missing(name)) {
name <- basename(path)
}

check_name_length(name)

name <- file_path_sans_ext(name)
new_file <- sprintf("%s.js", name)

dir_created <- create_if_needed(
dir,
type = "directory"
dir_created <- tryCatch(
create_if_needed(
dir,
type = "directory"
),
error = function(e) {
out <- FALSE
names(out) <- e[[1]]
return(out)
}
)

if (!dir_created) {
if (isFALSE(dir_created)) {
cat_dir_necessary()
return(invisible(FALSE))
}
Expand Down Expand Up @@ -325,30 +360,37 @@ use_internal_js_file <- function(
#' @rdname use_files
use_internal_css_file <- function(
path,
name,
name = NULL,
pkg = get_golem_wd(),
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
) {
check_name_length(name)

) {
old <- setwd(fs_path_abs(pkg))
on.exit(setwd(old))

if (missing(name)) {
name <- basename(path)
}

check_name_length(name)

name <- file_path_sans_ext(name)
new_file <- sprintf("%s.css", name)

dir_created <- create_if_needed(
dir,
type = "directory"
dir_created <- tryCatch(
create_if_needed(
dir,
type = "directory"
),
error = function(e) {
out <- FALSE
names(out) <- e[[1]]
return(out)
}
)

if (!dir_created) {
if (isFALSE(dir_created)) {
cat_dir_necessary()
return(invisible(FALSE))
}
Expand Down Expand Up @@ -396,7 +438,7 @@ use_internal_html_template <- function(
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
) {
) {
old <- setwd(fs_path_abs(pkg))
on.exit(setwd(old))

Expand All @@ -407,12 +449,19 @@ use_internal_html_template <- function(
file_path_sans_ext(name)
)

dir_created <- create_if_needed(
dir,
type = "directory"
dir_created <- tryCatch(
create_if_needed(
dir,
type = "directory"
),
error = function(e) {
out <- FALSE
names(out) <- e[[1]]
return(out)
}
)

if (!dir_created) {
if (isFALSE(dir_created)) {
cat_dir_necessary()
return(invisible(FALSE))
}
Expand Down Expand Up @@ -449,12 +498,12 @@ use_internal_html_template <- function(
#' @rdname use_files
use_internal_file <- function(
path,
name,
name = NULL,
pkg = get_golem_wd(),
dir = "inst/app/www",
open = FALSE,
dir_create = TRUE
) {
) {
if (missing(name)) {
name <- basename(path)
}
Expand All @@ -464,12 +513,19 @@ use_internal_file <- function(
old <- setwd(fs_path_abs(pkg))
on.exit(setwd(old))

dir_created <- create_if_needed(
dir,
type = "directory"
dir_created <- tryCatch(
create_if_needed(
dir,
type = "directory"
),
error = function(e) {
out <- FALSE
names(out) <- e[[1]]
return(out)
}
)

if (!dir_created) {
if (isFALSE(dir_created)) {
cat_dir_necessary()
return(invisible(FALSE))
}
Expand Down
13 changes: 13 additions & 0 deletions inst/utils/testfile_template_css.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
background-color: lightblue;
}

h1 {
color: white;
text-align: center;
}

p {
font-family: verdana;
font-size: 20px;
}
10 changes: 10 additions & 0 deletions inst/utils/testfile_template_html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>
2 changes: 2 additions & 0 deletions inst/utils/testfile_template_js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const myHeading = document.querySelector("h1");
myHeading.textContent = "Hello world!";
1 change: 1 addition & 0 deletions inst/utils/testfile_template_plainfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Some text.
Loading

0 comments on commit 6d9bb1c

Please sign in to comment.