Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 1058, 1060, 1062 and improve code coverage in use_files.R from 0 to 100% #1059

Merged
merged 6 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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