Skip to content

Commit

Permalink
single-option setter does not modify other options
Browse files Browse the repository at this point in the history
fixes #655
  • Loading branch information
aronatkins committed Feb 8, 2022
1 parent c648ce2 commit a0de85d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
repository. Improves cache reuse for packages originally installed by
`remotes<2.0.2`. (#652)

- The `packrat::opts` single-option setter no longer overwrites previously
written state. (#655)

# Packrat 0.7.0

- Fixed an issue where Packrat could inadvertently execute non-R code chunks
Expand Down
9 changes: 7 additions & 2 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,13 @@ get_opts <- function(options = NULL, simplify = TRUE, project = NULL) {
make_setter <- function(name) {
force(name)
function(x, persist = TRUE) {
if (missing(x)) return(get_opts(name))
else setOptions(setNames(list(x), name), persist = persist)
if (missing(x)) {
return(get_opts(name))
} else {
o <- get_opts()
o[[name]] <- x
setOptions(o, persist = persist)
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-options.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Confirms that we can use set_opts and the single-option setter and that the
# single-option setter does not overwrite previously configured state (#655)
test_that("an option can be set and retrieved", {
initial <- get_opts()

expected <- initial
expected[["ignored.packages"]] <- c("emo")
expected[["snapshot.recommended.packages"]] <- TRUE

packrat::set_opts(ignored.packages = c("emo"), persist = FALSE)
packrat::opts$snapshot.recommended.packages(TRUE, persist = FALSE)

actual <- get_opts()
expect_equal(actual, expected)
})

0 comments on commit a0de85d

Please sign in to comment.