Skip to content

Commit

Permalink
Use the$ convention for internal env
Browse files Browse the repository at this point in the history
  • Loading branch information
cderv committed Sep 10, 2021
1 parent 0316f6f commit 0a0d4db
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 31 deletions.
3 changes: 3 additions & 0 deletions R/aaa.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# internal environment acting as storage and cache
# Using convention in https://github.com/tidyverse/design/issues/126
the <- new.env(parent = emptyenv())
2 changes: 1 addition & 1 deletion R/pandoc-available.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#' @export
pandoc_available <- function(min = NULL, max = NULL, strict = FALSE) {
# TODO: get the version from calling pandoc instead.
active_version <- pandoc_active_get()
active_version <- the$active_version
# No active pandoc
if (active_version == "") return(FALSE)

Expand Down
24 changes: 8 additions & 16 deletions R/pandoc-install.R
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@ pandoc_uninstall <- function(version) {
latest <- pandoc_installed_latest()
if (is.null(latest)) {
# no more version installed
pandoc_active_unset()
the$active_version <- NULL
} else {
# Change to the latest version
pandoc_active_set(latest)
the$active_version <- latest
}

}
Expand All @@ -373,14 +373,6 @@ pandoc_active_set <- function(version) {
rlang::env_poke(pandocenv, "active_version", version, inherit = FALSE)
}

pandoc_active_unset <- function(version) {
rlang::env_unbind(pandocenv, "active_version", inherit = FALSE)
}

pandoc_active_get <- function() {
rlang::env_get(pandocenv, "active_version", default = "", inherit = FALSE)
}

#' Activate a specific Pandoc version to be used
#'
#' This function will set the specified version as the default version for the
Expand All @@ -395,16 +387,16 @@ pandoc_active_get <- function() {
#' @return invisibly, the previous active version.
#' @export
pandoc_set_version <- function(version, rmarkdown = TRUE) {
old_active <- pandoc_active_get()
old_active <- the$active_version
if (version == "latest") version <- pandoc_installed_latest()
if (is.null(version)) {
pandoc_active_set("")
the$active_version <- ""
} else {
if (!pandoc_is_installed(version)) {
rlang::abort(sprintf("Version %s is not yet installed", version))
}
pandoc_active_set(version)
rlang::inform(c(v = sprintf("Version %s is now the active one.", pandoc_active_get())))
the$active_version <- version
rlang::inform(c(v = sprintf("Version %s is now the active one.", the$active_version)))
if (rmarkdown && rlang::is_installed("rmarkdown")) {
rmarkdown::find_pandoc(cache = FALSE, dir = pandoc_locate())
rlang::inform(c(i = "This is also true for using with rmarkdown functions."))
Expand All @@ -420,7 +412,7 @@ pandoc_set_version <- function(version, rmarkdown = TRUE) {
#' @export
pandoc_is_active <- function(version) {
if (version == "latest") version <- pandoc_installed_latest()
version == pandoc_active_get()
version == the$active_version
}

#' Locate a specific installed Pandoc version
Expand All @@ -438,7 +430,7 @@ pandoc_locate <- function(version = "default") {
if (!is.character(version) && length(version) != 1L) {
rlang::abort("version must be a length one character")
}
if (version == "default") version <- pandoc_active_get()
if (version == "default") version <- the$active_version
if (version == "latest") version <- pandoc_installed_latest()
if (is.null(version) || !nzchar(version)) {
rlang::warn("No Pandoc version available.")
Expand Down
3 changes: 0 additions & 3 deletions R/pandoc-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,3 @@
## usethis namespace: start
## usethis namespace: end
NULL

# internal environment acting as storage and cache
pandocenv <- rlang::new_environment()
4 changes: 2 additions & 2 deletions R/pandoc-run.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ pandoc_version <- function(bin = pandoc_bin()) {
#' @export
with_pandoc_version <- function(version, code, rmarkdown = FALSE) {
old <- pandoc_set_version(version, rmarkdown = rmarkdown)
on.exit(pandoc_active_set(old))
on.exit(the$active_version <- old)
force(code)
}

local_pandoc_version <- function(version, rmarkdown = FALSE,
.local_envir = parent.frame()) {
rlang::check_installed("withr")
old <- suppressMessages(pandoc_set_version(version, rmarkdown = rmarkdown))
withr::defer(pandoc_active_set(old), envir = .local_envir)
withr::defer(the$active_version <- old, envir = .local_envir)
invisible(old)
}
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.onLoad <- function(libname, pkgname) {
latest <- pandoc_installed_latest()
if (!is.null(latest)) pandoc_active_set(latest)
if (!is.null(latest)) the$active_version <- latest
}

4 changes: 2 additions & 2 deletions tests/testthat/test-pandoc-available.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ test_that("pandoc_available() works", {
expect_false(pandoc_available(max = "2.11.4", strict = TRUE))
expect_false(pandoc_available(min = "2.11.4", strict = TRUE))
# as if no active version
pandoc_active_set("")
the$active_version <- ""
expect_false(pandoc_available())
suppressMessages(pandoc_active_set(old))
the$active_version <- old
})

12 changes: 6 additions & 6 deletions tests/testthat/test-pandoc-install.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ skip_if_offline()
test_that("Release information are cached", {
skip_on_cran()
skip_if_offline()
# clean cache
rlang::env_bind(pandocenv, pandoc_releases = rlang::zap())
# clean cached values
the$pandoc_releases <- NULL
# with message Fetching
expect_snapshot(x <- pandoc_releases())
# without message Fetching
Expand Down Expand Up @@ -102,7 +102,7 @@ test_that("No versions are installed", {
walk(pandoc_installed_versions(), pandoc_uninstall)
expect_null(pandoc_installed_versions())
expect_null(pandoc_installed_latest())
expect_identical(pandoc_active_get(), "")
expect_identical(the$active_version, "")
})

test_that("Pandoc nightly can be installed", {
Expand Down Expand Up @@ -186,11 +186,11 @@ test_that("Pandoc release can be uninstall", {
skip_if_offline()
suppressMessages(pandoc_install("2.11.4"))
suppressMessages(pandoc_install("2.7.3"))
pandoc_active_set("2.11.4")
the$active_version <- "2.11.4"
expect_true(pandoc_uninstall("2.11.4"))
expect_equal(pandoc_active_get(), pandoc_installed_latest())
expect_equal(the$active_version, pandoc_installed_latest())
expect_true(pandoc_uninstall("2.7.3"))
expect_equal(pandoc_active_get(), "")
expect_equal(the$active_version, "")
})

test_that("Pandoc latest release can be installed", {
Expand Down

0 comments on commit 0a0d4db

Please sign in to comment.