Skip to content

Commit

Permalink
Refactor "freeze" functions (#231)
Browse files Browse the repository at this point in the history
* refactor "freeze" functions

* fix
  • Loading branch information
etiennebacher authored Dec 17, 2023
1 parent f5ab484 commit 69a52f1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
21 changes: 15 additions & 6 deletions R/freeze.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
.read_freeze <- function(input, output, hashes) {

# input = original man page or vignette
# output = rendered man page or vignette (md file)
# hashes = content of altdoc/freeze.rds
.is_frozen <- function(input, output, hashes) {
if (!fs::file_exists(input) || !fs::file_exists(output) || is.null(hashes)) {
return(FALSE)
}

out <- FALSE
if (input %in% names(hashes)) {
old <- hashes[[input]]
new <- digest::digest(.readlines(input))
out <- identical(old, new)
}
return(out)
out
}

.write_freeze <- function(input, path, freeze, worked = TRUE) {
freeze_file <- fs::path_join(c(path, "altdoc/freeze.rds"))
# input = filename
# src_dir = path to package root
# freeze = TRUE/FALSE
# worked = TRUE/FALSE - did the conversion of the file work?
.write_freeze <- function(input, src_dir, freeze, worked) {
freeze_file <- fs::path_join(c(src_dir, "altdoc/freeze.rds"))

if (!fs::file_exists(freeze_file)) {
hashes <- vector("character")
} else {
hashes <- readRDS(freeze_file)
}

# if conversion failed we don't want to store the hash of the input because
# it contains an error
if (isTRUE(worked)) {
hashes[[input]] <- digest::digest(.readlines(input))
} else {
Expand All @@ -31,6 +38,8 @@
saveRDS(hashes, freeze_file)
}

# src_dir = path to package root
# freeze = TRUE/FALSE
.get_hashes <- function(src_dir, freeze) {
hashes <- NULL
if (isTRUE(freeze)) {
Expand Down
4 changes: 2 additions & 2 deletions R/import_man.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@

# Skip file when frozen
if (isTRUE(freeze)) {
flag <- .read_freeze(input = origin_Rd, output = destination_md, hashes = hashes)
flag <- .is_frozen(input = origin_Rd, output = destination_md, hashes = hashes)
if (isTRUE(flag)) {
return("skipped_unchanged")
}
Expand Down Expand Up @@ -168,7 +168,7 @@
# do not try to read/write the RDS file if we run in CI because the updated
# RDS won't be available to us anyway
if (!.on_ci()) {
.write_freeze(input = origin_Rd, path = src_dir, freeze = freeze, worked = worked)
.write_freeze(input = origin_Rd, src_dir = src_dir, freeze = freeze, worked = worked)
}

return(ifelse(worked, "success", "failure"))
Expand Down
4 changes: 2 additions & 2 deletions R/import_vignettes.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@

# Skip file when frozen
if (isTRUE(freeze)) {
flag <- .read_freeze(input = origin, output = gsub("\\.Rmd$|\\.qmd$", ".md", destination), hashes = hashes)
flag <- .is_frozen(input = origin, output = gsub("\\.Rmd$|\\.qmd$", ".md", destination), hashes = hashes)
if (isTRUE(flag)) {
return("skipped_unchanged")
}
Expand All @@ -180,7 +180,7 @@
# do not try to read/write the RDS file if we run in CI because the updated
# RDS won't be available to us anyway
if (!.on_ci()) {
.write_freeze(input = origin, path = src_dir, freeze = freeze, worked = worked)
.write_freeze(input = origin, src_dir = src_dir, freeze = freeze, worked = worked)
}

return(ifelse(worked, "success", "failure"))
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-freeze.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ test_that("comparing hashes works", {
fs::file_copy(test_path("examples/examples-man/between.Rd"), source_file)
hashes <- digest::digest(.readlines(source_file))
names(hashes) <- source_file
expect_true(.read_freeze(source_file, source_file, hashes))
expect_true(.is_frozen(source_file, source_file, hashes))

other_source_file <- test_path("examples/examples-man/between.md")
expect_false(.read_freeze(other_source_file, other_source_file, hashes))
expect_false(.is_frozen(other_source_file, other_source_file, hashes))

# modified file doesn't have the same hash
mod <- c(.readlines(source_file), "abc")
cat(mod, file = source_file)
expect_false(.read_freeze(source_file, source_file, hashes))
expect_false(.is_frozen(source_file, source_file, hashes))
})

test_that(".read_freeze is FALSE if files don't exist", {
test_that(".is_frozen is FALSE if files don't exist", {
source_file <- tempfile()
fs::file_copy(test_path("examples/examples-man/between.Rd"), source_file)
hashes <- digest::digest(.readlines(source_file))
names(hashes) <- source_file
expect_false(.read_freeze("foobar", "foobar", hashes))
expect_false(.is_frozen("foobar", "foobar", hashes))
})

0 comments on commit 69a52f1

Please sign in to comment.