Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cansavvy committed Feb 25, 2025
1 parent a618b59 commit 66f7cd1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 440 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ run_test.R
/doc/
/Meta/
.secrets
inst/extdata/tmp/default*
inst/extdata/tmp/default*
inst/extdata/tmp/parent_doc.html
43 changes: 21 additions & 22 deletions R/borrow_chapter.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
#' 'https://raw.githubusercontent.com/'
#' @param dest_dir A file path where the file should be stored upon arrival to
#' the current repository.
#' @param overwrite Only pertinent for local files -- we may want to make sure not
#' to overwrite stuff.
#'
#' @return An Rmarkdown or markdown is knitted into the document from another repository
#'
Expand Down Expand Up @@ -61,7 +59,8 @@
#' tag_replacement_list <- list(
#' "{A_TAG}" = "replacement here",
#' "{TEMPLATE_URL}" = "https://www.ottrproject.org/",
#' "{SECOND_TAG}" = "some other replacement here")
#' "{SECOND_TAG}" = "some other replacement here"
#' )
#'
#' # For replacing tags
#' # ```{r, echo=FALSE, results='asis'}
Expand All @@ -81,7 +80,6 @@ borrow_chapter <- function(doc_path,
base_url = "https://raw.githubusercontent.com",
dest_dir = file.path("resources", "other_chapters"),
overwrite = FALSE) {

# Declare file names
# normalize relative to Rmd file calling this
doc_path <- file.path(doc_path)
Expand All @@ -95,7 +93,6 @@ borrow_chapter <- function(doc_path,
dest_file <- file.path(dest_dir, doc_name)

if (!is.null(repo_name)) {

# Is this a wiki page?
is_wiki <- grepl("^wiki\\/", repo_name)

Expand Down Expand Up @@ -124,18 +121,17 @@ borrow_chapter <- function(doc_path,

# Let us know if the url didn't work
if (grepl("Error", response)) {
stop("URL failed: ", full_url,
"\n Double check doc_path and repo_name (and branch if set)")
stop(
"URL failed: ", full_url,
"\n Double check doc_path and repo_name (and branch if set)"
)
}
} else {
if(dest_file == doc_path & !overwrite) {
stop("The destination file and the original file are the same and overwrite
is set to FALSE")
}
file.copy(from = doc_path, to = dest_file)
}

# Remove leanbuild::set_knitr_image_path() from downloaded file
file_contents <- readLines(dest_file)
file_contents <- readLines(doc_path)
file_contents <- gsub("leanbuild::set_knitr_image_path\\(\\)", "", file_contents)

# If remove_header = TRUE
Expand All @@ -146,16 +142,19 @@ borrow_chapter <- function(doc_path,
### Do the replacment tags

if (!is.null(tag_replacement)) {
tag_df <-
data.frame(tag = names(tag_replacement),
replacement = unlist(tag_replacement))
tag_df <-
data.frame(
tag = names(tag_replacement),
replacement = unlist(tag_replacement)
)

for (tag in 1:nrow(tag_df)) {
file_contents <- stringi::stri_replace_all_fixed(
str = file_contents,
replacement = tag_df$replacement[tag],
pattern = tag_df$tag[tag])
}
for (tag in 1:nrow(tag_df)) {
file_contents <- stringi::stri_replace_all_fixed(
str = file_contents,
replacement = tag_df$replacement[tag],
pattern = tag_df$tag[tag]
)
}
}
writeLines(file_contents, dest_file)

Expand All @@ -174,4 +173,4 @@ borrow_chapter <- function(doc_path,
quiet = TRUE
)
cat(result, sep = "\n")
}
}
417 changes: 0 additions & 417 deletions inst/extdata/parent_doc.html

This file was deleted.

12 changes: 12 additions & 0 deletions inst/extdata/resources/other_chapters/_child_doc.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# This is an H1 header

This is a bunch of cool text here that we want to reuse sometimes.

Maybe sometimes we have replacement here that we would like to be replaced. In fact, sometimes we might even have a some other replacement here here that needs replacing.

Sometimes these tags might end up in a url: [replacement here](https://www.ottrproject.org/).

## [replacement here(https://www.ottrproject.org/)

Sometimes we might even have a url in a header like above.
4 changes: 4 additions & 0 deletions tests/testthat/test-borrow_chapter.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ test_that("Borrow chapter", {

rmarkdown::render(parent_doc)

testthat::expect_true(
file.exists(
file.path(dirname(parent_doc), "parent_doc.html"))
)
})

0 comments on commit 66f7cd1

Please sign in to comment.