Skip to content

Commit

Permalink
feat: unstashing (tested)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhrcook committed Mar 21, 2020
1 parent a0f03ce commit a0477eb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
32 changes: 32 additions & 0 deletions R/unstash.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Unstash an object
#'
#' Remove an object from the stash.
#'
#' @param var The name or a vector of names of objects to remove.
#'
#' @return \code{NULL}
#'
#' @examples
#' \dontrun{
#' stash("x",
#' {
#' x <- 1
#' })
#'
#' unstash("x")
#' }
#'
#' @export unstash
unstash <- function(var) {

f <- function(v) {
if (has_been_stashed(v)) {
message(paste0("Unstashing '", v, "'."))
file.remove(unlist(stash_filename(v)))
} else {
message(paste0("No object '", v, "' in stash."))
}
}
lapply(var, f)
invisible(NULL)
}
23 changes: 23 additions & 0 deletions tests/testthat/test-unstash.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
test_that("unstashing works", {
expect_null(stash("a", { a <- 1 }))

expect_true(has_been_stashed("a"))

expect_null(unstash("a"))

expect_false(has_been_stashed("a"))

expect_null(stash("b", { b <- 2 }))
expect_null(stash("c", { c <- 2 }))

expect_true(has_been_stashed("b"))
expect_true(has_been_stashed("c"))

expect_message(unstash("a"), "No object 'a'")
expect_message(unstash("b"), "Unstashing 'b'")

expect_false(has_been_stashed("b"))
expect_true(has_been_stashed("c"))

if (dir.exists(".mustashe")) unlink(".mustashe", recursive = TRUE)
})

0 comments on commit a0477eb

Please sign in to comment.