Skip to content

Commit

Permalink
Create dependency output directory recursively when copying (#332)
Browse files Browse the repository at this point in the history
* Add failing test describing #331

* Recursively create `outputDir`

Fixes #331

* Add NEWS item
  • Loading branch information
gadenbuie authored Oct 21, 2022
1 parent 10d245c commit 95f2796
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# htmltools 0.5.3.9000

## Bug fixes

* Closed #331: `copyDependencyToDir()` creates `outputDir` recursively, which happens in Quarto or when `lib_dir` points to a nested directory. (@gadenbuie, #332)


# htmltools 0.5.3
Expand Down
2 changes: 1 addition & 1 deletion R/html_dependency.R
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ copyDependencyToDir <- function(dependency, outputDir, mustWork = TRUE) {
stop('outputDir must be of length 1 and cannot be "" or "/"')

if (!dir_exists(outputDir))
dir.create(outputDir)
dir.create(outputDir, recursive = TRUE)

target_dir <- if (getOption('htmltools.dir.version', TRUE)) {
paste(dependency$name, dependency$version, sep = "-")
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/test-deps.r
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,36 @@ test_that("copyDependencyToDir() doesn't create an empty directory", {
expect_match(copied_dep$src$file, normalizePath(tmp_rmd, "/", TRUE), fixed = TRUE)
})

test_that("copyDependencyToDir() creates recursive output directories", {
tmp_dep <- tempfile("dep")
dir.create(tmp_dep)
on.exit(unlink(tmp_dep, recursive = TRUE))

writeLines(
c("alert('boo')"),
file.path(tmp_dep, "script.js")
)

dep <- htmltools::htmlDependency(
name = "simple",
version = "9.9.9",
src = tmp_dep,
script = "script.js",
all_files = FALSE
)

tmp_outputDir <- file.path(tempfile("outputDir"), "subdir")
on.exit(unlink(tmp_outputDir, recursive = TRUE), add = TRUE)

expect_silent(copyDependencyToDir(dep, tmp_outputDir))

# copyDependencyToDir() creates the nested outputDir
expect_true(dir_exists(file.path(tmp_outputDir)))
# it moves the dependency into this dir
expect_true(dir_exists(file.path(tmp_outputDir, "simple-9.9.9")))
expect_true(file.exists(file.path(tmp_outputDir, "simple-9.9.9", "script.js")))
})

test_that("copyDependencyToDir() handles attributes", {
tmp_dep <- tempfile("dep")
dir.create(tmp_dep)
Expand Down

0 comments on commit 95f2796

Please sign in to comment.