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

Don't assume all scripts are given as character variables. #321

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

* `copyDependencyToDir()` no longer creates empty directories for dependencies that do not have any files. (@gadenbuie, #276)

* Closed #320: `copyDependencyToDir()` now works with dependencies
with specified attributes. (@dmurdoch, #320)
dmurdoch marked this conversation as resolved.
Show resolved Hide resolved

# htmltools 0.5.2

## Breaking Changes
Expand Down
18 changes: 14 additions & 4 deletions R/html_dependency.R
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,18 @@ copyDependencyToDir <- function(dependency, outputDir, mustWork = TRUE) {
dir.create(target_dir)
dependency$src$file <- normalizePath(target_dir, "/", TRUE)

files <- if (dependency$all_files) list.files(dir) else {
unlist(dependency[c('script', 'stylesheet', 'attachment')])
if (dependency$all_files)
files <- list.files(dir)
else {
file_list <- c(dependency[['script']],
dependency[['stylesheet']],
dependency[['attachment']])
if (anyUnnamed(file_list))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be clearer what's going on here if we were to have a helper function "pluck" the files from each slot first, then put them together. Something like

pluck_src <- function(x) {
  if (is.character(x)) return(x)
  if (is.list(x) && "src" %in% names(x)) return(x$src) 
  if (is.list(x)) return(pluck_src(x))
  NULL
}

files <- c(
  pluck_src(dep$script),
  pluck_src(dep$stylesheet),
  pluck_src(dep$attachment),
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function makes sense. I'll add that.

files <- vapply(file_list,
function(f) if (is.list(f)) f[['src']] else f,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this correctly handle the case of htmlDependency("foo", "1.0", src="", script=list(list(src="file.js")))? I think the tests should verify that the files were actually copied in order to verify this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's worth testing. I'll add that.

"")
else
files <- file_list[['src']]
}

if (length(files) == 0) {
Expand All @@ -366,11 +376,11 @@ copyDependencyToDir <- function(dependency, outputDir, mustWork = TRUE) {
}

srcfiles <- file.path(dir, files)
if (any(!file.exists(srcfiles))) {
if (any(bad <- !file.exists(srcfiles))) {
stop(
sprintf(
"Can't copy dependency files that don't exist: '%s'",
wch marked this conversation as resolved.
Show resolved Hide resolved
paste(srcfiles, collapse = "', '")
paste(srcfiles[bad], collapse = "', '")
wch marked this conversation as resolved.
Show resolved Hide resolved
)
)
}
Expand Down
47 changes: 47 additions & 0 deletions tests/testthat/test-deps.r
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,50 @@ test_that("copyDependencyToDir() doesn't create an empty directory", {
# to keep relativeTo() from throwing an error later in Rmd render process
expect_match(copied_dep$src$file, normalizePath(tmp_rmd, "/", TRUE), fixed = TRUE)
})

test_that("copyDependencyToDir() handles attributes", {
tmp_dep <- tempfile("dep")
dir.create(tmp_dep)
on.exit(unlink(tmp_dep))

tmp_txt <- basename(tempfile("text", fileext = ".txt"))
tmp_path <- file.path(tmp_dep, tmp_txt)
writeLines("Some text in the text/plain dep", tmp_path)
on.exit(unlink(tmp_path), add = TRUE)

tmp_rmd <- tempfile("rmd_files")
dir.create(tmp_rmd)
on.exit(unlink(tmp_rmd), add = TRUE)

dep_with_attributes <- htmltools::htmlDependency(
name = "textPlain",
version = "9.9.9",
src = tmp_dep,
script = list(src = tmp_txt, type = "text/plain"),
all_files = FALSE
)

dep_without <- htmltools::htmlDependency(
name = "textPlain",
version = "9.9.9",
src = tmp_dep,
script = tmp_txt,
all_files = FALSE
)

dep_with_both <- htmltools::htmlDependency(
name = "textPlain",
version = "9.9.9",
src = tmp_dep,
script = list(tmp_txt,
list(src = tmp_txt, type = "text/plain")),
all_files = FALSE
)
# None of these should trigger errors as the first two did in
# issue #320

copyDependencyToDir(dep_with_attributes, tmp_rmd)
copyDependencyToDir(dep_with_both, tmp_rmd)
copyDependencyToDir(dep_without, tmp_rmd)
succeed()
})