Skip to content
Open
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
5 changes: 4 additions & 1 deletion R/bibliography.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ create_biblio_file <- function(packages, out.dir,
x = bib_lines[grep("PACKAGES_HERE", bib_lines)])

# path to bib
# Use forward slashes: on Windows, tempdir() returns "C:\Users\...",
# and a substring like "\U..." is parsed as an invalid Unicode escape
# when the chunk is re-evaluated in the Rmd.
bib_lines[grep("BIBPATH_HERE", bib_lines)] <- gsub(
pattern = "BIBPATH_HERE",
replacement = paste0('"', dir_temp, '"'),
replacement = paste0('"', gsub("\\\\", "/", dir_temp), '"'),
x = bib_lines[grep("BIBPATH_HERE", bib_lines)])

# Link citation
Expand Down
40 changes: 40 additions & 0 deletions tests/testthat/test-bibliography.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
test_that("create_biblio_file substitutes BIBPATH with forward slashes", {
# On Windows, tempdir() returns a path with backslashes (e.g. "C:\Users\...").
# If that path is interpolated as-is into the rendered Rmd, R parses
# substrings like "\U..." as Unicode escapes and the chunk fails with
# `Error in file(): cannot open the connection`. The substitution must
# use forward slashes so the path is a valid R string literal on every OS.

fake_dir <- "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\RtmpXXXX"
bib_template <- c(
"knitr::write_bib(packages, file.path(BIBPATH_HERE, 'packages.bib'))"
)
substituted <- gsub(
pattern = "BIBPATH_HERE",
replacement = paste0('"', gsub("\\\\", "/", fake_dir), '"'),
x = bib_template
)

expect_false(grepl("\\\\", substituted))
expect_true(grepl("C:/Users/RUNNER~1/AppData/Local/Temp/RtmpXXXX", substituted, fixed = TRUE))

expect_silent(
parse(text = substituted)
)
})

test_that("create_biblio_file produces an html bibliography", {
out <- tempfile()
dir.create(out)

suppressMessages(
create_biblio_file(
packages = c("rmarkdown", "knitr"),
out.dir = out,
to = "html",
edit = FALSE
)
)

expect_true(file.exists(file.path(out, "bibliography.html")))
})
Loading